kernel32: Prefer HW_MEMSIZE sysctl variable over HW_PHYSMEM, if available, since...
[wine/multimedia.git] / programs / winemine / main.c
blob91b20924853bd94ed683ff5cf13f83c1ad0af7e6
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,
84 (LPBYTE) &p_board->pos.x, &size ) == ERROR_SUCCESS )
85 p_board->pos.x = 0;
87 size = sizeof( p_board->pos.y );
88 if( !RegQueryValueExW( hkey, yposW, NULL, &type,
89 (LPBYTE) &p_board->pos.y, &size ) == ERROR_SUCCESS )
90 p_board->pos.y = 0;
92 size = sizeof( p_board->rows );
93 if( !RegQueryValueExW( hkey, heightW, NULL, &type,
94 (LPBYTE) &p_board->rows, &size ) == ERROR_SUCCESS )
95 p_board->rows = BEGINNER_ROWS;
97 size = sizeof( p_board->cols );
98 if( !RegQueryValueExW( hkey, widthW, NULL, &type,
99 (LPBYTE) &p_board->cols, &size ) == ERROR_SUCCESS )
100 p_board->cols = BEGINNER_COLS;
102 size = sizeof( p_board->mines );
103 if( !RegQueryValueExW( hkey, minesW, NULL, &type,
104 (LPBYTE) &p_board->mines, &size ) == ERROR_SUCCESS )
105 p_board->mines = BEGINNER_MINES;
107 size = sizeof( p_board->difficulty );
108 if( !RegQueryValueExW( hkey, difficultyW, NULL, &type,
109 (LPBYTE) &p_board->difficulty, &size ) == ERROR_SUCCESS )
110 p_board->difficulty = BEGINNER;
112 size = sizeof( p_board->IsMarkQ );
113 if( !RegQueryValueExW( hkey, markW, NULL, &type,
114 (LPBYTE) &p_board->IsMarkQ, &size ) == ERROR_SUCCESS )
115 p_board->IsMarkQ = TRUE;
117 for( i = 0; i < 3; i++ ) {
118 wsprintfW( key_name, nameW, i+1 );
119 size = sizeof( data );
120 if( RegQueryValueExW( hkey, key_name, NULL, &type,
121 (LPBYTE) data, &size ) == ERROR_SUCCESS )
122 lstrcpynW( p_board->best_name[i], data, sizeof(p_board->best_name[i])/sizeof(WCHAR) );
123 else
124 LoadStringW( p_board->hInst, IDS_NOBODY, p_board->best_name[i], MAX_PLAYER_NAME_SIZE+1 );
127 for( i = 0; i < 3; i++ ) {
128 wsprintfW( key_name, timeW, i+1 );
129 size = sizeof( p_board->best_time[i] );
130 if( !RegQueryValueExW( hkey, key_name, NULL, &type,
131 (LPBYTE) &p_board->best_time[i], &size ) == ERROR_SUCCESS )
132 p_board->best_time[i] = 999;
134 RegCloseKey( hkey );
137 static void InitBoard( BOARD *p_board )
139 HMENU hMenu;
141 p_board->hMinesBMP = LoadBitmapW( p_board->hInst, MAKEINTRESOURCEW(IDI_MINES));
142 p_board->hFacesBMP = LoadBitmapW( p_board->hInst, MAKEINTRESOURCEW(IDI_FACES));
143 p_board->hLedsBMP = LoadBitmapW( p_board->hInst, MAKEINTRESOURCEW(IDI_LEDS));
145 LoadBoard( p_board );
147 hMenu = GetMenu( p_board->hWnd );
148 CheckMenuItem( hMenu, IDM_BEGINNER + (unsigned) p_board->difficulty,
149 MF_CHECKED );
150 if( p_board->IsMarkQ )
151 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
152 else
153 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
154 CheckLevel( p_board );
157 static void SaveBoard( BOARD *p_board )
159 HKEY hkey;
160 unsigned i;
161 WCHAR data[MAX_PLAYER_NAME_SIZE+1];
162 WCHAR key_name[8];
164 if( RegCreateKeyExW( HKEY_CURRENT_USER, registry_key,
165 0, NULL,
166 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
167 &hkey, NULL ) != ERROR_SUCCESS)
168 return;
170 RegSetValueExW( hkey, xposW, 0, REG_DWORD, (LPBYTE) &p_board->pos.x, sizeof(p_board->pos.x) );
171 RegSetValueExW( hkey, yposW, 0, REG_DWORD, (LPBYTE) &p_board->pos.y, sizeof(p_board->pos.y) );
172 RegSetValueExW( hkey, difficultyW, 0, REG_DWORD, (LPBYTE) &p_board->difficulty, sizeof(p_board->difficulty) );
173 RegSetValueExW( hkey, heightW, 0, REG_DWORD, (LPBYTE) &p_board->rows, sizeof(p_board->rows) );
174 RegSetValueExW( hkey, widthW, 0, REG_DWORD, (LPBYTE) &p_board->cols, sizeof(p_board->cols) );
175 RegSetValueExW( hkey, minesW, 0, REG_DWORD, (LPBYTE) &p_board->mines, sizeof(p_board->mines) );
176 RegSetValueExW( hkey, markW, 0, REG_DWORD, (LPBYTE) &p_board->IsMarkQ, sizeof(p_board->IsMarkQ) );
178 for( i = 0; i < 3; i++ ) {
179 wsprintfW( key_name, nameW, i+1 );
180 lstrcpynW( data, p_board->best_name[i], sizeof(data)/sizeof(WCHAR) );
181 RegSetValueExW( hkey, key_name, 0, REG_SZ, (LPBYTE) data, (lstrlenW(data)+1) * sizeof(WCHAR) );
184 for( i = 0; i < 3; i++ ) {
185 wsprintfW( key_name, timeW, i+1 );
186 RegSetValueExW( hkey, key_name, 0, REG_DWORD, (LPBYTE) &p_board->best_time[i], sizeof(p_board->best_time[i]) );
188 RegCloseKey( hkey );
191 static void DestroyBoard( BOARD *p_board )
193 DeleteObject( p_board->hFacesBMP );
194 DeleteObject( p_board->hLedsBMP );
195 DeleteObject( p_board->hMinesBMP );
198 static void SetDifficulty( BOARD *p_board, DIFFICULTY difficulty )
200 HMENU hMenu;
202 if ( difficulty == CUSTOM )
203 if (DialogBoxParamW( p_board->hInst, MAKEINTRESOURCEW(DLG_CUSTOM), p_board->hWnd,
204 CustomDlgProc, (LPARAM) p_board) != 0)
205 return;
207 hMenu = GetMenu( p_board->hWnd );
208 CheckMenuItem( hMenu, IDM_BEGINNER + p_board->difficulty, MF_UNCHECKED );
209 p_board->difficulty = difficulty;
210 CheckMenuItem( hMenu, IDM_BEGINNER + difficulty, MF_CHECKED );
212 switch( difficulty ) {
213 case BEGINNER:
214 p_board->cols = BEGINNER_COLS;
215 p_board->rows = BEGINNER_ROWS;
216 p_board->mines = BEGINNER_MINES;
217 break;
219 case ADVANCED:
220 p_board->cols = ADVANCED_COLS;
221 p_board->rows = ADVANCED_ROWS;
222 p_board->mines = ADVANCED_MINES;
223 break;
225 case EXPERT:
226 p_board->cols = EXPERT_COLS;
227 p_board->rows = EXPERT_ROWS;
229 p_board->mines = EXPERT_MINES;
230 break;
232 case CUSTOM:
233 break;
237 static void ShiftBetween(LONG* x, LONG* y, LONG a, LONG b)
239 if (*x < a) {
240 *y += a - *x;
241 *x = a;
243 else if (*y > b) {
244 *x -= *y - b;
245 *y = b;
249 static void MoveOnScreen(RECT* rect)
251 HMONITOR hMonitor;
252 MONITORINFO mi;
254 /* find the nearest monitor ... */
255 hMonitor = MonitorFromRect(rect, MONITOR_DEFAULTTONEAREST);
257 /* ... and move it into the work area (ie excluding task bar)*/
258 mi.cbSize = sizeof(mi);
259 GetMonitorInfoW(hMonitor, &mi);
261 ShiftBetween(&rect->left, &rect->right, mi.rcWork.left, mi.rcWork.right);
262 ShiftBetween(&rect->top, &rect->bottom, mi.rcWork.top, mi.rcWork.bottom);
265 static void CreateBoard( BOARD *p_board )
267 int left, top, bottom, right;
268 unsigned col, row;
269 RECT wnd_rect;
271 p_board->mb = MB_NONE;
272 p_board->boxes_left = p_board->cols * p_board->rows - p_board->mines;
273 p_board->num_flags = 0;
275 /* Create the boxes...
276 * We actually create them with an empty border,
277 * so special care doesn't have to be taken on the edges
279 for( col = 0; col <= p_board->cols + 1; col++ )
280 for( row = 0; row <= p_board->rows + 1; row++ ) {
281 p_board->box[col][row].IsPressed = FALSE;
282 p_board->box[col][row].IsMine = FALSE;
283 p_board->box[col][row].FlagType = NORMAL;
284 p_board->box[col][row].NumMines = 0;
287 p_board->width = p_board->cols * MINE_WIDTH + BOARD_WMARGIN * 2;
289 p_board->height = p_board->rows * MINE_HEIGHT + LED_HEIGHT
290 + BOARD_HMARGIN * 3;
292 /* setting the mines rectangle boundary */
293 left = BOARD_WMARGIN;
294 top = BOARD_HMARGIN * 2 + LED_HEIGHT;
295 right = left + p_board->cols * MINE_WIDTH;
296 bottom = top + p_board->rows * MINE_HEIGHT;
297 SetRect( &p_board->mines_rect, left, top, right, bottom );
299 /* setting the face rectangle boundary */
300 left = p_board->width / 2 - FACE_WIDTH / 2;
301 top = BOARD_HMARGIN;
302 right = left + FACE_WIDTH;
303 bottom = top + FACE_HEIGHT;
304 SetRect( &p_board->face_rect, left, top, right, bottom );
306 /* setting the timer rectangle boundary */
307 left = BOARD_WMARGIN;
308 top = BOARD_HMARGIN;
309 right = left + LED_WIDTH * 3;
310 bottom = top + LED_HEIGHT;
311 SetRect( &p_board->timer_rect, left, top, right, bottom );
313 /* setting the counter rectangle boundary */
314 left = p_board->width - BOARD_WMARGIN - LED_WIDTH * 3;
315 top = BOARD_HMARGIN;
316 right = p_board->width - BOARD_WMARGIN;
317 bottom = top + LED_HEIGHT;
318 SetRect( &p_board->counter_rect, left, top, right, bottom );
320 p_board->status = WAITING;
321 p_board->face_bmp = SMILE_BMP;
322 p_board->time = 0;
324 wnd_rect.left = p_board->pos.x;
325 wnd_rect.right = p_board->pos.x + p_board->width;
326 wnd_rect.top = p_board->pos.y;
327 wnd_rect.bottom = p_board->pos.y + p_board->height;
328 AdjustWindowRect(&wnd_rect, wnd_style, TRUE);
330 /* Make sure the window is completely on the screen */
331 MoveOnScreen(&wnd_rect);
332 MoveWindow( p_board->hWnd, wnd_rect.left, wnd_rect.top,
333 wnd_rect.right - wnd_rect.left,
334 wnd_rect.bottom - wnd_rect.top,
335 TRUE );
336 RedrawWindow( p_board->hWnd, NULL, 0,
337 RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
341 /* Randomly places mines everywhere except the selected box. */
342 static void PlaceMines ( BOARD *p_board, int selected_col, int selected_row )
344 int i, j;
345 unsigned col, row;
347 srand( (unsigned) time( NULL ) );
349 /* Temporarily place a mine at the selected box until all the other
350 * mines are placed, this avoids checking in the mine creation loop. */
351 p_board->box[selected_col][selected_row].IsMine = TRUE;
353 /* create mines */
354 i = 0;
355 while( (unsigned) i < p_board->mines ) {
356 col = (int) (p_board->cols * (float) rand() / RAND_MAX + 1);
357 row = (int) (p_board->rows * (float) rand() / RAND_MAX + 1);
359 if( !p_board->box[col][row].IsMine ) {
360 i++;
361 p_board->box[col][row].IsMine = TRUE;
365 /* Remove temporarily placed mine for selected box */
366 p_board->box[selected_col][selected_row].IsMine = FALSE;
369 * Now we label the remaining boxes with the
370 * number of mines surrounding them.
372 for( col = 1; col < p_board->cols + 1; col++ )
373 for( row = 1; row < p_board->rows + 1; row++ ) {
374 for( i = -1; i <= 1; i++ )
375 for( j = -1; j <= 1; j++ ) {
376 if( p_board->box[col + i][row + j].IsMine ) {
377 p_board->box[col][row].NumMines++ ;
383 static void DrawMine( HDC hdc, HDC hMemDC, BOARD *p_board, unsigned col, unsigned row, BOOL IsPressed )
385 MINEBMP_OFFSET offset = BOX_BMP;
387 if( col == 0 || col > p_board->cols || row == 0 || row > p_board->rows )
388 return;
390 if( p_board->status == GAMEOVER ) {
391 if( p_board->box[col][row].IsMine ) {
392 switch( p_board->box[col][row].FlagType ) {
393 case FLAG:
394 offset = FLAG_BMP;
395 break;
396 case COMPLETE:
397 offset = EXPLODE_BMP;
398 break;
399 case QUESTION:
400 /* fall through */
401 case NORMAL:
402 offset = MINE_BMP;
404 } else {
405 switch( p_board->box[col][row].FlagType ) {
406 case QUESTION:
407 offset = QUESTION_BMP;
408 break;
409 case FLAG:
410 offset = WRONG_BMP;
411 break;
412 case NORMAL:
413 offset = BOX_BMP;
414 break;
415 case COMPLETE:
416 /* Do nothing */
417 break;
418 default:
419 WINE_TRACE("Unknown FlagType during game over in DrawMine\n");
420 break;
423 } else { /* WAITING or PLAYING */
424 switch( p_board->box[col][row].FlagType ) {
425 case QUESTION:
426 if( !IsPressed )
427 offset = QUESTION_BMP;
428 else
429 offset = QPRESS_BMP;
430 break;
431 case FLAG:
432 offset = FLAG_BMP;
433 break;
434 case NORMAL:
435 if( !IsPressed )
436 offset = BOX_BMP;
437 else
438 offset = MPRESS_BMP;
439 break;
440 case COMPLETE:
441 /* Do nothing */
442 break;
443 default:
444 WINE_TRACE("Unknown FlagType while playing in DrawMine\n");
445 break;
449 if( p_board->box[col][row].FlagType == COMPLETE
450 && !p_board->box[col][row].IsMine )
451 offset = (MINEBMP_OFFSET) p_board->box[col][row].NumMines;
453 BitBlt( hdc,
454 (col - 1) * MINE_WIDTH + p_board->mines_rect.left,
455 (row - 1) * MINE_HEIGHT + p_board->mines_rect.top,
456 MINE_WIDTH, MINE_HEIGHT,
457 hMemDC, 0, offset * MINE_HEIGHT, SRCCOPY );
460 static void DrawMines ( HDC hdc, HDC hMemDC, BOARD *p_board )
462 HGDIOBJ hOldObj;
463 unsigned col, row;
464 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
466 for( row = 1; row <= p_board->rows; row++ ) {
467 for( col = 1; col <= p_board->cols; col++ ) {
468 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
471 SelectObject( hMemDC, hOldObj );
474 static void DrawLeds( HDC hdc, HDC hMemDC, BOARD *p_board, int number, int x, int y )
476 HGDIOBJ hOldObj;
477 unsigned led[3], i;
478 int count;
480 count = number;
481 if( count < 1000 ) {
482 if( count >= 0 ) {
483 led[0] = count / 100 ;
484 count -= led[0] * 100;
486 else {
487 led[0] = 10; /* negative sign */
488 count = -count;
490 led[1] = count / 10;
491 count -= led[1] * 10;
492 led[2] = count;
494 else {
495 for( i = 0; i < 3; i++ )
496 led[i] = 10;
499 hOldObj = SelectObject (hMemDC, p_board->hLedsBMP);
501 for( i = 0; i < 3; i++ ) {
502 BitBlt( hdc,
503 i * LED_WIDTH + x,
505 LED_WIDTH,
506 LED_HEIGHT,
507 hMemDC,
509 led[i] * LED_HEIGHT,
510 SRCCOPY);
513 SelectObject( hMemDC, hOldObj );
517 static void DrawFace( HDC hdc, HDC hMemDC, BOARD *p_board )
519 HGDIOBJ hOldObj;
521 hOldObj = SelectObject (hMemDC, p_board->hFacesBMP);
523 BitBlt( hdc,
524 p_board->face_rect.left,
525 p_board->face_rect.top,
526 FACE_WIDTH,
527 FACE_HEIGHT,
528 hMemDC, 0, p_board->face_bmp * FACE_HEIGHT, SRCCOPY);
530 SelectObject( hMemDC, hOldObj );
534 static void DrawBoard( HDC hdc, HDC hMemDC, PAINTSTRUCT *ps, BOARD *p_board )
536 RECT tmp_rect;
538 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->counter_rect ) )
539 DrawLeds( hdc, hMemDC, p_board, p_board->mines - p_board->num_flags,
540 p_board->counter_rect.left,
541 p_board->counter_rect.top );
543 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->timer_rect ) )
544 DrawLeds( hdc, hMemDC, p_board, p_board->time,
545 p_board->timer_rect.left,
546 p_board->timer_rect.top );
548 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->face_rect ) )
549 DrawFace( hdc, hMemDC, p_board );
551 if( IntersectRect( &tmp_rect, &ps->rcPaint, &p_board->mines_rect ) )
552 DrawMines( hdc, hMemDC, p_board );
556 static void AddFlag( BOARD *p_board, unsigned col, unsigned row )
558 if( p_board->box[col][row].FlagType != COMPLETE ) {
559 switch( p_board->box[col][row].FlagType ) {
560 case FLAG:
561 if( p_board->IsMarkQ )
562 p_board->box[col][row].FlagType = QUESTION;
563 else
564 p_board->box[col][row].FlagType = NORMAL;
565 p_board->num_flags--;
566 break;
568 case QUESTION:
569 p_board->box[col][row].FlagType = NORMAL;
570 break;
572 default:
573 p_board->box[col][row].FlagType = FLAG;
574 p_board->num_flags++;
580 static void UnpressBox( BOARD *p_board, unsigned col, unsigned row )
582 HDC hdc;
583 HGDIOBJ hOldObj;
584 HDC hMemDC;
586 hdc = GetDC( p_board->hWnd );
587 hMemDC = CreateCompatibleDC( hdc );
588 hOldObj = SelectObject( hMemDC, p_board->hMinesBMP );
590 DrawMine( hdc, hMemDC, p_board, col, row, FALSE );
592 SelectObject( hMemDC, hOldObj );
593 DeleteDC( hMemDC );
594 ReleaseDC( p_board->hWnd, hdc );
598 static void UnpressBoxes( BOARD *p_board, unsigned col, unsigned row )
600 int i, j;
602 for( i = -1; i <= 1; i++ )
603 for( j = -1; j <= 1; j++ ) {
604 UnpressBox( p_board, col + i, row + j );
609 static void PressBox( BOARD *p_board, unsigned col, unsigned row )
611 HDC hdc;
612 HGDIOBJ hOldObj;
613 HDC hMemDC;
615 hdc = GetDC( p_board->hWnd );
616 hMemDC = CreateCompatibleDC( hdc );
617 hOldObj = SelectObject (hMemDC, p_board->hMinesBMP);
619 DrawMine( hdc, hMemDC, p_board, col, row, TRUE );
621 SelectObject( hMemDC, hOldObj );
622 DeleteDC( hMemDC );
623 ReleaseDC( p_board->hWnd, hdc );
627 static void PressBoxes( BOARD *p_board, unsigned col, unsigned row )
629 int i, j;
631 for( i = -1; i <= 1; i++ )
632 for( j = -1; j <= 1; j++ ) {
633 p_board->box[col + i][row + j].IsPressed = TRUE;
634 PressBox( p_board, col + i, row + j );
637 for( i = -1; i <= 1; i++ )
638 for( j = -1; j <= 1; j++ ) {
639 if( !p_board->box[p_board->press.x + i][p_board->press.y + j].IsPressed )
640 UnpressBox( p_board, p_board->press.x + i, p_board->press.y + j );
643 for( i = -1; i <= 1; i++ )
644 for( j = -1; j <= 1; j++ ) {
645 p_board->box[col + i][row + j].IsPressed = FALSE;
646 PressBox( p_board, col + i, row + j );
649 p_board->press.x = col;
650 p_board->press.y = row;
654 static void CompleteBox( BOARD *p_board, unsigned col, unsigned row )
656 int i, j;
658 if( p_board->box[col][row].FlagType != COMPLETE &&
659 p_board->box[col][row].FlagType != FLAG &&
660 col > 0 && col < p_board->cols + 1 &&
661 row > 0 && row < p_board->rows + 1 ) {
662 p_board->box[col][row].FlagType = COMPLETE;
664 if( p_board->box[col][row].IsMine ) {
665 p_board->face_bmp = DEAD_BMP;
666 p_board->status = GAMEOVER;
668 else if( p_board->status != GAMEOVER )
669 p_board->boxes_left--;
671 if( p_board->box[col][row].NumMines == 0 )
673 for( i = -1; i <= 1; i++ )
674 for( j = -1; j <= 1; j++ )
675 CompleteBox( p_board, col + i, row + j );
681 static void CompleteBoxes( BOARD *p_board, unsigned col, unsigned row )
683 unsigned numFlags = 0;
684 int i, j;
686 if( p_board->box[col][row].FlagType == COMPLETE ) {
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 numFlags++;
693 if( numFlags == p_board->box[col][row].NumMines ) {
694 for( i = -1; i <= 1; i++ )
695 for( j = -1; j <= 1; j++ ) {
696 if( p_board->box[col+i][row+j].FlagType != FLAG )
697 CompleteBox( p_board, col+i, row+j );
704 static void TestMines( BOARD *p_board, POINT pt, int msg )
706 BOOL draw = TRUE;
707 int col, row;
709 col = (pt.x - p_board->mines_rect.left) / MINE_WIDTH + 1;
710 row = (pt.y - p_board->mines_rect.top ) / MINE_HEIGHT + 1;
712 switch ( msg ) {
713 case WM_LBUTTONDOWN:
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 = col;
718 p_board->press.y = row;
719 PressBox( p_board, col, row );
721 draw = FALSE;
722 break;
724 case WM_LBUTTONUP:
725 if( p_board->press.x != col || p_board->press.y != row )
726 UnpressBox( p_board,
727 p_board->press.x, p_board->press.y );
728 p_board->press.x = 0;
729 p_board->press.y = 0;
730 if( p_board->box[col][row].FlagType != FLAG
731 && p_board->status != PLAYING )
733 p_board->status = PLAYING;
734 PlaceMines( p_board, col, row );
736 CompleteBox( p_board, col, row );
737 break;
739 case WM_MBUTTONDOWN:
740 PressBoxes( p_board, col, row );
741 draw = FALSE;
742 break;
744 case WM_MBUTTONUP:
745 if( p_board->press.x != col || p_board->press.y != row )
746 UnpressBoxes( p_board,
747 p_board->press.x, p_board->press.y );
748 p_board->press.x = 0;
749 p_board->press.y = 0;
750 CompleteBoxes( p_board, col, row );
751 break;
753 case WM_RBUTTONDOWN:
754 AddFlag( p_board, col, row );
755 break;
756 default:
757 WINE_TRACE("Unknown message type received in TestMines\n");
758 break;
761 if( draw )
763 RedrawWindow( p_board->hWnd, NULL, 0,
764 RDW_INVALIDATE | RDW_UPDATENOW );
769 static void TestFace( BOARD *p_board, POINT pt, int msg )
771 if( p_board->status == PLAYING || p_board->status == WAITING ) {
772 if( msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN )
773 p_board->face_bmp = OOH_BMP;
774 else p_board->face_bmp = SMILE_BMP;
776 else if( p_board->status == GAMEOVER )
777 p_board->face_bmp = DEAD_BMP;
778 else if( p_board->status == WON )
779 p_board->face_bmp = COOL_BMP;
781 if( PtInRect( &p_board->face_rect, pt ) ) {
782 if( msg == WM_LBUTTONDOWN )
783 p_board->face_bmp = SPRESS_BMP;
785 if( msg == WM_LBUTTONUP )
786 CreateBoard( p_board );
789 RedrawWindow( p_board->hWnd, &p_board->face_rect, 0,
790 RDW_INVALIDATE | RDW_UPDATENOW );
794 static void TestBoard( HWND hWnd, BOARD *p_board, int x, int y, int msg )
796 POINT pt;
797 unsigned col,row;
799 pt.x = x;
800 pt.y = y;
802 if( PtInRect( &p_board->mines_rect, pt ) && p_board->status != GAMEOVER
803 && p_board->status != WON )
804 TestMines( p_board, pt, msg );
805 else {
806 UnpressBoxes( p_board,
807 p_board->press.x,
808 p_board->press.y );
809 p_board->press.x = 0;
810 p_board->press.y = 0;
813 if( p_board->boxes_left == 0 ) {
814 p_board->status = WON;
816 if (p_board->num_flags < p_board->mines) {
817 for( row = 1; row <= p_board->rows; row++ ) {
818 for( col = 1; col <= p_board->cols; col++ ) {
819 if (p_board->box[col][row].IsMine && p_board->box[col][row].FlagType != FLAG)
820 p_board->box[col][row].FlagType = FLAG;
824 p_board->num_flags = p_board->mines;
826 RedrawWindow( p_board->hWnd, NULL, 0,
827 RDW_INVALIDATE | RDW_UPDATENOW );
830 if( p_board->difficulty != CUSTOM &&
831 p_board->time < p_board->best_time[p_board->difficulty] ) {
832 p_board->best_time[p_board->difficulty] = p_board->time;
834 DialogBoxParamW( p_board->hInst, MAKEINTRESOURCEW(DLG_CONGRATS), hWnd,
835 CongratsDlgProc, (LPARAM) p_board);
836 DialogBoxParamW( p_board->hInst, MAKEINTRESOURCEW(DLG_TIMES), hWnd,
837 TimesDlgProc, (LPARAM) p_board);
840 TestFace( p_board, pt, msg );
844 static LRESULT WINAPI MainProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
846 HDC hdc;
847 PAINTSTRUCT ps;
848 HMENU hMenu;
849 static BOARD board;
851 switch( msg ) {
852 case WM_CREATE:
853 board.hInst = ((LPCREATESTRUCTW) lParam)->hInstance;
854 board.hWnd = hWnd;
855 InitBoard( &board );
856 CreateBoard( &board );
857 return 0;
859 case WM_PAINT:
861 HDC hMemDC;
863 WINE_TRACE("WM_PAINT\n");
864 hdc = BeginPaint( hWnd, &ps );
865 hMemDC = CreateCompatibleDC( hdc );
867 DrawBoard( hdc, hMemDC, &ps, &board );
869 DeleteDC( hMemDC );
870 EndPaint( hWnd, &ps );
872 return 0;
875 case WM_MOVE:
876 WINE_TRACE("WM_MOVE\n");
877 board.pos.x = (short)LOWORD(lParam);
878 board.pos.y = (short)HIWORD(lParam);
879 return 0;
881 case WM_DESTROY:
882 SaveBoard( &board );
883 DestroyBoard( &board );
884 PostQuitMessage( 0 );
885 return 0;
887 case WM_TIMER:
888 if( board.status == PLAYING ) {
889 board.time++;
890 RedrawWindow( hWnd, &board.timer_rect, 0,
891 RDW_INVALIDATE | RDW_UPDATENOW );
893 return 0;
895 case WM_LBUTTONDOWN:
896 WINE_TRACE("WM_LBUTTONDOWN\n");
897 if( wParam & MK_RBUTTON )
898 msg = WM_MBUTTONDOWN;
899 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
900 SetCapture( hWnd );
901 return 0;
903 case WM_LBUTTONUP:
904 WINE_TRACE("WM_LBUTTONUP\n");
905 if( wParam & MK_RBUTTON )
906 msg = WM_MBUTTONUP;
907 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
908 ReleaseCapture();
909 return 0;
911 case WM_RBUTTONDOWN:
912 WINE_TRACE("WM_RBUTTONDOWN\n");
913 if( wParam & MK_LBUTTON ) {
914 board.press.x = 0;
915 board.press.y = 0;
916 msg = WM_MBUTTONDOWN;
918 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
919 return 0;
921 case WM_RBUTTONUP:
922 WINE_TRACE("WM_RBUTTONUP\n");
923 if( wParam & MK_LBUTTON )
924 msg = WM_MBUTTONUP;
925 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
926 return 0;
928 case WM_MBUTTONDOWN:
929 WINE_TRACE("WM_MBUTTONDOWN\n");
930 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
931 return 0;
933 case WM_MBUTTONUP:
934 WINE_TRACE("WM_MBUTTONUP\n");
935 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
936 return 0;
938 case WM_MOUSEMOVE:
940 if( ( wParam & MK_MBUTTON ) ||
941 ( ( wParam & MK_LBUTTON ) && ( wParam & MK_RBUTTON ) ) ) {
942 msg = WM_MBUTTONDOWN;
944 else if( wParam & MK_LBUTTON ) {
945 msg = WM_LBUTTONDOWN;
947 else {
948 return 0;
951 TestBoard( hWnd, &board, (short)LOWORD(lParam), (short)HIWORD(lParam), msg );
953 return 0;
956 case WM_COMMAND:
957 switch(LOWORD(wParam)) {
958 case IDM_NEW:
959 CreateBoard( &board );
960 return 0;
962 case IDM_MARKQ:
963 hMenu = GetMenu( hWnd );
964 board.IsMarkQ = !board.IsMarkQ;
965 if( board.IsMarkQ )
966 CheckMenuItem( hMenu, IDM_MARKQ, MF_CHECKED );
967 else
968 CheckMenuItem( hMenu, IDM_MARKQ, MF_UNCHECKED );
969 return 0;
971 case IDM_BEGINNER:
972 SetDifficulty( &board, BEGINNER );
973 CreateBoard( &board );
974 return 0;
976 case IDM_ADVANCED:
977 SetDifficulty( &board, ADVANCED );
978 CreateBoard( &board );
979 return 0;
981 case IDM_EXPERT:
982 SetDifficulty( &board, EXPERT );
983 CreateBoard( &board );
984 return 0;
986 case IDM_CUSTOM:
987 SetDifficulty( &board, CUSTOM );
988 CreateBoard( &board );
989 return 0;
991 case IDM_EXIT:
992 SendMessageW( hWnd, WM_CLOSE, 0, 0);
993 return 0;
995 case IDM_TIMES:
996 DialogBoxParamW( board.hInst, MAKEINTRESOURCEW(DLG_TIMES), hWnd,
997 TimesDlgProc, (LPARAM) &board);
998 return 0;
1000 case IDM_ABOUT:
1002 WCHAR appname[256], other[256];
1003 LoadStringW( board.hInst, IDS_APPNAME, appname, sizeof(appname)/sizeof(WCHAR) );
1004 LoadStringW( board.hInst, IDS_ABOUT, other, sizeof(other)/sizeof(WCHAR) );
1005 ShellAboutW( hWnd, appname, other,
1006 LoadImageA( board.hInst, "WINEMINE", IMAGE_ICON, 48, 48, LR_SHARED ));
1007 return 0;
1009 default:
1010 WINE_TRACE("Unknown WM_COMMAND command message received\n");
1011 break;
1014 return DefWindowProcW( hWnd, msg, wParam, lParam );
1017 int WINAPI wWinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow )
1019 MSG msg;
1020 WNDCLASSEXW wc;
1021 HWND hWnd;
1022 HACCEL haccel;
1023 WCHAR appname[20];
1025 LoadStringW( hInst, IDS_APPNAME, appname, sizeof(appname)/sizeof(WCHAR));
1027 wc.cbSize = sizeof(wc);
1028 wc.style = 0;
1029 wc.lpfnWndProc = MainProc;
1030 wc.cbClsExtra = 0;
1031 wc.cbWndExtra = 0;
1032 wc.hInstance = hInst;
1033 wc.hIcon = LoadIconW( hInst, MAKEINTRESOURCEW(IDI_WINEMINE) );
1034 wc.hCursor = LoadCursorW( 0, (LPWSTR)IDI_APPLICATION );
1035 wc.hbrBackground = GetStockObject( BLACK_BRUSH );
1036 wc.lpszMenuName = MAKEINTRESOURCEW(IDM_WINEMINE);
1037 wc.lpszClassName = appname;
1038 wc.hIconSm = LoadImageW( hInst, MAKEINTRESOURCEW(IDI_WINEMINE), IMAGE_ICON,
1039 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), LR_SHARED );
1041 if (!RegisterClassExW(&wc)) ExitProcess(1);
1042 hWnd = CreateWindowW( appname, appname,
1043 wnd_style,
1044 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1045 0, 0, hInst, NULL );
1047 if (!hWnd) ExitProcess(1);
1049 ShowWindow( hWnd, cmdshow );
1050 UpdateWindow( hWnd );
1052 haccel = LoadAcceleratorsW( hInst, MAKEINTRESOURCEW(IDA_WINEMINE) );
1053 SetTimer( hWnd, ID_TIMER, 1000, NULL );
1055 while( GetMessageW(&msg, 0, 0, 0) ) {
1056 if (!TranslateAcceleratorW( hWnd, haccel, &msg ))
1057 TranslateMessage( &msg );
1059 DispatchMessageW( &msg );
1061 return msg.wParam;