hge_tut07.cpp
上传用户:jnfxsk
上传日期:2022-06-16
资源大小:3675k
文件大小:5k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.8
  3. ** Copyright (C) 2003-2007, Relish Games
  4. ** hge.relishgames.com
  5. **
  6. ** hge_tut07 - Thousand of Hares
  7. */
  8. // Copy the files "font2.fnt", "font2.png", "bg2.png"
  9. // and "zazaka.png" from the folder "precompiled" to
  10. // the folder with executable file. Also copy hge.dll
  11. // to the same folder.
  12. #include "....includehge.h"
  13. #include "....includehgefont.h"
  14. #define SCREEN_WIDTH  800
  15. #define SCREEN_HEIGHT 600
  16. #define MIN_OBJECTS 100
  17. #define MAX_OBJECTS 2000
  18. struct sprObject
  19. {
  20. float x,y;
  21. float dx,dy;
  22. float scale,rot;
  23. float dscale,drot;
  24. DWORD color;
  25. };
  26. sprObject* pObjects;
  27. int nObjects;
  28. int nBlend;
  29. // Pointer to the HGE interface (helper classes require this to work)
  30. HGE *hge=0;
  31. // Resource handles
  32. HTEXTURE tex, bgtex;
  33. hgeSprite *spr, *bgspr;
  34. hgeFont *fnt;
  35. // Set up blending mode for the scene
  36. void SetBlend(int blend)
  37. {
  38. static int sprBlend[5]=
  39. {
  40. BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE,
  41. BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE,
  42. BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE,
  43. BLEND_COLORMUL | BLEND_ALPHAADD   | BLEND_NOZWRITE,
  44. BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE
  45. };
  46. static DWORD fntColor[5]=
  47. {
  48. 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF
  49. };
  50. static DWORD sprColors[5][5]=
  51. {
  52. { 0xFFFFFFFF, 0xFFFFE080, 0xFF80A0FF, 0xFFA0FF80, 0xFFFF80A0 },
  53. { 0xFF000000, 0xFF303000, 0xFF000060, 0xFF006000, 0xFF600000 },
  54. { 0x80FFFFFF, 0x80FFE080, 0x8080A0FF, 0x80A0FF80, 0x80FF80A0 },
  55. { 0x80FFFFFF, 0x80FFE080, 0x8080A0FF, 0x80A0FF80, 0x80FF80A0 },
  56. { 0x40202020, 0x40302010, 0x40102030, 0x40203010, 0x40102030 }
  57. };
  58. if(blend>4) blend=0;
  59. nBlend=blend;
  60. spr->SetBlendMode(sprBlend[blend]);
  61. fnt->SetColor(fntColor[blend]);
  62. for(int i=0;i<MAX_OBJECTS;i++) pObjects[i].color=sprColors[blend][hge->Random_Int(0,4)];
  63. }
  64. bool FrameFunc()
  65. {
  66. float dt=hge->Timer_GetDelta();
  67. int i;
  68. // Process keys
  69. switch(hge->Input_GetKey())
  70. {
  71. case HGEK_UP: if(nObjects<MAX_OBJECTS) nObjects+=100; break;
  72. case HGEK_DOWN: if(nObjects>MIN_OBJECTS) nObjects-=100; break;
  73. case HGEK_SPACE: SetBlend(++nBlend); break;
  74. case HGEK_ESCAPE: return true;
  75. }
  76. // Update the scene
  77. for(i=0;i<nObjects;i++)
  78. {
  79. pObjects[i].x+=pObjects[i].dx*dt;
  80. if(pObjects[i].x>SCREEN_WIDTH || pObjects[i].x<0) pObjects[i].dx=-pObjects[i].dx;
  81. pObjects[i].y+=pObjects[i].dy*dt;
  82. if(pObjects[i].y>SCREEN_HEIGHT || pObjects[i].y<0) pObjects[i].dy=-pObjects[i].dy;
  83. pObjects[i].scale+=pObjects[i].dscale*dt;
  84. if(pObjects[i].scale>2 || pObjects[i].scale<0.5) pObjects[i].dscale=-pObjects[i].dscale;
  85. pObjects[i].rot+=pObjects[i].drot*dt;
  86. }
  87. return false;
  88. }
  89. bool RenderFunc()
  90. {
  91. int i;
  92. // Render the scene
  93. hge->Gfx_BeginScene();
  94. bgspr->Render(0,0);
  95. for(i=0;i<nObjects;i++)
  96. {
  97. spr->SetColor(pObjects[i].color); 
  98. spr->RenderEx(pObjects[i].x, pObjects[i].y, pObjects[i].rot, pObjects[i].scale);
  99. }
  100. fnt->printf(7, 7, HGETEXT_LEFT, "UP and DOWN to adjust number of hares: %dnSPACE to change blending mode: %dnFPS: %d", nObjects, nBlend, hge->Timer_GetFPS());
  101. hge->Gfx_EndScene();
  102. return false;
  103. }
  104. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  105. {
  106. int i;
  107. hge = hgeCreate(HGE_VERSION);
  108. // Set desired system states and initialize HGE
  109. hge->System_SetState(HGE_LOGFILE, "hge_tut07.log");
  110. hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
  111. hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
  112. hge->System_SetState(HGE_TITLE, "HGE Tutorial 07 - Thousand of Hares");
  113. hge->System_SetState(HGE_USESOUND, false);
  114. hge->System_SetState(HGE_WINDOWED, true);
  115. hge->System_SetState(HGE_SCREENWIDTH, SCREEN_WIDTH);
  116. hge->System_SetState(HGE_SCREENHEIGHT, SCREEN_HEIGHT);
  117. hge->System_SetState(HGE_SCREENBPP, 32);
  118. if(hge->System_Initiate())
  119. {
  120. // Load textures
  121. bgtex=hge->Texture_Load("bg2.png");
  122. tex=hge->Texture_Load("zazaka.png");
  123. if(!bgtex || !tex)
  124. {
  125. // If one of the data files is not found,
  126. // display an error message and shutdown
  127. MessageBox(NULL, "Can't load BG2.PNG or ZAZAKA.PNG", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
  128. hge->System_Shutdown();
  129. hge->Release();
  130. return 0;
  131. }
  132. // Load font, create sprites
  133. fnt=new hgeFont("font2.fnt");
  134. spr=new hgeSprite(tex,0,0,64,64);
  135. spr->SetHotSpot(32,32);
  136. bgspr=new hgeSprite(bgtex,0,0,800,600);
  137. bgspr->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE);
  138. bgspr->SetColor(0xFF000000,0);
  139. bgspr->SetColor(0xFF000000,1);
  140. bgspr->SetColor(0xFF000040,2);
  141. bgspr->SetColor(0xFF000040,3);
  142. // Initialize objects list
  143. pObjects=new sprObject[MAX_OBJECTS];
  144. nObjects=1000;
  145. for(i=0;i<MAX_OBJECTS;i++)
  146. {
  147. pObjects[i].x=hge->Random_Float(0,SCREEN_WIDTH);
  148. pObjects[i].y=hge->Random_Float(0,SCREEN_HEIGHT);
  149. pObjects[i].dx=hge->Random_Float(-200,200);
  150. pObjects[i].dy=hge->Random_Float(-200,200);
  151. pObjects[i].scale=hge->Random_Float(0.5f,2.0f);
  152. pObjects[i].dscale=hge->Random_Float(-1.0f,1.0f);
  153. pObjects[i].rot=hge->Random_Float(0,M_PI*2);
  154. pObjects[i].drot=hge->Random_Float(-1.0f,1.0f);
  155. }
  156. SetBlend(0);
  157. // Let's rock now!
  158. hge->System_Start();
  159. // Delete created objects and free loaded resources
  160. delete[] pObjects;
  161. delete fnt;
  162. delete spr;
  163. delete bgspr;
  164. hge->Texture_Free(tex);
  165. hge->Texture_Free(bgtex);
  166. }
  167. // Clean up and shutdown
  168. hge->System_Shutdown();
  169. hge->Release();
  170. return 0;
  171. }