Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
11 KiB

  1. /*************/
  2. /* grafwin.c */
  3. /*************/
  4. #define _WINDOWS
  5. #include <windows.h>
  6. #include <port1632.h>
  7. #include "main.h"
  8. #include "res.h"
  9. #include "grafix.h"
  10. #include "rtns.h"
  11. #include "sound.h"
  12. #include "pref.h"
  13. INT rgDibOff[iBlkMax];
  14. INT rgDibLedOff[iLedMax];
  15. INT rgDibButton[iButtonMax];
  16. LPSTR lpDibBlks;
  17. LPSTR lpDibLed;
  18. LPSTR lpDibButton;
  19. HANDLE hResBlks;
  20. HANDLE hResLed;
  21. HANDLE hResButton;
  22. HPEN hGrayPen = NULL;
  23. //
  24. // MEM: make an array of memory bitmaps for storing DIBs
  25. //
  26. HDC MemBlkDc[iBlkMax];
  27. HBITMAP MemBlkBitmap[iBlkMax];
  28. //
  29. // end MEM:
  30. //
  31. extern INT dypCaption;
  32. extern INT dypMenu;
  33. extern INT dypBorder;
  34. extern INT dxpBorder;
  35. /*** External Data ***/
  36. extern HWND hwndMain;
  37. extern HANDLE hInst;
  38. extern PREF Preferences;
  39. extern CHAR rgBlk[cBlkMax];
  40. extern INT dxWindow;
  41. extern INT dyWindow;
  42. extern INT fStatus;
  43. extern INT cSec;
  44. extern INT cBombLeft;
  45. extern INT cBoxVisit;
  46. extern INT xBoxMac;
  47. extern INT yBoxMac;
  48. extern INT iButtonCur;
  49. /****** F I N I T L O C A L ******/
  50. BOOL FInitLocal(VOID)
  51. {
  52. if (!FLoadBitmaps())
  53. return fFalse;
  54. ClearField();
  55. return fTrue;
  56. }
  57. HANDLE HFindBitmap(INT id)
  58. {
  59. return FindResource(hInst, MAKEINTRESOURCE(id+(!Preferences.fColor)), RT_BITMAP);
  60. }
  61. INT CbBitmap(INT x, INT y)
  62. {
  63. x *= (Preferences.fColor ? 4 : 1); /* Convert pixels to bits */
  64. return (y * ( ((x+31) >> 5) << 2)); /* Bytes on 32 byte boundary */
  65. }
  66. /****** F L O A D B I T M A P S ******/
  67. BOOL FLoadBitmaps(VOID)
  68. {
  69. HANDLE hResInfo;
  70. REGISTER INT i;
  71. INT cbDibHeader;
  72. INT cb;
  73. HDC hDC;
  74. // initialize all the bitmaps to NULL.
  75. hResBlks = hResLed = hResButton = NULL;
  76. hResInfo = HFindBitmap(ID_BMP_BLOCKS);
  77. if (hResInfo)
  78. hResBlks = LoadResource(hInst, hResInfo);
  79. hResInfo = HFindBitmap(ID_BMP_LED);
  80. if (hResInfo)
  81. hResLed = LoadResource(hInst, hResInfo);
  82. hResInfo = HFindBitmap(ID_BMP_BUTTON);
  83. if (hResInfo)
  84. hResButton = LoadResource(hInst, hResInfo);
  85. if (hResBlks == NULL || hResLed == NULL || hResButton == NULL)
  86. return fFalse;
  87. lpDibBlks = LockResource(hResBlks);
  88. lpDibLed = LockResource(hResLed);
  89. lpDibButton= LockResource(hResButton);
  90. if (!Preferences.fColor)
  91. hGrayPen = GetStockObject(BLACK_PEN);
  92. else
  93. hGrayPen = CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
  94. cbDibHeader = sizeof(BITMAPINFOHEADER) + (Preferences.fColor ? 16*4 : 2*4);
  95. cb = CbBitmap(dxBlk, dyBlk);
  96. for (i = 0 ; i < iBlkMax; i++)
  97. rgDibOff[i] = cbDibHeader + (i * cb);
  98. cb = CbBitmap(dxLed, dyLed);
  99. for (i = 0 ; i < iLedMax; i++)
  100. rgDibLedOff[i] = cbDibHeader + (i * cb);
  101. cb = CbBitmap(dxButton, dyButton);
  102. for (i = 0 ; i < iButtonMax; i++)
  103. rgDibButton[i] = cbDibHeader + (i * cb);
  104. //
  105. // MEM: move each Blk DIB to a memory BitMap
  106. //
  107. hDC = GetDC(hwndMain);
  108. for (i = 0 ; i < iBlkMax; i++) {
  109. //
  110. // Create a device context compatible with the device's active
  111. // display for each of these bitmaps.
  112. //
  113. MemBlkDc[i] = CreateCompatibleDC(hDC);
  114. if (MemBlkDc[i] == (HDC)NULL) {
  115. OutputDebugStringA("FLoad failed to create compatible dc\n");
  116. }
  117. //
  118. // create the bitmap for the above memory DC and selct this bitmap.
  119. //
  120. // we really only need 1 bitmap and 1 dc as they have done before!
  121. //
  122. MemBlkBitmap[i] = CreateCompatibleBitmap(hDC,dxBlk,dxBlk);
  123. if (MemBlkBitmap[i] == (HBITMAP)NULL) {
  124. OutputDebugStringA("Failed to create Bitmap\n");
  125. }
  126. SelectObject(MemBlkDc[i],MemBlkBitmap[i]);
  127. //
  128. // copy the bits from the DIB into this bitmap
  129. //
  130. SetDIBitsToDevice(MemBlkDc[i],
  131. 0,
  132. 0,
  133. dxBlk,
  134. dyBlk,
  135. 0,
  136. 0,
  137. 0,
  138. dyBlk,
  139. lpDibBlks + rgDibOff[i],
  140. (LPBITMAPINFO) lpDibBlks,
  141. DIB_RGB_COLORS);
  142. }
  143. ReleaseDC(hwndMain, hDC);
  144. return fTrue;
  145. }
  146. VOID FreeBitmaps(VOID)
  147. {
  148. int i;
  149. if (hGrayPen != NULL)
  150. DeleteObject(hGrayPen);
  151. UnlockResource(hResBlks);
  152. UnlockResource(hResLed);
  153. UnlockResource(hResButton);
  154. for (i = 0 ; i < iBlkMax; i++) {
  155. DeleteDC(MemBlkDc[i]);
  156. DeleteObject(MemBlkBitmap[i]);
  157. }
  158. }
  159. /****** C L E A N U P ******/
  160. VOID CleanUp(VOID)
  161. {
  162. FreeBitmaps();
  163. EndTunes();
  164. }
  165. /****** D R A W B L K ******/
  166. VOID DrawBlk(HDC hDC, INT x, INT y)
  167. {
  168. BitBlt(hDC,
  169. (x<<4)+(dxGridOff-dxBlk),
  170. (y<<4)+(dyGridOff-dyBlk),
  171. dxBlk,
  172. dyBlk,
  173. MemBlkDc[iBLK(x,y)],
  174. 0,
  175. 0,
  176. SRCCOPY);
  177. }
  178. VOID DisplayBlk(INT x, INT y)
  179. {
  180. HDC hDC = GetDC(hwndMain);
  181. BitBlt(hDC,
  182. (x<<4)+(dxGridOff-dxBlk),
  183. (y<<4)+(dyGridOff-dyBlk),
  184. dxBlk,
  185. dyBlk,
  186. MemBlkDc[iBLK(x,y)],
  187. 0,
  188. 0,
  189. SRCCOPY);
  190. ReleaseDC(hwndMain, hDC);
  191. }
  192. /****** D R A W G R I D ******/
  193. VOID DrawGrid(HDC hDC)
  194. {
  195. REGISTER INT x;
  196. REGISTER INT y;
  197. INT dx;
  198. INT dy;
  199. dy = dyGridOff;
  200. for (y=1; y <= yBoxMac; y++, dy += dyBlk)
  201. {
  202. dx = dxGridOff;
  203. for (x=1; x <= xBoxMac; x++, dx += dxBlk)
  204. {
  205. BitBlt(hDC,
  206. dx,
  207. dy,
  208. dxBlk,
  209. dyBlk,
  210. MemBlkDc[iBLK(x,y)],
  211. 0,
  212. 0,
  213. SRCCOPY);
  214. }
  215. }
  216. }
  217. VOID DisplayGrid(VOID)
  218. {
  219. HDC hDC = GetDC(hwndMain);
  220. DrawGrid(hDC);
  221. ReleaseDC(hwndMain, hDC);
  222. }
  223. /****** D R A W L E D ******/
  224. VOID DrawLed(HDC hDC, INT x, INT iLed)
  225. {
  226. SetDIBitsToDevice(hDC, x, dyTopLed, dxLed, dyLed, 0, 0, 0, dyLed,
  227. lpDibLed + rgDibLedOff[iLed], (LPBITMAPINFO) lpDibLed, DIB_RGB_COLORS);
  228. }
  229. /****** D R A W B O M B C O U N T ******/
  230. VOID DrawBombCount(HDC hDC)
  231. {
  232. INT iLed;
  233. INT cBombs;
  234. DWORD dwDCLayout = GetLayout(hDC);
  235. if (dwDCLayout & LAYOUT_RTL) {
  236. SetLayout(hDC , 0); // Turn off mirroring before painting.
  237. }
  238. if (cBombLeft < 0)
  239. {
  240. iLed = iLedNegative;
  241. cBombs = (-cBombLeft) % 100;
  242. }
  243. else
  244. {
  245. iLed = cBombLeft / 100;
  246. cBombs = cBombLeft % 100;
  247. }
  248. DrawLed(hDC, dxLeftBomb, iLed);
  249. DrawLed(hDC, dxLeftBomb+dxLed, cBombs/10);
  250. DrawLed(hDC, dxLeftBomb+dxLed+dxLed, cBombs % 10);
  251. if (dwDCLayout & LAYOUT_RTL) {
  252. SetLayout(hDC , dwDCLayout); // Turn on the mirroring again.
  253. }
  254. }
  255. VOID DisplayBombCount(VOID)
  256. {
  257. HDC hDC = GetDC(hwndMain);
  258. DrawBombCount(hDC);
  259. ReleaseDC(hwndMain, hDC);
  260. }
  261. /****** D R A W T I M E ******/
  262. VOID DrawTime(HDC hDC)
  263. {
  264. INT iLed = cSec;
  265. DWORD dwDCLayout = GetLayout(hDC);
  266. if (dwDCLayout & LAYOUT_RTL) {
  267. SetLayout(hDC , 0); // Turn off mirroring before painting.
  268. }
  269. DrawLed(hDC, dxWindow-(dxRightTime+3*dxLed+dxpBorder), iLed/100); /* OverFlow ? */
  270. DrawLed(hDC, dxWindow-(dxRightTime+2*dxLed+dxpBorder),(iLed %= 100)/10 );
  271. DrawLed(hDC, dxWindow-(dxRightTime+ dxLed+dxpBorder), iLed % 10);
  272. if (dwDCLayout & LAYOUT_RTL) {
  273. SetLayout(hDC , dwDCLayout); // Turn on the mirroring again.
  274. }
  275. }
  276. VOID DisplayTime(VOID)
  277. {
  278. HDC hDC = GetDC(hwndMain);
  279. DrawTime(hDC);
  280. ReleaseDC(hwndMain, hDC);
  281. }
  282. /****** D R A W B U T T O N ******/
  283. VOID DrawButton(HDC hDC, INT iButton)
  284. {
  285. SetDIBitsToDevice(hDC, (dxWindow-dxButton)>>1, dyTopLed, dxButton, dyButton, 0, 0, 0, dyButton,
  286. lpDibButton + rgDibButton[iButton], (LPBITMAPINFO) lpDibButton, DIB_RGB_COLORS);
  287. }
  288. VOID DisplayButton(INT iButton)
  289. {
  290. HDC hDC = GetDC(hwndMain);
  291. DrawButton(hDC, iButton);
  292. ReleaseDC(hwndMain, hDC);
  293. }
  294. /****** S E T T H E P E N ******/
  295. VOID SetThePen(HDC hDC, INT fNormal)
  296. {
  297. if (fNormal & 1)
  298. SetROP2(hDC, R2_WHITE);
  299. else
  300. {
  301. SetROP2(hDC, R2_COPYPEN);
  302. SelectObject(hDC, hGrayPen);
  303. }
  304. }
  305. /****** D R A W B O R D E R ******/
  306. /* 0 - white, gray
  307. 1 - gray, white
  308. 2 - white, white
  309. 3 - grey, grey
  310. */
  311. VOID DrawBorder(HDC hDC, INT x1, INT y1, INT x2, INT y2, INT width, INT fNormal)
  312. {
  313. INT i = 0;
  314. SetThePen(hDC, fNormal);
  315. while (i++ < width)
  316. {
  317. (VOID)MMoveTo(hDC, x1, --y2);
  318. LineTo(hDC, x1++, y1);
  319. LineTo(hDC, x2--, y1++);
  320. }
  321. if (fNormal < 2)
  322. SetThePen(hDC, fNormal ^ 1);
  323. while (--i)
  324. {
  325. (VOID)MMoveTo(hDC, x1--, ++y2);
  326. LineTo(hDC, ++x2, y2);
  327. LineTo(hDC, x2, --y1);
  328. }
  329. }
  330. VOID DrawBackground(HDC hDC)
  331. {
  332. INT x, y;
  333. x = dxWindow-1;
  334. y = dyWindow-1;
  335. DrawBorder(hDC, 0, 0, x, y, 3, 1);
  336. x -= (dxRightSpace -3);
  337. y -= (dyBottomSpace-3);
  338. DrawBorder(hDC, dxGridOff-3, dyGridOff-3, x, y, 3, 0);
  339. DrawBorder(hDC, dxGridOff-3, dyTopSpace-3, x, dyTopLed+dyLed+(dyBottomSpace-6), 2, 0);
  340. x = dxLeftBomb+dxLed*3;
  341. y = dyTopLed+dyLed;
  342. DrawBorder(hDC, dxLeftBomb-1, dyTopLed-1, x, y, 1, 0);
  343. x = dxWindow-(dxRightTime+3*dxLed+dxpBorder+1);
  344. DrawBorder(hDC, x, dyTopLed-1, x+(dxLed*3+1), y, 1, 0);
  345. x = ((dxWindow-dxButton)>>1)-1;
  346. DrawBorder(hDC, x, dyTopLed-1, x+dxButton+1, dyTopLed+dyButton, 1, 2);
  347. }
  348. /****** D R A W S C R E E N ******/
  349. VOID DrawScreen(HDC hDC)
  350. {
  351. DrawBackground(hDC);
  352. DrawBombCount(hDC);
  353. DrawButton(hDC, iButtonCur);
  354. DrawTime(hDC);
  355. DrawGrid(hDC);
  356. }
  357. VOID DisplayScreen(VOID)
  358. {
  359. HDC hDC = GetDC(hwndMain);
  360. DrawScreen(hDC);
  361. ReleaseDC(hwndMain, hDC);
  362. }