-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.cpp
394 lines (328 loc) · 8.42 KB
/
graphics.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
#include "stdafx.h"
#pragma hdrstop
#include "winerrors.h"
#include "dxerrors.h"
#include "a_shared.h"
#include "graphics.h"
#include "screen.h"
#include "ddutil.h"
#include "winexception.h"
#include "debug.h"
#include "winarkanoid.h"
#include "win_gr.h"
#include "controls.h"
/* global variables */
// defined in winarkanoid.h
HWND g_hWnd = NULL;
int fActive = false;
int fMinimized = false;
/* Local variables */
static bool initialized = false;
static bool fullscreen = false;
Renderer* pRenderer;
static struct
{
int width;
int height;
unsigned short* data;
} cache[MAX_IMAGE_CACHE] = {0};
// Local function prototypes
void RegisterMainWindowClass();
HWND CreateMainWindow();
void DetermineWindowSize(SIZE* pSize);
int MapKey(int vkey)
{
switch (vkey)
{
case VK_UP:
return KEY_UP;
case VK_DOWN:
return KEY_DOWN;
case VK_LEFT:
return KEY_LEFT;
case VK_RIGHT:
return KEY_RIGHT;
case VK_ESCAPE:
return KEY_ESC;
default:
return KEY_OTHER;
}
}
static LRESULT CALLBACK wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) throw ()
{
switch (message)
{
case WM_HOTKEY:
return 0;
case WM_SYSKEYDOWN:
if (wParam == 13)
{
Graphics::Shutdown();
fullscreen = !fullscreen;
try
{
Graphics::Init();
}
catch (...)
{
fullscreen = !fullscreen;
Graphics::Init();
}
if (!fullscreen)
{
RECT clRect;
clRect.left = clRect.top = 0;
clRect.right = SCN_WIDTH;
clRect.bottom = SCN_HEIGHT;
::AdjustWindowRect(&clRect, WINDOW_STYLE, FALSE);
::MoveWindow(g_hWnd, 20, 20, clRect.right-clRect.left, clRect.bottom-clRect.top, TRUE);
::ShowCursor(TRUE);
}
else
::ShowCursor(FALSE);
return 0;
}
case WM_KEYDOWN:
Controls::keys[MapKey(wParam)] = true;
if (wParam == VK_ESCAPE)
PostQuitMessage(0);
break;
case WM_SYSKEYUP:
case WM_KEYUP:
Controls::keys[MapKey(wParam)] = false;
break;
case WM_ACTIVATE:
switch (LOWORD(wParam)) {
case WA_ACTIVE:
case WA_CLICKACTIVE:
fActive = true;
break;
case WA_INACTIVE:
fActive = false;
break;
}
fMinimized = (BOOL)HIWORD(wParam);
return DefWindowProc(hWnd, message, wParam, lParam);
case WM_SYSCOMMAND:
if (wParam == SC_SCREENSAVE)
return 0;
return DefWindowProc(hWnd, message, wParam, lParam);
//case WM_DESTROY:
//PostQuitMessage(0);
//return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void Graphics::Init()
{
if (initialized)
return;
g_hWnd = CreateMainWindow();
::ShowWindow(g_hWnd, SW_SHOW);
pRenderer = &DirectDrawRenderer1;
pRenderer->Init(fullscreen);
initialized = true;
}
HWND CreateMainWindow() {
RegisterMainWindowClass();
SIZE windowSize;
DetermineWindowSize(&windowSize);
HWND result = ::CreateWindow(
WINDOW_CLASS,
"Arkanoid",
WINDOW_STYLE,
CW_USEDEFAULT, CW_USEDEFAULT, windowSize.cx, windowSize.cy,
NULL, // root window
NULL, // no menu
g_hInstance,
NULL // no params
);
if (result == NULL)
throw WINException("Screen::Init(): CreateWindow(): ");
return result;
}
void RegisterMainWindowClass() {
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)wndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = g_hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = WINDOW_CLASS;
wcex.hIconSm = NULL;
if( !RegisterClassEx(&wcex) )
throw WINException("Îøèáêà ïðè ðåãèñòðàöèè êëàññà îêíà: ");
}
void DetermineWindowSize(SIZE* pSize) {
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = SCN_WIDTH;
rect.bottom = SCN_HEIGHT;
if( !AdjustWindowRect(&rect, WINDOW_STYLE, FALSE) ) {
pSize->cx = SCN_WIDTH + 10;
pSize->cy = SCN_HEIGHT + 10;
return;
}
pSize->cx = rect.right - rect.left;
pSize->cy = rect.bottom - rect.top;
}
void Graphics::Shutdown() throw()
{
initialized = false;
if(pRenderer)
pRenderer->Shutdown();
if (g_hWnd != NULL)
::DestroyWindow(g_hWnd);
::UnregisterClass(WINDOW_CLASS, g_hInstance);
}
/*void Graphics::DrawTransparent(int scx, int scy)
{
RECT rect;
DDSURFACEDESC desc = {0};
rect.left = scx;
rect.right = scx+20;
rect.top = scy;
rect.bottom = scy+20;
int rad = 10;
desc.dwSize = sizeof(desc);
assert_hres(_back->Lock(&rect, &desc, DDLOCK_WAIT, NULL));
for (int y = rect.top; y < rect.bottom; y++)
{
int ofs = desc.dwWidth*y;
int bally = y-rad-rect.top;
for (int x = rect.left; x < rect.right; x++)
{
int ballx = x-rad-rect.left;
int sqrad = ballx*ballx+bally*bally;
if (sqrad <= 10*10)
{
int srcx = x + (ballx<<1);
int srcy = y + (bally<<1);
int srcofs = srcy*desc.dwWidth;
DWORD color1 = ((DWORD*)desc.lpSurface)[srcofs+srcx];
DWORD color2 = ball_tex[ballx+rad][bally+rad];
((DWORD*)desc.lpSurface)[ofs+x] = (color1+color2)/2;
}
}
}
assert_hres(_back->Unlock(desc.lpSurface));
}*/
void Graphics::Clear()
{
if (!initialized)
return;
pRenderer->Clear();
//for (int y = 0; y < SCN_HEIGHT; y++)
// for (int x = 0; x < SCN_WIDTH; x++)
// *(pbitmap+y*pitch/2+x)=0;
}
void Graphics::Flush()
{
if (!initialized)
return;
pRenderer->Flush();
}
int Graphics::LoadImg(const char* img_name)
{
if (!initialized)
throw std::runtime_error("Called Graphics::LoadImg() on not initialized Graphics.");
int free_index = -1;
for (int i = 0; i < MAX_IMAGE_CACHE; i++)
if (cache[i].data == NULL)
{
free_index = i;
break;
}
if (free_index == -1)
throw std::runtime_error("GDI_LoadImage(): No more free cache.");
cache[free_index].data = (unsigned short*)pRenderer->LoadImg(img_name);
/*HBITMAP hbmp;
if ((hbmp = (HBITMAP)::LoadImage(g_hInstance, img_name, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION)) == NULL)
if ((hbmp = (HBITMAP)::LoadImage(NULL, img_name, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE)) == NULL)
throw std::runtime_error(std::string("GDI_LoadImage(): Cannot load image ") += img_name);
BITMAP bitmap;
assert_winerror(::GetObject(hbmp, sizeof(bitmap), &bitmap));
unsigned short* bits = new unsigned short[bitmap.bmWidth*bitmap.bmHeight];
for (int j = 0; j < bitmap.bmWidth*bitmap.bmHeight; j++)
{
unsigned char r, g, b;
b = *((unsigned char*)bitmap.bmBits+j*3)/8;
g = *((unsigned char*)bitmap.bmBits+j*3+1)/4;
r = *((unsigned char*)bitmap.bmBits+j*3+2)/8;
bits[j] = b | (g << 5) | (r << 11);
}
cache[free_index].width = bitmap.bmWidth;
cache[free_index].height = bitmap.bmHeight;
cache[free_index].data = bits;*/
return free_index;
}
void Graphics::DrawImg(int img_id, int scx, int scy, rect_s bmp_rect)
{
if (!initialized)
return;
pRenderer->DrawImg(reinterpret_cast<Image*>(cache[img_id].data), scx, scy, bmp_rect);
/*int srcy1 = cache[img_id].height - bmp_rect.top - bmp_rect.height;
int srcy2 = cache[img_id].height - bmp_rect.top;
int desty;
if (!fullscreen)
desty = SCN_HEIGHT-scy-1;
else
desty = scy;
int bmp_rect_right = bmp_rect.left + bmp_rect.width;
unsigned short* srcoffs = cache[img_id].data+(srcy2-1)*cache[img_id].width;
unsigned short* destoffs = pbitmap+desty*pitch/2;
for (int dy = desty, sy = srcy2-1; sy >= srcy1; !fullscreen ? dy-- : dy++, sy--)
{
for (int dx = scx, sx = bmp_rect.left; sx < bmp_rect_right; dx++, sx++)
{
unsigned short src = *(srcoffs + sx);
unsigned short *pdest = destoffs + dx;
if (src != COLOR_KEY)
*pdest = src;
}
srcoffs -= cache[img_id].width;
if (fullscreen)
destoffs += pitch/2;
else
destoffs -= pitch/2;
}*/
}
/*void Graphics::DrawTransparentImg(int img_id, int scx, int scy, rect_s bmp_rect)
{
if (!initialized)
return;
int srcy1 = cache[img_id].height - bmp_rect.top - bmp_rect.height;
int srcy2 = cache[img_id].height - bmp_rect.top;
int desty;
if (!fullscreen)
desty = SCN_HEIGHT-scy-1;
else
desty = scy;
int bmp_rect_right = bmp_rect.left + bmp_rect.width;
for (int dy = desty, sy = srcy2-1; sy >= srcy1; !fullscreen ? dy-- : dy++, sy--)
{
unsigned short* srcoffs = cache[img_id].data+sy*cache[img_id].width;
unsigned short* destoffs = pbitmap+dy*pitch/2;
for (int dx = scx, sx = bmp_rect.left; sx < bmp_rect_right; dx++, sx++)
{
unsigned short* src = srcoffs + sx;
unsigned short* dest = destoffs + dx;
if (*src != COLOR_KEY)
*dest = *dest/2+*src/2;
}
}
}*/
void Graphics::DrawTxt(int x, int y, const char* text)
{
if (!initialized)
return;
pRenderer->DrawText(x, y, text);
}