Include config.h.
[wine/gsoc_dplay.git] / programs / winemine / main.c
blob56b82b40811785f265f1ee43bed4d91a1fb96b6e
1 /*
2 * WineMine (main.c)
3 *
4 * Copyright 2000 Joshua Thielen <jt85296@ltu.edu>
5 * To be distributed under the Wine License
6 */
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <windows.h>
13 #include "main.h"
14 #include "dialog.h"
15 #include "resource.h"
17 /* Work around a Wine bug which defines handles as UINT rather than LPVOID */
18 #ifdef WINE_STRICT
19 #define NULL_HANDLE NULL
20 #else
21 #define NULL_HANDLE 0
22 #endif
24 #ifdef DUMB_DEBUG
25 #include <stdio.h>
26 #define DEBUG(x) fprintf(stderr,x)
27 #else
28 #define DEBUG(x)
29 #endif
31 int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdline, int cmdshow )
33 MSG msg;
34 WNDCLASS wc;
35 HWND hWnd;
36 HACCEL haccel;
37 char appname[9];
39 LoadString( hInst, IDS_APPNAME, appname, sizeof(appname));
41 wc.style = 0;
42 wc.lpfnWndProc = MainProc;
43 wc.cbClsExtra = 0;
44 wc.cbWndExtra = 0;
45 wc.hInstance = hInst;
46 wc.hIcon = LoadIcon( hInst, appname );
47 wc.hCursor = LoadCursor( NULL_HANDLE, IDI_APPLICATION );
48 wc.hbrBackground = (HBRUSH) GetStockObject( BLACK_BRUSH );
49 wc.lpszMenuName = "MENU_WINEMINE";
50 wc.lpszClassName = appname;
52 if (!RegisterClass(&wc)) exit(1);
53 hWnd = CreateWindow( appname, appname,
54 WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX,
55 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
56 NULL_HANDLE, NULL_HANDLE, hInst, NULL );
58 if (!hWnd) exit(1);
60 ShowWindow( hWnd, cmdshow );
61 UpdateWindow( hWnd );
63 haccel = LoadAccelerators( hInst, appname );
64 SetTimer( hWnd, ID_TIMER, 1000, NULL );
66 while( GetMessage(&msg, NULL_HANDLE, 0, 0) ) {
67 if (!TranslateAccelerator( hWnd, haccel, &msg ))
68 TranslateMessage( &msg );
70 DispatchMessage( &msg );
72 return msg.wParam;
75 LRESULT WINAPI MainProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
77 HDC hdc;
78 PAINTSTRUCT ps;
79 HMENU hMenu;
80 static BOARD board;
82 switch( msg ) {
83 case WM_CREATE:
84 board.hInst = ((LPCREATESTRUCT) lParam)->hInstance;
85 board.hWnd = hWnd;
86 InitBoard( &board );
87 CreateBoard( &board );
88 return 0;
90 case WM_PAINT:
92 HDC hMemDC;
94 DEBUG("WM_PAINT\n");
95 hdc = BeginPaint( hWnd, &ps );
96 hMemDC = CreateCompatibleDC( hdc );
98 DrawBoard( hdc, hMemDC, &ps, &board );
100 DeleteDC( hMemDC );
101 EndPaint( hWnd, &ps );
103 return 0;
106 case WM_MOVE:
107 DEBUG("WM_MOVE\n");
108 board.pos.x = (unsigned) LOWORD(lParam);
109 board.pos.y = (unsigned) HIWORD(lParam);
110 return 0;
112 case WM_DESTROY:
113 SaveBoard( &board );
114 DestroyBoard( &board );
115 PostQuitMessage( 0 );
116 return 0;
118 case WM_TIMER:
119 if( board.status == PLAYING ) {
120 board.time++;
121 RedrawWindow( hWnd, &board.timer_rect, NULL_HANDLE,
122 RDW_INVALIDATE | RDW_UPDATENOW );
124 return 0;
126 case WM_LBUTTONDOWN:
127 DEBUG("WM_LBUTTONDOWN\n");
128 if( wParam & MK_RBUTTON )
129 msg = WM_MBUTTONDOWN;
130 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
131 SetCapture( hWnd );
132 return 0;
134 case WM_LBUTTONUP:
135 DEBUG("WM_LBUTTONUP\n");
136 if( wParam & MK_RBUTTON )
137 msg = WM_MBUTTONUP;
138 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
139 ReleaseCapture();
140 return 0;
142 case WM_RBUTTONDOWN:
143 DEBUG("WM_RBUTTONDOWN\n");
144 if( wParam & MK_LBUTTON ) {
145 board.press.x = 0;
146 board.press.y = 0;
147 msg = WM_MBUTTONDOWN;
149 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
150 return 0;
152 case WM_RBUTTONUP:
153 DEBUG("WM_RBUTTONUP\n");
154 if( wParam & MK_LBUTTON )
155 msg = WM_MBUTTONUP;
156 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
157 return 0;
159 case WM_MBUTTONDOWN:
160 DEBUG("WM_MBUTTONDOWN\n");
161 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
162 return 0;
164 case WM_MBUTTONUP:
165 DEBUG("WM_MBUTTONUP\n");
166 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
167 return 0;
169 case WM_MOUSEMOVE:
171 if( (wParam & MK_LBUTTON) && (wParam & MK_RBUTTON) ) {
172 msg = WM_MBUTTONDOWN;
174 else if( wParam & MK_LBUTTON ) {
175 msg = WM_LBUTTONDOWN;
177 else {
178 return 0;
181 TestBoard( hWnd, &board, LOWORD(lParam), HIWORD(lParam), msg );
183 return 0;
186 case WM_COMMAND:
187 switch(LOWORD(wParam)) {
188 case IDM_NEW:
189 CreateBoard( &board );
190 return 0;
192 case IDM_MARKQ:
193 hMenu = GetMenu( hWnd );
194 board.IsMarkQ = !board.IsMarkQ;
195 if( board.IsMarkQ )
196 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
197 else
198 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
199 return 0;
201 case IDM_BEGINNER:
202 SetDifficulty( &board, BEGINNER );
203 CreateBoard( &board );
204 return 0;
206 case IDM_ADVANCED:
207 SetDifficulty( &board, ADVANCED );
208 CreateBoard( &board );
209 return 0;
211 case IDM_EXPERT:
212 SetDifficulty( &board, EXPERT );
213 CreateBoard( &board );
214 return 0;
216 case IDM_CUSTOM:
217 SetDifficulty( &board, CUSTOM );
218 CreateBoard( &board );
219 return 0;
221 case IDM_EXIT:
222 SendMessage( hWnd, WM_CLOSE, 0, 0);
223 return 0;
225 case IDM_TIMES:
226 DialogBoxParam( board.hInst, "DLG_TIMES", hWnd,
227 TimesDlgProc, (LPARAM) &board);
228 return 0;
230 case IDM_ABOUT:
231 DialogBox( board.hInst, "DLG_ABOUT", hWnd, AboutDlgProc );
232 return 0;
233 default:
234 DEBUG("Unknown WM_COMMAND command message received\n");
235 break;
238 return( DefWindowProc( hWnd, msg, wParam, lParam ));
241 void InitBoard( BOARD *p_board )
243 HMENU hMenu;
245 p_board->hMinesBMP = LoadBitmap( p_board->hInst, "mines");
246 p_board->hFacesBMP = LoadBitmap( p_board->hInst, "faces");
247 p_board->hLedsBMP = LoadBitmap( p_board->hInst, "leds");
249 LoadBoard( p_board );
251 if( p_board->pos.x < (unsigned) GetSystemMetrics( SM_CXFIXEDFRAME ))
252 p_board->pos.x = GetSystemMetrics( SM_CXFIXEDFRAME );
254 if( p_board->pos.x > (unsigned) (GetSystemMetrics( SM_CXSCREEN )
255 - GetSystemMetrics( SM_CXFIXEDFRAME ))) {
256 p_board->pos.x = GetSystemMetrics( SM_CXSCREEN )
257 - GetSystemMetrics( SM_CXFIXEDFRAME );
260 if( p_board->pos.y < (unsigned) (GetSystemMetrics( SM_CYMENU )
261 + GetSystemMetrics( SM_CYCAPTION )
262 + GetSystemMetrics( SM_CYFIXEDFRAME ))) {
263 p_board->pos.y = GetSystemMetrics( SM_CYMENU ) +
264 GetSystemMetrics( SM_CYCAPTION ) +
265 GetSystemMetrics( SM_CYFIXEDFRAME );
268 if( p_board->pos.y > (unsigned) (GetSystemMetrics( SM_CYSCREEN )
269 - GetSystemMetrics( SM_CYFIXEDFRAME ))) {
270 p_board->pos.y = GetSystemMetrics( SM_CYSCREEN )
271 - GetSystemMetrics( SM_CYFIXEDFRAME );
274 hMenu = GetMenu( p_board->hWnd );
275 CheckMenuItem( hMenu, IDM_BEGINNER + (unsigned) p_board->difficulty,
276 MF_CHECKED );
277 if( p_board->IsMarkQ )
278 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
279 else
280 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
281 CheckLevel( p_board );
284 void LoadBoard( BOARD *p_board )
286 DWORD size;
287 DWORD type;
288 HKEY hkey;
289 char data[16];
290 char key_name[8];
291 unsigned i;
294 RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\Wine\\WineMine",
295 0, KEY_QUERY_VALUE, &hkey );
297 size = sizeof( data );
298 if( RegQueryValueEx( hkey, "Xpos", NULL, (LPDWORD) &type,
299 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS ) {
300 p_board->pos.x = atoi( data );
302 else
303 p_board->pos.x = GetSystemMetrics( SM_CXFIXEDFRAME );
305 size = sizeof( data );
306 if( RegQueryValueEx( hkey, "Ypos", NULL, (LPDWORD) &type,
307 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
308 p_board->pos.y = atoi( data );
309 else
310 p_board->pos.y = GetSystemMetrics( SM_CYMENU )
311 + GetSystemMetrics( SM_CYCAPTION )
312 + GetSystemMetrics( SM_CYFIXEDFRAME );
314 size = sizeof( data );
315 if( RegQueryValueEx( hkey, "Rows", NULL, (LPDWORD) &type,
316 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
317 p_board->rows = atoi( data );
318 else
319 p_board->rows = BEGINNER_ROWS;
321 size = sizeof( data );
322 if( RegQueryValueEx( hkey, "Cols", NULL, (LPDWORD) &type,
323 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
324 p_board->cols = atoi( data );
325 else
326 p_board->cols = BEGINNER_COLS;
328 size = sizeof( data );
329 if( RegQueryValueEx( hkey, "Mines", NULL, (LPDWORD) &type,
330 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
331 p_board->mines = atoi( data );
332 else
333 p_board->rows = BEGINNER_ROWS;
335 size = sizeof( data );
336 if( RegQueryValueEx( hkey, "Difficulty", NULL, (LPDWORD) &type,
337 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
338 p_board->difficulty = (DIFFICULTY) atoi( data );
339 else
340 p_board->difficulty = BEGINNER;
342 size = sizeof( data );
343 if( RegQueryValueEx( hkey, "MarkQ", NULL, (LPDWORD) &type,
344 (LPBYTE) data, (LPDWORD) &size ) == ERROR_SUCCESS )
345 p_board->IsMarkQ = atoi( data );
346 else
347 p_board->IsMarkQ = TRUE;
349 for( i = 0; i < 3; i++ ) {
350 wsprintf( key_name, "Name%d", i );
351 size = sizeof( data );
352 if( RegQueryValueEx( hkey, key_name, NULL, (LPDWORD) &type,
353 (LPBYTE) data,
354 (LPDWORD) &size ) == ERROR_SUCCESS )
355 strncpy( p_board->best_name[i], data, sizeof( data ) );
356 else
357 wsprintf( p_board->best_name[i], "Nobody");
360 for( i = 0; i < 3; i++ ) {
361 wsprintf( key_name, "Time%d", i );
362 size = sizeof( data );
363 if( RegQueryValueEx( hkey, key_name, NULL, (LPDWORD) &type,
364 (LPBYTE) data,
365 (LPDWORD) &size ) == ERROR_SUCCESS )
366 p_board->best_time[i] = atoi( data );
367 else
368 p_board->best_time[i] = 999;
370 RegCloseKey( hkey );
373 void SaveBoard( BOARD *p_board )
375 DWORD disp;
376 HKEY hkey;
377 SECURITY_ATTRIBUTES sa;
378 unsigned i;
379 char data[16];
380 char key_name[8];
382 if( RegCreateKeyEx( HKEY_LOCAL_MACHINE,
383 "Software\\Wine\\WineMine", 0, NULL,
384 REG_OPTION_NON_VOLATILE, KEY_WRITE, &sa,
385 &hkey, &disp ) != ERROR_SUCCESS)
386 return;
388 wsprintf( data, "%d", p_board->pos.x );
389 RegSetValueEx( hkey, "Xpos", 0, REG_SZ, (LPBYTE) data,
390 sizeof( data ));
392 wsprintf( data, "%d", p_board->pos.x );
393 RegSetValueEx( hkey, "Ypos", 0, REG_SZ, (LPBYTE) data,
394 sizeof( data ));
396 wsprintf( data, "%d", (int) p_board->difficulty );
397 RegSetValueEx( hkey, "Difficulty", 0, REG_SZ, (LPBYTE) data,
398 sizeof( data ));
400 wsprintf( data, "%d", p_board->rows );
401 RegSetValueEx( hkey, "Rows", 0, REG_SZ, (LPBYTE) data,
402 sizeof( data ));
404 wsprintf( data, "%d", p_board->cols );
405 RegSetValueEx( hkey, "Cols", 0, REG_SZ, (LPBYTE) data,
406 sizeof( data ));
408 wsprintf( data, "%d", p_board->mines );
409 RegSetValueEx( hkey, "Mines", 0, REG_SZ, (LPBYTE) data,
410 sizeof( data ));
412 wsprintf( data, "%d", (int) p_board->IsMarkQ );
413 RegSetValueEx( hkey, "MarkQ", 0, REG_SZ, (LPBYTE) data,
414 sizeof( data ));
416 for( i = 0; i < 3; i++ ) {
417 wsprintf( key_name, "Name%u", i );
418 strncpy( data, p_board->best_name[i], sizeof( data ) );
419 RegSetValueEx( hkey, key_name, 0, REG_SZ, (LPBYTE) data,
420 sizeof( data ));
423 for( i = 0; i < 3; i++ ) {
424 wsprintf( key_name, "Time%u", i );
425 wsprintf( data, "%d", p_board->best_time[i] );
426 RegSetValueEx( hkey, key_name, 0, REG_SZ, (LPBYTE) data,
427 sizeof( data ));
429 RegCloseKey( hkey );
432 void DestroyBoard( BOARD *p_board )
434 DeleteObject( p_board->hFacesBMP );
435 DeleteObject( p_board->hLedsBMP );
436 DeleteObject( p_board->hMinesBMP );
439 void SetDifficulty( BOARD *p_board, DIFFICULTY difficulty )
441 HMENU hMenu = GetMenu( p_board->hWnd );
443 CheckMenuItem( hMenu, IDM_BEGINNER + p_board->difficulty, MF_UNCHECKED );
444 p_board->difficulty = difficulty;
445 CheckMenuItem( hMenu, IDM_BEGINNER + difficulty, MF_CHECKED );
447 switch( difficulty ) {
448 case BEGINNER:
449 p_board->cols = BEGINNER_COLS;
450 p_board->rows = BEGINNER_ROWS;
451 p_board->mines = BEGINNER_MINES;
452 break;
454 case ADVANCED:
455 p_board->cols = ADVANCED_COLS;
456 p_board->rows = ADVANCED_ROWS;
457 p_board->mines = ADVANCED_MINES;
458 break;
460 case EXPERT:
461 p_board->cols = EXPERT_COLS;
462 p_board->rows = EXPERT_ROWS;
463 p_board->mines = EXPERT_MINES;
464 break;
466 case CUSTOM:
467 DialogBoxParam( p_board->hInst, "DLG_CUSTOM", p_board->hWnd,
468 CustomDlgProc, (LPARAM) p_board);
469 break;
473 void CreateBoard( BOARD *p_board )
475 int left, top, bottom, right, wnd_x, wnd_y, wnd_width, wnd_height;
477 p_board->mb = MB_NONE;
478 p_board->boxes_left = p_board->cols * p_board->rows - p_board->mines;
479 p_board->num_flags = 0;
481 CreateBoxes( p_board );
483 p_board->width = p_board->cols * MINE_WIDTH + BOARD_WMARGIN * 2;
485 p_board->height = p_board->rows * MINE_HEIGHT + LED_HEIGHT
486 + BOARD_HMARGIN * 3;
488 wnd_x = p_board->pos.x - GetSystemMetrics( SM_CXFIXEDFRAME );
489 wnd_y = p_board->pos.y - GetSystemMetrics( SM_CYMENU )
490 - GetSystemMetrics( SM_CYCAPTION )
491 - GetSystemMetrics( SM_CYFIXEDFRAME );
492 wnd_width = p_board->width
493 + GetSystemMetrics( SM_CXFIXEDFRAME ) * 2;
494 wnd_height = p_board->height
495 + GetSystemMetrics( SM_CYMENU )
496 + GetSystemMetrics( SM_CYCAPTION )
497 + GetSystemMetrics( SM_CYFIXEDFRAME ) * 2;
499 /* setting the mines rectangle boundary */
500 left = BOARD_WMARGIN;
501 top = BOARD_HMARGIN * 2 + LED_HEIGHT;
502 right = left + p_board->cols * MINE_WIDTH;
503 bottom = top + p_board->rows * MINE_HEIGHT;
504 SetRect( &p_board->mines_rect, left, top, right, bottom );
506 /* setting the face rectangle boundary */
507 left = p_board->width / 2 - FACE_WIDTH / 2;
508 top = BOARD_HMARGIN;
509 right = left + FACE_WIDTH;
510 bottom = top + FACE_HEIGHT;
511 SetRect( &p_board->face_rect, left, top, right, bottom );
513 /* setting the timer rectangle boundary */
514 left = BOARD_WMARGIN;
515 top = BOARD_HMARGIN;
516 right = left + LED_WIDTH * 3;
517 bottom = top + LED_HEIGHT;
518 SetRect( &p_board->timer_rect, left, top, right, bottom );
520 /* setting the counter rectangle boundary */
521 left = p_board->width - BOARD_WMARGIN - LED_WIDTH * 3;
522 top = BOARD_HMARGIN;
523 right = p_board->width - BOARD_WMARGIN;
524 bottom = top + LED_HEIGHT;
525 SetRect( &p_board->counter_rect, left, top, right, bottom );
527 p_board->status = WAITING;
528 p_board->face_bmp = SMILE_BMP;
529 p_board->time = 0;
531 MoveWindow( p_board->hWnd, wnd_x, wnd_y, wnd_width, wnd_height, TRUE );
532 RedrawWindow( p_board->hWnd, NULL, NULL_HANDLE,
533 RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE );
537 void CheckLevel( BOARD *p_board )
539 if( p_board->rows < BEGINNER_ROWS )
540 p_board->rows = BEGINNER_ROWS;
542 if( p_board->rows > MAX_ROWS )
543 p_board->rows = MAX_ROWS;
545 if( p_board->cols < BEGINNER_COLS )
546 p_board->cols = BEGINNER_COLS;
548 if( p_board->cols > MAX_COLS )
549 p_board->cols = MAX_COLS;
551 if( p_board->mines < BEGINNER_MINES )
552 p_board->mines = BEGINNER_MINES;
554 if( p_board->mines > p_board->cols * p_board->rows - 1 )
555 p_board->mines = p_board->cols * p_board->rows - 1;
559 void CreateBoxes( BOARD *p_board )
561 int i, j;
562 unsigned col, row;
564 srand( (unsigned) time( NULL ) );
566 /* Create the boxes...
567 * We actually create them with an empty border,
568 * so special care doesn't have to be taken on the edges
571 for( col = 0; col <= p_board->cols + 1; col++ )
572 for( row = 0; row <= p_board->rows + 1; row++ ) {
573 p_board->box[col][row].IsPressed = FALSE;
574 p_board->box[col][row].IsMine = FALSE;
575 p_board->box[col][row].FlagType = NORMAL;
576 p_board->box[col][row].NumMines = 0;
579 /* create mines */
580 i = 0;
581 while( (unsigned) i < p_board->mines ) {
582 col = (int) (p_board->cols * (float) rand() / RAND_MAX + 1);
583 row = (int) (p_board->rows * (float) rand() / RAND_MAX + 1);
585 if( !p_board->box[col][row].IsMine ) {
586 i++;
587 p_board->box[col][row].IsMine = TRUE;
592 * Now we label the remaining boxes with the
593 * number of mines surrounding them.
596 for( col = 1; col < p_board->cols + 1; col++ )
597 for( row = 1; row < p_board->rows + 1; row++ ) {
598 for( i = -1; i <= 1; i++ )
599 for( j = -1; j <= 1; j++ ) {
600 if( p_board->box[col + i][row + j].IsMine ) {
601 p_board->box[col][row].NumMines++ ;
607 void DrawMines ( HDC hdc, HDC hMemDC, BOARD *p_board )
609 HGDIOBJ hOldObj;
610 unsigned col, row;
611 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
613 for( row = 1; row <= p_board->rows; row++ ) {
614 for( col = 1; col <= p_board->cols; col++ ) {
615 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
618 SelectObject( hMemDC, hOldObj );
621 void DrawMine( HDC hdc, HDC hMemDC, BOARD *p_board, unsigned col, unsigned row, BOOL IsPressed )
623 MINEBMP_OFFSET offset = BOX_BMP;
625 if( col == 0 || col > p_board->cols || row == 0 || row > p_board->rows )
626 return;
628 if( p_board->status == GAMEOVER ) {
629 if( p_board->box[col][row].IsMine ) {
630 switch( p_board->box[col][row].FlagType ) {
631 case FLAG:
632 offset = FLAG_BMP;
633 break;
634 case COMPLETE:
635 offset = EXPLODE_BMP;
636 break;
637 case NORMAL:
638 offset = MINE_BMP;
640 } else {
641 switch( p_board->box[col][row].FlagType ) {
642 case QUESTION:
643 offset = QUESTION_BMP;
644 break;
645 case FLAG:
646 offset = WRONG_BMP;
647 break;
648 case NORMAL:
649 offset = BOX_BMP;
650 break;
651 case COMPLETE:
652 /* Do nothing */
653 break;
654 default:
655 DEBUG("Unknown FlagType during game over in DrawMine\n");
656 break;
659 } else { /* WAITING or PLAYING */
660 switch( p_board->box[col][row].FlagType ) {
661 case QUESTION:
662 if( !IsPressed )
663 offset = QUESTION_BMP;
664 else
665 offset = QPRESS_BMP;
666 break;
667 case FLAG:
668 offset = FLAG_BMP;
669 break;
670 case NORMAL:
671 if( !IsPressed )
672 offset = BOX_BMP;
673 else
674 offset = MPRESS_BMP;
675 break;
676 case COMPLETE:
677 /* Do nothing */
678 break;
679 default:
680 DEBUG("Unknown FlagType while playing in DrawMine\n");
681 break;
685 if( p_board->box[col][row].FlagType == COMPLETE
686 && !p_board->box[col][row].IsMine )
687 offset = (MINEBMP_OFFSET) p_board->box[col][row].NumMines;
689 BitBlt( hdc,
690 (col - 1) * MINE_WIDTH + p_board->mines_rect.left,
691 (row - 1) * MINE_HEIGHT + p_board->mines_rect.top,
692 MINE_WIDTH, MINE_HEIGHT,
693 hMemDC, 0, offset * MINE_HEIGHT, SRCCOPY );
696 void DrawLeds( HDC hdc, HDC hMemDC, BOARD *p_board, int number, int x, int y )
698 HGDIOBJ hOldObj;
699 unsigned led[3], i;
700 int count;
702 count = number;
703 if( count < 1000 ) {
704 if( count >= 0 ) {
705 led[0] = count / 100 ;
706 count -= led[0] * 100;
708 else {
709 led[0] = 10; /* negative sign */
710 count = -count;
712 led[1] = count / 10;
713 count -= led[1] * 10;
714 led[2] = count;
716 else {
717 for( i = 0; i < 3; i++ )
718 led[i] = 10;
721 /* use unlit led if not playing */
722 if( p_board->status == WAITING )
723 for( i = 0; i < 3; i++ )
724 led[i] = 11;
726 hOldObj = SelectObject (hMemDC, p_board->hLedsBMP);
728 for( i = 0; i < 3; i++ ) {
729 BitBlt( hdc,
730 i * LED_WIDTH + x,
732 LED_WIDTH,
733 LED_HEIGHT,
734 hMemDC,
736 led[i] * LED_HEIGHT,
737 SRCCOPY);
740 SelectObject( hMemDC, hOldObj );
744 void DrawFace( HDC hdc, HDC hMemDC, BOARD *p_board )
746 HGDIOBJ hOldObj;
748 hOldObj = SelectObject (hMemDC, p_board->hFacesBMP);
750 BitBlt( hdc,
751 p_board->face_rect.left,
752 p_board->face_rect.top,
753 FACE_WIDTH,
754 FACE_HEIGHT,
755 hMemDC, 0, p_board->face_bmp * FACE_HEIGHT, SRCCOPY);
757 SelectObject( hMemDC, hOldObj );
761 void DrawBoard( HDC hdc, HDC hMemDC, PAINTSTRUCT *ps, BOARD *p_board )
763 RECT tmp_rect;
765 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->counter_rect ) )
766 DrawLeds( hdc, hMemDC, p_board, p_board->mines - p_board->num_flags,
767 p_board->counter_rect.left,
768 p_board->counter_rect.top );
770 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->timer_rect ) )
771 DrawLeds( hdc, hMemDC, p_board, p_board->time,
772 p_board->timer_rect.left,
773 p_board->timer_rect.top );
775 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->face_rect ) )
776 DrawFace( hdc, hMemDC, p_board );
778 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->mines_rect ) )
779 DrawMines( hdc, hMemDC, p_board );
783 void TestBoard( HWND hWnd, BOARD *p_board, unsigned x, unsigned y, int msg )
785 POINT pt;
787 pt.x = x;
788 pt.y = y;
790 if( PtInRect( &p_board->mines_rect, pt ) && p_board->status != GAMEOVER
791 && p_board->status != WON )
792 TestMines( p_board, pt, msg );
793 else {
794 UnpressBoxes( p_board,
795 p_board->press.x,
796 p_board->press.y );
797 p_board->press.x = 0;
798 p_board->press.y = 0;
801 if( p_board->boxes_left == 0 ) {
802 p_board->status = WON;
804 if( p_board->difficulty != CUSTOM &&
805 p_board->time < p_board->best_time[p_board->difficulty] ) {
806 p_board->best_time[p_board->difficulty] = p_board->time;
808 DialogBoxParam( p_board->hInst, "DLG_CONGRATS", hWnd,
809 CongratsDlgProc, (LPARAM) p_board);
811 DialogBoxParam( p_board->hInst, "DLG_TIMES", hWnd,
812 TimesDlgProc, (LPARAM) p_board);
815 TestFace( p_board, pt, msg );
818 void TestMines( BOARD *p_board, POINT pt, int msg )
820 BOOL draw = TRUE;
821 unsigned col, row;
823 col = (pt.x - p_board->mines_rect.left) / MINE_WIDTH + 1;
824 row = (pt.y - p_board->mines_rect.top ) / MINE_HEIGHT + 1;
826 switch ( msg ) {
827 case WM_LBUTTONDOWN:
828 if( p_board->press.x != col || p_board->press.y != row ) {
829 UnpressBox( p_board,
830 p_board->press.x, p_board->press.y );
831 p_board->press.x = col;
832 p_board->press.y = row;
833 PressBox( p_board, col, row );
835 draw = FALSE;
836 break;
838 case WM_LBUTTONUP:
839 if( p_board->press.x != col || p_board->press.y != row )
840 UnpressBox( p_board,
841 p_board->press.x, p_board->press.y );
842 p_board->press.x = 0;
843 p_board->press.y = 0;
844 if( p_board->box[col][row].FlagType != FLAG )
845 p_board->status = PLAYING;
846 CompleteBox( p_board, col, row );
847 break;
849 case WM_MBUTTONDOWN:
850 PressBoxes( p_board, col, row );
851 draw = FALSE;
852 break;
854 case WM_MBUTTONUP:
855 if( p_board->press.x != col || p_board->press.y != row )
856 UnpressBoxes( p_board,
857 p_board->press.x, p_board->press.y );
858 p_board->press.x = 0;
859 p_board->press.y = 0;
860 CompleteBoxes( p_board, col, row );
861 break;
863 case WM_RBUTTONDOWN:
864 AddFlag( p_board, col, row );
865 p_board->status = PLAYING;
866 break;
867 default:
868 DEBUG("Unknown message type received in TestMines\n");
869 break;
872 if( draw )
874 RedrawWindow( p_board->hWnd, NULL, NULL_HANDLE,
875 RDW_INVALIDATE | RDW_UPDATENOW );
880 void TestFace( BOARD *p_board, POINT pt, int msg )
882 if( p_board->status == PLAYING || p_board->status == WAITING ) {
883 if( msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN )
884 p_board->face_bmp = OOH_BMP;
885 else p_board->face_bmp = SMILE_BMP;
887 else if( p_board->status == GAMEOVER )
888 p_board->face_bmp = DEAD_BMP;
889 else if( p_board->status == WON )
890 p_board->face_bmp = COOL_BMP;
892 if( PtInRect( &p_board->face_rect, pt ) ) {
893 if( msg == WM_LBUTTONDOWN )
894 p_board->face_bmp = SPRESS_BMP;
896 if( msg == WM_LBUTTONUP )
897 CreateBoard( p_board );
900 RedrawWindow( p_board->hWnd, &p_board->face_rect, NULL_HANDLE,
901 RDW_INVALIDATE | RDW_UPDATENOW );
905 void CompleteBox( BOARD *p_board, unsigned col, unsigned row )
907 int i, j;
909 if( p_board->box[col][row].FlagType != COMPLETE &&
910 p_board->box[col][row].FlagType != FLAG &&
911 col > 0 && col < p_board->cols + 1 &&
912 row > 0 && row < p_board->rows + 1 ) {
913 p_board->box[col][row].FlagType = COMPLETE;
915 if( p_board->box[col][row].IsMine ) {
916 p_board->face_bmp = DEAD_BMP;
917 p_board->status = GAMEOVER;
919 else if( p_board->status != GAMEOVER )
920 p_board->boxes_left--;
922 if( p_board->box[col][row].NumMines == 0 )
924 for( i = -1; i <= 1; i++ )
925 for( j = -1; j <= 1; j++ )
926 CompleteBox( p_board, col + i, row + j );
932 void CompleteBoxes( BOARD *p_board, unsigned col, unsigned row )
934 unsigned numFlags = 0;
935 int i, j;
937 if( p_board->box[col][row].FlagType == COMPLETE ) {
938 for( i = -1; i <= 1; i++ )
939 for( j = -1; j <= 1; j++ ) {
940 if( p_board->box[col+i][row+j].FlagType == FLAG )
941 numFlags++;
944 if( numFlags == p_board->box[col][row].NumMines ) {
945 for( i = -1; i <= 1; i++ )
946 for( j = -1; j <= 1; j++ ) {
947 if( p_board->box[col+i][row+j].FlagType != FLAG )
948 CompleteBox( p_board, col+i, row+j );
955 void AddFlag( BOARD *p_board, unsigned col, unsigned row )
957 if( p_board->box[col][row].FlagType != COMPLETE ) {
958 switch( p_board->box[col][row].FlagType ) {
959 case FLAG:
960 if( p_board->IsMarkQ )
961 p_board->box[col][row].FlagType = QUESTION;
962 else
963 p_board->box[col][row].FlagType = NORMAL;
964 p_board->num_flags--;
965 break;
967 case QUESTION:
968 p_board->box[col][row].FlagType = NORMAL;
969 break;
971 default:
972 p_board->box[col][row].FlagType = FLAG;
973 p_board->num_flags++;
979 void PressBox( BOARD *p_board, unsigned col, unsigned row )
981 HDC hdc;
982 HGDIOBJ hOldObj;
983 HDC hMemDC;
985 hdc = GetDC( p_board->hWnd );
986 hMemDC = CreateCompatibleDC( hdc );
987 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
989 DrawMine( hdc, hMemDC, p_board, col, row, TRUE );
991 SelectObject( hMemDC, hOldObj );
992 DeleteDC( hMemDC );
993 ReleaseDC( p_board->hWnd, hdc );
997 void PressBoxes( BOARD *p_board, unsigned col, unsigned row )
999 int i, j;
1001 for( i = -1; i <= 1; i++ )
1002 for( j = -1; j <= 1; j++ ) {
1003 p_board->box[col + i][row + j].IsPressed = TRUE;
1004 PressBox( p_board, col + i, row + j );
1007 for( i = -1; i <= 1; i++ )
1008 for( j = -1; j <= 1; j++ ) {
1009 if( !p_board->box[p_board->press.x + i][p_board->press.y + j].IsPressed )
1010 UnpressBox( p_board, p_board->press.x + i, p_board->press.y + j );
1013 for( i = -1; i <= 1; i++ )
1014 for( j = -1; j <= 1; j++ ) {
1015 p_board->box[col + i][row + j].IsPressed = FALSE;
1016 PressBox( p_board, col + i, row + j );
1019 p_board->press.x = col;
1020 p_board->press.y = row;
1024 void UnpressBox( BOARD *p_board, unsigned col, unsigned row )
1026 HDC hdc;
1027 HGDIOBJ hOldObj;
1028 HDC hMemDC;
1030 hdc = GetDC( p_board->hWnd );
1031 hMemDC = CreateCompatibleDC( hdc );
1032 hOldObj = SelectObject( hMemDC, p_board->hMinesBMP );
1034 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
1036 SelectObject( hMemDC, hOldObj );
1037 DeleteDC( hMemDC );
1038 ReleaseDC( p_board->hWnd, hdc );
1042 void UnpressBoxes( BOARD *p_board, unsigned col, unsigned row )
1044 int i, j;
1046 for( i = -1; i <= 1; i++ )
1047 for( j = -1; j <= 1; j++ ) {
1048 UnpressBox( p_board, col + i, row + j );