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.

350 lines
6.2 KiB

  1. /************/
  2. /* grafix.c */ /* WARNING: The following code ain't pretty */
  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 "pref.h"
  12. #include "sound.h"
  13. HDC hDCTemp;
  14. HDC hDCBall;
  15. HDC hDCBack;
  16. HBITMAP hbmTemp;
  17. HBITMAP hbmBall;
  18. HBITMAP hbmBack;
  19. HPEN hPenLtGray;
  20. HPEN hPenGray;
  21. INT rgXBlk[cBlkMax]; /* Positions of squares on screen */
  22. INT rgYBlk[cBlkMax];
  23. INT rgBallOff[iBallMax]; /* Offsets into the ball bitmap */
  24. /*** External Data ***/
  25. extern HWND hwndMain;
  26. extern HANDLE hInst;
  27. extern INT dyBlkPlane;
  28. extern INT dypPlane;
  29. extern INT dxpPlane;
  30. extern INT dxpGrid;
  31. extern INT dypGrid;
  32. extern INT dxpWindow;
  33. extern INT dypWindow;
  34. extern INT dxpGridOff;
  35. extern INT cBlkRow;
  36. extern INT cBlkPlane;
  37. extern INT cPlane;
  38. extern INT cBlkMac;
  39. extern rgBlk[cBlkMax];
  40. extern rgFlash[cDimMax];
  41. extern INT iPlayer;
  42. extern BOOL f4;
  43. extern PREF Preferences;
  44. /****** F I N I T L O C A L ******/
  45. BOOL FInitLocal(VOID)
  46. {
  47. INT y;
  48. HDC hDCScrn;
  49. hPenLtGray = CreatePen(PS_SOLID, 1, RGB_LTGRAY);
  50. hPenGray = CreatePen(PS_SOLID, 1, RGB_GRAY);
  51. hDCScrn = GetDC(hwndMain);
  52. hDCTemp = CreateCompatibleDC(NULL);
  53. hbmTemp = CreateCompatibleBitmap(hDCScrn, dxpGridMax, dypGridMax);
  54. SelectObject(hDCTemp, hbmTemp);
  55. hDCBack = CreateCompatibleDC(NULL);
  56. hbmBack = CreateCompatibleBitmap(hDCScrn, dxpGridMax, dypGridMax);
  57. SelectObject(hDCBack, hbmBack);
  58. hDCBall = CreateCompatibleDC(NULL);
  59. ReleaseDC(hwndMain, hDCScrn);
  60. GetTheBitmap();
  61. for (y = 0; y < iBallMax; y++)
  62. rgBallOff[y] = y*dyBall;
  63. return ((hDCTemp != NULL) && (hbmTemp != NULL) &&
  64. (hDCBack != NULL) && (hbmBack != NULL) &&
  65. (hDCBall != NULL) && (hbmBall != NULL) );
  66. }
  67. VOID GetTheBitmap(VOID)
  68. {
  69. HBITMAP hbm;
  70. hbmBall = LoadBitmap(hInst, MAKEINTRESOURCE(ID_BMP_BALL+(!Preferences.fColor)));
  71. if (hDCBall != NULL)
  72. {
  73. hbm = SelectObject(hDCBall, hbmBall);
  74. if (hbm)
  75. DeleteObject(hbm);
  76. }
  77. }
  78. /****** C L E A N U P ******/
  79. VOID CleanUp(VOID)
  80. {
  81. EndTunes();
  82. DeleteDC(hDCBall);
  83. DeleteDC(hDCBack);
  84. DeleteDC(hDCTemp);
  85. DeleteObject(hbmBall);
  86. DeleteObject(hbmBack);
  87. DeleteObject(hbmTemp);
  88. DeleteObject(hPenGray);
  89. DeleteObject(hPenLtGray);
  90. }
  91. /****** S E T U P B O A R D ******/
  92. VOID SetupBoard(VOID)
  93. {
  94. INT x,y,z;
  95. INT dx,dy;
  96. INT i = 0;
  97. PatBlt(hDCBack, 0, 0, dxpGridMax, dypGridMax, BLACKNESS);
  98. for (z = 0; z < cPlane; z++)
  99. {
  100. dy = z * (dypPlane + dyLevel);
  101. for (y = 0; y < cBlkRow; y++, dy += dyBlk)
  102. {
  103. dx = ((cBlkRow-1)-y)*dxSlant;
  104. for (x = 0; x < cBlkRow; x++, dx += (dxBlk-1))
  105. {
  106. rgXBlk[i] = dx;
  107. rgYBlk[i] = dy;
  108. i++;
  109. }
  110. }
  111. }
  112. /* Just plot squares back to front *** CHANGE TO DRAW LINES */
  113. for (i = 0; i < cBlkMac; i++)
  114. BitBlt(hDCBack, rgXBlk[i], rgYBlk[i], dxBall, dyBall,
  115. hDCBall, 0, 0, SRCPAINT);
  116. BitBlt(hDCTemp, 0, 0, dxpGridMax, dypGridMax, hDCBack, 0, 0, SRCCOPY);
  117. }
  118. /****** D I S P L A Y B A L L ******/
  119. VOID DisplayBall(BLK blkFix, BALL ballFix)
  120. {
  121. HDC hDC;
  122. INT dx,dy;
  123. INT dyFix;
  124. BLK blk,blkStart, blkEnd;
  125. BALL iBall;
  126. #if DEBUG > 1
  127. CHAR sz[80];
  128. wsprintf(sz,"Disp blk=%d ball=%d\r\n",blkFix,ballFix);
  129. OutputDebugString(sz);
  130. #endif
  131. #ifdef DEBUG
  132. if (blkFix < 0)
  133. {
  134. Oops("DisplayBall: Invalid blk");
  135. return;
  136. }
  137. else if (ballFix >= iBallMax)
  138. {
  139. Oops("DisplayBall: Invalid iBall");
  140. return;
  141. }
  142. #endif
  143. hDC = GetDC(hwndMain);
  144. blkEnd = cBlkPlane + (blkStart = cBlkPlane * (blkFix/cBlkPlane));
  145. dyFix = rgYBlk[blkStart];
  146. BitBlt(hDCTemp, 0, dyFix, dxpPlane, dypPlane,
  147. hDCBack, 0, dyFix, SRCCOPY);
  148. iBall = rgBlk[blkFix];
  149. rgBlk[blkFix] = ballFix;
  150. ballFix = iBall;
  151. for (blk = blkStart; blk < blkEnd; blk++)
  152. {
  153. if ((iBall = rgBlk[blk]) != iBallBlank)
  154. {
  155. BitBlt(hDCTemp, dx = rgXBlk[blk], dy = rgYBlk[blk], dxBall, dyBall,
  156. hDCBall, 0, rgBallOff[iBallMask], SRCAND);
  157. BitBlt(hDCTemp, dx, dy, dxBall, dyBall,
  158. hDCBall, 0, rgBallOff[iBall], SRCPAINT);
  159. }
  160. }
  161. rgBlk[blkFix] = ballFix;
  162. BitBlt(hDC, dxpGridOff, (dyFix + dypGridOff), dxpPlane, dypPlane,
  163. hDCTemp, 0, dyFix, SRCCOPY);
  164. ReleaseDC(hwndMain, hDC);
  165. }
  166. /****** D R A W G R I D ******/
  167. VOID DrawGrid(HDC hDC)
  168. {
  169. BitBlt(hDC, dxpGridOff, dypGridOff, dxpGrid, dypGrid, hDCTemp, 0, 0, SRCCOPY);
  170. }
  171. VOID DisplayGrid(VOID)
  172. {
  173. HDC hDC = GetDC(hwndMain);
  174. DrawGrid(hDC);
  175. ReleaseDC(hwndMain, hDC);
  176. }
  177. /****** R E D O D I S P L A Y ******/
  178. VOID ReDoDisplay()
  179. {
  180. REGISTER INT i;
  181. REGISTER INT iBall;
  182. BitBlt(hDCTemp, 0, 0, dxpGrid, dypGrid, hDCBack, 0, 0, SRCCOPY);
  183. /* Place balls back to front */
  184. for (i = 0; i < cBlkMac; i++)
  185. {
  186. if ((iBall = rgBlk[i]) != iBallBlank)
  187. {
  188. BitBlt(hDCTemp, rgXBlk[i], rgYBlk[i], dxBall, dyBall,
  189. hDCBall, 0, rgBallOff[iBallMask], SRCAND);
  190. BitBlt(hDCTemp, rgXBlk[i], rgYBlk[i], dxBall, dyBall,
  191. hDCBall, 0, rgBallOff[iBall], SRCPAINT);
  192. }
  193. }
  194. }
  195. /****** P L A C E B A L L ******/
  196. VOID PlaceBall(BLK blk, BALL iBall)
  197. {
  198. HDC hDC = GetDC(hwndMain);
  199. #ifdef DEBUG
  200. if (blk < 0 || blk >= cBlkMac)
  201. Oops("PlaceBlk: Blk out of range");
  202. else if (iBall < iBallBlank || iBall >= iBallMax)
  203. Oops("PlaceBall: Ball out of range");
  204. #endif
  205. rgBlk[blk] = iBall;
  206. ReDoDisplay();
  207. DrawGrid(hDC);
  208. ReleaseDC(hwndMain, hDC);
  209. }
  210. VOID DrawRect(HDC hDC, INT x1, INT y1, INT x2, INT y2, HPEN hPen)
  211. {
  212. SelectObject(hDC, hPen);
  213. (VOID)MMoveTo(hDC, x1, y1);
  214. LineTo(hDC, x1, y2);
  215. LineTo(hDC, x2, y2);
  216. LineTo(hDC, x2, y1);
  217. LineTo(hDC, x1, y1);
  218. }
  219. VOID DrawBorder(HDC hDC)
  220. {
  221. REGISTER INT x;
  222. REGISTER INT y;
  223. x = dxpWindow-1;
  224. y = dypWindow-1;
  225. SetROP2(hDC, R2_COPYPEN);
  226. DrawRect(hDC, 0, 0, x, y, hPenGray);
  227. DrawRect(hDC, 1, 1, --x, --y, hPenLtGray);
  228. DrawRect(hDC, 2, 2, --x, --y, hPenLtGray);
  229. DrawRect(hDC, 3, 3, --x, --y, hPenGray);
  230. }
  231. /****** D R A W S C R E E N ******/
  232. VOID DrawScreen(HDC hDC)
  233. {
  234. DrawBorder(hDC);
  235. DrawGrid(hDC);
  236. }
  237. VOID DisplayScreen(VOID)
  238. {
  239. HDC hDC = GetDC(hwndMain);
  240. DrawScreen(hDC);
  241. ReleaseDC(hwndMain, hDC);
  242. }
  243. /****** D O F L A S H ******/
  244. VOID DoFlash(BOOL fOn)
  245. {
  246. REGISTER INT i;
  247. HDC hDC = GetDC(hwndMain);
  248. for (i = 0; i < cBlkRow; i++)
  249. rgBlk[rgFlash[i]] = fOn ? iPlayer :iBallGrey;
  250. ReDoDisplay();
  251. DrawGrid(hDC);
  252. ReleaseDC(hwndMain, hDC);
  253. }