forked from diasurgical/DevilutionX
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdx.cpp
268 lines (224 loc) · 5.52 KB
/
dx.cpp
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "diablo.h"
#include "../3rdParty/Storm/Source/storm.h"
#include "miniwin/ddraw.h"
#include "miniwin/com_macro.h"
#include <SDL.h>
#include "utils.h"
namespace dvl {
int sgdwLockCount;
BYTE *gpBuffer;
#ifdef _DEBUG
int locktbl[256];
#endif
static CCritSect sgMemCrit;
HMODULE ghDiabMod;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
/** Currently active palette */
SDL_Palette *palette;
unsigned int pal_surface_palette_version = 0;
/** 24-bit renderer texture surface */
SDL_Surface *renderer_texture_surface = nullptr;
/** 8-bit surface wrapper around #gpBuffer */
SDL_Surface *pal_surface;
bool bufferUpdated = false;
void dx_init(HWND hWnd)
{
SDL_RaiseWindow(window);
SDL_ShowWindow(window);
dx_create_primary_surface();
palette_init();
dx_create_back_buffer();
}
void dx_create_back_buffer()
{
pal_surface = SDL_CreateRGBSurfaceWithFormat(0, BUFFER_WIDTH, BUFFER_HEIGHT, 8, SDL_PIXELFORMAT_INDEX8);
if (pal_surface == NULL) {
ErrSdl();
}
gpBuffer = (BYTE *)pal_surface->pixels;
if (SDLC_SetSurfaceColors(pal_surface, palette) <= -1) {
ErrSdl();
}
pal_surface_palette_version = 1;
}
void dx_create_primary_surface()
{
#ifndef USE_SDL1
if (renderer) {
int width, height;
SDL_RenderGetLogicalSize(renderer, &width, &height);
Uint32 format;
if (SDL_QueryTexture(texture, &format, nullptr, nullptr, nullptr) < 0)
ErrSdl();
renderer_texture_surface = SDL_CreateRGBSurfaceWithFormat(0, width, height, SDL_BITSPERPIXEL(format), format);
}
#endif
if (GetOutputSurface() == nullptr) {
ErrSdl();
}
}
void lock_buf(BYTE idx)
{
#ifdef _DEBUG
locktbl[idx]++;
#endif
lock_buf_priv();
}
void lock_buf_priv()
{
sgMemCrit.Enter();
if (sgdwLockCount != 0) {
sgdwLockCount++;
return;
}
gpBufEnd += (uintptr_t)(BYTE *)pal_surface->pixels;
gpBuffer = (BYTE *)pal_surface->pixels;
sgdwLockCount++;
}
void unlock_buf(BYTE idx)
{
#ifdef _DEBUG
if (!locktbl[idx])
app_fatal("Draw lock underflow: 0x%x", idx);
locktbl[idx]--;
#endif
unlock_buf_priv();
}
void unlock_buf_priv()
{
if (sgdwLockCount == 0)
app_fatal("draw main unlock error");
if (!gpBuffer)
app_fatal("draw consistency error");
sgdwLockCount--;
if (sgdwLockCount == 0) {
gpBufEnd -= (uintptr_t)gpBuffer;
//gpBuffer = NULL; unable to return to menu
RenderPresent();
}
sgMemCrit.Leave();
}
void dx_cleanup()
{
if (ghMainWnd)
SDL_HideWindow(window);
sgMemCrit.Enter();
sgdwLockCount = 0;
gpBuffer = NULL;
sgMemCrit.Leave();
if (pal_surface == nullptr)
return;
SDL_FreeSurface(pal_surface);
pal_surface = nullptr;
SDL_FreePalette(palette);
SDL_FreeSurface(renderer_texture_surface);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
void dx_reinit()
{
int lockCount;
sgMemCrit.Enter();
ClearCursor();
lockCount = sgdwLockCount;
while (sgdwLockCount != 0)
unlock_buf_priv();
dx_cleanup();
drawpanflag = 255;
dx_init(ghMainWnd);
while (lockCount != 0) {
lock_buf_priv();
lockCount--;
}
sgMemCrit.Leave();
}
void CreatePalette()
{
palette = SDL_AllocPalette(256);
if (palette == NULL) {
ErrSdl();
}
}
void BltFast(DWORD dwX, DWORD dwY, LPRECT lpSrcRect)
{
auto w = static_cast<decltype(SDL_Rect().w)>(lpSrcRect->right - lpSrcRect->left + 1);
auto h = static_cast<decltype(SDL_Rect().h)>(lpSrcRect->bottom - lpSrcRect->top + 1);
SDL_Rect src_rect = {
static_cast<decltype(SDL_Rect().x)>(lpSrcRect->left),
static_cast<decltype(SDL_Rect().y)>(lpSrcRect->top),
w, h
};
if (GFX_IsRetroFW20()) {
SDL_Rect dst_rect = {
static_cast<decltype(SDL_Rect().x)>(dwX),
static_cast<decltype(SDL_Rect().y)>(dwY),
w, h
};
// Convert from 8-bit to 32-bit
if (SDL_BlitSurface(pal_surface, &src_rect, GetOutputSurface(), &dst_rect) <= -1) {
ErrSdl();
}
} else {
SDL_Rect dst_rect = {
static_cast<decltype(SDL_Rect().x)>(dwX) / 2,
static_cast<decltype(SDL_Rect().y)>(dwY),
w / 2, h
};
// Convert from 8-bit to 32-bit
SDL_Surface *tmp = SDL_ConvertSurface(pal_surface, GetOutputSurface()->format, 0);
if (SDL_BlitScaled(tmp, &src_rect, GetOutputSurface(), &dst_rect) <= -1) {
SDL_FreeSurface(tmp);
ErrSdl();
}
SDL_FreeSurface(tmp);
}
bufferUpdated = true;
}
void RenderPresent()
{
SDL_Surface *surface = GetOutputSurface();
assert(!SDL_MUSTLOCK(surface));
if (!bufferUpdated) {
return;
}
#ifdef USE_SDL1
if (SDL_Flip(surface) <= -1) {
ErrSdl();
}
#else
if (renderer) {
if (SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch) <= -1) { //pitch is 2560
ErrSdl();
}
// Clear buffer to avoid artifacts in case the window was resized
if (SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) <= -1) { // TODO only do this if window was resized
ErrSdl();
}
if (SDL_RenderClear(renderer) <= -1) {
ErrSdl();
}
if (SDL_RenderCopy(renderer, texture, NULL, NULL) <= -1) {
ErrSdl();
}
SDL_RenderPresent(renderer);
} else {
if (SDL_UpdateWindowSurface(window) <= -1) {
ErrSdl();
}
}
#endif
bufferUpdated = false;
}
void PaletteGetEntries(DWORD dwNumEntries, LPPALETTEENTRY lpEntries)
{
for (DWORD i = 0; i < dwNumEntries; i++) {
lpEntries[i].peFlags = 0;
lpEntries[i].peRed = system_palette[i].peRed;
lpEntries[i].peGreen = system_palette[i].peGreen;
lpEntries[i].peBlue = system_palette[i].peBlue;
}
}
} // namespace dvl