-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTileView.h
224 lines (188 loc) · 5.5 KB
/
TileView.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once
#include "Utils.h"
class Game;
struct MapRoom
{
int room{ -1 };
int x{ 0 };
int y{ 0 };
int width{ 0 };
int height{ 0 };
int offset_x{ 0 };
int offset_y{ 0 };
};
enum class PanelAlignment { TopLeft, Top, TopRight, Left, Right, BottomLeft, Bottom, BottomRight };
struct Panel
{
int offset_x{ 0 };
int offset_y{ 0 };
int width{ 0 };
int height{ 0 };
PanelAlignment align{ PanelAlignment::Top };
};
class Tile : public MapRoom
{
public:
int screen_index = -1;
zusize frame_cycles = 0;
bool visible = true;
bool running = true;
bool drawing = true;
bool mask_dirty = false;
Game *pGame{ nullptr };
Z80 z80{};
std::vector<uint8_t> mem;
private:
std::mutex mutex;
public:
static std::mutex shared_lock;
std::unique_lock<std::mutex> Lock()
{
return std::unique_lock<std::mutex>(mutex);
}
void CloneToTileNoLock(Tile &target_tile, uint16_t start_address = 0x4000)
{
if (target_tile.room != room)
{
std::copy(mem.begin() + start_address, mem.end(), target_tile.mem.begin() + start_address);
target_tile.z80.state = z80.state;
}
}
uint16_t DPeek(uint16_t address)
{
uint16_t value = mem[address++];
value |= (mem[address] << 8);
return value;
}
uint16_t DPeekDPeek(uint16_t address)
{
return DPeek(DPeek(address));
}
uint16_t DPoke(uint16_t address, uint16_t value)
{
mem[address++] = value & 0xff;
mem[address] = value >> 8;
return value;
}
};
constexpr float DEFAULT_VIEW_MAGNIFY = 2.0f;
struct VSConstants
{
uint32_t total_x{ 1 };
uint32_t total_y{ 1 };
float translate_x{ 0.0f };
float translate_y{ 0.0f };
float scale_x{ 1.0f };
float scale_y{ 1.0f };
float scale{ DEFAULT_VIEW_MAGNIFY };
int32_t screen_bytes{ 0 };
};
struct PSConstants
{
uint32_t flash_invert{ 0 };
uint32_t transparent{ 1 };
uint32_t padding[2]{};
};
struct InstanceType
{
int32_t x;
int32_t y;
int32_t width;
int32_t height;
int32_t offset_x;
int32_t offset_y;
int32_t screen_index;
};
class TileView
{
public:
TileView(HWND hwnd) : m_hwnd(hwnd) { }
void SetMap(const std::vector<MapRoom> &rooms);
void SetPanels(const std::vector<Panel> &panels);
void SetScreenBytes(int screen_bytes) { sVSConstants.screen_bytes = screen_bytes; }
SIZE TotalSize() const;
float GetScale() const { return m_scale; }
void SetSingleTile(bool single_tile) { m_single_tile = single_tile; }
bool CheckTearingSupport();
HRESULT InitD3D();
void ExitD3D();
HRESULT OnResize();
HRESULT Render();
void UpdateInstanceData();
void UpdateScreens();
void UpdateMasks();
void UpdateView();
void UpdateTiles();
void UpdatePanels();
void EnsureTileVisible(const Tile &tile, bool instant=false);
void CentreViewOnTile(const Tile &tile, bool instant=false);
void CentreOnWorldPointX(int x, bool instant=false);
void CentreOnWorldPointY(int y, bool instant=false);
void CentreViewOnWorldPoint(int x, int y, bool instant=false);
void ScaleToFit(bool invert_fit);
void SetViewScale(float scale, int nX = INT_MIN, int nY = INT_MIN, bool instant=false);
void SetRelativeViewScale(float scale, int nX = INT_MIN, int nY = INT_MIN, bool instant=false);
void SetViewRelativeOffset(int dX, int dY, bool scaled=true);
void MoveTileRelativeOffset(Tile &tile, int dX, int dY);
POINTF WindowPointToWorldF(int x, int y);
POINT WindowPointToWorld(int x_, int y_);
POINT WorldPointToWindow(int x, int y);
bool WorldPointToTile(float fx, float fy, int &x_, int &y_);
Tile *TileFromWindowPoint(int window_x, int window_y);
bool IsTilePointOnWindow(int x, int y);
bool IsTileVisible(_In_ const Tile &tile);
bool IsTileClipped(_In_ const Tile &tile);
private:
static constexpr int DEFAULT_ANIMATION_DURATION_MS = 1000;
template <typename TData>
HRESULT UpdateBuffer(ID3D11Buffer *pBuffer, const TData *buf, size_t buf_size)
{
HRESULT hr;
D3D11_MAPPED_SUBRESOURCE ms{};
if (SUCCEEDED(hr = m_pDeviceContext->Map(pBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms)))
{
memcpy(ms.pData, buf, buf_size);
m_pDeviceContext->Unmap(pBuffer, 0);
}
return hr;
}
private:
HWND m_hwnd;
int m_num_rooms{ 0 };
int m_num_panels{ 0 };
bool m_layout_dirty{ false };
std::vector<Panel> m_panels{};
POINT m_Origin{};
SIZE m_TotalSize{};
SIZE m_TileOffset{};
SIZE m_TileSize{};
SIZE m_WindowSize{};
EasedValue<float> m_scale{ DEFAULT_VIEW_MAGNIFY, std::chrono::milliseconds(DEFAULT_ANIMATION_DURATION_MS) };
EasedValue<float> m_translate_x{ 0.0f, std::chrono::milliseconds(DEFAULT_ANIMATION_DURATION_MS) };
EasedValue<float> m_translate_y{ 0.0f, std::chrono::milliseconds(DEFAULT_ANIMATION_DURATION_MS) };
VSConstants sVSConstants;
PSConstants sPSConstants;
bool m_allow_tearing{ false };
bool m_fullscreen{ false };
bool m_single_tile{ false };
ComPtr<ID3D11Device> m_pDevice;
ComPtr<ID3D11DeviceContext> m_pDeviceContext;
ComPtr<IDXGISwapChain1> m_pSwapChain;
ComPtr<ID3D11Buffer> m_pDisplay;
ComPtr<ID3D11Buffer> m_pMask;
ComPtr<ID3D11Buffer> m_pPalette;
ComPtr<ID3D11Buffer> m_pDisplayStaging;
ComPtr<ID3D11Buffer> m_pMaskStaging;
ComPtr<ID3D11Buffer> m_pInstanceBuffer;
ComPtr<ID3D11Buffer> m_pVSConstants;
ComPtr<ID3D11Buffer> m_pPSConstants;
ComPtr<ID3D11InputLayout> m_pInputLayout;
ComPtr<ID3D11RenderTargetView> m_pRenderTargetView;
ComPtr<ID3D11ShaderResourceView> m_pShaderResourceViewDisplay;
ComPtr<ID3D11ShaderResourceView> m_pShaderResourceViewMask;
ComPtr<ID3D11ShaderResourceView> m_pShaderResourceViewPalette;
ComPtr<ID3D11VertexShader> m_pVertexShader;
ComPtr<ID3D11PixelShader> m_pPixelShader;
ComPtr<ID3D11SamplerState> m_pSamplerState;
ComPtr<ID3D11RasterizerState> m_pRasterizerState;
};