forked from diasurgical/devilution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdx.cpp
More file actions
363 lines (327 loc) · 9.13 KB
/
dx.cpp
File metadata and controls
363 lines (327 loc) · 9.13 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "diablo.h"
#include "../3rdParty/Storm/Source/storm.h"
BYTE *sgpBackBuf;
LPDIRECTDRAW lpDDInterface;
IDirectDrawPalette *lpDDPalette; // idb
int sgdwLockCount;
BYTE *gpBuffer;
IDirectDrawSurface *lpDDSBackBuf;
IDirectDrawSurface *lpDDSPrimary;
#ifdef _DEBUG
int locktbl[256];
#endif
#ifdef __cplusplus
static CCritSect sgMemCrit;
#endif
char gbBackBuf; // weak
char gbEmulate; // weak
HMODULE ghDiabMod; // idb
void dx_init(HWND hWnd)
{
HRESULT hDDVal;
int winw, winh;
BOOL bSuccess;
GUID *lpGUID;
/// ASSERT: assert(! gpBuffer);
/// ASSERT: assert(! sgdwLockCount);
/// ASSERT: assert(! sgpBackBuf);
SetFocus(hWnd);
ShowWindow(hWnd, SW_SHOWNORMAL);
lpGUID = (GUID *)DDCREATE_EMULATIONONLY;
if(!gbEmulate) {
lpGUID = NULL;
}
hDDVal = dx_DirectDrawCreate(lpGUID, &lpDDInterface, NULL);
if(hDDVal != DD_OK) {
ErrDlg(IDD_DIALOG1, hDDVal, "C:\\Src\\Diablo\\Source\\dx.cpp", 149);
}
#ifdef COLORFIX
#ifdef __DDRAWI_INCLUDED__
((LPDDRAWI_DIRECTDRAW_INT)lpDDInterface)->lpLcl->dwAppHackFlags |= 0x800;
#else
((DWORD **)lpDDInterface)[1][18] |= 0x800;
#endif
#endif
#ifndef _DEBUG
fullscreen = TRUE;
#endif
if(!fullscreen) {
#ifdef __cplusplus
hDDVal = lpDDInterface->SetCooperativeLevel(hWnd, DDSCL_NORMAL | DDSCL_ALLOWREBOOT);
#else
hDDVal = lpDDInterface->lpVtbl->SetCooperativeLevel(lpDDInterface, hWnd, DDSCL_NORMAL | DDSCL_ALLOWREBOOT);
#endif
if(hDDVal == DDERR_EXCLUSIVEMODEALREADYSET) {
TriggerBreak();
} else if(hDDVal != DD_OK) {
ErrDlg(IDD_DIALOG1, hDDVal, "C:\\Diablo\\Direct\\dx.cpp", 155);
}
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
} else {
#ifdef __cplusplus
hDDVal = lpDDInterface->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT | DDSCL_FULLSCREEN);
#else
hDDVal = lpDDInterface->lpVtbl->SetCooperativeLevel(lpDDInterface, hWnd, DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT | DDSCL_FULLSCREEN);
#endif
if(hDDVal == DDERR_EXCLUSIVEMODEALREADYSET) {
TriggerBreak();
} else if(hDDVal != DD_OK) {
ErrDlg(IDD_DIALOG1, hDDVal, "C:\\Src\\Diablo\\Source\\dx.cpp", 170);
}
#ifdef __cplusplus
hDDVal = lpDDInterface->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
#else
hDDVal = lpDDInterface->lpVtbl->SetDisplayMode(lpDDInterface, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
#endif
if(hDDVal != DD_OK) {
winw = GetSystemMetrics(SM_CXSCREEN);
winh = GetSystemMetrics(SM_CYSCREEN);
#ifdef __cplusplus
hDDVal = lpDDInterface->SetDisplayMode(winw, winh, SCREEN_BPP);
#else
hDDVal = lpDDInterface->lpVtbl->SetDisplayMode(lpDDInterface, winw, winh, SCREEN_BPP);
#endif
if(hDDVal != DD_OK) {
ErrDlg(IDD_DIALOG1, hDDVal, "C:\\Src\\Diablo\\Source\\dx.cpp", 183);
}
}
}
dx_create_primary_surface();
palette_init();
GdiSetBatchLimit(1);
dx_create_back_buffer();
bSuccess = SDrawManualInitialize(hWnd, lpDDInterface, lpDDSPrimary, NULL, NULL, lpDDSBackBuf, lpDDPalette, NULL);
/// ASSERT: assert(bSuccess);
}
// 52A549: using guessed type char gbEmulate;
void dx_create_back_buffer()
{
DDSCAPS caps;
HRESULT error_code;
DDSURFACEDESC ddsd;
#ifdef __cplusplus
error_code = lpDDSPrimary->GetCaps(&caps);
#else
error_code = lpDDSPrimary->lpVtbl->GetCaps(lpDDSPrimary, &caps);
#endif
if (error_code != DD_OK)
DDErrMsg(error_code, 59, "C:\\Src\\Diablo\\Source\\dx.cpp");
if (!gbBackBuf) {
ddsd.dwSize = sizeof(ddsd);
#ifdef __cplusplus
error_code = lpDDSPrimary->Lock(NULL, &ddsd, DDLOCK_WRITEONLY | DDLOCK_WAIT, NULL);
#else
error_code = lpDDSPrimary->lpVtbl->Lock(lpDDSPrimary, NULL, &ddsd, DDLOCK_WRITEONLY | DDLOCK_WAIT, NULL);
#endif
if (error_code == DD_OK) {
#ifdef __cplusplus
lpDDSPrimary->Unlock(NULL);
#else
lpDDSPrimary->lpVtbl->Unlock(lpDDSPrimary, NULL);
#endif
sgpBackBuf = (BYTE *)DiabloAllocPtr(BUFFER_HEIGHT * BUFFER_WIDTH);
return;
}
if (error_code != DDERR_CANTLOCKSURFACE)
ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 81);
}
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwWidth = BUFFER_WIDTH;
ddsd.lPitch = BUFFER_WIDTH;
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN;
ddsd.dwHeight = BUFFER_HEIGHT;
ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat);
#ifdef __cplusplus
error_code = lpDDSPrimary->GetPixelFormat(&ddsd.ddpfPixelFormat);
#else
error_code = lpDDSPrimary->lpVtbl->GetPixelFormat(lpDDSPrimary, &ddsd.ddpfPixelFormat);
#endif
if (error_code != DD_OK)
ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 94);
#ifdef __cplusplus
error_code = lpDDInterface->CreateSurface(&ddsd, &lpDDSBackBuf, NULL);
#else
error_code = lpDDInterface->lpVtbl->CreateSurface(lpDDInterface, &ddsd, &lpDDSBackBuf, NULL);
#endif
if (error_code != DD_OK)
ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 96);
}
// 52A548: using guessed type char gbBackBuf;
void dx_create_primary_surface()
{
DDSURFACEDESC ddsd;
HRESULT error_code;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
#ifdef __cplusplus
error_code = lpDDInterface->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
#else
error_code = lpDDInterface->lpVtbl->CreateSurface(lpDDInterface, &ddsd, &lpDDSPrimary, NULL);
#endif
if (error_code != DD_OK)
ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 109);
}
HRESULT dx_DirectDrawCreate(LPGUID guid, LPDIRECTDRAW *lplpDD, LPUNKNOWN pUnkOuter)
{
HRESULT(WINAPI * DirectDrawCreate)
(LPGUID lpGuid, LPDIRECTDRAW * lplpDD, LPUNKNOWN pUnkOuter);
if (ghDiabMod == NULL) {
ghDiabMod = LoadLibrary("ddraw.dll");
if (ghDiabMod == NULL) {
ErrDlg(IDD_DIALOG4, GetLastError(), "C:\\Src\\Diablo\\Source\\dx.cpp", 122);
}
}
DirectDrawCreate = (HRESULT(WINAPI *)(LPGUID, LPDIRECTDRAW *, LPUNKNOWN))GetProcAddress(ghDiabMod, "DirectDrawCreate");
if (DirectDrawCreate == NULL) {
ErrDlg(IDD_DIALOG4, GetLastError(), "C:\\Src\\Diablo\\Source\\dx.cpp", 127);
}
return DirectDrawCreate(guid, lplpDD, pUnkOuter);
}
void lock_buf(BYTE idx)
{
#ifdef _DEBUG
++locktbl[idx];
#endif
lock_buf_priv();
}
void lock_buf_priv()
{
DDSURFACEDESC ddsd;
HRESULT error_code;
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
if (sgpBackBuf != NULL) {
gpBuffer = sgpBackBuf;
sgdwLockCount++;
return;
}
if (lpDDSBackBuf == NULL) {
Sleep(20000);
app_fatal("lock_buf_priv");
sgdwLockCount++;
return;
}
if (sgdwLockCount != 0) {
sgdwLockCount++;
return;
}
ddsd.dwSize = sizeof(ddsd);
#ifdef __cplusplus
error_code = lpDDSBackBuf->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
#else
error_code = lpDDSBackBuf->lpVtbl->Lock(lpDDSBackBuf, NULL, &ddsd, DDLOCK_WAIT, NULL);
#endif
if (error_code != DD_OK)
DDErrMsg(error_code, 235, "C:\\Src\\Diablo\\Source\\dx.cpp");
gpBufEnd += (int)ddsd.lpSurface;
gpBuffer = (BYTE *)ddsd.lpSurface;
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()
{
HRESULT error_code;
if (sgdwLockCount == 0)
app_fatal("draw main unlock error");
if (gpBuffer == NULL)
app_fatal("draw consistency error");
sgdwLockCount--;
if (sgdwLockCount == 0) {
gpBufEnd -= (int)gpBuffer;
gpBuffer = NULL;
if (sgpBackBuf == NULL) {
#ifdef __cplusplus
error_code = lpDDSBackBuf->Unlock(NULL);
#else
error_code = lpDDSBackBuf->lpVtbl->Unlock(lpDDSBackBuf, NULL);
#endif
if (error_code != DD_OK)
DDErrMsg(error_code, 273, "C:\\Src\\Diablo\\Source\\dx.cpp");
}
}
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}
void dx_cleanup()
{
if (ghMainWnd)
ShowWindow(ghMainWnd, SW_HIDE);
SDrawDestroy();
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
if (sgpBackBuf != NULL) {
MemFreeDbg(sgpBackBuf);
} else if (lpDDSBackBuf != NULL) {
#ifdef __cplusplus
lpDDSBackBuf->Release();
#else
lpDDSBackBuf->lpVtbl->Release(lpDDSBackBuf);
#endif
lpDDSBackBuf = NULL;
}
sgdwLockCount = 0;
gpBuffer = NULL;
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
if (lpDDSPrimary) {
#ifdef __cplusplus
lpDDSPrimary->Release();
#else
lpDDSPrimary->lpVtbl->Release(lpDDSPrimary);
#endif
lpDDSPrimary = NULL;
}
if (lpDDPalette) {
#ifdef __cplusplus
lpDDPalette->Release();
#else
lpDDPalette->lpVtbl->Release(lpDDPalette);
#endif
lpDDPalette = NULL;
}
if (lpDDInterface) {
#ifdef __cplusplus
lpDDInterface->Release();
#else
lpDDInterface->lpVtbl->Release(lpDDInterface);
#endif
lpDDInterface = NULL;
}
}
void dx_reinit()
{
int lockCount;
#ifdef __cplusplus
sgMemCrit.Enter();
#endif
ClearCursor();
lockCount = sgdwLockCount;
while (sgdwLockCount != 0)
unlock_buf_priv();
dx_cleanup();
drawpanflag = 255;
dx_init(ghMainWnd);
while (lockCount != 0) {
lock_buf_priv();
lockCount--;
}
#ifdef __cplusplus
sgMemCrit.Leave();
#endif
}