4 * Copyright 2000 Joshua Thielen <jt85296@ltu.edu>
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.
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.
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
29 /* Work around a Wine bug which defines handles as UINT rather than LPVOID */
31 #define NULL_HANDLE NULL
38 #define DEBUG(x) fprintf(stderr,x)
43 int WINAPI
WinMain( HINSTANCE hInst
, HINSTANCE hPrevInst
, LPSTR cmdline
, int cmdshow
)
51 LoadString( hInst
, IDS_APPNAME
, appname
, sizeof(appname
));
54 wc
.lpfnWndProc
= MainProc
;
58 wc
.hIcon
= LoadIcon( hInst
, appname
);
59 wc
.hCursor
= LoadCursor( NULL_HANDLE
, IDI_APPLICATION
);
60 wc
.hbrBackground
= (HBRUSH
) GetStockObject( BLACK_BRUSH
);
61 wc
.lpszMenuName
= "MENU_WINEMINE";
62 wc
.lpszClassName
= appname
;
64 if (!RegisterClass(&wc
)) exit(1);
65 hWnd
= CreateWindow( appname
, appname
,
66 WS_OVERLAPPEDWINDOW
& ~WS_THICKFRAME
& ~WS_MAXIMIZEBOX
,
67 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
68 NULL_HANDLE
, NULL_HANDLE
, hInst
, NULL
);
72 ShowWindow( hWnd
, cmdshow
);
75 haccel
= LoadAccelerators( hInst
, appname
);
76 SetTimer( hWnd
, ID_TIMER
, 1000, NULL
);
78 while( GetMessage(&msg
, NULL_HANDLE
, 0, 0) ) {
79 if (!TranslateAccelerator( hWnd
, haccel
, &msg
))
80 TranslateMessage( &msg
);
82 DispatchMessage( &msg
);
87 LRESULT WINAPI
MainProc( HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
96 board
.hInst
= ((LPCREATESTRUCT
) lParam
)->hInstance
;
99 CreateBoard( &board
);
107 hdc
= BeginPaint( hWnd
, &ps
);
108 hMemDC
= CreateCompatibleDC( hdc
);
110 DrawBoard( hdc
, hMemDC
, &ps
, &board
);
113 EndPaint( hWnd
, &ps
);
120 board
.pos
.x
= (unsigned) LOWORD(lParam
);
121 board
.pos
.y
= (unsigned) HIWORD(lParam
);
126 DestroyBoard( &board
);
127 PostQuitMessage( 0 );
131 if( board
.status
== PLAYING
) {
133 RedrawWindow( hWnd
, &board
.timer_rect
, NULL_HANDLE
,
134 RDW_INVALIDATE
| RDW_UPDATENOW
);
139 DEBUG("WM_LBUTTONDOWN\n");
140 if( wParam
& MK_RBUTTON
)
141 msg
= WM_MBUTTONDOWN
;
142 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
147 DEBUG("WM_LBUTTONUP\n");
148 if( wParam
& MK_RBUTTON
)
150 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
155 DEBUG("WM_RBUTTONDOWN\n");
156 if( wParam
& MK_LBUTTON
) {
159 msg
= WM_MBUTTONDOWN
;
161 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
165 DEBUG("WM_RBUTTONUP\n");
166 if( wParam
& MK_LBUTTON
)
168 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
172 DEBUG("WM_MBUTTONDOWN\n");
173 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
177 DEBUG("WM_MBUTTONUP\n");
178 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
183 if( (wParam
& MK_LBUTTON
) && (wParam
& MK_RBUTTON
) ) {
184 msg
= WM_MBUTTONDOWN
;
186 else if( wParam
& MK_LBUTTON
) {
187 msg
= WM_LBUTTONDOWN
;
193 TestBoard( hWnd
, &board
, LOWORD(lParam
), HIWORD(lParam
), msg
);
199 switch(LOWORD(wParam
)) {
201 CreateBoard( &board
);
205 hMenu
= GetMenu( hWnd
);
206 board
.IsMarkQ
= !board
.IsMarkQ
;
208 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
210 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
214 SetDifficulty( &board
, BEGINNER
);
215 CreateBoard( &board
);
219 SetDifficulty( &board
, ADVANCED
);
220 CreateBoard( &board
);
224 SetDifficulty( &board
, EXPERT
);
225 CreateBoard( &board
);
229 SetDifficulty( &board
, CUSTOM
);
230 CreateBoard( &board
);
234 SendMessage( hWnd
, WM_CLOSE
, 0, 0);
238 DialogBoxParam( board
.hInst
, "DLG_TIMES", hWnd
,
239 TimesDlgProc
, (LPARAM
) &board
);
243 DialogBox( board
.hInst
, "DLG_ABOUT", hWnd
, AboutDlgProc
);
246 DEBUG("Unknown WM_COMMAND command message received\n");
250 return( DefWindowProc( hWnd
, msg
, wParam
, lParam
));
253 void InitBoard( BOARD
*p_board
)
257 p_board
->hMinesBMP
= LoadBitmap( p_board
->hInst
, "mines");
258 p_board
->hFacesBMP
= LoadBitmap( p_board
->hInst
, "faces");
259 p_board
->hLedsBMP
= LoadBitmap( p_board
->hInst
, "leds");
261 LoadBoard( p_board
);
263 if( p_board
->pos
.x
< (unsigned) GetSystemMetrics( SM_CXFIXEDFRAME
))
264 p_board
->pos
.x
= GetSystemMetrics( SM_CXFIXEDFRAME
);
266 if( p_board
->pos
.x
> (unsigned) (GetSystemMetrics( SM_CXSCREEN
)
267 - GetSystemMetrics( SM_CXFIXEDFRAME
))) {
268 p_board
->pos
.x
= GetSystemMetrics( SM_CXSCREEN
)
269 - GetSystemMetrics( SM_CXFIXEDFRAME
);
272 if( p_board
->pos
.y
< (unsigned) (GetSystemMetrics( SM_CYMENU
)
273 + GetSystemMetrics( SM_CYCAPTION
)
274 + GetSystemMetrics( SM_CYFIXEDFRAME
))) {
275 p_board
->pos
.y
= GetSystemMetrics( SM_CYMENU
) +
276 GetSystemMetrics( SM_CYCAPTION
) +
277 GetSystemMetrics( SM_CYFIXEDFRAME
);
280 if( p_board
->pos
.y
> (unsigned) (GetSystemMetrics( SM_CYSCREEN
)
281 - GetSystemMetrics( SM_CYFIXEDFRAME
))) {
282 p_board
->pos
.y
= GetSystemMetrics( SM_CYSCREEN
)
283 - GetSystemMetrics( SM_CYFIXEDFRAME
);
286 hMenu
= GetMenu( p_board
->hWnd
);
287 CheckMenuItem( hMenu
, IDM_BEGINNER
+ (unsigned) p_board
->difficulty
,
289 if( p_board
->IsMarkQ
)
290 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
292 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
293 CheckLevel( p_board
);
296 void LoadBoard( BOARD
*p_board
)
306 RegOpenKeyEx( HKEY_LOCAL_MACHINE
, "Software\\Wine\\WineMine",
307 0, KEY_QUERY_VALUE
, &hkey
);
309 size
= sizeof( data
);
310 if( RegQueryValueEx( hkey
, "Xpos", NULL
, (LPDWORD
) &type
,
311 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
) {
312 p_board
->pos
.x
= atoi( data
);
315 p_board
->pos
.x
= GetSystemMetrics( SM_CXFIXEDFRAME
);
317 size
= sizeof( data
);
318 if( RegQueryValueEx( hkey
, "Ypos", NULL
, (LPDWORD
) &type
,
319 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
320 p_board
->pos
.y
= atoi( data
);
322 p_board
->pos
.y
= GetSystemMetrics( SM_CYMENU
)
323 + GetSystemMetrics( SM_CYCAPTION
)
324 + GetSystemMetrics( SM_CYFIXEDFRAME
);
326 size
= sizeof( data
);
327 if( RegQueryValueEx( hkey
, "Rows", NULL
, (LPDWORD
) &type
,
328 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
329 p_board
->rows
= atoi( data
);
331 p_board
->rows
= BEGINNER_ROWS
;
333 size
= sizeof( data
);
334 if( RegQueryValueEx( hkey
, "Cols", NULL
, (LPDWORD
) &type
,
335 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
336 p_board
->cols
= atoi( data
);
338 p_board
->cols
= BEGINNER_COLS
;
340 size
= sizeof( data
);
341 if( RegQueryValueEx( hkey
, "Mines", NULL
, (LPDWORD
) &type
,
342 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
343 p_board
->mines
= atoi( data
);
345 p_board
->rows
= BEGINNER_ROWS
;
347 size
= sizeof( data
);
348 if( RegQueryValueEx( hkey
, "Difficulty", NULL
, (LPDWORD
) &type
,
349 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
350 p_board
->difficulty
= (DIFFICULTY
) atoi( data
);
352 p_board
->difficulty
= BEGINNER
;
354 size
= sizeof( data
);
355 if( RegQueryValueEx( hkey
, "MarkQ", NULL
, (LPDWORD
) &type
,
356 (LPBYTE
) data
, (LPDWORD
) &size
) == ERROR_SUCCESS
)
357 p_board
->IsMarkQ
= atoi( data
);
359 p_board
->IsMarkQ
= TRUE
;
361 for( i
= 0; i
< 3; i
++ ) {
362 wsprintf( key_name
, "Name%d", i
);
363 size
= sizeof( data
);
364 if( RegQueryValueEx( hkey
, key_name
, NULL
, (LPDWORD
) &type
,
366 (LPDWORD
) &size
) == ERROR_SUCCESS
)
367 strncpy( p_board
->best_name
[i
], data
, sizeof( data
) );
369 wsprintf( p_board
->best_name
[i
], "Nobody");
372 for( i
= 0; i
< 3; i
++ ) {
373 wsprintf( key_name
, "Time%d", i
);
374 size
= sizeof( data
);
375 if( RegQueryValueEx( hkey
, key_name
, NULL
, (LPDWORD
) &type
,
377 (LPDWORD
) &size
) == ERROR_SUCCESS
)
378 p_board
->best_time
[i
] = atoi( data
);
380 p_board
->best_time
[i
] = 999;
385 void SaveBoard( BOARD
*p_board
)
392 if( RegCreateKeyEx( HKEY_LOCAL_MACHINE
,
393 "Software\\Wine\\WineMine", 0, NULL
,
394 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
,
395 &hkey
, NULL
) != ERROR_SUCCESS
)
398 wsprintf( data
, "%d", p_board
->pos
.x
);
399 RegSetValueEx( hkey
, "Xpos", 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
401 wsprintf( data
, "%d", p_board
->pos
.x
);
402 RegSetValueEx( hkey
, "Ypos", 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
404 wsprintf( data
, "%d", (int) p_board
->difficulty
);
405 RegSetValueEx( hkey
, "Difficulty", 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
407 wsprintf( data
, "%d", p_board
->rows
);
408 RegSetValueEx( hkey
, "Rows", 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
410 wsprintf( data
, "%d", p_board
->cols
);
411 RegSetValueEx( hkey
, "Cols", 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
413 wsprintf( data
, "%d", p_board
->mines
);
414 RegSetValueEx( hkey
, "Mines", 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
416 wsprintf( data
, "%d", (int) p_board
->IsMarkQ
);
417 RegSetValueEx( hkey
, "MarkQ", 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
419 for( i
= 0; i
< 3; i
++ ) {
420 wsprintf( key_name
, "Name%u", i
);
421 strncpy( data
, p_board
->best_name
[i
], sizeof( data
) );
422 RegSetValueEx( hkey
, key_name
, 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
425 for( i
= 0; i
< 3; i
++ ) {
426 wsprintf( key_name
, "Time%u", i
);
427 wsprintf( data
, "%d", p_board
->best_time
[i
] );
428 RegSetValueEx( hkey
, key_name
, 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
433 void DestroyBoard( BOARD
*p_board
)
435 DeleteObject( p_board
->hFacesBMP
);
436 DeleteObject( p_board
->hLedsBMP
);
437 DeleteObject( p_board
->hMinesBMP
);
440 void SetDifficulty( BOARD
*p_board
, DIFFICULTY difficulty
)
444 if ( difficulty
== CUSTOM
)
445 if (DialogBoxParam( p_board
->hInst
, "DLG_CUSTOM", p_board
->hWnd
,
446 CustomDlgProc
, (LPARAM
) p_board
) != 0)
449 hMenu
= GetMenu( p_board
->hWnd
);
450 CheckMenuItem( hMenu
, IDM_BEGINNER
+ p_board
->difficulty
, MF_UNCHECKED
);
451 p_board
->difficulty
= difficulty
;
452 CheckMenuItem( hMenu
, IDM_BEGINNER
+ difficulty
, MF_CHECKED
);
454 switch( difficulty
) {
456 p_board
->cols
= BEGINNER_COLS
;
457 p_board
->rows
= BEGINNER_ROWS
;
458 p_board
->mines
= BEGINNER_MINES
;
462 p_board
->cols
= ADVANCED_COLS
;
463 p_board
->rows
= ADVANCED_ROWS
;
464 p_board
->mines
= ADVANCED_MINES
;
468 p_board
->cols
= EXPERT_COLS
;
469 p_board
->rows
= EXPERT_ROWS
;
470 p_board
->mines
= EXPERT_MINES
;
478 void CreateBoard( BOARD
*p_board
)
480 int left
, top
, bottom
, right
, wnd_x
, wnd_y
, wnd_width
, wnd_height
;
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;
486 CreateBoxes( p_board
);
488 p_board
->width
= p_board
->cols
* MINE_WIDTH
+ BOARD_WMARGIN
* 2;
490 p_board
->height
= p_board
->rows
* MINE_HEIGHT
+ LED_HEIGHT
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;
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
);
511 /* setting the face rectangle boundary */
512 left
= p_board
->width
/ 2 - FACE_WIDTH
/ 2;
514 right
= left
+ FACE_WIDTH
;
515 bottom
= top
+ FACE_HEIGHT
;
516 SetRect( &p_board
->face_rect
, left
, top
, right
, bottom
);
518 /* setting the timer rectangle boundary */
519 left
= BOARD_WMARGIN
;
521 right
= left
+ LED_WIDTH
* 3;
522 bottom
= top
+ LED_HEIGHT
;
523 SetRect( &p_board
->timer_rect
, left
, top
, right
, bottom
);
525 /* setting the counter rectangle boundary */
526 left
= p_board
->width
- BOARD_WMARGIN
- LED_WIDTH
* 3;
528 right
= p_board
->width
- BOARD_WMARGIN
;
529 bottom
= top
+ LED_HEIGHT
;
530 SetRect( &p_board
->counter_rect
, left
, top
, right
, bottom
);
532 p_board
->status
= WAITING
;
533 p_board
->face_bmp
= SMILE_BMP
;
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
);
542 void CheckLevel( BOARD
*p_board
)
544 if( p_board
->rows
< BEGINNER_ROWS
)
545 p_board
->rows
= BEGINNER_ROWS
;
547 if( p_board
->rows
> MAX_ROWS
)
548 p_board
->rows
= MAX_ROWS
;
550 if( p_board
->cols
< BEGINNER_COLS
)
551 p_board
->cols
= BEGINNER_COLS
;
553 if( p_board
->cols
> MAX_COLS
)
554 p_board
->cols
= MAX_COLS
;
556 if( p_board
->mines
< BEGINNER_MINES
)
557 p_board
->mines
= BEGINNER_MINES
;
559 if( p_board
->mines
> p_board
->cols
* p_board
->rows
- 1 )
560 p_board
->mines
= p_board
->cols
* p_board
->rows
- 1;
564 void CreateBoxes( BOARD
*p_board
)
569 srand( (unsigned) time( NULL
) );
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
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;
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);
590 if( !p_board
->box
[col
][row
].IsMine
) {
592 p_board
->box
[col
][row
].IsMine
= TRUE
;
597 * Now we label the remaining boxes with the
598 * number of mines surrounding them.
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
++ ;
612 void DrawMines ( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
616 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
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
);
623 SelectObject( hMemDC
, hOldObj
);
626 void DrawMine( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, unsigned col
, unsigned row
, BOOL IsPressed
)
628 MINEBMP_OFFSET offset
= BOX_BMP
;
630 if( col
== 0 || col
> p_board
->cols
|| row
== 0 || row
> p_board
->rows
)
633 if( p_board
->status
== GAMEOVER
) {
634 if( p_board
->box
[col
][row
].IsMine
) {
635 switch( p_board
->box
[col
][row
].FlagType
) {
640 offset
= EXPLODE_BMP
;
646 switch( p_board
->box
[col
][row
].FlagType
) {
648 offset
= QUESTION_BMP
;
660 DEBUG("Unknown FlagType during game over in DrawMine\n");
664 } else { /* WAITING or PLAYING */
665 switch( p_board
->box
[col
][row
].FlagType
) {
668 offset
= QUESTION_BMP
;
685 DEBUG("Unknown FlagType while playing in DrawMine\n");
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
;
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
);
701 void DrawLeds( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, int number
, int x
, int y
)
710 led
[0] = count
/ 100 ;
711 count
-= led
[0] * 100;
714 led
[0] = 10; /* negative sign */
718 count
-= led
[1] * 10;
722 for( i
= 0; i
< 3; i
++ )
726 /* use unlit led if not playing */
727 if( p_board
->status
== WAITING
)
728 for( i
= 0; i
< 3; i
++ )
731 hOldObj
= SelectObject (hMemDC
, p_board
->hLedsBMP
);
733 for( i
= 0; i
< 3; i
++ ) {
745 SelectObject( hMemDC
, hOldObj
);
749 void DrawFace( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
753 hOldObj
= SelectObject (hMemDC
, p_board
->hFacesBMP
);
756 p_board
->face_rect
.left
,
757 p_board
->face_rect
.top
,
760 hMemDC
, 0, p_board
->face_bmp
* FACE_HEIGHT
, SRCCOPY
);
762 SelectObject( hMemDC
, hOldObj
);
766 void DrawBoard( HDC hdc
, HDC hMemDC
, PAINTSTRUCT
*ps
, BOARD
*p_board
)
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
);
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
);
780 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->face_rect
) )
781 DrawFace( hdc
, hMemDC
, p_board
);
783 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->mines_rect
) )
784 DrawMines( hdc
, hMemDC
, p_board
);
788 void TestBoard( HWND hWnd
, BOARD
*p_board
, unsigned x
, unsigned y
, int msg
)
795 if( PtInRect( &p_board
->mines_rect
, pt
) && p_board
->status
!= GAMEOVER
796 && p_board
->status
!= WON
)
797 TestMines( p_board
, pt
, msg
);
799 UnpressBoxes( p_board
,
802 p_board
->press
.x
= 0;
803 p_board
->press
.y
= 0;
806 if( p_board
->boxes_left
== 0 ) {
807 p_board
->status
= WON
;
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
;
813 DialogBoxParam( p_board
->hInst
, "DLG_CONGRATS", hWnd
,
814 CongratsDlgProc
, (LPARAM
) p_board
);
816 DialogBoxParam( p_board
->hInst
, "DLG_TIMES", hWnd
,
817 TimesDlgProc
, (LPARAM
) p_board
);
820 TestFace( p_board
, pt
, msg
);
823 void TestMines( BOARD
*p_board
, POINT pt
, int msg
)
828 col
= (pt
.x
- p_board
->mines_rect
.left
) / MINE_WIDTH
+ 1;
829 row
= (pt
.y
- p_board
->mines_rect
.top
) / MINE_HEIGHT
+ 1;
833 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
) {
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
);
844 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
)
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
);
855 PressBoxes( p_board
, col
, row
);
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
);
869 AddFlag( p_board
, col
, row
);
870 p_board
->status
= PLAYING
;
873 DEBUG("Unknown message type received in TestMines\n");
879 RedrawWindow( p_board
->hWnd
, NULL
, NULL_HANDLE
,
880 RDW_INVALIDATE
| RDW_UPDATENOW
);
885 void TestFace( BOARD
*p_board
, POINT pt
, int msg
)
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
;
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
;
897 if( PtInRect( &p_board
->face_rect
, pt
) ) {
898 if( msg
== WM_LBUTTONDOWN
)
899 p_board
->face_bmp
= SPRESS_BMP
;
901 if( msg
== WM_LBUTTONUP
)
902 CreateBoard( p_board
);
905 RedrawWindow( p_board
->hWnd
, &p_board
->face_rect
, NULL_HANDLE
,
906 RDW_INVALIDATE
| RDW_UPDATENOW
);
910 void CompleteBox( BOARD
*p_board
, unsigned col
, unsigned row
)
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
;
920 if( p_board
->box
[col
][row
].IsMine
) {
921 p_board
->face_bmp
= DEAD_BMP
;
922 p_board
->status
= GAMEOVER
;
924 else if( p_board
->status
!= GAMEOVER
)
925 p_board
->boxes_left
--;
927 if( p_board
->box
[col
][row
].NumMines
== 0 )
929 for( i
= -1; i
<= 1; i
++ )
930 for( j
= -1; j
<= 1; j
++ )
931 CompleteBox( p_board
, col
+ i
, row
+ j
);
937 void CompleteBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
939 unsigned numFlags
= 0;
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
)
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
);
960 void AddFlag( BOARD
*p_board
, unsigned col
, unsigned row
)
962 if( p_board
->box
[col
][row
].FlagType
!= COMPLETE
) {
963 switch( p_board
->box
[col
][row
].FlagType
) {
965 if( p_board
->IsMarkQ
)
966 p_board
->box
[col
][row
].FlagType
= QUESTION
;
968 p_board
->box
[col
][row
].FlagType
= NORMAL
;
969 p_board
->num_flags
--;
973 p_board
->box
[col
][row
].FlagType
= NORMAL
;
977 p_board
->box
[col
][row
].FlagType
= FLAG
;
978 p_board
->num_flags
++;
984 void PressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
990 hdc
= GetDC( p_board
->hWnd
);
991 hMemDC
= CreateCompatibleDC( hdc
);
992 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
994 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, TRUE
);
996 SelectObject( hMemDC
, hOldObj
);
998 ReleaseDC( p_board
->hWnd
, hdc
);
1002 void PressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
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
);
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
);
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
);
1024 p_board
->press
.x
= col
;
1025 p_board
->press
.y
= row
;
1029 void UnpressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
1035 hdc
= GetDC( p_board
->hWnd
);
1036 hMemDC
= CreateCompatibleDC( hdc
);
1037 hOldObj
= SelectObject( hMemDC
, p_board
->hMinesBMP
);
1039 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, FALSE
);
1041 SelectObject( hMemDC
, hOldObj
);
1043 ReleaseDC( p_board
->hWnd
, hdc
);
1047 void UnpressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
1051 for( i
= -1; i
<= 1; i
++ )
1052 for( j
= -1; j
<= 1; j
++ ) {
1053 UnpressBox( p_board
, col
+ i
, row
+ j
);