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