4 * Copyright 2000 Joshua Thielen
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define WIN32_LEAN_AND_MEAN
31 #include <wine/debug.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(winemine
);
35 static const DWORD wnd_style
= WS_OVERLAPPEDWINDOW
& ~WS_THICKFRAME
& ~WS_MAXIMIZEBOX
;
36 static const char* registry_key
= "Software\\Microsoft\\WinMine";
39 void CheckLevel( BOARD
*p_board
)
41 if( p_board
->rows
< BEGINNER_ROWS
)
42 p_board
->rows
= BEGINNER_ROWS
;
44 if( p_board
->rows
> MAX_ROWS
)
45 p_board
->rows
= MAX_ROWS
;
47 if( p_board
->cols
< BEGINNER_COLS
)
48 p_board
->cols
= BEGINNER_COLS
;
50 if( p_board
->cols
> MAX_COLS
)
51 p_board
->cols
= MAX_COLS
;
53 if( p_board
->mines
< BEGINNER_MINES
)
54 p_board
->mines
= BEGINNER_MINES
;
56 if( p_board
->mines
> ( p_board
->cols
- 1 ) * ( p_board
->rows
- 1 ) )
57 p_board
->mines
= ( p_board
->cols
- 1 ) * ( p_board
->rows
- 1 );
60 static void LoadBoard( BOARD
*p_board
)
65 char data
[MAX_PLAYER_NAME_SIZE
+1];
69 RegOpenKeyEx( HKEY_CURRENT_USER
, registry_key
,
70 0, KEY_QUERY_VALUE
, &hkey
);
72 size
= sizeof( p_board
->pos
.x
);
73 if( !RegQueryValueEx( hkey
, "Xpos", NULL
, &type
,
74 (LPBYTE
) &p_board
->pos
.x
, &size
) == ERROR_SUCCESS
)
77 size
= sizeof( p_board
->pos
.y
);
78 if( !RegQueryValueEx( hkey
, "Ypos", NULL
, &type
,
79 (LPBYTE
) &p_board
->pos
.y
, &size
) == ERROR_SUCCESS
)
82 size
= sizeof( p_board
->rows
);
83 if( !RegQueryValueEx( hkey
, "Height", NULL
, &type
,
84 (LPBYTE
) &p_board
->rows
, &size
) == ERROR_SUCCESS
)
85 p_board
->rows
= BEGINNER_ROWS
;
87 size
= sizeof( p_board
->cols
);
88 if( !RegQueryValueEx( hkey
, "Width", NULL
, &type
,
89 (LPBYTE
) &p_board
->cols
, &size
) == ERROR_SUCCESS
)
90 p_board
->cols
= BEGINNER_COLS
;
92 size
= sizeof( p_board
->mines
);
93 if( !RegQueryValueEx( hkey
, "Mines", NULL
, &type
,
94 (LPBYTE
) &p_board
->mines
, &size
) == ERROR_SUCCESS
)
95 p_board
->mines
= BEGINNER_MINES
;
97 size
= sizeof( p_board
->difficulty
);
98 if( !RegQueryValueEx( hkey
, "Difficulty", NULL
, &type
,
99 (LPBYTE
) &p_board
->difficulty
, &size
) == ERROR_SUCCESS
)
100 p_board
->difficulty
= BEGINNER
;
102 size
= sizeof( p_board
->IsMarkQ
);
103 if( !RegQueryValueEx( hkey
, "Mark", NULL
, &type
,
104 (LPBYTE
) &p_board
->IsMarkQ
, &size
) == ERROR_SUCCESS
)
105 p_board
->IsMarkQ
= TRUE
;
107 for( i
= 0; i
< 3; i
++ ) {
108 wsprintf( key_name
, "Name%d", i
+1 );
109 size
= sizeof( data
);
110 if( RegQueryValueEx( hkey
, key_name
, NULL
, &type
,
111 (LPBYTE
) data
, &size
) == ERROR_SUCCESS
)
112 lstrcpynA( p_board
->best_name
[i
], data
, sizeof(p_board
->best_name
[i
]) );
114 LoadString( p_board
->hInst
, IDS_NOBODY
, p_board
->best_name
[i
], MAX_PLAYER_NAME_SIZE
+1 );
117 for( i
= 0; i
< 3; i
++ ) {
118 wsprintf( key_name
, "Time%d", i
+1 );
119 size
= sizeof( p_board
->best_time
[i
] );
120 if( !RegQueryValueEx( hkey
, key_name
, NULL
, &type
,
121 (LPBYTE
) &p_board
->best_time
[i
], &size
) == ERROR_SUCCESS
)
122 p_board
->best_time
[i
] = 999;
127 static void InitBoard( BOARD
*p_board
)
131 p_board
->hMinesBMP
= LoadBitmap( p_board
->hInst
, "mines");
132 p_board
->hFacesBMP
= LoadBitmap( p_board
->hInst
, "faces");
133 p_board
->hLedsBMP
= LoadBitmap( p_board
->hInst
, "leds");
135 LoadBoard( p_board
);
137 hMenu
= GetMenu( p_board
->hWnd
);
138 CheckMenuItem( hMenu
, IDM_BEGINNER
+ (unsigned) p_board
->difficulty
,
140 if( p_board
->IsMarkQ
)
141 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
143 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
144 CheckLevel( p_board
);
147 static void SaveBoard( BOARD
*p_board
)
151 char data
[MAX_PLAYER_NAME_SIZE
+1];
154 if( RegCreateKeyEx( HKEY_CURRENT_USER
, registry_key
,
156 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
,
157 &hkey
, NULL
) != ERROR_SUCCESS
)
160 RegSetValueEx( hkey
, "Xpos", 0, REG_DWORD
, (LPBYTE
) &p_board
->pos
.x
, sizeof(p_board
->pos
.x
) );
161 RegSetValueEx( hkey
, "Ypos", 0, REG_DWORD
, (LPBYTE
) &p_board
->pos
.y
, sizeof(p_board
->pos
.y
) );
162 RegSetValueEx( hkey
, "Difficulty", 0, REG_DWORD
, (LPBYTE
) &p_board
->difficulty
, sizeof(p_board
->difficulty
) );
163 RegSetValueEx( hkey
, "Height", 0, REG_DWORD
, (LPBYTE
) &p_board
->rows
, sizeof(p_board
->rows
) );
164 RegSetValueEx( hkey
, "Width", 0, REG_DWORD
, (LPBYTE
) &p_board
->cols
, sizeof(p_board
->cols
) );
165 RegSetValueEx( hkey
, "Mines", 0, REG_DWORD
, (LPBYTE
) &p_board
->mines
, sizeof(p_board
->mines
) );
166 RegSetValueEx( hkey
, "Mark", 0, REG_DWORD
, (LPBYTE
) &p_board
->IsMarkQ
, sizeof(p_board
->IsMarkQ
) );
168 for( i
= 0; i
< 3; i
++ ) {
169 wsprintf( key_name
, "Name%u", i
+1 );
170 lstrcpyn( data
, p_board
->best_name
[i
], sizeof( data
) );
171 RegSetValueEx( hkey
, key_name
, 0, REG_SZ
, (LPBYTE
) data
, strlen(data
)+1 );
174 for( i
= 0; i
< 3; i
++ ) {
175 wsprintf( key_name
, "Time%u", i
+1 );
176 RegSetValueEx( hkey
, key_name
, 0, REG_DWORD
, (LPBYTE
) &p_board
->best_time
[i
], sizeof(p_board
->best_time
[i
]) );
181 static void DestroyBoard( BOARD
*p_board
)
183 DeleteObject( p_board
->hFacesBMP
);
184 DeleteObject( p_board
->hLedsBMP
);
185 DeleteObject( p_board
->hMinesBMP
);
188 static void SetDifficulty( BOARD
*p_board
, DIFFICULTY difficulty
)
192 if ( difficulty
== CUSTOM
)
193 if (DialogBoxParam( p_board
->hInst
, "DLG_CUSTOM", p_board
->hWnd
,
194 CustomDlgProc
, (LPARAM
) p_board
) != 0)
197 hMenu
= GetMenu( p_board
->hWnd
);
198 CheckMenuItem( hMenu
, IDM_BEGINNER
+ p_board
->difficulty
, MF_UNCHECKED
);
199 p_board
->difficulty
= difficulty
;
200 CheckMenuItem( hMenu
, IDM_BEGINNER
+ difficulty
, MF_CHECKED
);
202 switch( difficulty
) {
204 p_board
->cols
= BEGINNER_COLS
;
205 p_board
->rows
= BEGINNER_ROWS
;
206 p_board
->mines
= BEGINNER_MINES
;
210 p_board
->cols
= ADVANCED_COLS
;
211 p_board
->rows
= ADVANCED_ROWS
;
212 p_board
->mines
= ADVANCED_MINES
;
216 p_board
->cols
= EXPERT_COLS
;
217 p_board
->rows
= EXPERT_ROWS
;
219 p_board
->mines
= EXPERT_MINES
;
227 static void ShiftBetween(LONG
* x
, LONG
* y
, LONG a
, LONG b
)
239 static void MoveOnScreen(RECT
* rect
)
244 /* find the nearest monitor ... */
245 hMonitor
= MonitorFromRect(rect
, MONITOR_DEFAULTTONEAREST
);
247 /* ... and move it into the work area (ie excluding task bar)*/
248 mi
.cbSize
= sizeof(mi
);
249 GetMonitorInfo(hMonitor
, &mi
);
251 ShiftBetween(&rect
->left
, &rect
->right
, mi
.rcWork
.left
, mi
.rcWork
.right
);
252 ShiftBetween(&rect
->top
, &rect
->bottom
, mi
.rcWork
.top
, mi
.rcWork
.bottom
);
255 static void CreateBoard( BOARD
*p_board
)
257 int left
, top
, bottom
, right
;
261 p_board
->mb
= MB_NONE
;
262 p_board
->boxes_left
= p_board
->cols
* p_board
->rows
- p_board
->mines
;
263 p_board
->num_flags
= 0;
265 /* Create the boxes...
266 * We actually create them with an empty border,
267 * so special care doesn't have to be taken on the edges
269 for( col
= 0; col
<= p_board
->cols
+ 1; col
++ )
270 for( row
= 0; row
<= p_board
->rows
+ 1; row
++ ) {
271 p_board
->box
[col
][row
].IsPressed
= FALSE
;
272 p_board
->box
[col
][row
].IsMine
= FALSE
;
273 p_board
->box
[col
][row
].FlagType
= NORMAL
;
274 p_board
->box
[col
][row
].NumMines
= 0;
277 p_board
->width
= p_board
->cols
* MINE_WIDTH
+ BOARD_WMARGIN
* 2;
279 p_board
->height
= p_board
->rows
* MINE_HEIGHT
+ LED_HEIGHT
282 /* setting the mines rectangle boundary */
283 left
= BOARD_WMARGIN
;
284 top
= BOARD_HMARGIN
* 2 + LED_HEIGHT
;
285 right
= left
+ p_board
->cols
* MINE_WIDTH
;
286 bottom
= top
+ p_board
->rows
* MINE_HEIGHT
;
287 SetRect( &p_board
->mines_rect
, left
, top
, right
, bottom
);
289 /* setting the face rectangle boundary */
290 left
= p_board
->width
/ 2 - FACE_WIDTH
/ 2;
292 right
= left
+ FACE_WIDTH
;
293 bottom
= top
+ FACE_HEIGHT
;
294 SetRect( &p_board
->face_rect
, left
, top
, right
, bottom
);
296 /* setting the timer rectangle boundary */
297 left
= BOARD_WMARGIN
;
299 right
= left
+ LED_WIDTH
* 3;
300 bottom
= top
+ LED_HEIGHT
;
301 SetRect( &p_board
->timer_rect
, left
, top
, right
, bottom
);
303 /* setting the counter rectangle boundary */
304 left
= p_board
->width
- BOARD_WMARGIN
- LED_WIDTH
* 3;
306 right
= p_board
->width
- BOARD_WMARGIN
;
307 bottom
= top
+ LED_HEIGHT
;
308 SetRect( &p_board
->counter_rect
, left
, top
, right
, bottom
);
310 p_board
->status
= WAITING
;
311 p_board
->face_bmp
= SMILE_BMP
;
314 wnd_rect
.left
= p_board
->pos
.x
;
315 wnd_rect
.right
= p_board
->pos
.x
+ p_board
->width
;
316 wnd_rect
.top
= p_board
->pos
.y
;
317 wnd_rect
.bottom
= p_board
->pos
.y
+ p_board
->height
;
318 AdjustWindowRect(&wnd_rect
, wnd_style
, TRUE
);
320 /* Make sure the window is completely on the screen */
321 MoveOnScreen(&wnd_rect
);
322 MoveWindow( p_board
->hWnd
, wnd_rect
.left
, wnd_rect
.top
,
323 wnd_rect
.right
- wnd_rect
.left
,
324 wnd_rect
.bottom
- wnd_rect
.top
,
326 RedrawWindow( p_board
->hWnd
, NULL
, 0,
327 RDW_INVALIDATE
| RDW_UPDATENOW
| RDW_ERASE
);
331 /* Randomly places mines everywhere except the selected box. */
332 static void PlaceMines ( BOARD
*p_board
, int selected_col
, int selected_row
)
337 srand( (unsigned) time( NULL
) );
339 /* Temporarily place a mine at the selected box until all the other
340 * mines are placed, this avoids checking in the mine creation loop. */
341 p_board
->box
[selected_col
][selected_row
].IsMine
= TRUE
;
345 while( (unsigned) i
< p_board
->mines
) {
346 col
= (int) (p_board
->cols
* (float) rand() / RAND_MAX
+ 1);
347 row
= (int) (p_board
->rows
* (float) rand() / RAND_MAX
+ 1);
349 if( !p_board
->box
[col
][row
].IsMine
) {
351 p_board
->box
[col
][row
].IsMine
= TRUE
;
355 /* Remove temporarily placed mine for selected box */
356 p_board
->box
[selected_col
][selected_row
].IsMine
= FALSE
;
359 * Now we label the remaining boxes with the
360 * number of mines surrounding them.
362 for( col
= 1; col
< p_board
->cols
+ 1; col
++ )
363 for( row
= 1; row
< p_board
->rows
+ 1; row
++ ) {
364 for( i
= -1; i
<= 1; i
++ )
365 for( j
= -1; j
<= 1; j
++ ) {
366 if( p_board
->box
[col
+ i
][row
+ j
].IsMine
) {
367 p_board
->box
[col
][row
].NumMines
++ ;
373 static void DrawMine( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, unsigned col
, unsigned row
, BOOL IsPressed
)
375 MINEBMP_OFFSET offset
= BOX_BMP
;
377 if( col
== 0 || col
> p_board
->cols
|| row
== 0 || row
> p_board
->rows
)
380 if( p_board
->status
== GAMEOVER
) {
381 if( p_board
->box
[col
][row
].IsMine
) {
382 switch( p_board
->box
[col
][row
].FlagType
) {
387 offset
= EXPLODE_BMP
;
395 switch( p_board
->box
[col
][row
].FlagType
) {
397 offset
= QUESTION_BMP
;
409 WINE_TRACE("Unknown FlagType during game over in DrawMine\n");
413 } else { /* WAITING or PLAYING */
414 switch( p_board
->box
[col
][row
].FlagType
) {
417 offset
= QUESTION_BMP
;
434 WINE_TRACE("Unknown FlagType while playing in DrawMine\n");
439 if( p_board
->box
[col
][row
].FlagType
== COMPLETE
440 && !p_board
->box
[col
][row
].IsMine
)
441 offset
= (MINEBMP_OFFSET
) p_board
->box
[col
][row
].NumMines
;
444 (col
- 1) * MINE_WIDTH
+ p_board
->mines_rect
.left
,
445 (row
- 1) * MINE_HEIGHT
+ p_board
->mines_rect
.top
,
446 MINE_WIDTH
, MINE_HEIGHT
,
447 hMemDC
, 0, offset
* MINE_HEIGHT
, SRCCOPY
);
450 static void DrawMines ( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
454 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
456 for( row
= 1; row
<= p_board
->rows
; row
++ ) {
457 for( col
= 1; col
<= p_board
->cols
; col
++ ) {
458 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, FALSE
);
461 SelectObject( hMemDC
, hOldObj
);
464 static void DrawLeds( HDC hdc
, HDC hMemDC
, BOARD
*p_board
, int number
, int x
, int y
)
473 led
[0] = count
/ 100 ;
474 count
-= led
[0] * 100;
477 led
[0] = 10; /* negative sign */
481 count
-= led
[1] * 10;
485 for( i
= 0; i
< 3; i
++ )
489 hOldObj
= SelectObject (hMemDC
, p_board
->hLedsBMP
);
491 for( i
= 0; i
< 3; i
++ ) {
503 SelectObject( hMemDC
, hOldObj
);
507 static void DrawFace( HDC hdc
, HDC hMemDC
, BOARD
*p_board
)
511 hOldObj
= SelectObject (hMemDC
, p_board
->hFacesBMP
);
514 p_board
->face_rect
.left
,
515 p_board
->face_rect
.top
,
518 hMemDC
, 0, p_board
->face_bmp
* FACE_HEIGHT
, SRCCOPY
);
520 SelectObject( hMemDC
, hOldObj
);
524 static void DrawBoard( HDC hdc
, HDC hMemDC
, PAINTSTRUCT
*ps
, BOARD
*p_board
)
528 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->counter_rect
) )
529 DrawLeds( hdc
, hMemDC
, p_board
, p_board
->mines
- p_board
->num_flags
,
530 p_board
->counter_rect
.left
,
531 p_board
->counter_rect
.top
);
533 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->timer_rect
) )
534 DrawLeds( hdc
, hMemDC
, p_board
, p_board
->time
,
535 p_board
->timer_rect
.left
,
536 p_board
->timer_rect
.top
);
538 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->face_rect
) )
539 DrawFace( hdc
, hMemDC
, p_board
);
541 if( IntersectRect( &tmp_rect
, &ps
->rcPaint
, &p_board
->mines_rect
) )
542 DrawMines( hdc
, hMemDC
, p_board
);
546 static void AddFlag( BOARD
*p_board
, unsigned col
, unsigned row
)
548 if( p_board
->box
[col
][row
].FlagType
!= COMPLETE
) {
549 switch( p_board
->box
[col
][row
].FlagType
) {
551 if( p_board
->IsMarkQ
)
552 p_board
->box
[col
][row
].FlagType
= QUESTION
;
554 p_board
->box
[col
][row
].FlagType
= NORMAL
;
555 p_board
->num_flags
--;
559 p_board
->box
[col
][row
].FlagType
= NORMAL
;
563 p_board
->box
[col
][row
].FlagType
= FLAG
;
564 p_board
->num_flags
++;
570 static void UnpressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
576 hdc
= GetDC( p_board
->hWnd
);
577 hMemDC
= CreateCompatibleDC( hdc
);
578 hOldObj
= SelectObject( hMemDC
, p_board
->hMinesBMP
);
580 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, FALSE
);
582 SelectObject( hMemDC
, hOldObj
);
584 ReleaseDC( p_board
->hWnd
, hdc
);
588 static void UnpressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
592 for( i
= -1; i
<= 1; i
++ )
593 for( j
= -1; j
<= 1; j
++ ) {
594 UnpressBox( p_board
, col
+ i
, row
+ j
);
599 static void PressBox( BOARD
*p_board
, unsigned col
, unsigned row
)
605 hdc
= GetDC( p_board
->hWnd
);
606 hMemDC
= CreateCompatibleDC( hdc
);
607 hOldObj
= SelectObject (hMemDC
, p_board
->hMinesBMP
);
609 DrawMine( hdc
, hMemDC
, p_board
, col
, row
, TRUE
);
611 SelectObject( hMemDC
, hOldObj
);
613 ReleaseDC( p_board
->hWnd
, hdc
);
617 static void PressBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
621 for( i
= -1; i
<= 1; i
++ )
622 for( j
= -1; j
<= 1; j
++ ) {
623 p_board
->box
[col
+ i
][row
+ j
].IsPressed
= TRUE
;
624 PressBox( p_board
, col
+ i
, row
+ j
);
627 for( i
= -1; i
<= 1; i
++ )
628 for( j
= -1; j
<= 1; j
++ ) {
629 if( !p_board
->box
[p_board
->press
.x
+ i
][p_board
->press
.y
+ j
].IsPressed
)
630 UnpressBox( p_board
, p_board
->press
.x
+ i
, p_board
->press
.y
+ j
);
633 for( i
= -1; i
<= 1; i
++ )
634 for( j
= -1; j
<= 1; j
++ ) {
635 p_board
->box
[col
+ i
][row
+ j
].IsPressed
= FALSE
;
636 PressBox( p_board
, col
+ i
, row
+ j
);
639 p_board
->press
.x
= col
;
640 p_board
->press
.y
= row
;
644 static void CompleteBox( BOARD
*p_board
, unsigned col
, unsigned row
)
648 if( p_board
->box
[col
][row
].FlagType
!= COMPLETE
&&
649 p_board
->box
[col
][row
].FlagType
!= FLAG
&&
650 col
> 0 && col
< p_board
->cols
+ 1 &&
651 row
> 0 && row
< p_board
->rows
+ 1 ) {
652 p_board
->box
[col
][row
].FlagType
= COMPLETE
;
654 if( p_board
->box
[col
][row
].IsMine
) {
655 p_board
->face_bmp
= DEAD_BMP
;
656 p_board
->status
= GAMEOVER
;
658 else if( p_board
->status
!= GAMEOVER
)
659 p_board
->boxes_left
--;
661 if( p_board
->box
[col
][row
].NumMines
== 0 )
663 for( i
= -1; i
<= 1; i
++ )
664 for( j
= -1; j
<= 1; j
++ )
665 CompleteBox( p_board
, col
+ i
, row
+ j
);
671 static void CompleteBoxes( BOARD
*p_board
, unsigned col
, unsigned row
)
673 unsigned numFlags
= 0;
676 if( p_board
->box
[col
][row
].FlagType
== COMPLETE
) {
677 for( i
= -1; i
<= 1; i
++ )
678 for( j
= -1; j
<= 1; j
++ ) {
679 if( p_board
->box
[col
+i
][row
+j
].FlagType
== FLAG
)
683 if( numFlags
== p_board
->box
[col
][row
].NumMines
) {
684 for( i
= -1; i
<= 1; i
++ )
685 for( j
= -1; j
<= 1; j
++ ) {
686 if( p_board
->box
[col
+i
][row
+j
].FlagType
!= FLAG
)
687 CompleteBox( p_board
, col
+i
, row
+j
);
694 static void TestMines( BOARD
*p_board
, POINT pt
, int msg
)
699 col
= (pt
.x
- p_board
->mines_rect
.left
) / MINE_WIDTH
+ 1;
700 row
= (pt
.y
- p_board
->mines_rect
.top
) / MINE_HEIGHT
+ 1;
704 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
) {
706 p_board
->press
.x
, p_board
->press
.y
);
707 p_board
->press
.x
= col
;
708 p_board
->press
.y
= row
;
709 PressBox( p_board
, col
, row
);
715 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
)
717 p_board
->press
.x
, p_board
->press
.y
);
718 p_board
->press
.x
= 0;
719 p_board
->press
.y
= 0;
720 if( p_board
->box
[col
][row
].FlagType
!= FLAG
721 && p_board
->status
!= PLAYING
)
723 p_board
->status
= PLAYING
;
724 PlaceMines( p_board
, col
, row
);
726 CompleteBox( p_board
, col
, row
);
730 PressBoxes( p_board
, col
, row
);
735 if( p_board
->press
.x
!= col
|| p_board
->press
.y
!= row
)
736 UnpressBoxes( p_board
,
737 p_board
->press
.x
, p_board
->press
.y
);
738 p_board
->press
.x
= 0;
739 p_board
->press
.y
= 0;
740 CompleteBoxes( p_board
, col
, row
);
744 AddFlag( p_board
, col
, row
);
747 WINE_TRACE("Unknown message type received in TestMines\n");
753 RedrawWindow( p_board
->hWnd
, NULL
, 0,
754 RDW_INVALIDATE
| RDW_UPDATENOW
);
759 static void TestFace( BOARD
*p_board
, POINT pt
, int msg
)
761 if( p_board
->status
== PLAYING
|| p_board
->status
== WAITING
) {
762 if( msg
== WM_LBUTTONDOWN
|| msg
== WM_MBUTTONDOWN
)
763 p_board
->face_bmp
= OOH_BMP
;
764 else p_board
->face_bmp
= SMILE_BMP
;
766 else if( p_board
->status
== GAMEOVER
)
767 p_board
->face_bmp
= DEAD_BMP
;
768 else if( p_board
->status
== WON
)
769 p_board
->face_bmp
= COOL_BMP
;
771 if( PtInRect( &p_board
->face_rect
, pt
) ) {
772 if( msg
== WM_LBUTTONDOWN
)
773 p_board
->face_bmp
= SPRESS_BMP
;
775 if( msg
== WM_LBUTTONUP
)
776 CreateBoard( p_board
);
779 RedrawWindow( p_board
->hWnd
, &p_board
->face_rect
, 0,
780 RDW_INVALIDATE
| RDW_UPDATENOW
);
784 static void TestBoard( HWND hWnd
, BOARD
*p_board
, int x
, int y
, int msg
)
792 if( PtInRect( &p_board
->mines_rect
, pt
) && p_board
->status
!= GAMEOVER
793 && p_board
->status
!= WON
)
794 TestMines( p_board
, pt
, msg
);
796 UnpressBoxes( p_board
,
799 p_board
->press
.x
= 0;
800 p_board
->press
.y
= 0;
803 if( p_board
->boxes_left
== 0 ) {
804 p_board
->status
= WON
;
806 if (p_board
->num_flags
< p_board
->mines
) {
807 for( row
= 1; row
<= p_board
->rows
; row
++ ) {
808 for( col
= 1; col
<= p_board
->cols
; col
++ ) {
809 if (p_board
->box
[col
][row
].IsMine
&& p_board
->box
[col
][row
].FlagType
!= FLAG
)
810 p_board
->box
[col
][row
].FlagType
= FLAG
;
814 p_board
->num_flags
= p_board
->mines
;
816 RedrawWindow( p_board
->hWnd
, NULL
, 0,
817 RDW_INVALIDATE
| RDW_UPDATENOW
);
820 if( p_board
->difficulty
!= CUSTOM
&&
821 p_board
->time
< p_board
->best_time
[p_board
->difficulty
] ) {
822 p_board
->best_time
[p_board
->difficulty
] = p_board
->time
;
824 DialogBoxParam( p_board
->hInst
, "DLG_CONGRATS", hWnd
,
825 CongratsDlgProc
, (LPARAM
) p_board
);
827 DialogBoxParam( p_board
->hInst
, "DLG_TIMES", hWnd
,
828 TimesDlgProc
, (LPARAM
) p_board
);
831 TestFace( p_board
, pt
, msg
);
835 static LRESULT WINAPI
MainProc( HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
844 board
.hInst
= ((LPCREATESTRUCT
) lParam
)->hInstance
;
847 CreateBoard( &board
);
854 WINE_TRACE("WM_PAINT\n");
855 hdc
= BeginPaint( hWnd
, &ps
);
856 hMemDC
= CreateCompatibleDC( hdc
);
858 DrawBoard( hdc
, hMemDC
, &ps
, &board
);
861 EndPaint( hWnd
, &ps
);
867 WINE_TRACE("WM_MOVE\n");
868 board
.pos
.x
= (short)LOWORD(lParam
);
869 board
.pos
.y
= (short)HIWORD(lParam
);
874 DestroyBoard( &board
);
875 PostQuitMessage( 0 );
879 if( board
.status
== PLAYING
) {
881 RedrawWindow( hWnd
, &board
.timer_rect
, 0,
882 RDW_INVALIDATE
| RDW_UPDATENOW
);
887 WINE_TRACE("WM_LBUTTONDOWN\n");
888 if( wParam
& MK_RBUTTON
)
889 msg
= WM_MBUTTONDOWN
;
890 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
895 WINE_TRACE("WM_LBUTTONUP\n");
896 if( wParam
& MK_RBUTTON
)
898 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
903 WINE_TRACE("WM_RBUTTONDOWN\n");
904 if( wParam
& MK_LBUTTON
) {
907 msg
= WM_MBUTTONDOWN
;
909 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
913 WINE_TRACE("WM_RBUTTONUP\n");
914 if( wParam
& MK_LBUTTON
)
916 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
920 WINE_TRACE("WM_MBUTTONDOWN\n");
921 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
925 WINE_TRACE("WM_MBUTTONUP\n");
926 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
931 if( ( wParam
& MK_MBUTTON
) ||
932 ( ( wParam
& MK_LBUTTON
) && ( wParam
& MK_RBUTTON
) ) ) {
933 msg
= WM_MBUTTONDOWN
;
935 else if( wParam
& MK_LBUTTON
) {
936 msg
= WM_LBUTTONDOWN
;
942 TestBoard( hWnd
, &board
, (short)LOWORD(lParam
), (short)HIWORD(lParam
), msg
);
948 switch(LOWORD(wParam
)) {
950 CreateBoard( &board
);
954 hMenu
= GetMenu( hWnd
);
955 board
.IsMarkQ
= !board
.IsMarkQ
;
957 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_CHECKED
);
959 CheckMenuItem( hMenu
, IDM_MARKQ
, MF_UNCHECKED
);
963 SetDifficulty( &board
, BEGINNER
);
964 CreateBoard( &board
);
968 SetDifficulty( &board
, ADVANCED
);
969 CreateBoard( &board
);
973 SetDifficulty( &board
, EXPERT
);
974 CreateBoard( &board
);
978 SetDifficulty( &board
, CUSTOM
);
979 CreateBoard( &board
);
983 SendMessage( hWnd
, WM_CLOSE
, 0, 0);
987 DialogBoxParam( board
.hInst
, "DLG_TIMES", hWnd
,
988 TimesDlgProc
, (LPARAM
) &board
);
993 WCHAR appname
[256], other
[256];
994 LoadStringW( board
.hInst
, IDS_APPNAME
, appname
, sizeof(appname
)/sizeof(WCHAR
) );
995 LoadStringW( board
.hInst
, IDS_ABOUT
, other
, sizeof(other
)/sizeof(WCHAR
) );
996 ShellAboutW( hWnd
, appname
, other
,
997 LoadImageA( board
.hInst
, "WINEMINE", IMAGE_ICON
, 48, 48, LR_SHARED
));
1001 WINE_TRACE("Unknown WM_COMMAND command message received\n");
1005 return( DefWindowProc( hWnd
, msg
, wParam
, lParam
));
1008 int WINAPI
WinMain( HINSTANCE hInst
, HINSTANCE hPrevInst
, LPSTR cmdline
, int cmdshow
)
1016 LoadString( hInst
, IDS_APPNAME
, appname
, sizeof(appname
));
1018 wc
.cbSize
= sizeof(wc
);
1020 wc
.lpfnWndProc
= MainProc
;
1023 wc
.hInstance
= hInst
;
1024 wc
.hIcon
= LoadIcon( hInst
, "WINEMINE" );
1025 wc
.hCursor
= LoadCursor( 0, IDI_APPLICATION
);
1026 wc
.hbrBackground
= GetStockObject( BLACK_BRUSH
);
1027 wc
.lpszMenuName
= "MENU_WINEMINE";
1028 wc
.lpszClassName
= appname
;
1029 wc
.hIconSm
= LoadImage( hInst
, "WINEMINE", IMAGE_ICON
,
1030 GetSystemMetrics(SM_CXSMICON
), GetSystemMetrics(SM_CYSMICON
), LR_SHARED
);
1032 if (!RegisterClassEx(&wc
)) ExitProcess(1);
1033 hWnd
= CreateWindow( appname
, appname
,
1035 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
1036 0, 0, hInst
, NULL
);
1038 if (!hWnd
) ExitProcess(1);
1040 ShowWindow( hWnd
, cmdshow
);
1041 UpdateWindow( hWnd
);
1043 haccel
= LoadAccelerators( hInst
, MAKEINTRESOURCE(IDA_WINEMINE
) );
1044 SetTimer( hWnd
, ID_TIMER
, 1000, NULL
);
1046 while( GetMessage(&msg
, 0, 0, 0) ) {
1047 if (!TranslateAccelerator( hWnd
, haccel
, &msg
))
1048 TranslateMessage( &msg
);
1050 DispatchMessage( &msg
);