added *.map to the cvsignore file
[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, 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 SECURITY_ATTRIBUTES sa;
392 unsigned i;
393 char data[16];
394 char key_name[8];
395
396 if( RegCreateKeyEx( HKEY_LOCAL_MACHINE,
397 "Software\\Wine\\WineMine", 0, NULL,
398 REG_OPTION_NON_VOLATILE, KEY_WRITE, &sa,
399 &hkey, &disp ) != ERROR_SUCCESS)
400 return;
401
402 wsprintf( data, "%d", p_board->pos.x );
403 RegSetValueEx( hkey, "Xpos", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
404
405 wsprintf( data, "%d", p_board->pos.x );
406 RegSetValueEx( hkey, "Ypos", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
407
408 wsprintf( data, "%d", (int) p_board->difficulty );
409 RegSetValueEx( hkey, "Difficulty", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
410
411 wsprintf( data, "%d", p_board->rows );
412 RegSetValueEx( hkey, "Rows", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
413
414 wsprintf( data, "%d", p_board->cols );
415 RegSetValueEx( hkey, "Cols", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
416
417 wsprintf( data, "%d", p_board->mines );
418 RegSetValueEx( hkey, "Mines", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
419
420 wsprintf( data, "%d", (int) p_board->IsMarkQ );
421 RegSetValueEx( hkey, "MarkQ", 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
422
423 for( i = 0; i < 3; i++ ) {
424 wsprintf( key_name, "Name%u", i );
425 strncpy( data, p_board->best_name[i], sizeof( data ) );
426 RegSetValueEx( hkey, key_name, 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
427 }
428
429 for( i = 0; i < 3; i++ ) {
430 wsprintf( key_name, "Time%u", i );
431 wsprintf( data, "%d", p_board->best_time[i] );
432 RegSetValueEx( hkey, key_name, 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
433 }
434 RegCloseKey( hkey );
435 }
436
437 void DestroyBoard( BOARD *p_board )
438 {
439 DeleteObject( p_board->hFacesBMP );
440 DeleteObject( p_board->hLedsBMP );
441 DeleteObject( p_board->hMinesBMP );
442 }
443
444 void SetDifficulty( BOARD *p_board, DIFFICULTY difficulty )
445 {
446 HMENU hMenu = GetMenu( p_board->hWnd );
447
448 CheckMenuItem( hMenu, IDM_BEGINNER + p_board->difficulty, MF_UNCHECKED );
449 p_board->difficulty = difficulty;
450 CheckMenuItem( hMenu, IDM_BEGINNER + difficulty, MF_CHECKED );
451
452 switch( difficulty ) {
453 case BEGINNER:
454 p_board->cols = BEGINNER_COLS;
455 p_board->rows = BEGINNER_ROWS;
456 p_board->mines = BEGINNER_MINES;
457 break;
458
459 case ADVANCED:
460 p_board->cols = ADVANCED_COLS;
461 p_board->rows = ADVANCED_ROWS;
462 p_board->mines = ADVANCED_MINES;
463 break;
464
465 case EXPERT:
466 p_board->cols = EXPERT_COLS;
467 p_board->rows = EXPERT_ROWS;
468 p_board->mines = EXPERT_MINES;
469 break;
470
471 case CUSTOM:
472 DialogBoxParam( p_board->hInst, "DLG_CUSTOM", p_board->hWnd,
473 CustomDlgProc, (LPARAM) p_board);
474 break;
475 }
476 }
477
478 void CreateBoard( BOARD *p_board )
479 {
480 int left, top, bottom, right, wnd_x, wnd_y, wnd_width, wnd_height;
481
482 p_board->mb = MB_NONE;
483 p_board->boxes_left = p_board->cols * p_board->rows - p_board->mines;
484 p_board->num_flags = 0;
485
486 CreateBoxes( p_board );
487
488 p_board->width = p_board->cols * MINE_WIDTH + BOARD_WMARGIN * 2;
489
490 p_board->height = p_board->rows * MINE_HEIGHT + LED_HEIGHT
491 + BOARD_HMARGIN * 3;
492
493 wnd_x = p_board->pos.x - GetSystemMetrics( SM_CXFIXEDFRAME );
494 wnd_y = p_board->pos.y - GetSystemMetrics( SM_CYMENU )
495 - GetSystemMetrics( SM_CYCAPTION )
496 - GetSystemMetrics( SM_CYFIXEDFRAME );
497 wnd_width = p_board->width
498 + GetSystemMetrics( SM_CXFIXEDFRAME ) * 2;
499 wnd_height = p_board->height
500 + GetSystemMetrics( SM_CYMENU )
501 + GetSystemMetrics( SM_CYCAPTION )
502 + GetSystemMetrics( SM_CYFIXEDFRAME ) * 2;
503
504 /* setting the mines rectangle boundary */
505 left = BOARD_WMARGIN;
506 top = BOARD_HMARGIN * 2 + LED_HEIGHT;
507 right = left + p_board->cols * MINE_WIDTH;
508 bottom = top + p_board->rows * MINE_HEIGHT;
509 SetRect( &p_board->mines_rect, left, top, right, bottom );
510
511 /* setting the face rectangle boundary */
512 left = p_board->width / 2 - FACE_WIDTH / 2;
513 top = BOARD_HMARGIN;
514 right = left + FACE_WIDTH;
515 bottom = top + FACE_HEIGHT;
516 SetRect( &p_board->face_rect, left, top, right, bottom );
517
518 /* setting the timer rectangle boundary */
519 left = BOARD_WMARGIN;
520 top = BOARD_HMARGIN;
521 right = left + LED_WIDTH * 3;
522 bottom = top + LED_HEIGHT;
523 SetRect( &p_board->timer_rect, left, top, right, bottom );
524
525 /* setting the counter rectangle boundary */
526 left = p_board->width - BOARD_WMARGIN - LED_WIDTH * 3;
527 top = BOARD_HMARGIN;
528 right = p_board->width - BOARD_WMARGIN;
529 bottom = top + LED_HEIGHT;
530 SetRect( &p_board->counter_rect, left, top, right, bottom );
531
532 p_board->status = WAITING;
533 p_board->face_bmp = SMILE_BMP;
534 p_board->time = 0;
535
536 MoveWindow( p_board->hWnd, wnd_x, wnd_y, wnd_width, wnd_height, TRUE );
537 RedrawWindow( p_board->hWnd, NULL, NULL_HANDLE,
538 RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE );
539 }
540
541
542 void CheckLevel( BOARD *p_board )
543 {
544 if( p_board->rows < BEGINNER_ROWS )
545 p_board->rows = BEGINNER_ROWS;
546
547 if( p_board->rows > MAX_ROWS )
548 p_board->rows = MAX_ROWS;
549
550 if( p_board->cols < BEGINNER_COLS )
551 p_board->cols = BEGINNER_COLS;
552
553 if( p_board->cols > MAX_COLS )
554 p_board->cols = MAX_COLS;
555
556 if( p_board->mines < BEGINNER_MINES )
557 p_board->mines = BEGINNER_MINES;
558
559 if( p_board->mines > p_board->cols * p_board->rows - 1 )
560 p_board->mines = p_board->cols * p_board->rows - 1;
561 }
562
563
564 void CreateBoxes( BOARD *p_board )
565 {
566 int i, j;
567 unsigned col, row;
568
569 srand( (unsigned) time( NULL ) );
570
571 /* Create the boxes...
572 * We actually create them with an empty border,
573 * so special care doesn't have to be taken on the edges
574 */
575
576 for( col = 0; col <= p_board->cols + 1; col++ )
577 for( row = 0; row <= p_board->rows + 1; row++ ) {
578 p_board->box[col][row].IsPressed = FALSE;
579 p_board->box[col][row].IsMine = FALSE;
580 p_board->box[col][row].FlagType = NORMAL;
581 p_board->box[col][row].NumMines = 0;
582 }
583
584 /* create mines */
585 i = 0;
586 while( (unsigned) i < p_board->mines ) {
587 col = (int) (p_board->cols * (float) rand() / RAND_MAX + 1);
588 row = (int) (p_board->rows * (float) rand() / RAND_MAX + 1);
589
590 if( !p_board->box[col][row].IsMine ) {
591 i++;
592 p_board->box[col][row].IsMine = TRUE;
593 }
594 }
595
596 /*
597 * Now we label the remaining boxes with the
598 * number of mines surrounding them.
599 */
600
601 for( col = 1; col < p_board->cols + 1; col++ )
602 for( row = 1; row < p_board->rows + 1; row++ ) {
603 for( i = -1; i <= 1; i++ )
604 for( j = -1; j <= 1; j++ ) {
605 if( p_board->box[col + i][row + j].IsMine ) {
606 p_board->box[col][row].NumMines++ ;
607 }
608 }
609 }
610 }
611
612 void DrawMines ( HDC hdc, HDC hMemDC, BOARD *p_board )
613 {
614 HGDIOBJ hOldObj;
615 unsigned col, row;
616 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
617
618 for( row = 1; row <= p_board->rows; row++ ) {
619 for( col = 1; col <= p_board->cols; col++ ) {
620 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
621 }
622 }
623 SelectObject( hMemDC, hOldObj );
624 }
625
626 void DrawMine( HDC hdc, HDC hMemDC, BOARD *p_board, unsigned col, unsigned row, BOOL IsPressed )
627 {
628 MINEBMP_OFFSET offset = BOX_BMP;
629
630 if( col == 0 || col > p_board->cols || row == 0 || row > p_board->rows )
631 return;
632
633 if( p_board->status == GAMEOVER ) {
634 if( p_board->box[col][row].IsMine ) {
635 switch( p_board->box[col][row].FlagType ) {
636 case FLAG:
637 offset = FLAG_BMP;
638 break;
639 case COMPLETE:
640 offset = EXPLODE_BMP;
641 break;
642 case NORMAL:
643 offset = MINE_BMP;
644 }
645 } else {
646 switch( p_board->box[col][row].FlagType ) {
647 case QUESTION:
648 offset = QUESTION_BMP;
649 break;
650 case FLAG:
651 offset = WRONG_BMP;
652 break;
653 case NORMAL:
654 offset = BOX_BMP;
655 break;
656 case COMPLETE:
657 /* Do nothing */
658 break;
659 default:
660 DEBUG("Unknown FlagType during game over in DrawMine\n");
661 break;
662 }
663 }
664 } else { /* WAITING or PLAYING */
665 switch( p_board->box[col][row].FlagType ) {
666 case QUESTION:
667 if( !IsPressed )
668 offset = QUESTION_BMP;
669 else
670 offset = QPRESS_BMP;
671 break;
672 case FLAG:
673 offset = FLAG_BMP;
674 break;
675 case NORMAL:
676 if( !IsPressed )
677 offset = BOX_BMP;
678 else
679 offset = MPRESS_BMP;
680 break;
681 case COMPLETE:
682 /* Do nothing */
683 break;
684 default:
685 DEBUG("Unknown FlagType while playing in DrawMine\n");
686 break;
687 }
688 }
689
690 if( p_board->box[col][row].FlagType == COMPLETE
691 && !p_board->box[col][row].IsMine )
692 offset = (MINEBMP_OFFSET) p_board->box[col][row].NumMines;
693
694 BitBlt( hdc,
695 (col - 1) * MINE_WIDTH + p_board->mines_rect.left,
696 (row - 1) * MINE_HEIGHT + p_board->mines_rect.top,
697 MINE_WIDTH, MINE_HEIGHT,
698 hMemDC, 0, offset * MINE_HEIGHT, SRCCOPY );
699 }
700
701 void DrawLeds( HDC hdc, HDC hMemDC, BOARD *p_board, int number, int x, int y )
702 {
703 HGDIOBJ hOldObj;
704 unsigned led[3], i;
705 int count;
706
707 count = number;
708 if( count < 1000 ) {
709 if( count >= 0 ) {
710 led[0] = count / 100 ;
711 count -= led[0] * 100;
712 }
713 else {
714 led[0] = 10; /* negative sign */
715 count = -count;
716 }
717 led[1] = count / 10;
718 count -= led[1] * 10;
719 led[2] = count;
720 }
721 else {
722 for( i = 0; i < 3; i++ )
723 led[i] = 10;
724 }
725
726 /* use unlit led if not playing */
727 if( p_board->status == WAITING )
728 for( i = 0; i < 3; i++ )
729 led[i] = 11;
730
731 hOldObj = SelectObject (hMemDC, p_board->hLedsBMP);
732
733 for( i = 0; i < 3; i++ ) {
734 BitBlt( hdc,
735 i * LED_WIDTH + x,
736 y,
737 LED_WIDTH,
738 LED_HEIGHT,
739 hMemDC,
740 0,
741 led[i] * LED_HEIGHT,
742 SRCCOPY);
743 }
744
745 SelectObject( hMemDC, hOldObj );
746 }
747
748
749 void DrawFace( HDC hdc, HDC hMemDC, BOARD *p_board )
750 {
751 HGDIOBJ hOldObj;
752
753 hOldObj = SelectObject (hMemDC, p_board->hFacesBMP);
754
755 BitBlt( hdc,
756 p_board->face_rect.left,
757 p_board->face_rect.top,
758 FACE_WIDTH,
759 FACE_HEIGHT,
760 hMemDC, 0, p_board->face_bmp * FACE_HEIGHT, SRCCOPY);
761
762 SelectObject( hMemDC, hOldObj );
763 }
764
765
766 void DrawBoard( HDC hdc, HDC hMemDC, PAINTSTRUCT *ps, BOARD *p_board )
767 {
768 RECT tmp_rect;
769
770 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->counter_rect ) )
771 DrawLeds( hdc, hMemDC, p_board, p_board->mines - p_board->num_flags,
772 p_board->counter_rect.left,
773 p_board->counter_rect.top );
774
775 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->timer_rect ) )
776 DrawLeds( hdc, hMemDC, p_board, p_board->time,
777 p_board->timer_rect.left,
778 p_board->timer_rect.top );
779
780 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->face_rect ) )
781 DrawFace( hdc, hMemDC, p_board );
782
783 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->mines_rect ) )
784 DrawMines( hdc, hMemDC, p_board );
785 }
786
787
788 void TestBoard( HWND hWnd, BOARD *p_board, unsigned x, unsigned y, int msg )
789 {
790 POINT pt;
791
792 pt.x = x;
793 pt.y = y;
794
795 if( PtInRect( &p_board->mines_rect, pt ) && p_board->status != GAMEOVER
796 && p_board->status != WON )
797 TestMines( p_board, pt, msg );
798 else {
799 UnpressBoxes( p_board,
800 p_board->press.x,
801 p_board->press.y );
802 p_board->press.x = 0;
803 p_board->press.y = 0;
804 }
805
806 if( p_board->boxes_left == 0 ) {
807 p_board->status = WON;
808
809 if( p_board->difficulty != CUSTOM &&
810 p_board->time < p_board->best_time[p_board->difficulty] ) {
811 p_board->best_time[p_board->difficulty] = p_board->time;
812
813 DialogBoxParam( p_board->hInst, "DLG_CONGRATS", hWnd,
814 CongratsDlgProc, (LPARAM) p_board);
815
816 DialogBoxParam( p_board->hInst, "DLG_TIMES", hWnd,
817 TimesDlgProc, (LPARAM) p_board);
818 }
819 }
820 TestFace( p_board, pt, msg );
821 }
822
823 void TestMines( BOARD *p_board, POINT pt, int msg )
824 {
825 BOOL draw = TRUE;
826 unsigned col, row;
827
828 col = (pt.x - p_board->mines_rect.left) / MINE_WIDTH + 1;
829 row = (pt.y - p_board->mines_rect.top ) / MINE_HEIGHT + 1;
830
831 switch ( msg ) {
832 case WM_LBUTTONDOWN:
833 if( p_board->press.x != col || p_board->press.y != row ) {
834 UnpressBox( p_board,
835 p_board->press.x, p_board->press.y );
836 p_board->press.x = col;
837 p_board->press.y = row;
838 PressBox( p_board, col, row );
839 }
840 draw = FALSE;
841 break;
842
843 case WM_LBUTTONUP:
844 if( p_board->press.x != col || p_board->press.y != row )
845 UnpressBox( p_board,
846 p_board->press.x, p_board->press.y );
847 p_board->press.x = 0;
848 p_board->press.y = 0;
849 if( p_board->box[col][row].FlagType != FLAG )
850 p_board->status = PLAYING;
851 CompleteBox( p_board, col, row );
852 break;
853
854 case WM_MBUTTONDOWN:
855 PressBoxes( p_board, col, row );
856 draw = FALSE;
857 break;
858
859 case WM_MBUTTONUP:
860 if( p_board->press.x != col || p_board->press.y != row )
861 UnpressBoxes( p_board,
862 p_board->press.x, p_board->press.y );
863 p_board->press.x = 0;
864 p_board->press.y = 0;
865 CompleteBoxes( p_board, col, row );
866 break;
867
868 case WM_RBUTTONDOWN:
869 AddFlag( p_board, col, row );
870 p_board->status = PLAYING;
871 break;
872 default:
873 DEBUG("Unknown message type received in TestMines\n");
874 break;
875 }
876
877 if( draw )
878 {
879 RedrawWindow( p_board->hWnd, NULL, NULL_HANDLE,
880 RDW_INVALIDATE | RDW_UPDATENOW );
881 }
882 }
883
884
885 void TestFace( BOARD *p_board, POINT pt, int msg )
886 {
887 if( p_board->status == PLAYING || p_board->status == WAITING ) {
888 if( msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN )
889 p_board->face_bmp = OOH_BMP;
890 else p_board->face_bmp = SMILE_BMP;
891 }
892 else if( p_board->status == GAMEOVER )
893 p_board->face_bmp = DEAD_BMP;
894 else if( p_board->status == WON )
895 p_board->face_bmp = COOL_BMP;
896
897 if( PtInRect( &p_board->face_rect, pt ) ) {
898 if( msg == WM_LBUTTONDOWN )
899 p_board->face_bmp = SPRESS_BMP;
900
901 if( msg == WM_LBUTTONUP )
902 CreateBoard( p_board );
903 }
904
905 RedrawWindow( p_board->hWnd, &p_board->face_rect, NULL_HANDLE,
906 RDW_INVALIDATE | RDW_UPDATENOW );
907 }
908
909
910 void CompleteBox( BOARD *p_board, unsigned col, unsigned row )
911 {
912 int i, j;
913
914 if( p_board->box[col][row].FlagType != COMPLETE &&
915 p_board->box[col][row].FlagType != FLAG &&
916 col > 0 && col < p_board->cols + 1 &&
917 row > 0 && row < p_board->rows + 1 ) {
918 p_board->box[col][row].FlagType = COMPLETE;
919
920 if( p_board->box[col][row].IsMine ) {
921 p_board->face_bmp = DEAD_BMP;
922 p_board->status = GAMEOVER;
923 }
924 else if( p_board->status != GAMEOVER )
925 p_board->boxes_left--;
926
927 if( p_board->box[col][row].NumMines == 0 )
928 {
929 for( i = -1; i <= 1; i++ )
930 for( j = -1; j <= 1; j++ )
931 CompleteBox( p_board, col + i, row + j );
932 }
933 }
934 }
935
936
937 void CompleteBoxes( BOARD *p_board, unsigned col, unsigned row )
938 {
939 unsigned numFlags = 0;
940 int i, j;
941
942 if( p_board->box[col][row].FlagType == COMPLETE ) {
943 for( i = -1; i <= 1; i++ )
944 for( j = -1; j <= 1; j++ ) {
945 if( p_board->box[col+i][row+j].FlagType == FLAG )
946 numFlags++;
947 }
948
949 if( numFlags == p_board->box[col][row].NumMines ) {
950 for( i = -1; i <= 1; i++ )
951 for( j = -1; j <= 1; j++ ) {
952 if( p_board->box[col+i][row+j].FlagType != FLAG )
953 CompleteBox( p_board, col+i, row+j );
954 }
955 }
956 }
957 }
958
959
960 void AddFlag( BOARD *p_board, unsigned col, unsigned row )
961 {
962 if( p_board->box[col][row].FlagType != COMPLETE ) {
963 switch( p_board->box[col][row].FlagType ) {
964 case FLAG:
965 if( p_board->IsMarkQ )
966 p_board->box[col][row].FlagType = QUESTION;
967 else
968 p_board->box[col][row].FlagType = NORMAL;
969 p_board->num_flags--;
970 break;
971
972 case QUESTION:
973 p_board->box[col][row].FlagType = NORMAL;
974 break;
975
976 default:
977 p_board->box[col][row].FlagType = FLAG;
978 p_board->num_flags++;
979 }
980 }
981 }
982
983
984 void PressBox( BOARD *p_board, unsigned col, unsigned row )
985 {
986 HDC hdc;
987 HGDIOBJ hOldObj;
988 HDC hMemDC;
989
990 hdc = GetDC( p_board->hWnd );
991 hMemDC = CreateCompatibleDC( hdc );
992 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
993
994 DrawMine( hdc, hMemDC, p_board, col, row, TRUE );
995
996 SelectObject( hMemDC, hOldObj );
997 DeleteDC( hMemDC );
998 ReleaseDC( p_board->hWnd, hdc );
999 }
1000
1001
1002 void PressBoxes( BOARD *p_board, unsigned col, unsigned row )
1003 {
1004 int i, j;
1005
1006 for( i = -1; i <= 1; i++ )
1007 for( j = -1; j <= 1; j++ ) {
1008 p_board->box[col + i][row + j].IsPressed = TRUE;
1009 PressBox( p_board, col + i, row + j );
1010 }
1011
1012 for( i = -1; i <= 1; i++ )
1013 for( j = -1; j <= 1; j++ ) {
1014 if( !p_board->box[p_board->press.x + i][p_board->press.y + j].IsPressed )
1015 UnpressBox( p_board, p_board->press.x + i, p_board->press.y + j );
1016 }
1017
1018 for( i = -1; i <= 1; i++ )
1019 for( j = -1; j <= 1; j++ ) {
1020 p_board->box[col + i][row + j].IsPressed = FALSE;
1021 PressBox( p_board, col + i, row + j );
1022 }
1023
1024 p_board->press.x = col;
1025 p_board->press.y = row;
1026 }
1027
1028
1029 void UnpressBox( BOARD *p_board, unsigned col, unsigned row )
1030 {
1031 HDC hdc;
1032 HGDIOBJ hOldObj;
1033 HDC hMemDC;
1034
1035 hdc = GetDC( p_board->hWnd );
1036 hMemDC = CreateCompatibleDC( hdc );
1037 hOldObj = SelectObject( hMemDC, p_board->hMinesBMP );
1038
1039 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
1040
1041 SelectObject( hMemDC, hOldObj );
1042 DeleteDC( hMemDC );
1043 ReleaseDC( p_board->hWnd, hdc );
1044 }
1045
1046
1047 void UnpressBoxes( BOARD *p_board, unsigned col, unsigned row )
1048 {
1049 int i, j;
1050
1051 for( i = -1; i <= 1; i++ )
1052 for( j = -1; j <= 1; j++ ) {
1053 UnpressBox( p_board, col + i, row + j );
1054 }
1055 }