参考代码.cpp
上传用户:may_xy
上传日期:2007-08-09
资源大小:1519k
文件大小:13k
源码类别:

游戏

开发平台:

C/C++

  1. //从位图载入到表面的代码;
  2. /*
  3. if (!pMy_Draw->Load_Bitmap_File(&pMy_Draw->bitmap,"desk.bmp"))
  4. {
  5.     return(0);
  6. }
  7. // lock the surface
  8. lpddsbackground->Lock(NULL,&pMy_Draw->ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
  9. // get video pointer to primary surfce
  10.        
  11. DWORD *image_buffer = (DWORD *)pMy_Draw->ddsd.lpSurface;
  12. // process each line and copy it into the primary buffer
  13. for (int index_y = 0; index_y < Screen_Height; index_y++)
  14.     {
  15. for (int index_x = 0; index_x < Screen_Width; index_x++)
  16.         {
  17.         // get BGR values
  18.         UCHAR blue  = (pMy_Draw->bitmap.buffer[index_y*Screen_Width*3 + index_x*3 + 0]),
  19.               green = (pMy_Draw->bitmap.buffer[index_y*Screen_Width*3 + index_x*3 + 1]),
  20.               red   = (pMy_Draw->bitmap.buffer[index_y*Screen_Width*3 + index_x*3 + 2]);
  21.         // this builds a 32 bit color value in A.8.8.8 format (8-bit alpha mode)
  22.         DWORD pixel = _RGB32BIT(0,red,green,blue);
  23.         // write the pixel
  24.         image_buffer[index_x + (index_y*pMy_Draw->ddsd.lPitch >> 2)] = pixel;
  25.         } // end for index_x
  26.     } // end for index_y
  27. // now unlock the primary surface
  28. if (FAILED(lpddsbackground->Unlock(NULL)))
  29. {
  30. return(0);
  31. }
  32. // unload the bitmap file, we no longer need it
  33. pMy_Draw->Unload_Bitmap_File(&pMy_Draw->bitmap);
  34. */
  35. ///////////////////////////////////////////////////////////////////////////////////
  36. //渲染一个表面!COOL!
  37. /*  渲染的例子;可以改变画像颜色;
  38. DDRAW_INIT_STRUCT(ddsd); 
  39. // lock the primary surface
  40. if (FAILED(lpddsbackground->Lock(NULL, &ddsd,
  41.                     DDLOCK_WAIT,
  42.                    NULL)))
  43.    return(0);
  44. int lpitch32 = (int)(ddsd.lPitch >> 2);
  45. UINT *video_buffer = (UINT *)ddsd.lpSurface;
  46. for (int index_y = 0; index_y < SCREEN_HEIGHT; index_y++)
  47.     {
  48. for (int index_x = 0; index_x < SCREEN_WIDTH; index_x++)
  49.         {
  50.         // get BGR values
  51. //        UCHAR blue  = (UCHAR)(video_buffer[index_x + (index_y*ddsd.lPitch >> 2)]) ,
  52. //              green = (UCHAR)(video_buffer[index_x + (index_y*ddsd.lPitch >> 2)] >> 8),
  53. //              red   = (UCHAR)(video_buffer[index_x + (index_y*ddsd.lPitch >> 2)] >> 16);
  54.         // this builds a 32 bit color value in A.8.8.8 format (8-bit alpha mode)
  55. // green+=40;
  56. // blue-=30;
  57. //        DWORD pixel = _RGB32BIT(0,red,green,blue);
  58.         // write the pixel
  59. //        video_buffer[index_x + (index_y*ddsd.lPitch >> 2)] = pixel;
  60. video_buffer[index_x + (index_y*ddsd.lPitch >> 2)] += 0x3000;
  61.         } // end for index_x
  62.     } // end for index_y      
  63. if (FAILED(lpddsbackground->Unlock(NULL)))
  64.    return(0);
  65. */
  66. // char buffer[256];   //记录键盘的缓冲区;
  67. // HRESULT result = pDI_Keyborad->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);  
  68. //    if(result != DI_OK)
  69. // {
  70. // AfxMessageBox("取得键盘状态失败!");
  71. // }
  72. ///////////////////////////////////////////////////////////
  73. //牌的布局代码;
  74. /*
  75. //中间的牌;
  76. if(person.counter < 20)
  77. {
  78. //中间已出的牌;
  79. DDraw_Draw_Surface(person.frames[0], 
  80.                        centerx1, centery1,
  81.                        person.width,person.height,
  82.                        lpddsback);
  83. //中间没出的牌;
  84. if(counter%3 == 0)
  85. {
  86. DDraw_Draw_Surface(person.frames[0], 
  87.                        centerx, centery-20,
  88.                        person.width,person.height,
  89.                        lpddsback);
  90. }
  91. else
  92. {
  93. DDraw_Draw_Surface(person.frames[0], 
  94.                        centerx, centery,
  95.                        person.width,person.height,
  96.                        lpddsback);
  97. }
  98. centerx+=22;
  99. centerx1+=16;
  100. person.counter++;
  101. }
  102. //左边的牌;
  103. if(counter < 20 )
  104. {
  105. DDraw_Draw_Surface(person.frames[1], 
  106.                        leftx, lefty,
  107.                        person.width,person.height,
  108.                        lpddsback);
  109. //左边已出的牌;
  110. DDraw_Draw_Surface(person.frames[0], 
  111.                        leftx1, lefty1,
  112.                        person.width,person.height,
  113.                        lpddsback);
  114. lefty+=16;
  115. lefty1+=16;
  116. }
  117. //右边的牌;
  118. if(counter < 20 )
  119. {
  120. DDraw_Draw_Surface(person.frames[1], 
  121.                        rightx, righty,
  122.                        person.width,person.height,
  123.                        lpddsback);
  124. //已
  125. DDraw_Draw_Surface(person.frames[0], 
  126.                        rightx1, righty1,
  127.                        person.width,person.height,
  128.                        lpddsback);
  129. righty1+=16;
  130. righty+=16;
  131. counter++;
  132. }
  133. //左边刚出的牌
  134. if(counter2<12)
  135. {
  136. DDraw_Draw_Surface(person.frames[0], 
  137.                        leftx2, lefty2,
  138.                        person.width,person.height,
  139.                        lpddsback);
  140. lefty2+=25;
  141. //右边刚出的牌
  142. DDraw_Draw_Surface(person.frames[0], 
  143.                        rightx2, righty2,
  144.                        person.width,person.height,
  145.                        lpddsback);
  146. righty2+=25;
  147. //中间刚出的牌
  148. DDraw_Draw_Surface(person.frames[0], 
  149.                        centerx2, centery2,
  150.                        person.width,person.height,
  151.                        lpddsback);
  152. centerx2+=25;
  153. counter2++;
  154. }
  155. */
  156. int Dealing_Cards_Center(int Current_x,
  157. int Card_Color,
  158. int Card_Value
  159. );
  160. int Dealing_Cards_Left(int Current_y);
  161. int Dealing_Cards_Right(int Current_y);
  162. //发中间的牌;
  163. int CDraw_Cards_Engine::Dealing_Cards_Center(int Current_x,int Card_Color,int Card_Value)
  164. {
  165. int dx,dy,dx2,dy2,x_inc,y_inc,error;
  166. int x = Card_x,y = Card_y;
  167. dx = Current_x - Card_x;
  168. dy = Center_y0 - Card_y;
  169. if(dx>=0)
  170. {
  171. x_inc = 1;
  172. }
  173. else
  174. {
  175. x_inc = -1;
  176. dx = -dx;
  177. }
  178. if(dy >= 0)
  179. {
  180. y_inc = 1;
  181. }
  182. else
  183. {
  184. y_inc = -1;
  185. dy = -dy;
  186. }
  187. dx2 = dx << 1;
  188. dy2 = dy << 1;
  189. //这里只考虑dy > dx;
  190. error = dx2 - dy;
  191. for(int i=0;i<int(dy);i++)
  192. {
  193. if(i%80 == 0)
  194. {
  195. Redraw_Game();
  196. Draw_A_Card(x,y,Card_Color,Card_Value,lpddsback);
  197. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  198. }
  199. if(error >= 0)
  200. {
  201. error-=dy2;
  202. x+=x_inc;
  203. }
  204. error+=dx2;
  205. y+=y_inc;
  206. }//end for~
  207. Redraw_Game();
  208. Draw_A_Card(x,y,Card_Color,Card_Value,lpddsback);
  209. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  210. return 1;
  211. }
  212. //发左边的牌;
  213. int CDraw_Cards_Engine::Dealing_Cards_Left(int Current_y)
  214. {
  215. int dx,dy,dx2,dy2,x_inc,y_inc,error;
  216. int x = Card_x,y = Card_y;
  217. dx = Left_x0 - Card_x;
  218. dy = Current_y - Card_y;
  219. if(dx>=0)
  220. {
  221. x_inc = 1;
  222. }
  223. else
  224. {
  225. x_inc = -1;
  226. dx = -dx;
  227. }
  228. if(dy >= 0)
  229. {
  230. y_inc = 1;
  231. }
  232. else
  233. {
  234. y_inc = -1;
  235. dy = -dy;
  236. }
  237. dx2 = dx << 1;
  238. dy2 = dy << 1;
  239. if(dx > dy)
  240. {
  241. error = dy2 - dx;
  242. for(int i=0;i<int(dx);i++)
  243. {
  244. if(i%140 == 0)
  245. {
  246. Redraw_Game();
  247. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Cards[14], 
  248. x, y,
  249.                        Cards_Width,Cards_Height,
  250.                        lpddsback,1);
  251. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  252. }
  253. if(error >= 0)
  254. {
  255. error-=dx2;
  256. y+=y_inc;
  257. }
  258. error+=dy2;
  259. x+=x_inc;
  260. }//end for~
  261. }
  262. else
  263. {
  264. error = dx2 - dy;
  265. for(int i=0;i<int(dy);i++)
  266. {
  267. if(i%140 == 0)
  268. {
  269. Redraw_Game();
  270. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Cards[14], 
  271. x, y,
  272.                        Cards_Width,Cards_Height,
  273.                        lpddsback,1);
  274. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  275. }
  276. if(error >= 0)
  277. {
  278. error-=dy2;
  279. x+=x_inc;
  280. }
  281. error+=dx2;
  282. y+=y_inc;
  283. }//end for~
  284. }
  285. Redraw_Game();
  286. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Cards[14], 
  287. x, y,
  288.                       Cards_Width,Cards_Height,
  289.                        lpddsback,1);
  290. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  291. return 1;
  292. }
  293. int CDraw_Cards_Engine::Dealing_Cards_Right(int Current_y)
  294. {
  295. int dx,dy,dx2,dy2,x_inc,y_inc,error;
  296. int x = Card_x,y = Card_y;
  297. dx = Right_x0 - Card_x;
  298. dy = Current_y - Card_y;
  299. if(dx>=0)
  300. {
  301. x_inc = 1;
  302. }
  303. else
  304. {
  305. x_inc = -1;
  306. dx = -dx;
  307. }
  308. if(dy >= 0)
  309. {
  310. y_inc = 1;
  311. }
  312. else
  313. {
  314. y_inc = -1;
  315. dy = -dy;
  316. }
  317. dx2 = dx << 1;
  318. dy2 = dy << 1;
  319. if(dx > dy)
  320. {
  321. error = dy2 - dx;
  322. for(int i=0;i<int(dx);i++)
  323. {
  324. if(i%140 == 0)
  325. {
  326. Redraw_Game();
  327. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Cards[14], 
  328. x, y,
  329.                        Cards_Width,Cards_Height,
  330.                        lpddsback,1);
  331. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  332. }
  333. if(error >= 0)
  334. {
  335. error-=dx2;
  336. y+=y_inc;
  337. }
  338. error+=dy2;
  339. x+=x_inc;
  340. }//end for~
  341. }
  342. else
  343. {
  344. error = dx2 - dy;
  345. for(int i=0;i<int(dy);i++)
  346. {
  347. if(i%140 == 0)
  348. {
  349. Redraw_Game();
  350. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Cards[14], 
  351. x, y,
  352.                        Cards_Width,Cards_Height,
  353.                        lpddsback,1);
  354. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  355. }
  356. if(error >= 0)
  357. {
  358. error-=dy2;
  359. x+=x_inc;
  360. }
  361. error+=dx2;
  362. y+=y_inc;
  363. }//end for~
  364. }
  365. Redraw_Game();
  366. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Cards[14], 
  367. x, y,
  368.                       Cards_Width,Cards_Height,
  369.                        lpddsback,1);
  370. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  371. return 1;
  372. }
  373. //CGame
  374. // pCDraw_Cards->Draw_A_Card(Left_x0,
  375. //   pGame_Map->Left0[counter].y,
  376. //   0,
  377. //   17,
  378. //   lpddsbg_Dealing);
  379. // pMy_Draw->DDraw_Draw_Surface(lpddsbg_Dealing,0,0, Screen_Width,Screen_Height, lpddsback,0); 
  380. // pCDraw_Cards->Draw_A_Card(pGame_Map->Center0[counter].x,
  381. //   Center_y0,
  382. //   pMy_Cards->Cards[counter].Color,
  383. //   pMy_Cards->Cards[counter].Value,
  384. //   lpddsbg_Dealing);
  385. // pMy_Draw->DDraw_Draw_Surface(lpddsbg_Dealing,0,0, Screen_Width,Screen_Height, lpddsback,0); 
  386. // pCDraw_Cards->Draw_A_Card(Right_x0,
  387. //   pGame_Map->Left0[counter].y,
  388. //   0,
  389. //   17,
  390. //   lpddsbg_Dealing);
  391. // pCDraw_Cards->Dealing_Cards_Center(Center_x0 + counter*Card_Distance1,
  392. //     Lord_Cards.Cards[counter].Color,Lord_Cards.Cards[counter].Value);
  393. // pCDraw_Cards->Dealing_Cards_Center(Center_x0 + counter*Card_Distance1,
  394. //    Lord_Cards.Cards[counter+1].Color,Lord_Cards.Cards[counter].Value);
  395. // pCDraw_Cards->Dealing_Cards_Center(Center_x0 + counter*Card_Distance1,
  396. //     Lord_Cards.Cards[counter+2].Color,Lord_Cards.Cards[counter].Value);
  397. // pCDraw_Cards->Dealing_Cards_Up(Lord_Card1_x);
  398. // pCDraw_Cards->Dealing_Cards_Up(Lord_Card2_x);
  399. // pCDraw_Cards->Dealing_Cards_Up(Lord_Card3_x);
  400. int CGame::Clean_Up_Cards()
  401. {
  402. // pLeft_Cards  = new CPlaying_Cards;    //左边玩家的牌对象;
  403. // pMy_Cards    = new CPlaying_Cards;   //主机玩家的牌对象;
  404. // pRight_Cards = new CPlaying_Cards;   //右边玩家的牌对象;
  405. // ::Sleep(10000);
  406. for(int k=0;k<17;k++)
  407. {
  408. lpddsbg_Dealing->Blt(
  409. CRect(Center_x0 + k*Card_Distance1,
  410.   Center_y0,
  411.   Center_x0 + k*Card_Distance1 + Cards_Width,
  412.   Center_y0 + Cards_Height),      //目标区域;
  413. lpddsbg_Cards[pMy_Cards->Cards[k].Value - 3],  //源表面;
  414. CRect(pMy_Cards->Cards[k].Color*Cards_Width,
  415.   0,
  416.   pMy_Cards->Cards[k].Color*Cards_Width + Cards_Width,
  417.   Cards_Height),   //源区域;
  418. DDBLT_WAIT | DDBLT_KEYSRC,
  419. NULL);
  420. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Dealing,0,0, Screen_Width,Screen_Height, lpddsback,0); 
  421. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  422. }
  423. return 1;
  424. }
  425. //其他精灵;
  426. //角花
  427. /*
  428. file = "bmp\item\left_top.bmp" ;
  429. lpddsbg_Person[0] = pMy_Draw->DDraw_Create_Surface(15,15,0,blue);
  430. pMy_Draw->BltBitmap_to_Surface(lpddsbg_Person[0],0,0,file,15,15);
  431. //角花贴到角上;
  432. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Person[0], 
  433.                        0, 0,
  434.                        15,15,
  435.                        lpddsbg_Game,1);
  436. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Person[0], 
  437.                        1024-15, 0,
  438.                        15,15,
  439.                        lpddsbg_Game,1);
  440. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Person[0], 
  441.                        0, 768-15,
  442.                        15,15,
  443.                        lpddsbg_Game,1);
  444. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Person[0], 
  445.                        1024-15, 768-15,
  446.                        15,15,
  447.                        lpddsbg_Game,1);
  448. //地主牌边框;
  449. file = "bmp\item\lord2.bmp" ;
  450. pMy_Draw->BltBitmap_to_Surface(lpddsbg_Game,
  451.    Lord_Card1_x - 11,
  452.    Lord_Card1_y - 9,
  453.    file,
  454.    288,
  455.    132);
  456. pMy_Draw->DDraw_Draw_Surface(lpddsbg_Game,0,0, Screen_Width,Screen_Height,lpddsback,0); 
  457. while (FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
  458. Sleep(4000);
  459. */