riched20: The background colour is set using \highlight.
[wine.git] / programs / winemine / main.c
blob27fc159e0d5a10b385676ea0b1492fffec15168f
1 /*
2 * WineMine (main.c)
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
23 #include <string.h>
24 #include <time.h>
25 #include <windows.h>
26 #include <stdlib.h>
27 #include <shellapi.h>
28 #include "main.h"
29 #include "resource.h"
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 WCHAR registry_key[] = {'S','o','f','t','w','a','r','e','\\',
37 'M','i','c','r','o','s','o','f','t','\\',
38 'W','i','n','M','i','n','e',0};
40 static const WCHAR xposW[] = {'X','p','o','s',0};
41 static const WCHAR yposW[] = {'Y','p','o','s',0};
42 static const WCHAR heightW[] = {'H','e','i','g','h','t',0};
43 static const WCHAR widthW[] = {'W','i','d','t','h',0};
44 static const WCHAR minesW[] = {'M','i','n','e','s',0};
45 static const WCHAR difficultyW[] = {'D','i','f','f','i','c','u','l','t','y',0};
46 static const WCHAR markW[] = {'M','a','r','k',0};
47 static const WCHAR nameW[] = {'N','a','m','e','%','u',0};
48 static const WCHAR timeW[] = {'T','i','m','e','%','u',0};
50 void CheckLevel( BOARD *p_board )
52 if( p_board->rows < BEGINNER_ROWS )
53 p_board->rows = BEGINNER_ROWS;
55 if( p_board->rows > MAX_ROWS )
56 p_board->rows = MAX_ROWS;
58 if( p_board->cols < BEGINNER_COLS )
59 p_board->cols = BEGINNER_COLS;
61 if( p_board->cols > MAX_COLS )
62 p_board->cols = MAX_COLS;
64 if( p_board->mines < BEGINNER_MINES )
65 p_board->mines = BEGINNER_MINES;
67 if( p_board->mines > ( p_board->cols - 1 ) * ( p_board->rows - 1 ) )
68 p_board->mines = ( p_board->cols - 1 ) * ( p_board->rows - 1 );
71 static void LoadBoard( BOARD *p_board )
73 DWORD size;
74 DWORD type;
75 HKEY hkey;
76 WCHAR data[MAX_PLAYER_NAME_SIZE+1];
77 WCHAR key_name[8];
78 unsigned i;
80 RegOpenKeyExW( HKEY_CURRENT_USER, registry_key, 0, KEY_QUERY_VALUE, &hkey );
82 size = sizeof( p_board->pos.x );
83 if( RegQueryValueExW( hkey, xposW, NULL, &type, (BYTE*) &p_board->pos.x, &size ) )
84 p_board->pos.x = 0;
86 size = sizeof( p_board->pos.y );
87 if( RegQueryValueExW( hkey, yposW, NULL, &type, (BYTE*) &p_board->pos.y, &size ) )
88 p_board->pos.y = 0;
90 size = sizeof( p_board->rows );
91 if( RegQueryValueExW( hkey, heightW, NULL, &type, (BYTE*) &p_board->rows, &size ) )
92 p_board->rows = BEGINNER_ROWS;
94 size = sizeof( p_board->cols );
95 if( RegQueryValueExW( hkey, widthW, NULL, &type, (BYTE*) &p_board->cols, &size ) )
96 p_board->cols = BEGINNER_COLS;
98 size = sizeof( p_board->mines );
99 if( RegQueryValueExW( hkey, minesW, NULL, &type, (BYTE*) &p_board->mines, &size ) )
100 p_board->mines = BEGINNER_MINES;
102 size = sizeof( p_board->difficulty );
103 if( RegQueryValueExW( hkey, difficultyW, NULL, &type, (BYTE*) &p_board->difficulty, &size ) )
104 p_board->difficulty = BEGINNER;
106 size = sizeof( p_board->IsMarkQ );
107 if( RegQueryValueExW( hkey, markW, NULL, &type, (BYTE*) &p_board->IsMarkQ, &size ) )
108 p_board->IsMarkQ = TRUE;
110 for( i = 0; i < 3; i++ ) {
111 wsprintfW( key_name, nameW, i+1 );
112 size = sizeof( data );
113 if( RegQueryValueExW( hkey, key_name, NULL, &type,
114 (LPBYTE) data, &size ) == ERROR_SUCCESS )
115 lstrcpynW( p_board->best_name[i], data, sizeof(p_board->best_name[i])/sizeof(WCHAR) );
116 else
117 LoadStringW( p_board->hInst, IDS_NOBODY, p_board->best_name[i], MAX_PLAYER_NAME_SIZE+1 );
120 for( i = 0; i < 3; i++ ) {
121 wsprintfW( key_name, timeW, i+1 );
122 size = sizeof( p_board->best_time[i] );
123 if( RegQueryValueExW( hkey, key_name, NULL, &type, (BYTE*) &p_board->best_time[i], &size ) )
124 p_board->best_time[i] = 999;
126 RegCloseKey( hkey );
129 static void InitBoard( BOARD *p_board )
131 HMENU hMenu;
133 p_board->hMinesBMP = LoadBitmapW( p_board->hInst, MAKEINTRESOURCEW(IDI_MINES));
134 p_board->hFacesBMP = LoadBitmapW( p_board->hInst, MAKEINTRESOURCEW(IDI_FACES));
135 p_board->hLedsBMP = LoadBitmapW( p_board->hInst, MAKEINTRESOURCEW(IDI_LEDS));
137 LoadBoard( p_board );
139 hMenu = GetMenu( p_board->hWnd );
140 CheckMenuItem( hMenu, IDM_BEGINNER + (unsigned) p_board->difficulty,
141 MF_CHECKED );
142 if( p_board->IsMarkQ )
143 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
144 else
145 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
146 CheckLevel( p_board );
149 static void SaveBoard( BOARD *p_board )
151 HKEY hkey;
152 unsigned i;
153 WCHAR data[MAX_PLAYER_NAME_SIZE+1];
154 WCHAR key_name[8];
156 if( RegCreateKeyExW( HKEY_CURRENT_USER, registry_key,
157 0, NULL,
158 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
159 &hkey, NULL ) != ERROR_SUCCESS)
160 return;
162 RegSetValueExW( hkey, xposW, 0, REG_DWORD, (LPBYTE) &p_board->pos.x, sizeof(p_board->pos.x) );
163 RegSetValueExW( hkey, yposW, 0, REG_DWORD, (LPBYTE) &p_board->pos.y, sizeof(p_board->pos.y) );
164 RegSetValueExW( hkey, difficultyW, 0, REG_DWORD, (LPBYTE) &p_board->difficulty, sizeof(p_board->difficulty) );
165 RegSetValueExW( hkey, heightW, 0, REG_DWORD, (LPBYTE) &p_board->rows, sizeof(p_board->rows) );
166 RegSetValueExW( hkey, widthW, 0, REG_DWORD, (LPBYTE) &p_board->cols, sizeof(p_board->cols) );
167 RegSetValueExW( hkey, minesW, 0, REG_DWORD, (LPBYTE) &p_board->mines, sizeof(p_board->mines) );
168 RegSetValueExW( hkey, markW, 0, REG_DWORD, (LPBYTE) &p_board->IsMarkQ, sizeof(p_board->IsMarkQ) );
170 for( i = 0; i < 3; i++ ) {
171 wsprintfW( key_name, nameW, i+1 );
172 lstrcpynW( data, p_board->best_name[i], sizeof(data)/sizeof(WCHAR) );
173 RegSetValueExW( hkey, key_name, 0, REG_SZ, (LPBYTE) data, (lstrlenW(data)+1) * sizeof(WCHAR) );
176 for( i = 0; i < 3; i++ ) {
177 wsprintfW( key_name, timeW, i+1 );
178 RegSetValueExW( hkey, key_name, 0, REG_DWORD, (LPBYTE) &p_board->best_time[i], sizeof(p_board->best_time[i]) );
180 RegCloseKey( hkey );
183 static void DestroyBoard( BOARD *p_board )
185 DeleteObject( p_board->hFacesBMP );
186 DeleteObject( p_board->hLedsBMP );
187 DeleteObject( p_board->hMinesBMP );
190 static void SetDifficulty( BOARD *p_board, DIFFICULTY difficulty )
192 HMENU hMenu;
194 if ( difficulty == CUSTOM )
195 if (DialogBoxParamW( p_board->hInst, MAKEINTRESOURCEW(DLG_CUSTOM), p_board->hWnd,
196 CustomDlgProc, (LPARAM) p_board) != 0)
197 return;
199 hMenu = GetMenu( p_board->hWnd );
200 CheckMenuItem( hMenu, IDM_BEGINNER + p_board->difficulty, MF_UNCHECKED );
201 p_board->difficulty = difficulty;
202 CheckMenuItem( hMenu, IDM_BEGINNER + difficulty, MF_CHECKED );
204 switch( difficulty ) {
205 case BEGINNER:
206 p_board->cols = BEGINNER_COLS;
207 p_board->rows = BEGINNER_ROWS;
208 p_board->mines = BEGINNER_MINES;
209 break;
211 case ADVANCED:
212 p_board->cols = ADVANCED_COLS;
213 p_board->rows = ADVANCED_ROWS;
214 p_board->mines = ADVANCED_MINES;
215 break;
217 case EXPERT:
218 p_board->cols = EXPERT_COLS;
219 p_board->rows = EXPERT_ROWS;
221 p_board->mines = EXPERT_MINES;
222 break;
224 case CUSTOM:
225 break;
229 static void ShiftBetween(LONG* x, LONG* y, LONG a, LONG b)
231 if (*x < a) {
232 *y += a - *x;
233 *x = a;
235 else if (*y > b) {
236 *x -= *y - b;
237 *y = b;
241 static void MoveOnScreen(RECT* rect)
243 HMONITOR hMonitor;
244 MONITORINFO mi;
246 /* find the nearest monitor ... */
247 hMonitor = MonitorFromRect(rect, MONITOR_DEFAULTTONEAREST);
249 /* ... and move it into the work area (ie excluding task bar)*/
250 mi.cbSize = sizeof(mi);
251 GetMonitorInfoW(hMonitor, &mi);
253 ShiftBetween(&rect->left, &rect->right, mi.rcWork.left, mi.rcWork.right);
254 ShiftBetween(&rect->top, &rect->bottom, mi.rcWork.top, mi.rcWork.bottom);
257 static void CreateBoard( BOARD *p_board )
259 int left, top, bottom, right;
260 unsigned col, row;
261 RECT wnd_rect;
263 p_board->mb = MB_NONE;
264 p_board->boxes_left = p_board->cols * p_board->rows - p_board->mines;
265 p_board->num_flags = 0;
267 /* Create the boxes...
268 * We actually create them with an empty border,
269 * so special care doesn't have to be taken on the edges
271 for( col = 0; col <= p_board->cols + 1; col++ )
272 for( row = 0; row <= p_board->rows + 1; row++ ) {
273 p_board->box[col][row].IsPressed = FALSE;
274 p_board->box[col][row].IsMine = FALSE;
275 p_board->box[col][row].FlagType = NORMAL;
276 p_board->box[col][row].NumMines = 0;
279 p_board->width = p_board->cols * MINE_WIDTH + BOARD_WMARGIN * 2;
281 p_board->height = p_board->rows * MINE_HEIGHT + LED_HEIGHT
282 + BOARD_HMARGIN * 3;
284 /* setting the mines rectangle boundary */
285 left = BOARD_WMARGIN;
286 top = BOARD_HMARGIN * 2 + LED_HEIGHT;
287 right = left + p_board->cols * MINE_WIDTH;
288 bottom = top + p_board->rows * MINE_HEIGHT;
289 SetRect( &p_board->mines_rect, left, top, right, bottom );
291 /* setting the face rectangle boundary */
292 left = p_board->width / 2 - FACE_WIDTH / 2;
293 top = BOARD_HMARGIN;
294 right = left + FACE_WIDTH;
295 bottom = top + FACE_HEIGHT;
296 SetRect( &p_board->face_rect, left, top, right, bottom );
298 /* setting the timer rectangle boundary */
299 left = BOARD_WMARGIN;
300 top = BOARD_HMARGIN;
301 right = left + LED_WIDTH * 3;
302 bottom = top + LED_HEIGHT;
303 SetRect( &p_board->timer_rect, left, top, right, bottom );
305 /* setting the counter rectangle boundary */
306 left = p_board->width - BOARD_WMARGIN - LED_WIDTH * 3;
307 top = BOARD_HMARGIN;
308 right = p_board->width - BOARD_WMARGIN;
309 bottom = top + LED_HEIGHT;
310 SetRect( &p_board->counter_rect, left, top, right, bottom );
312 p_board->status = WAITING;
313 p_board->face_bmp = SMILE_BMP;
314 p_board->time = 0;
316 SetRect(&wnd_rect, p_board->pos.x, p_board->pos.y, p_board->pos.x + p_board->width,
317 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,
325 TRUE );
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 )
334 int i, j;
335 unsigned col, 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;
343 /* create mines */
344 i = 0;
345 while( (unsigned) i < p_board->mines ) {
346 col = rand() % p_board->cols + 1;
347 row = rand() % p_board->rows + 1;
349 if( !p_board->box[col][row].IsMine ) {
350 i++;
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 )
378 return;
380 if( p_board->status == GAMEOVER ) {
381 if( p_board->box[col][row].IsMine ) {
382 switch( p_board->box[col][row].FlagType ) {
383 case FLAG:
384 offset = FLAG_BMP;
385 break;
386 case COMPLETE:
387 offset = EXPLODE_BMP;
388 break;
389 case QUESTION:
390 /* fall through */
391 case NORMAL:
392 offset = MINE_BMP;
394 } else {
395 switch( p_board->box[col][row].FlagType ) {
396 case QUESTION:
397 offset = QUESTION_BMP;
398 break;
399 case FLAG:
400 offset = WRONG_BMP;
401 break;
402 case NORMAL:
403 offset = BOX_BMP;
404 break;
405 case COMPLETE:
406 /* Do nothing */
407 break;
408 default:
409 WINE_TRACE("Unknown FlagType during game over in DrawMine\n");
410 break;
413 } else { /* WAITING or PLAYING */
414 switch( p_board->box[col][row].FlagType ) {
415 case QUESTION:
416 if( !IsPressed )
417 offset = QUESTION_BMP;
418 else
419 offset = QPRESS_BMP;
420 break;
421 case FLAG:
422 offset = FLAG_BMP;
423 break;
424 case NORMAL:
425 if( !IsPressed )
426 offset = BOX_BMP;
427 else
428 offset = MPRESS_BMP;
429 break;
430 case COMPLETE:
431 /* Do nothing */
432 break;
433 default:
434 WINE_TRACE("Unknown FlagType while playing in DrawMine\n");
435 break;
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;
443 BitBlt( hdc,
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 )
452 HGDIOBJ hOldObj;
453 unsigned col, row;
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 )
466 HGDIOBJ hOldObj;
467 unsigned led[3], i;
468 int count;
470 count = number;
471 if( count < 1000 ) {
472 if( count >= 0 ) {
473 led[0] = count / 100 ;
474 count -= led[0] * 100;
476 else {
477 led[0] = 10; /* negative sign */
478 count = -count;
480 led[1] = count / 10;
481 count -= led[1] * 10;
482 led[2] = count;
484 else {
485 for( i = 0; i < 3; i++ )
486 led[i] = 10;
489 hOldObj = SelectObject (hMemDC, p_board->hLedsBMP);
491 for( i = 0; i < 3; i++ ) {
492 BitBlt( hdc,
493 i * LED_WIDTH + x,
495 LED_WIDTH,
496 LED_HEIGHT,
497 hMemDC,
499 led[i] * LED_HEIGHT,
500 SRCCOPY);
503 SelectObject( hMemDC, hOldObj );
507 static void DrawFace( HDC hdc, HDC hMemDC, BOARD *p_board )
509 HGDIOBJ hOldObj;
511 hOldObj = SelectObject (hMemDC, p_board->hFacesBMP);
513 BitBlt( hdc,
514 p_board->face_rect.left,
515 p_board->face_rect.top,
516 FACE_WIDTH,
517 FACE_HEIGHT,
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 )
526 RECT tmp_rect;
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 ) {
550 case FLAG:
551 if( p_board->IsMarkQ )
552 p_board->box[col][row].FlagType = QUESTION;
553 else
554 p_board->box[col][row].FlagType = NORMAL;
555 p_board->num_flags--;
556 break;
558 case QUESTION:
559 p_board->box[col][row].FlagType = NORMAL;
560 break;
562 default:
563 p_board->box[col][row].FlagType = FLAG;
564 p_board->num_flags++;
570 static void UnpressBox( BOARD *p_board, unsigned col, unsigned row )
572 HDC hdc;
573 HGDIOBJ hOldObj;
574 HDC hMemDC;
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 );
583 DeleteDC( hMemDC );
584 ReleaseDC( p_board->hWnd, hdc );
588 static void UnpressBoxes( BOARD *p_board, unsigned col, unsigned row )
590 int i, j;
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 )
601 HDC hdc;
602 HGDIOBJ hOldObj;
603 HDC hMemDC;
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 );
612 DeleteDC( hMemDC );
613 ReleaseDC( p_board->hWnd, hdc );
617 static void PressBoxes( BOARD *p_board, unsigned col, unsigned row )
619 int i, j;
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 )
646 int i, j;
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;
674 int i, j;
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 )
680 numFlags++;
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 )
696 BOOL draw = TRUE;
697 int col, row;
699 col = (pt.x - p_board->mines_rect.left) / MINE_WIDTH + 1;
700 row = (pt.y - p_board->mines_rect.top ) / MINE_HEIGHT + 1;
702 switch ( msg ) {
703 case WM_LBUTTONDOWN:
704 if( p_board->press.x != col || p_board->press.y != row ) {
705 UnpressBox( p_board,
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 );
711 draw = FALSE;
712 break;
714 case WM_LBUTTONUP:
715 if( p_board->press.x != col || p_board->press.y != row )
716 UnpressBox( p_board,
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 );
727 break;
729 case WM_MBUTTONDOWN:
730 PressBoxes( p_board, col, row );
731 draw = FALSE;
732 break;
734 case WM_MBUTTONUP:
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 );
741 break;
743 case WM_RBUTTONDOWN:
744 AddFlag( p_board, col, row );
745 break;
746 default:
747 WINE_TRACE("Unknown message type received in TestMines\n");
748 break;
751 if( draw )
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 )
786 POINT pt;
787 unsigned col,row;
789 pt.x = x;
790 pt.y = y;
792 if( PtInRect( &p_board->mines_rect, pt ) && p_board->status != GAMEOVER
793 && p_board->status != WON )
794 TestMines( p_board, pt, msg );
795 else {
796 UnpressBoxes( p_board,
797 p_board->press.x,
798 p_board->press.y );
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 DialogBoxParamW( p_board->hInst, MAKEINTRESOURCEW(DLG_CONGRATS), hWnd,
825 CongratsDlgProc, (LPARAM) p_board);
826 DialogBoxParamW( p_board->hInst, MAKEINTRESOURCEW(DLG_TIMES), hWnd,
827 TimesDlgProc, (LPARAM) p_board);
830 TestFace( p_board, pt, msg );
834 static LRESULT WINAPI MainProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
836 HDC hdc;
837 PAINTSTRUCT ps;
838 HMENU hMenu;
839 static BOARD board;
841 switch( msg ) {
842 case WM_CREATE:
843 board.hInst = ((LPCREATESTRUCTW) lParam)->hInstance;
844 board.hWnd = hWnd;
845 InitBoard( &board );
846 CreateBoard( &board );
847 return 0;
849 case WM_PAINT:
851 HDC hMemDC;
853 WINE_TRACE("WM_PAINT\n");
854 hdc = BeginPaint( hWnd, &ps );
855 hMemDC = CreateCompatibleDC( hdc );
857 DrawBoard( hdc, hMemDC, &ps, &board );
859 DeleteDC( hMemDC );
860 EndPaint( hWnd, &ps );
862 return 0;
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;
871 case WM_DESTROY:
872 SaveBoard( &board );
873 DestroyBoard( &board );
874 PostQuitMessage( 0 );
875 return 0;
877 case WM_TIMER:
878 if( board.status == PLAYING ) {
879 board.time++;
880 RedrawWindow( hWnd, &board.timer_rect, 0,
881 RDW_INVALIDATE | RDW_UPDATENOW );
883 return 0;
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;
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;
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;
908 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
909 return 0;
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;
918 case WM_MBUTTONDOWN:
919 WINE_TRACE("WM_MBUTTONDOWN\n");
920 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
921 return 0;
923 case WM_MBUTTONUP:
924 WINE_TRACE("WM_MBUTTONUP\n");
925 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
926 return 0;
928 case WM_MOUSEMOVE:
930 if( ( wParam & MK_MBUTTON ) ||
931 ( ( wParam & MK_LBUTTON ) && ( wParam & MK_RBUTTON ) ) ) {
932 msg = WM_MBUTTONDOWN;
934 else if( wParam & MK_LBUTTON ) {
935 msg = WM_LBUTTONDOWN;
937 else {
938 return 0;
941 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
943 return 0;
946 case WM_COMMAND:
947 switch(LOWORD(wParam)) {
948 case IDM_NEW:
949 CreateBoard( &board );
950 return 0;
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;
961 case IDM_BEGINNER:
962 SetDifficulty( &board, BEGINNER );
963 CreateBoard( &board );
964 return 0;
966 case IDM_ADVANCED:
967 SetDifficulty( &board, ADVANCED );
968 CreateBoard( &board );
969 return 0;
971 case IDM_EXPERT:
972 SetDifficulty( &board, EXPERT );
973 CreateBoard( &board );
974 return 0;
976 case IDM_CUSTOM:
977 SetDifficulty( &board, CUSTOM );
978 CreateBoard( &board );
979 return 0;
981 case IDM_EXIT:
982 SendMessageW( hWnd, WM_CLOSE, 0, 0);
983 return 0;
985 case IDM_TIMES:
986 DialogBoxParamW( board.hInst, MAKEINTRESOURCEW(DLG_TIMES), hWnd,
987 TimesDlgProc, (LPARAM) &board);
988 return 0;
990 case IDM_ABOUT:
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 LoadImageW(board.hInst, MAKEINTRESOURCEW(IDI_WINEMINE), IMAGE_ICON, 48, 48, LR_SHARED));
997 return 0;
999 default:
1000 WINE_TRACE("Unknown WM_COMMAND command message received\n");
1001 break;
1004 return DefWindowProcW( hWnd, msg, wParam, lParam );
1007 int WINAPI wWinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow )
1009 MSG msg;
1010 WNDCLASSEXW wc;
1011 HWND hWnd;
1012 HACCEL haccel;
1013 WCHAR appname[20];
1015 LoadStringW( hInst, IDS_APPNAME, appname, sizeof(appname)/sizeof(WCHAR));
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 = LoadIconW( hInst, MAKEINTRESOURCEW(IDI_WINEMINE) );
1024 wc.hCursor = LoadCursorW( 0, (LPWSTR)IDI_APPLICATION );
1025 wc.hbrBackground = GetStockObject( BLACK_BRUSH );
1026 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_WINEMINE);
1027 wc.lpszClassName = appname;
1028 wc.hIconSm = LoadImageW( hInst, MAKEINTRESOURCEW(IDI_WINEMINE), IMAGE_ICON,
1029 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1031 if (!RegisterClassExW(&wc)) ExitProcess(1);
1032 hWnd = CreateWindowW( appname, appname,
1033 wnd_style,
1034 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1035 0, 0, hInst, NULL );
1037 if (!hWnd) ExitProcess(1);
1039 ShowWindow( hWnd, cmdshow );
1040 UpdateWindow( hWnd );
1042 haccel = LoadAcceleratorsW( hInst, MAKEINTRESOURCEW(IDA_WINEMINE) );
1043 SetTimer( hWnd, ID_TIMER, 1000, NULL );
1045 while( GetMessageW(&msg, 0, 0, 0) ) {
1046 if (!TranslateAcceleratorW( hWnd, haccel, &msg ))
1047 TranslateMessage( &msg );
1049 DispatchMessageW( &msg );
1051 return msg.wParam;