-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameWindow.cpp
531 lines (421 loc) · 14.1 KB
/
GameWindow.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "stdafx.h"
//this function is primarily to help me debug.
void CheckErr(HRESULT hr)
{
//char szHR[16];
//ltoa((LONG)hr, szHR, 10);
//::MessageBox(NULL, (LPCTSTR)szHR, "HRESULT VALUE", MB_OK);
switch(hr)
{
case S_OK:
::MessageBox(NULL, "S_OK", "NO ERROR", MB_OK);
break;
case S_FALSE:
::MessageBox(NULL, "S_FALSE", "FALSE", MB_OK);
break;
case E_FAIL:
::MessageBox(NULL, "Err: E_FAIL", "ERROR!", MB_OK);
break;
case E_POINTER:
::MessageBox(NULL, "Err: E_POINTER", "ERROR!", MB_OK);
break;
case CONNECT_E_NOCONNECTION:
::MessageBox(NULL, "Err: CONNECT_E_NOCONNECTION", "ERROR!", MB_OK);
break;
case E_NOINTERFACE:
::MessageBox(NULL, "Err: E_NOINTERFACE", "ERROR!", MB_OK);
break;
case E_UNEXPECTED:
::MessageBox(NULL, "Err: E_UNEXPECTED", "ERROR!", MB_OK);
break;
case DISP_E_EXCEPTION:
::MessageBox(NULL, "Err: DISP_E_EXCEPTION", "ERROR!", MB_OK);
break;
case DISP_E_UNKNOWNLCID:
::MessageBox(NULL, "Err: DISP_E_UNKNOWNLCID", "ERROR!", MB_OK);
break;
default:
::MessageBox(NULL, "Err: ??????????", "", MB_OK);
break;
}
}
void ShowLastError(LPTSTR funcName)
{
TCHAR szErrMsg[100];
LPVOID lpMsgBuf;
DWORD lastErr = GetLastError(); //grab the error code of last error
//this will format the message into our buffer, using GetLastError() as its source
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL,
lastErr, GetSystemDefaultLangID(), (LPTSTR)&lpMsgBuf, 0, NULL);
//format, and output our information from the buffer into szErrMsg string.
wsprintf(szErrMsg, " Function: %s\n Error#: %d\n Description: %s",
funcName, lastErr, lpMsgBuf);
::MessageBox(NULL, szErrMsg, "Internal Error", MB_OK);
//free the buffer from memory
LocalFree(lpMsgBuf);
}
//converts binary data in ascii form to binary data in a BSTR
void BinAsciiToBinBSTR(LPCSTR szBinDataA, UINT iBinALen, BSTR* szBinData_Buf)
{
//specify the length for the unicode string
UINT iBinAWLen = iBinALen * 2;
//allocate memory for our unicode string
WCHAR* szBinDataW = new WCHAR[iBinALen];
//allocate memory for the ascii disguised unicode string
CHAR* szBinDataAW = new CHAR[iBinAWLen];
//this api function will convert our ascii data to unicode
INT convLen = MultiByteToWideChar( CP_ACP,
0,
szBinDataA,
iBinALen,
szBinDataW,
iBinALen);
//copy our unicode memory block into our ascii string memory buffer
CopyMemory(szBinDataAW, szBinDataW, iBinAWLen);
//places the ascii data (as it is) into our BSTR buffer
*szBinData_Buf = SysAllocStringByteLen(szBinDataAW, iBinAWLen);
//free memory
delete[] szBinDataW;
delete[] szBinDataAW;
}
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC hdcMem, hdcMem2;
HBITMAP hbmMask;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
SelectObject(hdcMem, hbmColour);
SelectObject(hdcMem2, hbmMask);
SetBkColor(hdcMem, crTransparent);
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
return hbmMask;
}
CHIPVECINFO* CheckConnect4(SHORT CurWndIndex, SLOTWNDINFO *SlotWndInfoArr)
{
CHIPVECINFO *vecConFour = new CHIPVECINFO[4];
SHORT chipX, chipY;
chipX = CurWndIndex;
chipY = (GAME_ROWS-1) - (SlotWndInfoArr[CurWndIndex].chipCnt - 1);
//Get rect of overall area
RECT SlotsRect;
SlotsRect.left = 0;
SlotsRect.top = 0;
SlotsRect.right = GAME_COLS-1;
SlotsRect.bottom = GAME_ROWS-1;
//Get rect of the area bounding all possibilities
//i.e CurWndIndex - 3, CurWndIndex + 3
// SlotWndInfo.chipCnt - 3, SlotWndInfo.chipCnt + 3
RECT Con4RectBounds;
Con4RectBounds.left = chipX - 3;
Con4RectBounds.top = chipY - 3;
Con4RectBounds.right = chipX + 3;
Con4RectBounds.bottom = chipY + 3;
//use IntersectRect to clip off all areas in the possibilities rect that
//go out of the bounds in the ovarall area rect.
RECT Con4Rect;
IntersectRect(&Con4Rect, &SlotsRect, &Con4RectBounds);
//create 4 RECT arrays representing the horizontal, vertical,
//left-diagonal, right-diagonal directions of possibilities
SHORT chipCnt = 0;
bool foundCon4 = false;
UINT lastChipState;
UINT curChipState;
//check for a horizontal connect 4
for(SHORT i = Con4Rect.left; i <= Con4Rect.right; i++)
{
curChipState = SlotWndInfoArr[i].chipLocState[chipY];
//if its the first time through don't bother checking if our current
//chip matches the last chip
if(i > Con4Rect.left)
chipCnt = (curChipState == lastChipState) ? chipCnt+1 : 0;
if(chipCnt == 3)
{
vecConFour[0].points[0].x = i-3;
vecConFour[0].points[1].x = i-2;
vecConFour[0].points[2].x = i-1;
vecConFour[0].points[3].x = i;
vecConFour[0].points[3].y = vecConFour[0].points[2].y =
vecConFour[0].points[1].y = vecConFour[0].points[0].y = chipY;
vecConFour[0].vecType = HORZ;
vecConFour[0].IsConnectFour = true;
foundCon4 = true;
break;
}
lastChipState = curChipState;
}
//check for vertical connect 4
chipCnt = 0;
for(SHORT i = Con4Rect.top; i <= Con4Rect.bottom; i++)
{
curChipState = SlotWndInfoArr[chipX].chipLocState[i];
if(i > Con4Rect.top)
chipCnt = (curChipState == lastChipState) ? chipCnt+1 : 0;
if(chipCnt == 3)
{
vecConFour[1].points[0].y = i-3;
vecConFour[1].points[1].y = i-2;
vecConFour[1].points[2].y = i-1;
vecConFour[1].points[3].y = i;
vecConFour[1].points[3].x = vecConFour[1].points[2].x =
vecConFour[1].points[1].x = vecConFour[1].points[0].x = chipX;
vecConFour[1].vecType = VERT;
vecConFour[1].IsConnectFour = true;
foundCon4 = true;
break;
}
lastChipState = curChipState;
}
//grab the coordinates from our possibilites bound rect, not just from the
//possibilities bounds rect that intersects with the overall slots rect.
//so that we know our points align up with the placement of our chip
POINT diagStartPnt;
diagStartPnt.x = Con4RectBounds.left;
diagStartPnt.y = Con4RectBounds.top;
//check for left diagonal connect4
bool firstLoop = true;
chipCnt = 0;
while(PtInRect(&Con4RectBounds, diagStartPnt) |
(diagStartPnt.x == Con4RectBounds.right &&
diagStartPnt.y == Con4RectBounds.bottom))
{
if(PtInRect(&Con4Rect, diagStartPnt) |
(diagStartPnt.x == Con4Rect.right && diagStartPnt.y == Con4Rect.bottom))
{
curChipState = SlotWndInfoArr[diagStartPnt.x].chipLocState[diagStartPnt.y];
if(firstLoop == false)
chipCnt = (curChipState == lastChipState) ? chipCnt+1 : 0;
if(chipCnt == 3)
{
vecConFour[2].points[0].x = diagStartPnt.x-3;
vecConFour[2].points[0].y = diagStartPnt.y-3;
vecConFour[2].points[1].x = diagStartPnt.x-2;
vecConFour[2].points[1].y = diagStartPnt.y-2;
vecConFour[2].points[2].x = diagStartPnt.x-1;
vecConFour[2].points[2].y = diagStartPnt.y-1;
vecConFour[2].points[3].x = diagStartPnt.x;
vecConFour[2].points[3].y = diagStartPnt.y;
vecConFour[2].vecType = DESCDIAG;
vecConFour[2].IsConnectFour = true;
foundCon4 = true;
break;
}
if(firstLoop)
firstLoop = false;
lastChipState = curChipState;
}
diagStartPnt.x++;
diagStartPnt.y++;
};
diagStartPnt.x = Con4RectBounds.right;
diagStartPnt.y = Con4RectBounds.top;
//check for right diagonal connect four
firstLoop = true;
chipCnt = 0;
while((diagStartPnt.x <= Con4RectBounds.right &&
diagStartPnt.y >= Con4RectBounds.top) &&
(diagStartPnt.x >= Con4RectBounds.left &&
diagStartPnt.y <= Con4RectBounds.bottom))
{
if((diagStartPnt.x <= Con4Rect.right &&
diagStartPnt.y >= Con4Rect.top) &&
(diagStartPnt.x >= Con4Rect.left &&
diagStartPnt.y <= Con4Rect.bottom))
{
curChipState = SlotWndInfoArr[diagStartPnt.x].chipLocState[diagStartPnt.y];
if(firstLoop == false)
chipCnt = (curChipState == lastChipState) ? chipCnt+1 : 0;
if(chipCnt == 3)
{
vecConFour[3].points[0].x = diagStartPnt.x+3;
vecConFour[3].points[0].y = diagStartPnt.y-3;
vecConFour[3].points[1].x = diagStartPnt.x+2;
vecConFour[3].points[1].y = diagStartPnt.y-2;
vecConFour[3].points[2].x = diagStartPnt.x+1;
vecConFour[3].points[2].y = diagStartPnt.y-1;
vecConFour[3].points[3].x = diagStartPnt.x;
vecConFour[3].points[3].y = diagStartPnt.y;
vecConFour[3].vecType = ASCDIAG;
vecConFour[3].IsConnectFour = true;
foundCon4 = true;
break;
}
if(firstLoop)
firstLoop = false;
lastChipState = curChipState;
}
diagStartPnt.x--;
diagStartPnt.y++;
};
if(foundCon4)
return vecConFour;
else
return NULL;
}
void DrawAllChips(HDC hdc, HDC hdcMem, HBITMAP hbmChip,
CHIPLOCSTATE ChipType, SLOTWNDINFO *lpSlotWndInfo, DWORD dwROP)
{
BITMAP bm;
INT Y = SLOT_HEIGHT - 34;
SelectObject(hdcMem, hbmChip);
GetObject(hbmChip, sizeof(bm), &bm);
//look through, and draw the specified bitmap for the specified chiptype
for(int i = 0; i < lpSlotWndInfo->chipCnt; i++)
{
if(lpSlotWndInfo->chipLocState[(GAME_ROWS-1) - i] == ChipType)
BitBlt(hdc, 0, Y, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, dwROP);
Y -= 36;
}
}
void DrawTurnIndicator(HWND hWnd, CURRENTTURN CurrentTurn, COLORREF clrRect)
{
HDC hdc = GetDC(hWnd);
DrawTurnIndicator(hdc, CurrentTurn, clrRect);
ReleaseDC(hWnd, hdc);
}
void DrawTurnIndicator(HDC hdc, CURRENTTURN CurrentTurn, COLORREF clrRect)
{
RECT rectIndicator;
if(CurrentTurn == REDPLAYER)
{
rectIndicator.left = 310;
rectIndicator.top = 5;
rectIndicator.right = rectIndicator.left+58;
rectIndicator.bottom = rectIndicator.top+290;
}
else if(CurrentTurn == BLUEPLAYER)
{
rectIndicator.left = 368;
rectIndicator.top = 5;
rectIndicator.right = rectIndicator.left+58;
rectIndicator.bottom = rectIndicator.top+290;
}
HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
/*if(IsTransparent)
SelectObject(hdc, GetStockObject(NULL_PEN));
RoundRect(hdc, rectIndicator.left, rectIndicator.top,
rectIndicator.right, rectIndicator.bottom, 15, 15);
SelectObject(hdc, GetStockObject(BLACK_PEN))
else
{*/
SelectObject(hdc, CreatePen(PS_SOLID, 3, clrRect));
RoundRect(hdc, rectIndicator.left, rectIndicator.top,
rectIndicator.right, rectIndicator.bottom, 15, 15);
DeleteObject(SelectObject(hdc, GetStockObject(BLACK_PEN)));
//}
SelectObject(hdc, oldBrush);
}
void HighLightConnectFour(HBITMAP hbmHLChip, HBITMAP hbmHLChipMask,
CHIPVECINFO *lpConFourVecs, SLOTWNDINFO *SlotWndInfo)
{
for(int i = 0; i < 4; i++)
{
if(lpConFourVecs[i].IsConnectFour == true)
{
POINTS *pnt = lpConFourVecs[i].points;
switch(lpConFourVecs[i].vecType)
{
//if its vertical all the chips are within the same slot window
case VERT:
{
HDC hdc = GetDC(SlotWndInfo[pnt[0].x].hWnd);
HDC hdcChipBuffer = CreateCompatibleDC(hdc);
HDC hdcBuffer = CreateCompatibleDC(hdc);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmBuffer = CreateCompatibleBitmap( hdc,
CHIP_WIDTH,
(36 * 4));
HBITMAP hbmChipBuffer = CreateCompatibleBitmap( hdc,
CHIP_WIDTH,
CHIP_HEIGHT);
HBITMAP hbmOld;
HBITMAP hbmOldBuffer;
HBITMAP hbmOldChipBuffer;
BITMAP bm;
hbmOldChipBuffer = (HBITMAP)SelectObject(hdcChipBuffer, hbmChipBuffer);
hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);
COLORREF clrYellow = RGB(214, 214, 0);
RECT r;
SetRect(&r, 0, 0, CHIP_WIDTH, (36 * 4));
RECT r2;
SetRect(&r2, 0, 0, CHIP_WIDTH, CHIP_HEIGHT);
HBRUSH hBrush = CreateSolidBrush(clrYellow);
FillRect(hdcChipBuffer, &r2, hBrush);
FillRect(hdcBuffer, &r, hBrush);
hbmOld = (HBITMAP)SelectObject(hdcMem, hbmHLChipMask);
GetObject(hbmHLChipMask, sizeof(bm), &bm);
BitBlt(hdcChipBuffer, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCAND);
SelectObject(hdcMem, hbmHLChip);
GetObject(hbmHLChip, sizeof(bm), &bm);
BitBlt(hdcChipBuffer, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCPAINT);
int i = 0;
UINT Y = 0;
for(i = 0; i < 4; i++)
{
BitBlt(hdcBuffer, 0, Y, bm.bmWidth, bm.bmHeight, hdcChipBuffer, 0, 0, SRCCOPY);
Y += 36;
}
Y = (36 * pnt[0].y);
BitBlt(hdc, 0, Y, bm.bmWidth, (36 * 4), hdcBuffer, 0, 0, SRCCOPY);
SelectObject(hdcChipBuffer, hbmOldChipBuffer);
DeleteDC(hdcChipBuffer);
SelectObject(hdcBuffer, hbmOldBuffer);
DeleteDC(hdcBuffer);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
DeleteObject(hbmBuffer);
DeleteObject(hBrush);
ReleaseDC(SlotWndInfo[pnt[0].x].hWnd, hdc);
}
break;
//otherwise there spread accross seperate slot windows
default:
{
HDC hdc[4];
HDC hdcMem;
HBITMAP hbmOld;
BITMAP bm;
//get the dc's of the 4 windows the connect four vector
//are spread across;
hdc[0] = GetDC(SlotWndInfo[pnt[0].x].hWnd);
hdc[1] = GetDC(SlotWndInfo[pnt[1].x].hWnd);
hdc[2] = GetDC(SlotWndInfo[pnt[2].x].hWnd);
hdc[3] = GetDC(SlotWndInfo[pnt[3].x].hWnd);
//we only need one memory dc, it will work with all of our
//previously obtained dc's
hdcMem = CreateCompatibleDC(hdc[0]);
hbmOld = (HBITMAP)SelectObject(hdcMem, hbmHLChipMask);
GetObject(hbmHLChipMask, sizeof(bm), &bm);
INT i = 0;
INT Y;
for(; i < 4; i++)
{
Y = ((CHIP_HEIGHT + SLOT_VSPACE) * pnt[i].y);
BitBlt(hdc[i], 0, Y, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCAND);
}
SelectObject(hdcMem, hbmHLChip);
GetObject(hbmHLChip, sizeof(bm), &bm);
for(i = 0; i < 4; i++)
{
Y = ((CHIP_HEIGHT + SLOT_VSPACE) * pnt[i].y);
BitBlt(hdc[i], 0, Y, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCPAINT);
}
//free our bitmap GDI object out of the dc.
SelectObject(hdcMem, hbmOld);
//free all of our dc's
DeleteDC(hdcMem);
ReleaseDC(SlotWndInfo[pnt[0].x].hWnd, hdc[0]);
ReleaseDC(SlotWndInfo[pnt[1].x].hWnd, hdc[1]);
ReleaseDC(SlotWndInfo[pnt[2].x].hWnd, hdc[2]);
ReleaseDC(SlotWndInfo[pnt[3].x].hWnd, hdc[3]);
}
break;
}
}
}
}