This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
czGraphics.cpp
487 lines (409 loc) · 12.2 KB
/
czGraphics.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
UD directdraw library by Gargaj & Vercy
we've been rolling this code in front of us since 2001
so shut up.
*/
#include "czGraphics.h"
//#include "czDebug.h"
//#include "resource.h"
#include <tchar.h>
//#pragma comment(lib,"ddraw.lib")
//#pragma comment(lib,"dxguid.lib")
czGraphics::czGraphics( int x, int y, bool fs ) {
CZ_WIDTH = x;
CZ_HEIGHT = y;
quit=0;
ZeroMemory(&g_rcWindow ,sizeof(RECT));
ZeroMemory(&g_rcViewport,sizeof(RECT));
ZeroMemory(&g_rcScreen ,sizeof(RECT));
bpp = 32;
mouseclicked = 0;
mouserelease = 0;
fullscreen = fs;
doublesize = 0;
ontop = 0;
mousex = 0;
mousey = 0;
active = 1;
}
czGraphics::~czGraphics() {}
#define CZDEBUG_ASSERT_RETURN(x) if (!(x)) return 0;
LRESULT czGraphics::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_ACTIVATE:
{
active = (LOWORD(wParam)!=0) && (HIWORD(wParam)==0);
} break;
case WM_MOVE: {
GetWindowRect(hWnd, &g_rcWindow);
GetClientRect(hWnd, &g_rcViewport);
GetClientRect(hWnd, &g_rcScreen);
ClientToScreen(hWnd, (POINT*)&g_rcScreen.left);
ClientToScreen(hWnd, (POINT*)&g_rcScreen.right);
} break;
case WM_SYSKEYDOWN:
{
if (wParam == VK_RETURN)
{
DestroyDirectDraw();
fullscreen = !fullscreen;
DWORD wStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
if (!fullscreen) wStyle |= WS_OVERLAPPED | WS_CAPTION;
SetWindowLong( hWnd, GWL_STYLE, wStyle );
DWORD wExStyle = WS_EX_APPWINDOW | WS_EX_TOOLWINDOW;
RECT wr={0,0,CZ_WIDTH,CZ_HEIGHT};
AdjustWindowRectEx(&wr, wStyle, FALSE, wExStyle);
if (!fullscreen)
{
int cx = (GetSystemMetrics(SM_CXSCREEN)-CZ_WIDTH)/2;
int cy = (GetSystemMetrics(SM_CYSCREEN)-CZ_HEIGHT)/2;
wr.left += cx;
wr.top += cy;
wr.right += cx;
wr.bottom += cy;
}
SetWindowPos( hWnd, NULL, wr.left, wr.top, wr.right-wr.left, wr.bottom-wr.top, SWP_NOZORDER | SWP_SHOWWINDOW );
InitDirectDraw();
}
} break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
{
PostMessage(hWnd, WM_QUIT, 0, 0);
quit=1;
return 0L;
}
} break;
case WM_LBUTTONDOWN: {
mouseclicked = 1;
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mousex = x;
mousey = y;
if (!(33 < x && x < 33+25 && 13 < y && y < 13+94))
SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, NULL);
} break;
case WM_MOUSEMOVE: {
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mousex = x;
mousey = y;
} break;
case WM_NCLBUTTONUP:
case WM_LBUTTONUP:
{
int x = LOWORD(lParam);
int y = HIWORD(lParam);
mouseclicked=0;
mouserelease=1;
mousex = x;
mousey = y;
} break;
case WM_CLOSE:
case WM_DESTROY: {
quit = 1;
PostMessage(hWnd, WM_QUIT, 0, 0);
} break;
case WM_SYSCOMMAND: {
switch (wParam) {
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
break;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
czGraphics * pGlobalDisp = NULL;
LRESULT CALLBACK DlgFuncHook( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if (uMsg == WM_CREATE) {
CREATESTRUCT * s = (CREATESTRUCT*)lParam; // todo: split to multiple hWnd-s! (if needed)
pGlobalDisp = (czGraphics*)s->lpCreateParams;
}
if (!pGlobalDisp->WndProc(hWnd,uMsg,wParam,lParam))
return 0;
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int czGraphics::Init(HINSTANCE hInst) {
int xres = CZ_WIDTH;
int yres = CZ_HEIGHT;
if (doublesize) {
xres<<=1;
yres<<=1;
}
DWORD wExStyle = WS_EX_APPWINDOW | WS_EX_TOOLWINDOW;
DWORD wStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
if (!fullscreen) wStyle |= WS_OVERLAPPED | WS_CAPTION;
WNDCLASS WC;
WC.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
WC.lpfnWndProc = &DlgFuncHook;
WC.cbClsExtra = 0;
WC.cbWndExtra = 0;
WC.hInstance = hInst;
WC.hIcon = NULL;
WC.hCursor = LoadCursor(NULL, IDC_ARROW);
WC.hbrBackground = NULL;
WC.lpszMenuName = NULL;
WC.lpszClassName = _T("ud2dwindow");
//LOG.Printf("[gfx] RegisterClass()...\n");
CZDEBUG_ASSERT_RETURN(RegisterClass(&WC));
RECT wr={0,0,xres,yres};
if (!fullscreen)
AdjustWindowRectEx(&wr, wStyle, FALSE, wExStyle);
//LOG.Printf("[gfx] CreateWindow()...\n");
hWnd = CreateWindowEx(wExStyle,WC.lpszClassName,_T("czGraphics"),wStyle,
(GetSystemMetrics(SM_CXSCREEN)-xres)/2,
(GetSystemMetrics(SM_CYSCREEN)-yres)/2,
wr.right-wr.left, wr.bottom-wr.top,
NULL,NULL,hInst,this);
CZDEBUG_ASSERT_RETURN(hWnd);
if (ontop)
SetWindowPos( hWnd, HWND_TOPMOST, NULL, NULL, NULL, NULL, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
GetClientRect(hWnd, &g_rcViewport);
GetClientRect(hWnd, &g_rcScreen);
ClientToScreen(hWnd, (POINT*)&g_rcScreen.left);
ClientToScreen(hWnd, (POINT*)&g_rcScreen.right);
//////////////////////////////////////////////////////////////////////////
if (InitDirectDraw() == 0)
return 0;
return 1;
}
void czGraphics::Close() {
//LOG.Printf("[gfx] Close()...\n");
DestroyDirectDraw();
DestroyWindow(hWnd);
}
void czGraphics::Messages() {
MSG msg;
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
void czGraphics::Update(void*vscr) {
unsigned int * rot = (unsigned int *)vscr;
DDSURFACEDESC2 ddsdBack;
memset(&ddsdBack, 0, sizeof(DDSURFACEDESC));
ddsdBack.dwSize = sizeof (ddsdBack);
g_pDDSBack->Lock (0, &ddsdBack, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT ,0);
long wta = ddsdBack.lPitch-ddsdBack.dwWidth*((bpp+1) / 8);
LPVOID surf = ddsdBack.lpSurface;
// int* vscr = scr32;
switch(bpp){
case 32 : __asm{
push es
mov ax,ds
mov es,ax
mov esi,[rot]
mov edi,[surf]
mov ecx,[ddsdBack.dwHeight]
Oneline32:
push ecx
mov ecx,[ddsdBack.dwWidth]
rep movsd
add edi,[wta]
pop ecx
loop Oneline32
pop es
} break;
case 24 : __asm{
push es
mov ax,ds
mov es,ax
mov esi,[rot]
mov edi,[surf]
mov ecx,[ddsdBack.dwHeight]
Oneline24:
push ecx
mov ecx,[ddsdBack.dwWidth]
Onepix24:
movsw
movsb
inc esi
loop Onepix24
add edi,[wta]
pop ecx
loop Oneline24
pop es
} break;
case 16 : __asm{
push es
mov ax,ds
mov es,ax
mov esi,[rot]
mov edi,[surf]
mov ecx,[ddsdBack.dwHeight]
Oneline16:
push ecx
mov ecx,[ddsdBack.dwWidth]
Onepix16:
lodsd
shr ax,2
shl al,2
shr ax,3
mov bx,ax
shr eax,8
and ax,1111100000000000b
or ax,bx
stosw
loop Onepix16
add edi,[wta]
pop ecx
loop Oneline16
pop es
} break;
case 15 : __asm{
push es
mov ax,ds
mov es,ax
mov esi,[rot]
mov edi,[surf]
mov ecx,[ddsdBack.dwHeight]
Oneline15:
push ecx
mov ecx,[ddsdBack.dwWidth]
Onepix15:
lodsd
shr ax,3
shl al,3
shr ax,3
mov bx,ax
shr eax,9
and ax,0111110000000000b
or ax,bx
stosw
loop Onepix15
add edi,[wta]
pop ecx
loop Oneline15
pop es
} break;
}
g_pDDSBack->Unlock(NULL);
if (fullscreen)
g_pDDSPrimary->Flip(NULL,NULL);
else
g_pDDSPrimary->Blt(&g_rcScreen, g_pDDSBack,&g_rcViewport,DDBLT_WAIT,NULL);
mouserelease = 0;
}
typedef HRESULT(WINAPI * DIRECTDRAWCREATEEX)( GUID*, VOID**, REFIID, IUnknown* );
DIRECTDRAWCREATEEX _DirectDrawCreateEx = NULL;
#undef DEFINE_GUID
#define DEFINE_GUID(n,a,b,c,d,e,f,g,h,i,j,k) const GUID n = {a,b,c,{d,e,f,g,h,i,j,k}}
DEFINE_GUID( IID_IDirectDraw7, 0x15e65ec0, 0x3b9c, 0x11d2, 0xb9, 0x2f, 0x00,0x60,0x97,0x97,0xea,0x5b );
int czGraphics::InitDirectDraw( )
{
int xres = CZ_WIDTH;
int yres = CZ_HEIGHT;
HINSTANCE hDLL = LoadLibrary( _T("DDRAW.DLL") );
CZDEBUG_ASSERT_RETURN( hDLL );
_DirectDrawCreateEx = (DIRECTDRAWCREATEEX)GetProcAddress( hDLL, "DirectDrawCreateEx" );
CZDEBUG_ASSERT_RETURN( _DirectDrawCreateEx );
int h;
//LOG.Printf("[gfx] DirectDrawCreateEx()...\n");
h = _DirectDrawCreateEx(NULL,(VOID**)&g_pDD, IID_IDirectDraw7, NULL);
CZDEBUG_ASSERT_RETURN( h == DD_OK );
if (fullscreen) {
//LOG.Printf("[gfx] SetCooperativeLevel()...\n");
h=g_pDD->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE| DDSCL_FULLSCREEN);
CZDEBUG_ASSERT_RETURN( h == DD_OK );
//LOG.Printf("[gfx] SetDisplayMode(32)...\n");
h = g_pDD->SetDisplayMode(xres, yres, bpp=32, 0, 0);
if(h != DD_OK) {
//LOG.Printf("[gfx] SetDisplayMode(24)...\n");
h = g_pDD->SetDisplayMode(xres, yres, bpp=24, 0, 0);
if(h != DD_OK) {
//LOG.Printf("[gfx] SetDisplayMode(16)...\n");
h = g_pDD->SetDisplayMode(xres, yres, bpp=16, 0, 0);
if(h != DD_OK) {
CZDEBUG_ASSERT_RETURN( h == DD_OK );
} else {
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
//LOG.Printf("[gfx] GetDisplayMode(16)...\n");
g_pDD->GetDisplayMode(&ddsd);
if(bpp == 16){
if(ddsd.ddpfPixelFormat.dwRBitMask == 0x7c00){
bpp = 15;
}else if(ddsd.ddpfPixelFormat.dwRBitMask != 0xf800){
CZDEBUG_ASSERT_RETURN( h == DD_OK );
exit(0);
}
}
}
}
}
//LOG.Printf("[gfx] bpp == %d...\n",bpp);
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
//LOG.Printf("[gfx] CreateSurface()...\n");
h = g_pDD->CreateSurface( &ddsd, &g_pDDSPrimary, NULL);
CZDEBUG_ASSERT_RETURN( h == DD_OK );
DDSCAPS2 ddscaps;
ZeroMemory(&ddscaps, sizeof(ddscaps));
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
//LOG.Printf("[gfx] GetAttachedSurface()...\n");
h = g_pDDSPrimary->GetAttachedSurface(&ddscaps, &g_pDDSBack);
CZDEBUG_ASSERT_RETURN( h == DD_OK );
ShowCursor(FALSE);
}
else
{
//LOG.Printf("[gfx] SetCooperativeLevel()...\n");
h=g_pDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
CZDEBUG_ASSERT_RETURN( h == DD_OK );
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
//LOG.Printf("[gfx] CreateSurface(g_pDDSPrimary)...\n");
h = g_pDD->CreateSurface(&ddsd, &g_pDDSPrimary, NULL);
CZDEBUG_ASSERT_RETURN( h == DD_OK );
LPDIRECTDRAWCLIPPER pClipper;
//LOG.Printf("[gfx] CreateClipper()...\n");
h = g_pDD->CreateClipper(0, &pClipper, NULL);
CZDEBUG_ASSERT_RETURN( h == DD_OK );
pClipper->SetHWnd(0, hWnd);
//LOG.Printf("[gfx] SetClipper()...\n");
h = g_pDDSPrimary->SetClipper(pClipper);
pClipper->Release();
CZDEBUG_ASSERT_RETURN( h == DD_OK );
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
ddsd.dwWidth = xres;
ddsd.dwHeight = yres;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
//LOG.Printf("[gfx] CreateSurface(g_pDDSBack)...\n");
h = g_pDD->CreateSurface(&ddsd, &g_pDDSBack, NULL);
CZDEBUG_ASSERT_RETURN( h == DD_OK );
bpp = GetDeviceCaps(GetDC(hWnd),BITSPIXEL);
}
return 1;
}
void czGraphics::DestroyDirectDraw()
{
if (g_pDD != NULL){
g_pDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
if (g_pDDSBack != NULL) {
g_pDDSBack->Release();
g_pDDSBack = NULL;
}
if (g_pDDSPrimary != NULL) {
g_pDDSPrimary->Release();
g_pDDSPrimary = NULL;
}
g_pDD->Release();
}
}