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