Take care of one BSOD in NtGdiDdCreateDirectDrawObject, it is not correct fix, it...
[reactos.git] / rosapps / games / winemine / main.c
1 /*
2 * WineMine (main.c)
3 *
4 * Copyright 2000 Joshua Thielen <jt85296@ltu.edu>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 #include <windows.h>
25 #include "main.h"
26 #include "dialog.h"
27 #include "resource.h"
28
29 /* Work around a Wine bug which defines handles as UINT rather than LPVOID */
30 #ifdef WINE_STRICT
31 #define NULL_HANDLE NULL
32 #else
33 #define NULL_HANDLE 0
34 #endif
35
36 #ifdef DUMB_DEBUG
37 #include <stdio.h>
38 #define DEBUG(x) fprintf(stderr,x)
39 #else
40 #define DEBUG(x)
41 #endif
42
43
44 int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdline, int cmdshow )
45 {
46 MSG msg;
47 WNDCLASS wc;
48 HWND hWnd;
49 HACCEL haccel;
50 char appname[9];
51
52 LoadString( hInst, IDS_APPNAME, appname, sizeof(appname));
53
54 wc.style = 0;
55 wc.lpfnWndProc = MainProc;
56 wc.cbClsExtra = 0;
57 wc.cbWndExtra = 0;
58 wc.hInstance = hInst;
59 wc.hIcon = LoadIcon( hInst, appname );
60 wc.hCursor = LoadCursor( NULL_HANDLE, (LPCTSTR)IDI_APPLICATION );
61 wc.hbrBackground = (HBRUSH) GetStockObject( BLACK_BRUSH );
62 wc.lpszMenuName = "MENU_WINEMINE";
63 wc.lpszClassName = appname;
64
65 if (!RegisterClass(&wc)) exit(1);
66 hWnd = CreateWindow( appname, appname,
67 WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
68 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
69 NULL_HANDLE, NULL_HANDLE, hInst, NULL );
70
71 if (!hWnd) exit(1);
72
73 ShowWindow( hWnd, cmdshow );
74 UpdateWindow( hWnd );
75
76 haccel = LoadAccelerators( hInst, appname );
77 SetTimer( hWnd, ID_TIMER, 1000, NULL );
78
79 while( GetMessage(&msg, NULL_HANDLE, 0, 0) ) {
80 if (!TranslateAccelerator( hWnd, haccel, &msg ))
81 TranslateMessage( &msg );
82
83 DispatchMessage( &msg );
84 }
85 return msg.wParam;
86 }
87
88
89 LRESULT WINAPI MainProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
90 {
91 HDC hdc;
92 PAINTSTRUCT ps;
93 HMENU hMenu;
94 static BOARD board;
95
96 switch( msg ) {
97 case WM_CREATE:
98 board.hInst = ((LPCREATESTRUCT) lParam)->hInstance;
99 board.hWnd = hWnd;
100 InitBoard( &board );
101 CreateBoard( &board );
102 return 0;
103
104 case WM_PAINT:
105 {
106 HDC hMemDC;
107
108 DEBUG("WM_PAINT\n");
109 hdc = BeginPaint( hWnd, &ps );
110 hMemDC = CreateCompatibleDC( hdc );
111
112 DrawBoard( hdc, hMemDC, &ps, &board );
113
114 DeleteDC( hMemDC );
115 EndPaint( hWnd, &ps );
116
117 return 0;
118 }
119
120 case WM_MOVE:
121 DEBUG("WM_MOVE\n");
122 board.pos.x = (unsigned) LOWORD(lParam);
123 board.pos.y = (unsigned) HIWORD(lParam);
124 return 0;
125
126 case WM_DESTROY:
127 SaveBoard( &board );
128 DestroyBoard( &board );
129 PostQuitMessage( 0 );
130 return 0;
131
132 case WM_TIMER:
133 if( board.status == PLAYING ) {
134 board.time++;
135 RedrawWindow( hWnd, &board.timer_rect, NULL_HANDLE,
136 RDW_INVALIDATE | RDW_UPDATENOW );
137 }
138 return 0;
139
140 case WM_LBUTTONDOWN:
141 DEBUG("WM_LBUTTONDOWN\n");
142 if( wParam & MK_RBUTTON )
143 msg = WM_MBUTTONDOWN;
144 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
145 SetCapture( hWnd );
146 return 0;
147
148 case WM_LBUTTONUP:
149 DEBUG("WM_LBUTTONUP\n");
150 if( wParam & MK_RBUTTON )
151 msg = WM_MBUTTONUP;
152 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
153 ReleaseCapture();
154 return 0;
155
156 case WM_RBUTTONDOWN:
157 DEBUG("WM_RBUTTONDOWN\n");
158 if( wParam & MK_LBUTTON ) {
159 board.press.x = 0;
160 board.press.y = 0;
161 msg = WM_MBUTTONDOWN;
162 }
163 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
164 return 0;
165
166 case WM_RBUTTONUP:
167 DEBUG("WM_RBUTTONUP\n");
168 if( wParam & MK_LBUTTON )
169 msg = WM_MBUTTONUP;
170 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
171 return 0;
172
173 case WM_MBUTTONDOWN:
174 DEBUG("WM_MBUTTONDOWN\n");
175 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
176 return 0;
177
178 case WM_MBUTTONUP:
179 DEBUG("WM_MBUTTONUP\n");
180 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
181 return 0;
182
183 case WM_MOUSEMOVE:
184 {
185 if( (wParam & MK_LBUTTON) && (wParam & MK_RBUTTON) ) {
186 msg = WM_MBUTTONDOWN;
187 }
188 else if( wParam & MK_LBUTTON ) {
189 msg = WM_LBUTTONDOWN;
190 }
191 else {
192 return 0;
193 }
194
195 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
196
197 return 0;
198 }
199
200 case WM_COMMAND:
201 switch(LOWORD(wParam)) {
202 case IDM_NEW:
203 CreateBoard( &board );
204 return 0;
205
206 case IDM_MARKQ:
207 hMenu = GetMenu( hWnd );
208 board.IsMarkQ = !board.IsMarkQ;
209 if( board.IsMarkQ )
210 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
211 else
212 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
213 return 0;
214
215 case IDM_BEGINNER:
216 SetDifficulty( &board, BEGINNER );
217 CreateBoard( &board );
218 return 0;
219
220 case IDM_ADVANCED:
221 SetDifficulty( &board, ADVANCED );
222 CreateBoard( &board );
223 return 0;
224
225 case IDM_EXPERT:
226 SetDifficulty( &board, EXPERT );
227 CreateBoard( &board );
228 return 0;
229
230 case IDM_CUSTOM:
231 SetDifficulty( &board, CUSTOM );
232 CreateBoard( &board );
233 return 0;
234
235 case IDM_EXIT:
236 SendMessage( hWnd, WM_CLOSE, 0, 0);
237 return 0;
238
239 case IDM_TIMES:
240 DialogBoxParam( board.hInst, "DLG_TIMES", hWnd,
241 TimesDlgProc, (LPARAM) &board);
242 return 0;
243
244 case IDM_ABOUT:
245 DialogBox( board.hInst, "DLG_ABOUT", hWnd, AboutDlgProc );
246 return 0;
247 default:
248 DEBUG("Unknown WM_COMMAND command message received\n");
249 break;
250 }
251 }
252 return( DefWindowProc( hWnd, msg, wParam, lParam ));
253 }
254
255 void InitBoard( BOARD *p_board )
256 {
257 HMENU hMenu;
258
259 p_board->hMinesBMP = LoadBitmap( p_board->hInst, (LPCSTR) IDB_MINES);
260 p_board->hFacesBMP = LoadBitmap( p_board->hInst, (LPCSTR) IDB_FACES);
261 p_board->hLedsBMP = LoadBitmap( p_board->hInst, (LPCSTR) IDB_LEDS);
262
263 LoadBoard( p_board );
264
265 if( p_board->pos.x < (unsigned) GetSystemMetrics( SM_CXFIXEDFRAME ))
266 p_board->pos.x = GetSystemMetrics( SM_CXFIXEDFRAME );
267
268 if( p_board->pos.x > (unsigned) (GetSystemMetrics( SM_CXSCREEN )
269 - GetSystemMetrics( SM_CXFIXEDFRAME ))) {
270 p_board->pos.x = GetSystemMetrics( SM_CXSCREEN )
271 - GetSystemMetrics( SM_CXFIXEDFRAME );
272 }
273
274 if( p_board->pos.y < (unsigned) (GetSystemMetrics( SM_CYMENU )
275 + GetSystemMetrics( SM_CYCAPTION )
276 + GetSystemMetrics( SM_CYFIXEDFRAME ))) {
277 p_board->pos.y = GetSystemMetrics( SM_CYMENU ) +
278 GetSystemMetrics( SM_CYCAPTION ) +
279 GetSystemMetrics( SM_CYFIXEDFRAME );
280 }
281
282 if( p_board->pos.y > (unsigned) (GetSystemMetrics( SM_CYSCREEN )
283 - GetSystemMetrics( SM_CYFIXEDFRAME ))) {
284 p_board->pos.y = GetSystemMetrics( SM_CYSCREEN )
285 - GetSystemMetrics( SM_CYFIXEDFRAME );
286 }
287
288 hMenu = GetMenu( p_board->hWnd );
289 CheckMenuItem( hMenu, IDM_BEGINNER + (unsigned) p_board->difficulty,
290 MF_CHECKED );
291 if( p_board->IsMarkQ )
292 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
293 else
294 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
295 CheckLevel( p_board );
296 }
297
298 void LoadBoard( BOARD *p_board )
299 {
300 DWORD size;
301 DWORD type;
302 HKEY hkey;
303 char data[16];
304 char key_name[8];
305 unsigned i;
306
307
308 RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\Wine\\WineMine",
309 0, KEY_QUERY_VALUE, &hkey );
310
311 size = sizeof( data );
312 if( RegQueryValueEx( hkey, "Xpos", NULL, (LPDWORD) &type,
313 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS ) {
314 p_board->pos.x = atoi( data );
315 }
316 else
317 p_board->pos.x = GetSystemMetrics( SM_CXFIXEDFRAME );
318
319 size = sizeof( data );
320 if( RegQueryValueEx( hkey, "Ypos", NULL, (LPDWORD) &type,
321 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
322 p_board->pos.y = atoi( data );
323 else
324 p_board->pos.y = GetSystemMetrics( SM_CYMENU )
325 + GetSystemMetrics( SM_CYCAPTION )
326 + GetSystemMetrics( SM_CYFIXEDFRAME );
327
328 size = sizeof( data );
329 if( RegQueryValueEx( hkey, "Rows", NULL, (LPDWORD) &type,
330 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
331 p_board->rows = atoi( data );
332 else
333 p_board->rows = BEGINNER_ROWS;
334
335 size = sizeof( data );
336 if( RegQueryValueEx( hkey, "Cols", NULL, (LPDWORD) &type,
337 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
338 p_board->cols = atoi( data );
339 else
340 p_board->cols = BEGINNER_COLS;
341
342 size = sizeof( data );
343 if( RegQueryValueEx( hkey, "Mines", NULL, (LPDWORD) &type,
344 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
345 p_board->mines = atoi( data );
346 else
347 p_board->rows = BEGINNER_ROWS;
348
349 size = sizeof( data );
350 if( RegQueryValueEx( hkey, "Difficulty", NULL, (LPDWORD) &type,
351 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
352 p_board->difficulty = (DIFFICULTY) atoi( data );
353 else
354 p_board->difficulty = BEGINNER;
355
356 size = sizeof( data );
357 if( RegQueryValueEx( hkey, "MarkQ", NULL, (LPDWORD) &type,
358 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
359 p_board->IsMarkQ = atoi( data );
360 else
361 p_board->IsMarkQ = TRUE;
362
363 for( i = 0; i < 3; i++ ) {
364 wsprintf( key_name, "Name%d", i );
365 size = sizeof( data );
366 if( RegQueryValueEx( hkey, key_name, NULL, (LPDWORD) &type,
367 (LPBYTE) data,
368 (LPDWORD) &size ) == ERROR_SUCCESS )
369 strncpy( p_board->best_name[i], data, sizeof( data ) );
370 else
371 wsprintf( p_board->best_name[i], "Nobody");
372 }
373
374 for( i = 0; i < 3; i++ ) {
375 wsprintf( key_name, "Time%d", i );
376 size = sizeof( data );
377 if( RegQueryValueEx( hkey, key_name, NULL, (LPDWORD) &type,
378 (LPBYTE) data,
379 (LPDWORD) &size ) == ERROR_SUCCESS )
380 p_board->best_time[i] = atoi( data );
381 else
382 p_board->best_time[i] = 999;
383 }
384 RegCloseKey( hkey );
385 }
386
387 void SaveBoard( BOARD *p_board )
388 {
389 DWORD disp;
390 HKEY hkey;
391 unsigned i;
392 char data[16];
393 char key_name[8];
394
395 if( RegCreateKeyEx( HKEY_LOCAL_MACHINE,
396 "Software\\Wine\\WineMine", 0, NULL,
397 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
398 &hkey, &disp ) != ERROR_SUCCESS)
399 return;
400
401 wsprintf( data, "%d", p_board->pos.x );
402 RegSetValueEx( hkey, "Xpos", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
403
404 wsprintf( data, "%d", p_board->pos.x );
405 RegSetValueEx( hkey, "Ypos", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
406
407 wsprintf( data, "%d", (int) p_board->difficulty );
408 RegSetValueEx( hkey, "Difficulty", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
409
410 wsprintf( data, "%d", p_board->rows );
411 RegSetValueEx( hkey, "Rows", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
412
413 wsprintf( data, "%d", p_board->cols );
414 RegSetValueEx( hkey, "Cols", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
415
416 wsprintf( data, "%d", p_board->mines );
417 RegSetValueEx( hkey, "Mines", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
418
419 wsprintf( data, "%d", (int) p_board->IsMarkQ );
420 RegSetValueEx( hkey, "MarkQ", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
421
422 for( i = 0; i < 3; i++ ) {
423 wsprintf( key_name, "Name%u", i );
424 strncpy( data, p_board->best_name[i], sizeof( data ) );
425 RegSetValueEx( hkey, key_name, 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
426 }
427
428 for( i = 0; i < 3; i++ ) {
429 wsprintf( key_name, "Time%u", i );
430 wsprintf( data, "%d", p_board->best_time[i] );
431 RegSetValueEx( hkey, key_name, 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
432 }
433 RegCloseKey( hkey );
434 }
435
436 void DestroyBoard( BOARD *p_board )
437 {
438 DeleteObject( p_board->hFacesBMP );
439 DeleteObject( p_board->hLedsBMP );
440 DeleteObject( p_board->hMinesBMP );
441 }
442
443 void SetDifficulty( BOARD *p_board, DIFFICULTY difficulty )
444 {
445 HMENU hMenu = GetMenu( p_board->hWnd );
446
447 CheckMenuItem( hMenu, IDM_BEGINNER + p_board->difficulty, MF_UNCHECKED );
448 p_board->difficulty = difficulty;
449 CheckMenuItem( hMenu, IDM_BEGINNER + difficulty, MF_CHECKED );
450
451 switch( difficulty ) {
452 case BEGINNER:
453 p_board->cols = BEGINNER_COLS;
454 p_board->rows = BEGINNER_ROWS;
455 p_board->mines = BEGINNER_MINES;
456 break;
457
458 case ADVANCED:
459 p_board->cols = ADVANCED_COLS;
460 p_board->rows = ADVANCED_ROWS;
461 p_board->mines = ADVANCED_MINES;
462 break;
463
464 case EXPERT:
465 p_board->cols = EXPERT_COLS;
466 p_board->rows = EXPERT_ROWS;
467 p_board->mines = EXPERT_MINES;
468 break;
469
470 case CUSTOM:
471 DialogBoxParam( p_board->hInst, "DLG_CUSTOM", p_board->hWnd,
472 CustomDlgProc, (LPARAM) p_board);
473 break;
474 }
475 }
476
477 void CreateBoard( BOARD *p_board )
478 {
479 int left, top, bottom, right, wnd_x, wnd_y, wnd_width, wnd_height;
480
481 p_board->mb = MB_NONE;
482 p_board->boxes_left = p_board->cols * p_board->rows - p_board->mines;
483 p_board->num_flags = 0;
484
485 CreateBoxes( p_board );
486
487 p_board->width = p_board->cols * MINE_WIDTH + BOARD_WMARGIN * 2;
488
489 p_board->height = p_board->rows * MINE_HEIGHT + LED_HEIGHT
490 + BOARD_HMARGIN * 3;
491
492 wnd_x = p_board->pos.x - GetSystemMetrics( SM_CXFIXEDFRAME );
493 wnd_y = p_board->pos.y - GetSystemMetrics( SM_CYMENU )
494 - GetSystemMetrics( SM_CYCAPTION )
495 - GetSystemMetrics( SM_CYFIXEDFRAME );
496 wnd_width = p_board->width
497 + GetSystemMetrics( SM_CXFIXEDFRAME ) * 2;
498 wnd_height = p_board->height
499 + GetSystemMetrics( SM_CYMENU )
500 + GetSystemMetrics( SM_CYCAPTION )
501 + GetSystemMetrics( SM_CYFIXEDFRAME ) * 2;
502
503 /* setting the mines rectangle boundary */
504 left = BOARD_WMARGIN;
505 top = BOARD_HMARGIN * 2 + LED_HEIGHT;
506 right = left + p_board->cols * MINE_WIDTH;
507 bottom = top + p_board->rows * MINE_HEIGHT;
508 SetRect( &p_board->mines_rect, left, top, right, bottom );
509
510 /* setting the face rectangle boundary */
511 left = p_board->width / 2 - FACE_WIDTH / 2;
512 top = BOARD_HMARGIN;
513 right = left + FACE_WIDTH;
514 bottom = top + FACE_HEIGHT;
515 SetRect( &p_board->face_rect, left, top, right, bottom );
516
517 /* setting the timer rectangle boundary */
518 left = BOARD_WMARGIN;
519 top = BOARD_HMARGIN;
520 right = left + LED_WIDTH * 3;
521 bottom = top + LED_HEIGHT;
522 SetRect( &p_board->timer_rect, left, top, right, bottom );
523
524 /* setting the counter rectangle boundary */
525 left = p_board->width - BOARD_WMARGIN - LED_WIDTH * 3;
526 top = BOARD_HMARGIN;
527 right = p_board->width - BOARD_WMARGIN;
528 bottom = top + LED_HEIGHT;
529 SetRect( &p_board->counter_rect, left, top, right, bottom );
530
531 p_board->status = WAITING;
532 p_board->face_bmp = SMILE_BMP;
533 p_board->time = 0;
534
535 MoveWindow( p_board->hWnd, wnd_x, wnd_y, wnd_width, wnd_height, TRUE );
536 RedrawWindow( p_board->hWnd, NULL, NULL_HANDLE,
537 RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE );
538 }
539
540
541 void CheckLevel( BOARD *p_board )
542 {
543 if( p_board->rows < BEGINNER_ROWS )
544 p_board->rows = BEGINNER_ROWS;
545
546 if( p_board->rows > MAX_ROWS )
547 p_board->rows = MAX_ROWS;
548
549 if( p_board->cols < BEGINNER_COLS )
550 p_board->cols = BEGINNER_COLS;
551
552 if( p_board->cols > MAX_COLS )
553 p_board->cols = MAX_COLS;
554
555 if( p_board->mines < BEGINNER_MINES )
556 p_board->mines = BEGINNER_MINES;
557
558 if( p_board->mines > p_board->cols * p_board->rows - 1 )
559 p_board->mines = p_board->cols * p_board->rows - 1;
560 }
561
562
563 void CreateBoxes( BOARD *p_board )
564 {
565 int i, j;
566 unsigned col, row;
567
568 srand( (unsigned) time( NULL ) );
569
570 /* Create the boxes...
571 * We actually create them with an empty border,
572 * so special care doesn't have to be taken on the edges
573 */
574
575 for( col = 0; col <= p_board->cols + 1; col++ )
576 for( row = 0; row <= p_board->rows + 1; row++ ) {
577 p_board->box[col][row].IsPressed = FALSE;
578 p_board->box[col][row].IsMine = FALSE;
579 p_board->box[col][row].FlagType = NORMAL;
580 p_board->box[col][row].NumMines = 0;
581 }
582
583 /* create mines */
584 i = 0;
585 while( (unsigned) i < p_board->mines ) {
586 col = (int) (p_board->cols * (float) rand() / RAND_MAX + 1);
587 row = (int) (p_board->rows * (float) rand() / RAND_MAX + 1);
588
589 if( !p_board->box[col][row].IsMine ) {
590 i++;
591 p_board->box[col][row].IsMine = TRUE;
592 }
593 }
594
595 /*
596 * Now we label the remaining boxes with the
597 * number of mines surrounding them.
598 */
599
600 for( col = 1; col < p_board->cols + 1; col++ )
601 for( row = 1; row < p_board->rows + 1; row++ ) {
602 for( i = -1; i <= 1; i++ )
603 for( j = -1; j <= 1; j++ ) {
604 if( p_board->box[col + i][row + j].IsMine ) {
605 p_board->box[col][row].NumMines++ ;
606 }
607 }
608 }
609 }
610
611 void DrawMines ( HDC hdc, HDC hMemDC, BOARD *p_board )
612 {
613 HGDIOBJ hOldObj;
614 unsigned col, row;
615 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
616
617 for( row = 1; row <= p_board->rows; row++ ) {
618 for( col = 1; col <= p_board->cols; col++ ) {
619 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
620 }
621 }
622 SelectObject( hMemDC, hOldObj );
623 }
624
625 void DrawMine( HDC hdc, HDC hMemDC, BOARD *p_board, unsigned col, unsigned row, BOOL IsPressed )
626 {
627 MINEBMP_OFFSET offset = BOX_BMP;
628
629 if( col == 0 || col > p_board->cols || row == 0 || row > p_board->rows )
630 return;
631
632 if( p_board->status == GAMEOVER ) {
633 if( p_board->box[col][row].IsMine ) {
634 switch( p_board->box[col][row].FlagType ) {
635 case FLAG:
636 offset = FLAG_BMP;
637 break;
638 case COMPLETE:
639 offset = EXPLODE_BMP;
640 break;
641 case NORMAL:
642 offset = MINE_BMP;
643 }
644 } else {
645 switch( p_board->box[col][row].FlagType ) {
646 case QUESTION:
647 offset = QUESTION_BMP;
648 break;
649 case FLAG:
650 offset = WRONG_BMP;
651 break;
652 case NORMAL:
653 offset = BOX_BMP;
654 break;
655 case COMPLETE:
656 /* Do nothing */
657 break;
658 default:
659 DEBUG("Unknown FlagType during game over in DrawMine\n");
660 break;
661 }
662 }
663 } else { /* WAITING or PLAYING */
664 switch( p_board->box[col][row].FlagType ) {
665 case QUESTION:
666 if( !IsPressed )
667 offset = QUESTION_BMP;
668 else
669 offset = QPRESS_BMP;
670 break;
671 case FLAG:
672 offset = FLAG_BMP;
673 break;
674 case NORMAL:
675 if( !IsPressed )
676 offset = BOX_BMP;
677 else
678 offset = MPRESS_BMP;
679 break;
680 case COMPLETE:
681 /* Do nothing */
682 break;
683 default:
684 DEBUG("Unknown FlagType while playing in DrawMine\n");
685 break;
686 }
687 }
688
689 if( p_board->box[col][row].FlagType == COMPLETE
690 && !p_board->box[col][row].IsMine )
691 offset = (MINEBMP_OFFSET) p_board->box[col][row].NumMines;
692
693 BitBlt( hdc,
694 (col - 1) * MINE_WIDTH + p_board->mines_rect.left,
695 (row - 1) * MINE_HEIGHT + p_board->mines_rect.top,
696 MINE_WIDTH, MINE_HEIGHT,
697 hMemDC, 0, offset * MINE_HEIGHT, SRCCOPY );
698 }
699
700 void DrawLeds( HDC hdc, HDC hMemDC, BOARD *p_board, int number, int x, int y )
701 {
702 HGDIOBJ hOldObj;
703 unsigned led[3], i;
704 int count;
705
706 count = number;
707 if( count < 1000 ) {
708 if( count >= 0 ) {
709 led[0] = count / 100 ;
710 count -= led[0] * 100;
711 }
712 else {
713 led[0] = 10; /* negative sign */
714 count = -count;
715 }
716 led[1] = count / 10;
717 count -= led[1] * 10;
718 led[2] = count;
719 }
720 else {
721 for( i = 0; i < 3; i++ )
722 led[i] = 10;
723 }
724
725 /* use unlit led if not playing */
726 if( p_board->status == WAITING )
727 for( i = 0; i < 3; i++ )
728 led[i] = 11;
729
730 hOldObj = SelectObject (hMemDC, p_board->hLedsBMP);
731
732 for( i = 0; i < 3; i++ ) {
733 BitBlt( hdc,
734 i * LED_WIDTH + x,
735 y,
736 LED_WIDTH,
737 LED_HEIGHT,
738 hMemDC,
739 0,
740 led[i] * LED_HEIGHT,
741 SRCCOPY);
742 }
743
744 SelectObject( hMemDC, hOldObj );
745 }
746
747
748 void DrawFace( HDC hdc, HDC hMemDC, BOARD *p_board )
749 {
750 HGDIOBJ hOldObj;
751
752 hOldObj = SelectObject (hMemDC, p_board->hFacesBMP);
753
754 BitBlt( hdc,
755 p_board->face_rect.left,
756 p_board->face_rect.top,
757 FACE_WIDTH,
758 FACE_HEIGHT,
759 hMemDC, 0, p_board->face_bmp * FACE_HEIGHT, SRCCOPY);
760
761 SelectObject( hMemDC, hOldObj );
762 }
763
764
765 void DrawBoard( HDC hdc, HDC hMemDC, PAINTSTRUCT *ps, BOARD *p_board )
766 {
767 RECT tmp_rect;
768
769 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->counter_rect ) )
770 DrawLeds( hdc, hMemDC, p_board, p_board->mines - p_board->num_flags,
771 p_board->counter_rect.left,
772 p_board->counter_rect.top );
773
774 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->timer_rect ) )
775 DrawLeds( hdc, hMemDC, p_board, p_board->time,
776 p_board->timer_rect.left,
777 p_board->timer_rect.top );
778
779 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->face_rect ) )
780 DrawFace( hdc, hMemDC, p_board );
781
782 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->mines_rect ) )
783 DrawMines( hdc, hMemDC, p_board );
784 }
785
786
787 void TestBoard( HWND hWnd, BOARD *p_board, unsigned x, unsigned y, int msg )
788 {
789 POINT pt;
790
791 pt.x = x;
792 pt.y = y;
793
794 if( PtInRect( &p_board->mines_rect, pt ) && p_board->status != GAMEOVER
795 && p_board->status != WON )
796 TestMines( p_board, pt, msg );
797 else {
798 UnpressBoxes( p_board,
799 p_board->press.x,
800 p_board->press.y );
801 p_board->press.x = 0;
802 p_board->press.y = 0;
803 }
804
805 if( p_board->boxes_left == 0 ) {
806 p_board->status = WON;
807
808 if( p_board->difficulty != CUSTOM &&
809 p_board->time < p_board->best_time[p_board->difficulty] ) {
810 p_board->best_time[p_board->difficulty] = p_board->time;
811
812 DialogBoxParam( p_board->hInst, "DLG_CONGRATS", hWnd,
813 CongratsDlgProc, (LPARAM) p_board);
814
815 DialogBoxParam( p_board->hInst, "DLG_TIMES", hWnd,
816 TimesDlgProc, (LPARAM) p_board);
817 }
818 }
819 TestFace( p_board, pt, msg );
820 }
821
822 void TestMines( BOARD *p_board, POINT pt, int msg )
823 {
824 BOOL draw = TRUE;
825 unsigned col, row;
826
827 col = (pt.x - p_board->mines_rect.left) / MINE_WIDTH + 1;
828 row = (pt.y - p_board->mines_rect.top ) / MINE_HEIGHT + 1;
829
830 switch ( msg ) {
831 case WM_LBUTTONDOWN:
832 if( p_board->press.x != col || p_board->press.y != row ) {
833 UnpressBox( p_board,
834 p_board->press.x, p_board->press.y );
835 p_board->press.x = col;
836 p_board->press.y = row;
837 PressBox( p_board, col, row );
838 }
839 draw = FALSE;
840 break;
841
842 case WM_LBUTTONUP:
843 if( p_board->press.x != col || p_board->press.y != row )
844 UnpressBox( p_board,
845 p_board->press.x, p_board->press.y );
846 p_board->press.x = 0;
847 p_board->press.y = 0;
848 if( p_board->box[col][row].FlagType != FLAG )
849 p_board->status = PLAYING;
850 CompleteBox( p_board, col, row );
851 break;
852
853 case WM_MBUTTONDOWN:
854 PressBoxes( p_board, col, row );
855 draw = FALSE;
856 break;
857
858 case WM_MBUTTONUP:
859 if( p_board->press.x != col || p_board->press.y != row )
860 UnpressBoxes( p_board,
861 p_board->press.x, p_board->press.y );
862 p_board->press.x = 0;
863 p_board->press.y = 0;
864 CompleteBoxes( p_board, col, row );
865 break;
866
867 case WM_RBUTTONDOWN:
868 AddFlag( p_board, col, row );
869 p_board->status = PLAYING;
870 break;
871 default:
872 DEBUG("Unknown message type received in TestMines\n");
873 break;
874 }
875
876 if( draw )
877 {
878 RedrawWindow( p_board->hWnd, NULL, NULL_HANDLE,
879 RDW_INVALIDATE | RDW_UPDATENOW );
880 }
881 }
882
883
884 void TestFace( BOARD *p_board, POINT pt, int msg )
885 {
886 if( p_board->status == PLAYING || p_board->status == WAITING ) {
887 if( msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN )
888 p_board->face_bmp = OOH_BMP;
889 else p_board->face_bmp = SMILE_BMP;
890 }
891 else if( p_board->status == GAMEOVER )
892 p_board->face_bmp = DEAD_BMP;
893 else if( p_board->status == WON )
894 p_board->face_bmp = COOL_BMP;
895
896 if( PtInRect( &p_board->face_rect, pt ) ) {
897 if( msg == WM_LBUTTONDOWN )
898 p_board->face_bmp = SPRESS_BMP;
899
900 if( msg == WM_LBUTTONUP )
901 CreateBoard( p_board );
902 }
903
904 RedrawWindow( p_board->hWnd, &p_board->face_rect, NULL_HANDLE,
905 RDW_INVALIDATE | RDW_UPDATENOW );
906 }
907
908
909 void CompleteBox( BOARD *p_board, unsigned col, unsigned row )
910 {
911 int i, j;
912
913 if( p_board->box[col][row].FlagType != COMPLETE &&
914 p_board->box[col][row].FlagType != FLAG &&
915 col > 0 && col < p_board->cols + 1 &&
916 row > 0 && row < p_board->rows + 1 ) {
917 p_board->box[col][row].FlagType = COMPLETE;
918
919 if( p_board->box[col][row].IsMine ) {
920 p_board->face_bmp = DEAD_BMP;
921 p_board->status = GAMEOVER;
922 }
923 else if( p_board->status != GAMEOVER )
924 p_board->boxes_left--;
925
926 if( p_board->box[col][row].NumMines == 0 )
927 {
928 for( i = -1; i <= 1; i++ )
929 for( j = -1; j <= 1; j++ )
930 CompleteBox( p_board, col + i, row + j );
931 }
932 }
933 }
934
935
936 void CompleteBoxes( BOARD *p_board, unsigned col, unsigned row )
937 {
938 unsigned numFlags = 0;
939 int i, j;
940
941 if( p_board->box[col][row].FlagType == COMPLETE ) {
942 for( i = -1; i <= 1; i++ )
943 for( j = -1; j <= 1; j++ ) {
944 if( p_board->box[col+i][row+j].FlagType == FLAG )
945 numFlags++;
946 }
947
948 if( numFlags == p_board->box[col][row].NumMines ) {
949 for( i = -1; i <= 1; i++ )
950 for( j = -1; j <= 1; j++ ) {
951 if( p_board->box[col+i][row+j].FlagType != FLAG )
952 CompleteBox( p_board, col+i, row+j );
953 }
954 }
955 }
956 }
957
958
959 void AddFlag( BOARD *p_board, unsigned col, unsigned row )
960 {
961 if( p_board->box[col][row].FlagType != COMPLETE ) {
962 switch( p_board->box[col][row].FlagType ) {
963 case FLAG:
964 if( p_board->IsMarkQ )
965 p_board->box[col][row].FlagType = QUESTION;
966 else
967 p_board->box[col][row].FlagType = NORMAL;
968 p_board->num_flags--;
969 break;
970
971 case QUESTION:
972 p_board->box[col][row].FlagType = NORMAL;
973 break;
974
975 default:
976 p_board->box[col][row].FlagType = FLAG;
977 p_board->num_flags++;
978 }
979 }
980 }
981
982
983 void PressBox( BOARD *p_board, unsigned col, unsigned row )
984 {
985 HDC hdc;
986 HGDIOBJ hOldObj;
987 HDC hMemDC;
988
989 hdc = GetDC( p_board->hWnd );
990 hMemDC = CreateCompatibleDC( hdc );
991 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
992
993 DrawMine( hdc, hMemDC, p_board, col, row, TRUE );
994
995 SelectObject( hMemDC, hOldObj );
996 DeleteDC( hMemDC );
997 ReleaseDC( p_board->hWnd, hdc );
998 }
999
1000
1001 void PressBoxes( BOARD *p_board, unsigned col, unsigned row )
1002 {
1003 int i, j;
1004
1005 for( i = -1; i <= 1; i++ )
1006 for( j = -1; j <= 1; j++ ) {
1007 p_board->box[col + i][row + j].IsPressed = TRUE;
1008 PressBox( p_board, col + i, row + j );
1009 }
1010
1011 for( i = -1; i <= 1; i++ )
1012 for( j = -1; j <= 1; j++ ) {
1013 if( !p_board->box[p_board->press.x + i][p_board->press.y + j].IsPressed )
1014 UnpressBox( p_board, p_board->press.x + i, p_board->press.y + j );
1015 }
1016
1017 for( i = -1; i <= 1; i++ )
1018 for( j = -1; j <= 1; j++ ) {
1019 p_board->box[col + i][row + j].IsPressed = FALSE;
1020 PressBox( p_board, col + i, row + j );
1021 }
1022
1023 p_board->press.x = col;
1024 p_board->press.y = row;
1025 }
1026
1027
1028 void UnpressBox( BOARD *p_board, unsigned col, unsigned row )
1029 {
1030 HDC hdc;
1031 HGDIOBJ hOldObj;
1032 HDC hMemDC;
1033
1034 hdc = GetDC( p_board->hWnd );
1035 hMemDC = CreateCompatibleDC( hdc );
1036 hOldObj = SelectObject( hMemDC, p_board->hMinesBMP );
1037
1038 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
1039
1040 SelectObject( hMemDC, hOldObj );
1041 DeleteDC( hMemDC );
1042 ReleaseDC( p_board->hWnd, hdc );
1043 }
1044
1045
1046 void UnpressBoxes( BOARD *p_board, unsigned col, unsigned row )
1047 {
1048 int i, j;
1049
1050 for( i = -1; i <= 1; i++ )
1051 for( j = -1; j <= 1; j++ ) {
1052 UnpressBox( p_board, col + i, row + j );
1053 }
1054 }