-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGame.h
166 lines (139 loc) · 3.6 KB
/
Game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
#include "TileView.h"
class Game;
extern std::shared_ptr<Game> g_pGame;
struct SparseMapRoom
{
int room;
int tile_x;
int tile_y;
};
class Game
{
public:
template <class TGame>
static std::unique_ptr<Game> Create(std::shared_ptr<TileView> pTileView, bool cheat_pokes)
{
return std::make_unique<TGame>(pTileView, cheat_pokes);
}
Game(std::shared_ptr<TileView> pTileView, int start_room) :
m_pTileView(pTileView),
m_current_room(start_room)
{
}
virtual ~Game() {}
public:
virtual void SetMap(const std::vector<MapRoom> &map) = 0;
virtual void RunFrame() = 0;
virtual void CloneTile(const Tile &from_tile, Tile &to_tile) = 0;
void CloneToAllTiles(const Tile &from_tile, std::function<void(Tile&)> func = [](Tile&) {})
{
for (auto &tile : m_tiles)
{
if (tile.room != from_tile.room)
{
auto lock = tile.Lock();
CloneTile(from_tile, tile);
func(tile);
}
}
}
using HookFunction = std::function<void(Tile&)>;
virtual void Hook(std::vector<uint8_t> &mem, uint16_t address, uint8_t expected_opcode, HookFunction fn) = 0;
virtual void OnHook(uint16_t address, Tile &tile) = 0;
virtual void BlankScreen(Tile &tile) = 0;
virtual void CreateTileMask(Tile &tile, uint8_t *pDisplay, uint8_t *pMask) = 0;
Tile &FindRoomTile(int room)
{
for (auto &td : m_tiles)
{
if (td.room == room)
return td;
}
throw std::logic_error("non-existent room requested");
}
Tile &ActiveTile()
{
return FindRoomTile(m_current_room);
}
bool IsActiveTile(const Tile &tile)
{
return tile.room == m_current_room;
}
void ActivateTile(Tile &tile, bool force_centre=false)
{
m_current_room = tile.room;
tile.running = true;
if (force_centre)
m_pTileView->CentreViewOnTile(tile);
else
m_pTileView->EnsureTileVisible(tile);
m_pTileView->UpdatePanels();
}
int GetCurrentRoom() { return m_current_room; };
std::vector<Tile> &Tiles()
{
return m_tiles;
}
std::vector<MapRoom> BuildRegularMap(
int map_width, int map_height,
int tile_width, int tile_height,
int gap_x=0, int gap_y=0,
int offset_x=0, int offset_y=0,
std::function<int(int, int, int)> room_func = [](int x, int y, int pitch) { return y * pitch + x; })
{
std::vector<MapRoom> tiles;
tiles.reserve(map_width * map_height);
for (int tile_y = 0; tile_y < map_height; ++tile_y)
{
for (int tile_x = 0; tile_x < map_width; ++tile_x)
{
MapRoom tile;
tile.room = room_func(tile_x, tile_y, map_width);
tile.x = TileToPixels(tile_x, tile_width, gap_x);
tile.y = TileToPixels(tile_y, tile_height, gap_y);
tile.width = tile_width;
tile.height = tile_height;
tile.offset_x = offset_x;
tile.offset_y = offset_y;
tiles.push_back(tile);
}
}
return tiles;
}
std::vector<MapRoom> BuildSparseMap(
const std::vector<SparseMapRoom> &map_rooms,
int tile_width, int tile_height,
int gap_x = 0, int gap_y = 0,
int offset_x = 0, int offset_y = 0)
{
std::vector<MapRoom> tiles;
tiles.reserve(map_rooms.size());
for (auto &room : map_rooms)
{
MapRoom tile;
tile.room = room.room;
tile.x = TileToPixels(room.tile_x, tile_width, gap_x);
tile.y = TileToPixels(room.tile_y, tile_height, gap_y);
tile.width = tile_width;
tile.height = tile_height;
tile.offset_x = offset_x;
tile.offset_y = offset_y;
tiles.push_back(tile);
}
return tiles;
}
public:
std::shared_ptr<TileView> m_pTileView;
ThreadPool m_thread_pool;
std::vector<std::future<void>> m_thread_futures;
protected:
int m_current_room = -1;
std::vector<Tile> m_tiles;
struct HookData
{
HookFunction func;
uint8_t orig_opcode;
};
std::map<uint16_t, HookData> m_hooks;
};