4 * Copyright 2000 Joshua Thielen <jt85296@ltu.edu>
5 * To be distributed under the Wine License
17 /* Work around a Wine bug which defines handles as UINT rather than LPVOID */
19 #define NULL_HANDLE NULL
26 #define DEBUG(x) fprintf(stderr,x)
31 int WINAPI
WinMain( HINSTANCE hInst
, HINSTANCE hPrevInst
, LPSTR cmdline
, int cmdshow
)
39 LoadString( hInst
, IDS_APPNAME
, appname
, sizeof(appname
));
42 wc
.lpfnWndProc
= MainProc
;
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
);
60 ShowWindow( hWnd
, cmdshow
);
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
);
75 LRESULT WINAPI
MainProc( HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
84 board
.hInst
= ((LPCREATESTRUCT
) lParam
)->hInstance
;
87 CreateBoard( &board
);
95 hdc
= BeginPaint( hWnd
, &ps
);
96 hMemDC
= CreateCompatibleDC( hdc
);
98 DrawBoard( hdc
, hMemDC
, &ps
, &board
);
101 EndPaint( hWnd
, &ps
);
108 board
.pos
.x
= (unsigned) LOWORD(lParam
);
109 board
.pos
.y
= (unsigned) HIWORD(lParam
);
114 DestroyBoard( &board
);
115 PostQuitMessage( 0 );
119 if( board
.status
== PLAYING
) {
121 RedrawWindow( hWnd
, &board
.timer_rect
, NULL_HANDLE
,
122 RDW_INVALIDATE
| RDW_UPDATENOW
);
127 DEBUG("WM_LBUTTONDOWN\n");
128 if( wParam
& MK_RBUTTON
)
129 msg
= WM_MBUTTONDOWN
;
130 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
135 DEBUG("WM_LBUTTONUP\n");
136 if( wParam
& MK_RBUTTON
)
138 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
143 DEBUG("WM_RBUTTONDOWN\n");
144 if( wParam
& MK_LBUTTON
) {
147 msg
= WM_MBUTTONDOWN
;
149 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
153 DEBUG("WM_RBUTTONUP\n");
154 if( wParam
& MK_LBUTTON
)
156 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
160 DEBUG("WM_MBUTTONDOWN\n");
161 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
165 DEBUG("WM_MBUTTONUP\n");
166 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
171 if( (wParam
& MK_LBUTTON
) && (wParam
& MK_RBUTTON
) ) {
172 msg
= WM_MBUTTONDOWN
;
174 else if( wParam
& MK_LBUTTON
) {
175 msg
= WM_LBUTTONDOWN
;
181 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
187 switch(LOWORD(wParam
)) {
189 CreateBoard( &board
);
193 hMenu
= GetMenu( hWnd
);
194 board
.IsMarkQ
= !board
.IsMarkQ
;
196 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
198 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
202 SetDifficulty( &board
, BEGINNER
);
203 CreateBoard( &board
);
207 SetDifficulty( &board
, ADVANCED
);
208 CreateBoard( &board
);
212 SetDifficulty( &board
, EXPERT
);
213 CreateBoard( &board
);
217 SetDifficulty( &board
, CUSTOM
);
218 CreateBoard( &board
);
222 SendMessage( hWnd
, WM_CLOSE
, 0, 0);
226 DialogBoxParam( board
.hInst
, "DLG_TIMES", hWnd
,
227 TimesDlgProc
, (LPARAM
) &board
);
231 DialogBox( board
.hInst
, "DLG_ABOUT", hWnd
, AboutDlgProc
);
234 DEBUG("Unknown WM_COMMAND command message received\n");
238 return( DefWindowProc( hWnd
, msg
, wParam
, lParam
));
241 void InitBoard( BOARD
*p_board
)
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
,
277 if( p_board
->IsMarkQ
)
278 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
280 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
281 CheckLevel( p_board
);
284 void LoadBoard( BOARD
*p_board
)
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
,
354 (LPDWORD
) &size
) == ERROR_SUCCESS
)
355 strncpy( p_board
->best_name
[i
], data
, sizeof( data
) );
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
,
365 (LPDWORD
) &size
) == ERROR_SUCCESS
)
366 p_board
->best_time
[i
] = atoi( data
);
368 p_board
->best_time
[i
] = 999;
373 void SaveBoard( BOARD
*p_board
)
377 SECURITY_ATTRIBUTES sa
;
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
)
388 wsprintf( data
, "%d", p_board
->pos
.x
);
389 RegSetValueEx( hkey
, "Xpos", 0, REG_SZ
, (LPBYTE
) data
,
392 wsprintf( data
, "%d", p_board
->pos
.x
);
393 RegSetValueEx( hkey
, "Ypos", 0, REG_SZ
, (LPBYTE
) data
,
396 wsprintf( data
, "%d", (int) p_board
->difficulty
);
397 RegSetValueEx( hkey
, "Difficulty", 0, REG_SZ
, (LPBYTE
) data
,
400 wsprintf( data
, "%d", p_board
->rows
);
401 RegSetValueEx( hkey
, "Rows", 0, REG_SZ
, (LPBYTE
) data
,
404 wsprintf( data
, "%d", p_board
->cols
);
405 RegSetValueEx( hkey
, "Cols", 0, REG_SZ
, (LPBYTE
) data
,
408 wsprintf( data
, "%d", p_board
->mines
);
409 RegSetValueEx( hkey
, "Mines", 0, REG_SZ
, (LPBYTE
) data
,
412 wsprintf( data
, "%d", (int) p_board
->IsMarkQ
);
413 RegSetValueEx( hkey
, "MarkQ", 0, REG_SZ
, (LPBYTE
) 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
,
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
,
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
) {
449 p_board
->cols
= BEGINNER_COLS
;
450 p_board
->rows
= BEGINNER_ROWS
;
451 p_board
->mines
= BEGINNER_MINES
;
455 p_board
->cols
= ADVANCED_COLS
;
456 p_board
->rows
= ADVANCED_ROWS
;
457 p_board
->mines
= ADVANCED_MINES
;
461 p_board
->cols
= EXPERT_COLS
;
462 p_board
->rows
= EXPERT_ROWS
;
463 p_board
->mines
= EXPERT_MINES
;
467 DialogBoxParam( p_board
->hInst
, "DLG_CUSTOM", p_board
->hWnd
,
468 CustomDlgProc
, (LPARAM
) p_board
);
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
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;
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
;
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;
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
;
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
)
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;
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
) {
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
)
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
)
628 if( p_board
->status
== GAMEOVER
) {
629 if( p_board
->box
[col
][row
].IsMine
) {
630 switch( p_board
->box
[col
][row
].FlagType
) {
635 offset
= EXPLODE_BMP
;
641 switch( p_board
->box
[col
][row
].FlagType
) {
643 offset
= QUESTION_BMP
;
655 DEBUG("Unknown FlagType during game over in DrawMine\n");
659 } else { /* WAITING or PLAYING */
660 switch( p_board
->box
[col
][row
].FlagType
) {
663 offset
= QUESTION_BMP
;
680 DEBUG("Unknown FlagType while playing in DrawMine\n");
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
;
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
)
705 led
[0] = count
/ 100 ;
706 count
-= led
[0] * 100;
709 led
[0] = 10; /* negative sign */
713 count
-= led
[1] * 10;
717 for( i
= 0; i
< 3; i
++ )
721 /* use unlit led if not playing */
722 if( p_board
->status
== WAITING
)
723 for( i
= 0; i
< 3; i
++ )
726 hOldObj
= SelectObject (hMemDC
, p_board
->hLedsBMP
);
728 for( i
= 0; i
< 3; i
++ ) {
740 SelectObject( hMemDC
, hOldObj
);
744 void DrawFace( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
748 hOldObj
= SelectObject (hMemDC
, p_board
->hFacesBMP
);
751 p_board
->face_rect
.left
,
752 p_board
->face_rect
.top
,
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
)
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
)
790 if( PtInRect( &p_board
->mines_rect
, pt
) && p_board
->status
!= GAMEOVER
791 && p_board
->status
!= WON
)
792 TestMines( p_board
, pt
, msg
);
794 UnpressBoxes( p_board
,
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
)
823 col
= (pt
.x
- p_board
->mines_rect
.left
) / MINE_WIDTH
+ 1;
824 row
= (pt
.y
- p_board
->mines_rect
.top
) / MINE_HEIGHT
+ 1;
828 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
) {
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
);
839 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
)
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
);
850 PressBoxes( p_board
, col
, row
);
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
);
864 AddFlag( p_board
, col
, row
);
865 p_board
->status
= PLAYING
;
868 DEBUG("Unknown message type received in TestMines\n");
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
)
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;
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
)
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
) {
960 if( p_board
->IsMarkQ
)
961 p_board
->box
[col
][row
].FlagType
= QUESTION
;
963 p_board
->box
[col
][row
].FlagType
= NORMAL
;
964 p_board
->num_flags
--;
968 p_board
->box
[col
][row
].FlagType
= NORMAL
;
972 p_board
->box
[col
][row
].FlagType
= FLAG
;
973 p_board
->num_flags
++;
979 void PressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
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
);
993 ReleaseDC( p_board
->hWnd
, hdc
);
997 void PressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
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
)
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
);
1038 ReleaseDC( p_board
->hWnd
, hdc
);
1042 void UnpressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
1046 for( i
= -1; i
<= 1; i
++ )
1047 for( j
= -1; j
<= 1; j
++ ) {
1048 UnpressBox( p_board
, col
+ i
, row
+ j
);