main.c 30 KB

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