[FONTVIEW]
[reactos.git] / reactos / base / applications / games / winmine / main.c
1 /*
2 * WineMine (main.c)
3 *
4 * Copyright 2000 Joshua Thielen
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winreg.h>
26 #include <wingdi.h>
27 #include <time.h>
28 #include <stdlib.h>
29 #include <shellapi.h>
30
31 #include "main.h"
32 #include "resource.h"
33
34 #include <wine/debug.h>
35
36 WINE_DEFAULT_DEBUG_CHANNEL(winemine);
37
38 static const DWORD wnd_style = WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX;
39 static const char* registry_key = "Software\\Microsoft\\WinMine";
40
41
42 void CheckLevel( BOARD *p_board )
43 {
44 if( p_board->rows < BEGINNER_ROWS )
45 p_board->rows = BEGINNER_ROWS;
46
47 if( p_board->rows > MAX_ROWS )
48 p_board->rows = MAX_ROWS;
49
50 if( p_board->cols < BEGINNER_COLS )
51 p_board->cols = BEGINNER_COLS;
52
53 if( p_board->cols > MAX_COLS )
54 p_board->cols = MAX_COLS;
55
56 if( p_board->mines < BEGINNER_MINES )
57 p_board->mines = BEGINNER_MINES;
58
59 if( p_board->mines > ( p_board->cols - 1 ) * ( p_board->rows - 1 ) )
60 p_board->mines = ( p_board->cols - 1 ) * ( p_board->rows - 1 );
61 }
62
63 static void LoadBoard( BOARD *p_board )
64 {
65 DWORD size;
66 DWORD type;
67 HKEY hkey;
68 char data[MAX_PLAYER_NAME_SIZE+1];
69 char key_name[8];
70 unsigned i;
71
72 RegOpenKeyEx( HKEY_CURRENT_USER, registry_key,
73 0, KEY_QUERY_VALUE, &hkey );
74
75 size = sizeof( p_board->pos.x );
76 if( !RegQueryValueEx( hkey, "Xpos", NULL, &type,
77 (LPBYTE) &p_board->pos.x, &size ) == ERROR_SUCCESS )
78 p_board->pos.x = 0;
79
80 size = sizeof( p_board->pos.y );
81 if( !RegQueryValueEx( hkey, "Ypos", NULL, &type,
82 (LPBYTE) &p_board->pos.y, &size ) == ERROR_SUCCESS )
83 p_board->pos.y = 0;
84
85 size = sizeof( p_board->rows );
86 if( !RegQueryValueEx( hkey, "Height", NULL, &type,
87 (LPBYTE) &p_board->rows, &size ) == ERROR_SUCCESS )
88 p_board->rows = BEGINNER_ROWS;
89
90 size = sizeof( p_board->cols );
91 if( !RegQueryValueEx( hkey, "Width", NULL, &type,
92 (LPBYTE) &p_board->cols, &size ) == ERROR_SUCCESS )
93 p_board->cols = BEGINNER_COLS;
94
95 size = sizeof( p_board->mines );
96 if( !RegQueryValueEx( hkey, "Mines", NULL, &type,
97 (LPBYTE) &p_board->mines, &size ) == ERROR_SUCCESS )
98 p_board->mines = BEGINNER_MINES;
99
100 size = sizeof( p_board->difficulty );
101 if( !RegQueryValueEx( hkey, "Difficulty", NULL, &type,
102 (LPBYTE) &p_board->difficulty, &size ) == ERROR_SUCCESS )
103 p_board->difficulty = BEGINNER;
104
105 size = sizeof( p_board->IsMarkQ );
106 if( !RegQueryValueEx( hkey, "Mark", NULL, &type,
107 (LPBYTE) &p_board->IsMarkQ, &size ) == ERROR_SUCCESS )
108 p_board->IsMarkQ = TRUE;
109
110 for( i = 0; i < 3; i++ ) {
111 wsprintf( key_name, "Name%u", i+1 );
112 size = sizeof( data );
113 if( RegQueryValueEx( hkey, key_name, NULL, &type,
114 (LPBYTE) data, &size ) == ERROR_SUCCESS )
115 lstrcpynA( p_board->best_name[i], data, sizeof(p_board->best_name[i]) );
116 else
117 LoadString( p_board->hInst, IDS_NOBODY, p_board->best_name[i], MAX_PLAYER_NAME_SIZE+1 );
118 }
119
120 for( i = 0; i < 3; i++ ) {
121 wsprintf( key_name, "Time%u", i+1 );
122 size = sizeof( p_board->best_time[i] );
123 if( !RegQueryValueEx( hkey, key_name, NULL, &type,
124 (LPBYTE) &p_board->best_time[i], &size ) == ERROR_SUCCESS )
125 p_board->best_time[i] = 999;
126 }
127 RegCloseKey( hkey );
128 }
129
130 static void InitBoard( BOARD *p_board )
131 {
132 HMENU hMenu;
133
134 p_board->hMinesBMP = LoadBitmap( p_board->hInst, "mines");
135 p_board->hFacesBMP = LoadBitmap( p_board->hInst, "faces");
136 p_board->hLedsBMP = LoadBitmap( p_board->hInst, "leds");
137
138 LoadBoard( p_board );
139
140 hMenu = GetMenu( p_board->hWnd );
141 CheckMenuItem( hMenu, IDM_BEGINNER + (unsigned) p_board->difficulty,
142 MF_CHECKED );
143 if( p_board->IsMarkQ )
144 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
145 else
146 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
147 CheckLevel( p_board );
148 }
149
150 static void SaveBoard( BOARD *p_board )
151 {
152 HKEY hkey;
153 unsigned i;
154 char data[MAX_PLAYER_NAME_SIZE+1];
155 char key_name[8];
156
157 if( RegCreateKeyEx( HKEY_CURRENT_USER, registry_key,
158 0, NULL,
159 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
160 &hkey, NULL ) != ERROR_SUCCESS)
161 return;
162
163 RegSetValueEx( hkey, "Xpos", 0, REG_DWORD, (LPBYTE) &p_board->pos.x, sizeof(p_board->pos.x) );
164 RegSetValueEx( hkey, "Ypos", 0, REG_DWORD, (LPBYTE) &p_board->pos.y, sizeof(p_board->pos.y) );
165 RegSetValueEx( hkey, "Difficulty", 0, REG_DWORD, (LPBYTE) &p_board->difficulty, sizeof(p_board->difficulty) );
166 RegSetValueEx( hkey, "Height", 0, REG_DWORD, (LPBYTE) &p_board->rows, sizeof(p_board->rows) );
167 RegSetValueEx( hkey, "Width", 0, REG_DWORD, (LPBYTE) &p_board->cols, sizeof(p_board->cols) );
168 RegSetValueEx( hkey, "Mines", 0, REG_DWORD, (LPBYTE) &p_board->mines, sizeof(p_board->mines) );
169 RegSetValueEx( hkey, "Mark", 0, REG_DWORD, (LPBYTE) &p_board->IsMarkQ, sizeof(p_board->IsMarkQ) );
170
171 for( i = 0; i < 3; i++ ) {
172 wsprintf( key_name, "Name%u", i+1 );
173 lstrcpyn( data, p_board->best_name[i], sizeof( data ) );
174 RegSetValueEx( hkey, key_name, 0, REG_SZ, (LPBYTE) data, strlen(data)+1 );
175 }
176
177 for( i = 0; i < 3; i++ ) {
178 wsprintf( key_name, "Time%u", i+1 );
179 RegSetValueEx( hkey, key_name, 0, REG_DWORD, (LPBYTE) &p_board->best_time[i], sizeof(p_board->best_time[i]) );
180 }
181 RegCloseKey( hkey );
182 }
183
184 static void DestroyBoard( BOARD *p_board )
185 {
186 DeleteObject( p_board->hFacesBMP );
187 DeleteObject( p_board->hLedsBMP );
188 DeleteObject( p_board->hMinesBMP );
189 }
190
191 static void SetDifficulty( BOARD *p_board, DIFFICULTY difficulty )
192 {
193 HMENU hMenu;
194
195 if ( difficulty == CUSTOM )
196 if (DialogBoxParam( p_board->hInst, "DLG_CUSTOM", p_board->hWnd,
197 CustomDlgProc, (LPARAM) p_board) != 0)
198 return;
199
200 hMenu = GetMenu( p_board->hWnd );
201 CheckMenuItem( hMenu, IDM_BEGINNER + p_board->difficulty, MF_UNCHECKED );
202 p_board->difficulty = difficulty;
203 CheckMenuItem( hMenu, IDM_BEGINNER + difficulty, MF_CHECKED );
204
205 switch( difficulty ) {
206 case BEGINNER:
207 p_board->cols = BEGINNER_COLS;
208 p_board->rows = BEGINNER_ROWS;
209 p_board->mines = BEGINNER_MINES;
210 break;
211
212 case ADVANCED:
213 p_board->cols = ADVANCED_COLS;
214 p_board->rows = ADVANCED_ROWS;
215 p_board->mines = ADVANCED_MINES;
216 break;
217
218 case EXPERT:
219 p_board->cols = EXPERT_COLS;
220 p_board->rows = EXPERT_ROWS;
221
222 p_board->mines = EXPERT_MINES;
223 break;
224
225 case CUSTOM:
226 break;
227 }
228 }
229
230 static void ShiftBetween(LONG* x, LONG* y, LONG a, LONG b)
231 {
232 if (*x < a) {
233 *y += a - *x;
234 *x = a;
235 }
236 else if (*y > b) {
237 *x -= *y - b;
238 *y = b;
239 }
240 }
241
242 static void MoveOnScreen(RECT* rect)
243 {
244 HMONITOR hMonitor;
245 MONITORINFO mi;
246
247 /* find the nearest monitor ... */
248 hMonitor = MonitorFromRect(rect, MONITOR_DEFAULTTONEAREST);
249
250 /* ... and move it into the work area (ie excluding task bar)*/
251 mi.cbSize = sizeof(mi);
252 GetMonitorInfo(hMonitor, &mi);
253
254 ShiftBetween(&rect->left, &rect->right, mi.rcWork.left, mi.rcWork.right);
255 ShiftBetween(&rect->top, &rect->bottom, mi.rcWork.top, mi.rcWork.bottom);
256 }
257
258 static void CreateBoard( BOARD *p_board )
259 {
260 int left, top, bottom, right;
261 unsigned col, row;
262 RECT wnd_rect;
263
264 p_board->mb = MB_NONE;
265 p_board->boxes_left = p_board->cols * p_board->rows - p_board->mines;
266 p_board->num_flags = 0;
267
268 /* Create the boxes...
269 * We actually create them with an empty border,
270 * so special care doesn't have to be taken on the edges
271 */
272 for( col = 0; col <= p_board->cols + 1; col++ )
273 for( row = 0; row <= p_board->rows + 1; row++ ) {
274 p_board->box[col][row].IsPressed = FALSE;
275 p_board->box[col][row].IsMine = FALSE;
276 p_board->box[col][row].FlagType = NORMAL;
277 p_board->box[col][row].NumMines = 0;
278 }
279
280 p_board->width = p_board->cols * MINE_WIDTH + BOARD_WMARGIN * 2;
281
282 p_board->height = p_board->rows * MINE_HEIGHT + LED_HEIGHT
283 + BOARD_HMARGIN * 3;
284
285 /* setting the mines rectangle boundary */
286 left = BOARD_WMARGIN;
287 top = BOARD_HMARGIN * 2 + LED_HEIGHT;
288 right = left + p_board->cols * MINE_WIDTH;
289 bottom = top + p_board->rows * MINE_HEIGHT;
290 SetRect( &p_board->mines_rect, left, top, right, bottom );
291
292 /* setting the face rectangle boundary */
293 left = p_board->width / 2 - FACE_WIDTH / 2;
294 top = BOARD_HMARGIN;
295 right = left + FACE_WIDTH;
296 bottom = top + FACE_HEIGHT;
297 SetRect( &p_board->face_rect, left, top, right, bottom );
298
299 /* setting the timer rectangle boundary */
300 left = BOARD_WMARGIN;
301 top = BOARD_HMARGIN;
302 right = left + LED_WIDTH * 3;
303 bottom = top + LED_HEIGHT;
304 SetRect( &p_board->timer_rect, left, top, right, bottom );
305
306 /* setting the counter rectangle boundary */
307 left = p_board->width - BOARD_WMARGIN - LED_WIDTH * 3;
308 top = BOARD_HMARGIN;
309 right = p_board->width - BOARD_WMARGIN;
310 bottom = top + LED_HEIGHT;
311 SetRect( &p_board->counter_rect, left, top, right, bottom );
312
313 p_board->status = WAITING;
314 p_board->face_bmp = SMILE_BMP;
315 p_board->time = 0;
316
317 wnd_rect.left = p_board->pos.x;
318 wnd_rect.right = p_board->pos.x + p_board->width;
319 wnd_rect.top = p_board->pos.y;
320 wnd_rect.bottom = p_board->pos.y + p_board->height;
321 AdjustWindowRect(&wnd_rect, wnd_style, TRUE);
322
323 /* Make sure the window is completely on the screen */
324 MoveOnScreen(&wnd_rect);
325 MoveWindow( p_board->hWnd, wnd_rect.left, wnd_rect.top,
326 wnd_rect.right - wnd_rect.left,
327 wnd_rect.bottom - wnd_rect.top,
328 TRUE );
329 RedrawWindow( p_board->hWnd, NULL, 0,
330 RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
331 }
332
333
334 /* Randomly places mines everywhere except the selected box. */
335 static void PlaceMines ( BOARD *p_board, int selected_col, int selected_row )
336 {
337 int i, j;
338 unsigned col, row;
339
340 srand( (unsigned) time( NULL ) );
341
342 /* Temporarily place a mine at the selected box until all the other
343 * mines are placed, this avoids checking in the mine creation loop. */
344 p_board->box[selected_col][selected_row].IsMine = TRUE;
345
346 /* create mines */
347 i = 0;
348 while( (unsigned) i < p_board->mines ) {
349 col = (int) (p_board->cols * (float) rand() / RAND_MAX + 1);
350 row = (int) (p_board->rows * (float) rand() / RAND_MAX + 1);
351
352 if( !p_board->box[col][row].IsMine ) {
353 i++;
354 p_board->box[col][row].IsMine = TRUE;
355 }
356 }
357
358 /* Remove temporarily placed mine for selected box */
359 p_board->box[selected_col][selected_row].IsMine = FALSE;
360
361 /*
362 * Now we label the remaining boxes with the
363 * number of mines surrounding them.
364 */
365 for( col = 1; col < p_board->cols + 1; col++ )
366 for( row = 1; row < p_board->rows + 1; row++ ) {
367 for( i = -1; i <= 1; i++ )
368 for( j = -1; j <= 1; j++ ) {
369 if( p_board->box[col + i][row + j].IsMine ) {
370 p_board->box[col][row].NumMines++ ;
371 }
372 }
373 }
374 }
375
376 static void DrawMine( HDC hdc, HDC hMemDC, BOARD *p_board, unsigned col, unsigned row, BOOL IsPressed )
377 {
378 MINEBMP_OFFSET offset = BOX_BMP;
379
380 if( col == 0 || col > p_board->cols || row == 0 || row > p_board->rows )
381 return;
382
383 if( p_board->status == GAMEOVER ) {
384 if( p_board->box[col][row].IsMine ) {
385 switch( p_board->box[col][row].FlagType ) {
386 case FLAG:
387 offset = FLAG_BMP;
388 break;
389 case COMPLETE:
390 offset = EXPLODE_BMP;
391 break;
392 case QUESTION:
393 /* fall through */
394 case NORMAL:
395 offset = MINE_BMP;
396 }
397 } else {
398 switch( p_board->box[col][row].FlagType ) {
399 case QUESTION:
400 offset = QUESTION_BMP;
401 break;
402 case FLAG:
403 offset = WRONG_BMP;
404 break;
405 case NORMAL:
406 offset = BOX_BMP;
407 break;
408 case COMPLETE:
409 /* Do nothing */
410 break;
411 default:
412 WINE_TRACE("Unknown FlagType during game over in DrawMine\n");
413 break;
414 }
415 }
416 } else { /* WAITING or PLAYING */
417 switch( p_board->box[col][row].FlagType ) {
418 case QUESTION:
419 if( !IsPressed )
420 offset = QUESTION_BMP;
421 else
422 offset = QPRESS_BMP;
423 break;
424 case FLAG:
425 offset = FLAG_BMP;
426 break;
427 case NORMAL:
428 if( !IsPressed )
429 offset = BOX_BMP;
430 else
431 offset = MPRESS_BMP;
432 break;
433 case COMPLETE:
434 /* Do nothing */
435 break;
436 default:
437 WINE_TRACE("Unknown FlagType while playing in DrawMine\n");
438 break;
439 }
440 }
441
442 if( p_board->box[col][row].FlagType == COMPLETE
443 && !p_board->box[col][row].IsMine )
444 offset = (MINEBMP_OFFSET) p_board->box[col][row].NumMines;
445
446 BitBlt( hdc,
447 (col - 1) * MINE_WIDTH + p_board->mines_rect.left,
448 (row - 1) * MINE_HEIGHT + p_board->mines_rect.top,
449 MINE_WIDTH, MINE_HEIGHT,
450 hMemDC, 0, offset * MINE_HEIGHT, SRCCOPY );
451 }
452
453 static void DrawMines ( HDC hdc, HDC hMemDC, BOARD *p_board )
454 {
455 HGDIOBJ hOldObj;
456 unsigned col, row;
457 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
458
459 for( row = 1; row <= p_board->rows; row++ ) {
460 for( col = 1; col <= p_board->cols; col++ ) {
461 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
462 }
463 }
464 SelectObject( hMemDC, hOldObj );
465 }
466
467 static void DrawLeds( HDC hdc, HDC hMemDC, BOARD *p_board, int number, int x, int y )
468 {
469 HGDIOBJ hOldObj;
470 unsigned led[3], i;
471 int count;
472
473 count = number;
474 if( count < 1000 ) {
475 if( count >= 0 ) {
476 led[0] = count / 100 ;
477 count -= led[0] * 100;
478 }
479 else {
480 led[0] = 10; /* negative sign */
481 count = -count;
482 }
483 led[1] = count / 10;
484 count -= led[1] * 10;
485 led[2] = count;
486 }
487 else {
488 for( i = 0; i < 3; i++ )
489 led[i] = 10;
490 }
491
492 hOldObj = SelectObject (hMemDC, p_board->hLedsBMP);
493
494 for( i = 0; i < 3; i++ ) {
495 BitBlt( hdc,
496 i * LED_WIDTH + x,
497 y,
498 LED_WIDTH,
499 LED_HEIGHT,
500 hMemDC,
501 0,
502 led[i] * LED_HEIGHT,
503 SRCCOPY);
504 }
505
506 SelectObject( hMemDC, hOldObj );
507 }
508
509
510 static void DrawFace( HDC hdc, HDC hMemDC, BOARD *p_board )
511 {
512 HGDIOBJ hOldObj;
513
514 hOldObj = SelectObject (hMemDC, p_board->hFacesBMP);
515
516 BitBlt( hdc,
517 p_board->face_rect.left,
518 p_board->face_rect.top,
519 FACE_WIDTH,
520 FACE_HEIGHT,
521 hMemDC, 0, p_board->face_bmp * FACE_HEIGHT, SRCCOPY);
522
523 SelectObject( hMemDC, hOldObj );
524 }
525
526
527 static void DrawBoard( HDC hdc, HDC hMemDC, PAINTSTRUCT *ps, BOARD *p_board )
528 {
529 RECT tmp_rect;
530
531 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->counter_rect ) )
532 DrawLeds( hdc, hMemDC, p_board, p_board->mines - p_board->num_flags,
533 p_board->counter_rect.left,
534 p_board->counter_rect.top );
535
536 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->timer_rect ) )
537 DrawLeds( hdc, hMemDC, p_board, p_board->time,
538 p_board->timer_rect.left,
539 p_board->timer_rect.top );
540
541 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->face_rect ) )
542 DrawFace( hdc, hMemDC, p_board );
543
544 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->mines_rect ) )
545 DrawMines( hdc, hMemDC, p_board );
546 }
547
548
549 static void AddFlag( BOARD *p_board, unsigned col, unsigned row )
550 {
551 if( p_board->box[col][row].FlagType != COMPLETE ) {
552 switch( p_board->box[col][row].FlagType ) {
553 case FLAG:
554 if( p_board->IsMarkQ )
555 p_board->box[col][row].FlagType = QUESTION;
556 else
557 p_board->box[col][row].FlagType = NORMAL;
558 p_board->num_flags--;
559 break;
560
561 case QUESTION:
562 p_board->box[col][row].FlagType = NORMAL;
563 break;
564
565 default:
566 p_board->box[col][row].FlagType = FLAG;
567 p_board->num_flags++;
568 }
569 }
570 }
571
572
573 static void UnpressBox( BOARD *p_board, unsigned col, unsigned row )
574 {
575 HDC hdc;
576 HGDIOBJ hOldObj;
577 HDC hMemDC;
578
579 hdc = GetDC( p_board->hWnd );
580 hMemDC = CreateCompatibleDC( hdc );
581 hOldObj = SelectObject( hMemDC, p_board->hMinesBMP );
582
583 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
584
585 SelectObject( hMemDC, hOldObj );
586 DeleteDC( hMemDC );
587 ReleaseDC( p_board->hWnd, hdc );
588 }
589
590
591 static void UnpressBoxes( BOARD *p_board, unsigned col, unsigned row )
592 {
593 int i, j;
594
595 for( i = -1; i <= 1; i++ )
596 for( j = -1; j <= 1; j++ ) {
597 UnpressBox( p_board, col + i, row + j );
598 }
599 }
600
601
602 static void PressBox( BOARD *p_board, unsigned col, unsigned row )
603 {
604 HDC hdc;
605 HGDIOBJ hOldObj;
606 HDC hMemDC;
607
608 hdc = GetDC( p_board->hWnd );
609 hMemDC = CreateCompatibleDC( hdc );
610 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
611
612 DrawMine( hdc, hMemDC, p_board, col, row, TRUE );
613
614 SelectObject( hMemDC, hOldObj );
615 DeleteDC( hMemDC );
616 ReleaseDC( p_board->hWnd, hdc );
617 }
618
619
620 static void PressBoxes( BOARD *p_board, unsigned col, unsigned row )
621 {
622 int i, j;
623
624 for( i = -1; i <= 1; i++ )
625 for( j = -1; j <= 1; j++ ) {
626 p_board->box[col + i][row + j].IsPressed = TRUE;
627 PressBox( p_board, col + i, row + j );
628 }
629
630 for( i = -1; i <= 1; i++ )
631 for( j = -1; j <= 1; j++ ) {
632 if( !p_board->box[p_board->press.x + i][p_board->press.y + j].IsPressed )
633 UnpressBox( p_board, p_board->press.x + i, p_board->press.y + j );
634 }
635
636 for( i = -1; i <= 1; i++ )
637 for( j = -1; j <= 1; j++ ) {
638 p_board->box[col + i][row + j].IsPressed = FALSE;
639 PressBox( p_board, col + i, row + j );
640 }
641
642 p_board->press.x = col;
643 p_board->press.y = row;
644 }
645
646
647 static void CompleteBox( BOARD *p_board, unsigned col, unsigned row )
648 {
649 int i, j;
650
651 if( p_board->box[col][row].FlagType != COMPLETE &&
652 p_board->box[col][row].FlagType != FLAG &&
653 col > 0 && col < p_board->cols + 1 &&
654 row > 0 && row < p_board->rows + 1 ) {
655 p_board->box[col][row].FlagType = COMPLETE;
656
657 if( p_board->box[col][row].IsMine ) {
658 p_board->face_bmp = DEAD_BMP;
659 p_board->status = GAMEOVER;
660 }
661 else if( p_board->status != GAMEOVER )
662 p_board->boxes_left--;
663
664 if( p_board->box[col][row].NumMines == 0 )
665 {
666 for( i = -1; i <= 1; i++ )
667 for( j = -1; j <= 1; j++ )
668 CompleteBox( p_board, col + i, row + j );
669 }
670 }
671 }
672
673
674 static void CompleteBoxes( BOARD *p_board, unsigned col, unsigned row )
675 {
676 unsigned numFlags = 0;
677 int i, j;
678
679 if( p_board->box[col][row].FlagType == COMPLETE ) {
680 for( i = -1; i <= 1; i++ )
681 for( j = -1; j <= 1; j++ ) {
682 if( p_board->box[col+i][row+j].FlagType == FLAG )
683 numFlags++;
684 }
685
686 if( numFlags == p_board->box[col][row].NumMines ) {
687 for( i = -1; i <= 1; i++ )
688 for( j = -1; j <= 1; j++ ) {
689 if( p_board->box[col+i][row+j].FlagType != FLAG )
690 CompleteBox( p_board, col+i, row+j );
691 }
692 }
693 }
694 }
695
696
697 static void TestMines( BOARD *p_board, POINT pt, int msg )
698 {
699 BOOL draw = TRUE;
700 int col, row;
701
702 col = (pt.x - p_board->mines_rect.left) / MINE_WIDTH + 1;
703 row = (pt.y - p_board->mines_rect.top ) / MINE_HEIGHT + 1;
704
705 switch ( msg ) {
706 case WM_LBUTTONDOWN:
707 if( p_board->press.x != col || p_board->press.y != row ) {
708 UnpressBox( p_board,
709 p_board->press.x, p_board->press.y );
710 p_board->press.x = col;
711 p_board->press.y = row;
712 PressBox( p_board, col, row );
713 }
714 draw = FALSE;
715 break;
716
717 case WM_LBUTTONUP:
718 if( p_board->press.x != col || p_board->press.y != row )
719 UnpressBox( p_board,
720 p_board->press.x, p_board->press.y );
721 p_board->press.x = 0;
722 p_board->press.y = 0;
723 if( p_board->box[col][row].FlagType != FLAG
724 && p_board->status != PLAYING )
725 {
726 p_board->status = PLAYING;
727 PlaceMines( p_board, col, row );
728 }
729 CompleteBox( p_board, col, row );
730 break;
731
732 case WM_MBUTTONDOWN:
733 PressBoxes( p_board, col, row );
734 draw = FALSE;
735 break;
736
737 case WM_MBUTTONUP:
738 if( p_board->press.x != col || p_board->press.y != row )
739 UnpressBoxes( p_board,
740 p_board->press.x, p_board->press.y );
741 p_board->press.x = 0;
742 p_board->press.y = 0;
743 CompleteBoxes( p_board, col, row );
744 break;
745
746 case WM_RBUTTONDOWN:
747 AddFlag( p_board, col, row );
748 break;
749 default:
750 WINE_TRACE("Unknown message type received in TestMines\n");
751 break;
752 }
753
754 if( draw )
755 {
756 RedrawWindow( p_board->hWnd, NULL, 0,
757 RDW_INVALIDATE | RDW_UPDATENOW );
758 }
759 }
760
761
762 static void TestFace( BOARD *p_board, POINT pt, int msg )
763 {
764 if( p_board->status == PLAYING || p_board->status == WAITING ) {
765 if( msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN )
766 p_board->face_bmp = OOH_BMP;
767 else p_board->face_bmp = SMILE_BMP;
768 }
769 else if( p_board->status == GAMEOVER )
770 p_board->face_bmp = DEAD_BMP;
771 else if( p_board->status == WON )
772 p_board->face_bmp = COOL_BMP;
773
774 if( PtInRect( &p_board->face_rect, pt ) ) {
775 if( msg == WM_LBUTTONDOWN )
776 p_board->face_bmp = SPRESS_BMP;
777
778 if( msg == WM_LBUTTONUP )
779 CreateBoard( p_board );
780 }
781
782 RedrawWindow( p_board->hWnd, &p_board->face_rect, 0,
783 RDW_INVALIDATE | RDW_UPDATENOW );
784 }
785
786
787 static void TestBoard( HWND hWnd, BOARD *p_board, int x, int y, int msg )
788 {
789 POINT pt;
790 unsigned col,row;
791
792 pt.x = x;
793 pt.y = y;
794
795 if( PtInRect( &p_board->mines_rect, pt ) && p_board->status != GAMEOVER
796 && p_board->status != WON )
797 TestMines( p_board, pt, msg );
798 else {
799 UnpressBoxes( p_board,
800 p_board->press.x,
801 p_board->press.y );
802 p_board->press.x = 0;
803 p_board->press.y = 0;
804 }
805
806 if( p_board->boxes_left == 0 ) {
807 p_board->status = WON;
808
809 if (p_board->num_flags < p_board->mines) {
810 for( row = 1; row <= p_board->rows; row++ ) {
811 for( col = 1; col <= p_board->cols; col++ ) {
812 if (p_board->box[col][row].IsMine && p_board->box[col][row].FlagType != FLAG)
813 p_board->box[col][row].FlagType = FLAG;
814 }
815 }
816
817 p_board->num_flags = p_board->mines;
818
819 RedrawWindow( p_board->hWnd, NULL, 0,
820 RDW_INVALIDATE | RDW_UPDATENOW );
821 }
822
823 if( p_board->difficulty != CUSTOM &&
824 p_board->time < p_board->best_time[p_board->difficulty] ) {
825 p_board->best_time[p_board->difficulty] = p_board->time;
826
827 DialogBoxParam( p_board->hInst, "DLG_CONGRATS", hWnd,
828 CongratsDlgProc, (LPARAM) p_board);
829
830 DialogBoxParam( p_board->hInst, "DLG_TIMES", hWnd,
831 TimesDlgProc, (LPARAM) p_board);
832 }
833 }
834 TestFace( p_board, pt, msg );
835 }
836
837
838 static LRESULT WINAPI MainProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
839 {
840 HDC hdc;
841 PAINTSTRUCT ps;
842 HMENU hMenu;
843 static BOARD board;
844
845 switch( msg ) {
846 case WM_CREATE:
847 board.hInst = ((LPCREATESTRUCT) lParam)->hInstance;
848 board.hWnd = hWnd;
849 InitBoard( &board );
850 CreateBoard( &board );
851 return 0;
852
853 case WM_PAINT:
854 {
855 HDC hMemDC;
856
857 WINE_TRACE("WM_PAINT\n");
858 hdc = BeginPaint( hWnd, &ps );
859 hMemDC = CreateCompatibleDC( hdc );
860
861 DrawBoard( hdc, hMemDC, &ps, &board );
862
863 DeleteDC( hMemDC );
864 EndPaint( hWnd, &ps );
865
866 return 0;
867 }
868
869 case WM_MOVE:
870 WINE_TRACE("WM_MOVE\n");
871 board.pos.x = (short)LOWORD(lParam);
872 board.pos.y = (short)HIWORD(lParam);
873 return 0;
874
875 case WM_DESTROY:
876 SaveBoard( &board );
877 DestroyBoard( &board );
878 PostQuitMessage( 0 );
879 return 0;
880
881 case WM_TIMER:
882 if( board.status == PLAYING ) {
883 board.time++;
884 RedrawWindow( hWnd, &board.timer_rect, 0,
885 RDW_INVALIDATE | RDW_UPDATENOW );
886 }
887 return 0;
888
889 case WM_LBUTTONDOWN:
890 WINE_TRACE("WM_LBUTTONDOWN\n");
891 if( wParam & (MK_RBUTTON | MK_SHIFT) )
892 msg = WM_MBUTTONDOWN;
893 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
894 SetCapture( hWnd );
895 return 0;
896
897 case WM_LBUTTONUP:
898 WINE_TRACE("WM_LBUTTONUP\n");
899 if( wParam & (MK_RBUTTON | MK_SHIFT) )
900 msg = WM_MBUTTONUP;
901 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
902 ReleaseCapture();
903 return 0;
904
905 case WM_RBUTTONDOWN:
906 WINE_TRACE("WM_RBUTTONDOWN\n");
907 if( wParam & MK_LBUTTON ) {
908 board.press.x = 0;
909 board.press.y = 0;
910 msg = WM_MBUTTONDOWN;
911 }
912 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
913 return 0;
914
915 case WM_RBUTTONUP:
916 WINE_TRACE("WM_RBUTTONUP\n");
917 if( wParam & MK_LBUTTON )
918 msg = WM_MBUTTONUP;
919 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
920 return 0;
921
922 case WM_MBUTTONDOWN:
923 WINE_TRACE("WM_MBUTTONDOWN\n");
924 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
925 return 0;
926
927 case WM_MBUTTONUP:
928 WINE_TRACE("WM_MBUTTONUP\n");
929 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
930 return 0;
931
932 case WM_MOUSEMOVE:
933 {
934 if( ( wParam & MK_MBUTTON ) ||
935 ( ( wParam & MK_LBUTTON ) && ( wParam & MK_RBUTTON ) ) ) {
936 msg = WM_MBUTTONDOWN;
937 }
938 else if( wParam & MK_LBUTTON ) {
939 msg = WM_LBUTTONDOWN;
940 }
941 else {
942 return 0;
943 }
944
945 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
946
947 return 0;
948 }
949
950 case WM_COMMAND:
951 switch(LOWORD(wParam)) {
952 case IDM_NEW:
953 CreateBoard( &board );
954 return 0;
955
956 case IDM_MARKQ:
957 hMenu = GetMenu( hWnd );
958 board.IsMarkQ = !board.IsMarkQ;
959 if( board.IsMarkQ )
960 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
961 else
962 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
963 return 0;
964
965 case IDM_BEGINNER:
966 SetDifficulty( &board, BEGINNER );
967 CreateBoard( &board );
968 return 0;
969
970 case IDM_ADVANCED:
971 SetDifficulty( &board, ADVANCED );
972 CreateBoard( &board );
973 return 0;
974
975 case IDM_EXPERT:
976 SetDifficulty( &board, EXPERT );
977 CreateBoard( &board );
978 return 0;
979
980 case IDM_CUSTOM:
981 SetDifficulty( &board, CUSTOM );
982 CreateBoard( &board );
983 return 0;
984
985 case IDM_EXIT:
986 SendMessage( hWnd, WM_CLOSE, 0, 0);
987 return 0;
988
989 case IDM_TIMES:
990 DialogBoxParam( board.hInst, "DLG_TIMES", hWnd,
991 TimesDlgProc, (LPARAM) &board);
992 return 0;
993
994 case IDM_ABOUT:
995 {
996 WCHAR appname[256], other[256];
997 LoadStringW( board.hInst, IDS_APPNAME, appname, sizeof(appname)/sizeof(WCHAR) );
998 LoadStringW( board.hInst, IDS_ABOUT, other, sizeof(other)/sizeof(WCHAR) );
999 ShellAboutW( hWnd, appname, other,
1000 LoadImageA( board.hInst, "WINEMINE", IMAGE_ICON, 48, 48, LR_SHARED ));
1001 return 0;
1002 }
1003 default:
1004 WINE_TRACE("Unknown WM_COMMAND command message received\n");
1005 break;
1006 }
1007 }
1008 return( DefWindowProc( hWnd, msg, wParam, lParam ));
1009 }
1010
1011 int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdline, int cmdshow )
1012 {
1013 MSG msg;
1014 WNDCLASSEX wc;
1015 HWND hWnd;
1016 HACCEL haccel;
1017 char appname[20];
1018
1019 LoadString( hInst, IDS_APPNAME, appname, sizeof(appname));
1020
1021 wc.cbSize = sizeof(wc);
1022 wc.style = 0;
1023 wc.lpfnWndProc = MainProc;
1024 wc.cbClsExtra = 0;
1025 wc.cbWndExtra = 0;
1026 wc.hInstance = hInst;
1027 wc.hIcon = LoadIcon( hInst, "WINEMINE" );
1028 wc.hCursor = LoadCursor( 0, IDI_APPLICATION );
1029 wc.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); //MOD for ROS
1030 wc.lpszMenuName = "MENU_WINEMINE";
1031 wc.lpszClassName = appname;
1032 wc.hIconSm = LoadImage( hInst, "WINEMINE", IMAGE_ICON,
1033 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1034
1035 if (!RegisterClassEx(&wc)) ExitProcess(1);
1036 hWnd = CreateWindow( appname, appname,
1037 wnd_style,
1038 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1039 0, 0, hInst, NULL );
1040
1041 if (!hWnd) ExitProcess(1);
1042
1043 ShowWindow( hWnd, cmdshow );
1044 UpdateWindow( hWnd );
1045
1046 haccel = LoadAccelerators( hInst, MAKEINTRESOURCE(IDA_WINEMINE) );
1047 SetTimer( hWnd, ID_TIMER, 1000, NULL );
1048
1049 while( GetMessage(&msg, 0, 0, 0) ) {
1050 if (!TranslateAccelerator( hWnd, haccel, &msg ))
1051 TranslateMessage( &msg );
1052
1053 DispatchMessage( &msg );
1054 }
1055 return msg.wParam;
1056 }