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.

470 lines
8.2 KiB

  1. /*******************/
  2. /* file: grafix.c */
  3. /*******************/
  4. #define _WINDOWS
  5. #include <windows.h>
  6. #include <port1632.h>
  7. #include "snake.h"
  8. #include "res.h"
  9. #include "rtns.h"
  10. #include "grafix.h"
  11. #include "blk.h"
  12. #include "sound.h"
  13. #include "pref.h"
  14. #define cbDibHeader (16*4 + sizeof(BITMAPINFOHEADER))
  15. HANDLE hresWall = NULL;
  16. LPSTR lpDibWall;
  17. HANDLE hresLvl = NULL;
  18. LPSTR lpDibLvl;
  19. HANDLE hresSnk = NULL;
  20. LPSTR lpDibSnk;
  21. HDC hdcBlk;
  22. HBITMAP hbmpBlk;
  23. HDC hdcCor;
  24. HBITMAP hbmpCor;
  25. HDC hdcNum;
  26. HBITMAP hbmpNum;
  27. HDC hdcMain; /* DC for main window */
  28. HPEN hpenGray;
  29. HBRUSH hbrushGreen;
  30. /* Score Stuff */
  31. INT rgnum[numMax];
  32. INT cMoveScore = 0; /* 0 if not moving score */
  33. INT inumMoveMac; /* first number to move ( >1 if moving several 9 -> 0) */
  34. INT score;
  35. INT scoreCurr; /* Current displayed score */
  36. #define DKGREEN 0x00007F00L
  37. #define LTGREEN 0x0000FF00L
  38. LONG coDarkGreen;
  39. LONG coLiteGreen;
  40. /*** External Data ***/
  41. extern HWND hwndMain;
  42. extern HANDLE hInst;
  43. extern BOOL fEGA;
  44. extern POS posHead;
  45. extern POS posTail;
  46. extern BLK mpposblk[posMax];
  47. extern INT cLevel;
  48. extern INT cTimeLeft;
  49. extern INT cLives;
  50. extern INT ctickMove;
  51. extern INT dypCaption;
  52. extern INT dypMenu;
  53. extern INT dypBorder;
  54. extern INT dxpBorder;
  55. extern INT dxpWindow;
  56. extern INT dypWindow;
  57. extern STATUS status;
  58. extern PREF Preferences;
  59. extern BOOL fWipe;
  60. /****** S T A R T D R A W ******/
  61. VOID StartDraw(VOID)
  62. {
  63. hdcMain = GetDC(hwndMain);
  64. }
  65. /****** E N D D R A W ******/
  66. VOID EndDraw(VOID)
  67. {
  68. ReleaseDC(hwndMain, hdcMain);
  69. }
  70. BOOL FLoadBitmaps(VOID)
  71. {
  72. hresSnk = LoadResource(hInst,
  73. FindResource(hInst, MAKEINTRESOURCE(ID_BMP_SNK+(!Preferences.fColor)), RT_BITMAP));
  74. if (hresSnk == NULL)
  75. return fFalse;
  76. lpDibSnk = LockResource(hresSnk);
  77. hbmpBlk = LoadBitmap(hInst, MAKEINTRESOURCE(ID_BMP_BLK+(!Preferences.fColor)) );
  78. SelectObject(hdcBlk, hbmpBlk);
  79. coDarkGreen = Preferences.fColor ? DKGREEN : 0x007F7F7FL;
  80. coLiteGreen = Preferences.fColor ? LTGREEN : 0x00FFFFFFL;
  81. SetLevelColor();
  82. return fTrue;
  83. }
  84. VOID FreeBitmaps(VOID)
  85. {
  86. UnlockResource(hresSnk);
  87. }
  88. /****** F I N I T L O C A L ******/
  89. BOOL FInitLocal(VOID)
  90. {
  91. hresWall = LoadResource(hInst, FindResource(hInst, MAKEINTRESOURCE(ID_BMP_WALL), RT_BITMAP));
  92. hresLvl = LoadResource(hInst, FindResource(hInst, MAKEINTRESOURCE(ID_BMP_LVL), RT_BITMAP));
  93. hdcNum = CreateCompatibleDC(NULL);
  94. hbmpNum = LoadBitmap(hInst, MAKEINTRESOURCE(ID_BMP_NUM));
  95. hdcBlk = CreateCompatibleDC(NULL);
  96. StartDraw();
  97. hdcCor = CreateCompatibleDC(NULL);
  98. hbmpCor = CreateCompatibleBitmap(hdcMain, dxpCor, dypCorMax);
  99. EndDraw();
  100. if ((hresWall == NULL) || (hresLvl == NULL) ||
  101. (hdcNum == NULL) || (hbmpNum == NULL) ||
  102. (hdcCor == NULL) || (hbmpCor == NULL) || (hdcBlk == NULL) )
  103. return fFalse;
  104. lpDibWall = LockResource(hresWall);
  105. lpDibLvl = (LPVOID) ((LPBYTE) LockResource(hresLvl) + sizeof(BITMAPINFOHEADER) + 8);
  106. SelectObject(hdcNum, hbmpNum);
  107. SelectObject(hdcCor, hbmpCor);
  108. if (fEGA)
  109. {
  110. hpenGray = CreatePen(PS_SOLID, 1, RGB(64, 64, 64));
  111. }
  112. else
  113. {
  114. hpenGray = CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
  115. }
  116. hbrushGreen = CreateSolidBrush(RGB(0,128,0));
  117. InitTunes();
  118. return (FLoadBitmaps());
  119. }
  120. /****** C L E A N U P ******/
  121. VOID CleanUp(VOID)
  122. {
  123. EndTunes();
  124. FreeBitmaps();
  125. UnlockResource(hresWall);
  126. UnlockResource(hresLvl);
  127. DeleteDC(hdcBlk);
  128. DeleteDC(hdcCor);
  129. DeleteDC(hdcNum);
  130. }
  131. /*======================== Snakes/Lives ================================*/
  132. VOID DrawLives(VOID)
  133. {
  134. INT x = dxpLifeOff;
  135. HBRUSH hbrush;
  136. INT i;
  137. SetROP2(hdcMain, R2_BLACK); /* ???? */
  138. hbrush = SelectObject(hdcMain, hbrushGreen);
  139. for (i = 0; i++ < cLives;)
  140. {
  141. SetDIBitsToDevice(hdcMain, x, dypLifeOff, dxpLife, dypLife,
  142. 0, 0, 0, dypLife,
  143. lpDibSnk + (Preferences.fColor ? cbDibHeader : (2*4 + sizeof(BITMAPINFOHEADER))),
  144. (LPBITMAPINFO) lpDibSnk, DIB_RGB_COLORS);
  145. x += dxpLifeSpace;
  146. }
  147. SelectObject(hdcMain, GetStockObject(LTGRAY_BRUSH));
  148. for (i=cLives; i++ < cLivesMax;)
  149. {
  150. PatBlt(hdcMain, x, dypLifeOff, dxpLife, dypLife, PATCOPY);
  151. x += dxpLifeSpace;
  152. }
  153. SelectObject(hdcMain, hbrush);
  154. }
  155. VOID DisplayLives(VOID)
  156. {
  157. StartDraw();
  158. DrawLives();
  159. EndDraw();
  160. }
  161. /*============================ SCORE ROUTINES ============================*/
  162. /****** D R A W N U M ******/
  163. VOID DrawNum(INT x, INT num)
  164. {
  165. BitBlt(hdcMain, x, dypNumOff, dxpNum, dypNum-1, hdcNum, 0, num, SRCCOPY);
  166. }
  167. /****** D R A W S C O R E ******/
  168. VOID DrawScore(VOID)
  169. {
  170. INT x;
  171. INT inum;
  172. x = dxpNumOff;
  173. for (inum = numMax; inum-- > 0;)
  174. {
  175. DrawNum(x, rgnum[inum]);
  176. x += dxpSpaceNum;
  177. }
  178. SetROP2(hdcMain, R2_BLACK); /* ???? */
  179. x = dxpNumOff-1;
  180. for (inum = numMax+1; inum-- > 0;)
  181. {
  182. MMoveTo(hdcMain, x, dypNumOff-1);
  183. LineTo(hdcMain, x, dypNumOff+dypNum);
  184. x += dxpSpaceNum;
  185. }
  186. MMoveTo(hdcMain, dxpNumOff-1, dypNumOff-1);
  187. LineTo(hdcMain, dxpNumOff+dxpSpaceNum*numMax, dypNumOff-1);
  188. MMoveTo(hdcMain, dxpNumOff-1, dypNumOff+dypNum-1);
  189. LineTo(hdcMain, dxpNumOff+dxpSpaceNum*numMax, dypNumOff+dypNum-1);
  190. }
  191. VOID DisplayScore(VOID)
  192. {
  193. StartDraw();
  194. DrawScore();
  195. EndDraw();
  196. }
  197. /****** I N C S C O R E ******/
  198. VOID IncScore(VOID)
  199. {
  200. cMoveScore = dypNum/2 + 1;
  201. inumMoveMac = 0;
  202. while ((rgnum[inumMoveMac++] == 9*dypNum) && (inumMoveMac < numMax))
  203. ;
  204. if ((++scoreCurr % 100) == 0)
  205. if (cLives < cLivesMax)
  206. {
  207. cLives++;
  208. DisplayLives();
  209. }
  210. }
  211. /****** M O V E S C O R E ******/
  212. VOID MoveScore(VOID)
  213. {
  214. REGISTER INT x = dxpNumOff + dxpNumMax;
  215. REGISTER dy = 2;
  216. INT inum;
  217. #if 0 /* More efficient to do in "DoTimer" */
  218. if (cMoveScore == 0)
  219. return;
  220. #endif
  221. if (cMoveScore == 1)
  222. dy = 1;
  223. StartDraw();
  224. for (inum = 0; inum < inumMoveMac; inum++)
  225. #if 1
  226. DrawNum(x -= dxpSpaceNum, rgnum[inum] += dy);
  227. #else
  228. DrawNum(x -= dxpSpaceNum, ++(rgnum[inum]));
  229. #endif
  230. EndDraw();
  231. if (--cMoveScore == 0)
  232. {
  233. inum = 0;
  234. while (rgnum[inum] >= 10*dypNum) /* Reset number to proper zero */
  235. rgnum[inum++] = 0;
  236. if (scoreCurr != score)
  237. IncScore();
  238. }
  239. }
  240. /****** A D D S C O R E ******/
  241. VOID AddScore(INT n)
  242. {
  243. score += n;
  244. if (score > 9999)
  245. ResetScore();
  246. if (cMoveScore == 0)
  247. IncScore();
  248. }
  249. /****** R E S E T S C O R E ******/
  250. VOID ResetScore(VOID)
  251. {
  252. REGISTER INT i;
  253. scoreCurr = score = cMoveScore = 0;
  254. for (i = numMax; i-- > 0;)
  255. rgnum[i] = 0;
  256. }
  257. /*======================== TIMER ================================*/
  258. VOID DrawTime(VOID)
  259. {
  260. HBRUSH hbrush;
  261. hbrush = SelectObject(hdcMain, Preferences.fColor ? hbrushGreen : GetStockObject(GRAY_BRUSH));
  262. PatBlt(hdcMain, dxpTimeOff, dypTimeOff, cTimeLeft, dypTime, PATCOPY);
  263. PatBlt(hdcMain, dxpTimeOff+cTimeLeft, dypTimeOff, dxpTime-cTimeLeft, dypTime, BLACKNESS);
  264. SelectObject(hdcMain, hbrush);
  265. }
  266. VOID DisplayTime(VOID)
  267. {
  268. StartDraw();
  269. DrawTime();
  270. EndDraw();
  271. }
  272. VOID UpdateTime(VOID)
  273. {
  274. /* HBRUSH hbrush; */
  275. StartDraw();
  276. PatBlt(hdcMain, dxpTimeOff+cTimeLeft, dypTimeOff, 1, dypTime, BLACKNESS);
  277. EndDraw();
  278. }
  279. /*======================== Border stuff ================================*/
  280. /****** S E T T H E P E N ******/
  281. VOID SetThePen(BOOL fNormal)
  282. {
  283. if (fNormal)
  284. SetROP2(hdcMain, R2_WHITE);
  285. else
  286. {
  287. SetROP2(hdcMain, R2_COPYPEN);
  288. SelectObject(hdcMain, hpenGray);
  289. }
  290. }
  291. /****** D R A W B O R D E R ******/
  292. /* 0 - white, gray
  293. 1 - gray, white
  294. */
  295. VOID DrawBorder(INT x1, INT y1, INT x2, INT y2, INT width, BOOL fNormal)
  296. {
  297. INT i = 0;
  298. SetThePen(fNormal);
  299. while (i++ < width)
  300. {
  301. MMoveTo(hdcMain, x1, --y2);
  302. LineTo(hdcMain, x1++, y1);
  303. LineTo(hdcMain, x2--, y1++);
  304. }
  305. SetThePen(!fNormal);
  306. while (--i)
  307. {
  308. MMoveTo(hdcMain, x1--, ++y2);
  309. LineTo(hdcMain, ++x2, y2);
  310. LineTo(hdcMain, x2, --y1);
  311. }
  312. }
  313. VOID DrawBackground(VOID)
  314. {
  315. /* Main Raised Border */
  316. /* int x, y; */
  317. /* DrawBorder(0, 0, x = dxpWindow, y = dypWindow, 3, fTrue); */
  318. DrawBorder(dxpNumOff-2, dypNumOff-2,
  319. dxpNumOff + dxpSpaceNum*numMax, dypNumOff+dypNum, 1, fFalse);
  320. DrawBorder(dxpTimeOff-2, dypTimeOff-2,
  321. dxpTimeOff + dxpTime+1, dypTimeOff+dypTime+1, 2, fTrue);
  322. }
  323. /*======================== ? ? ? ================================*/
  324. /****** D R A W S C R E E N ******/
  325. VOID DrawScreen(HDC hdc)
  326. {
  327. hdcMain = hdc;
  328. if (fWipe)
  329. ReWipe();
  330. DrawBackground();
  331. DrawLives();
  332. DrawScore();
  333. if (!fWipe)
  334. DrawGrid();
  335. DrawTime();
  336. }
  337. VOID DisplayScreen(VOID)
  338. {
  339. StartDraw();
  340. DrawScreen(hdcMain);
  341. EndDraw();
  342. }