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

游戏引擎

开发平台:

Visual C++

  1. /*
  2. ** Haaf's Game Engine 1.8
  3. ** Copyright (C) 2003-2007, Relish Games
  4. ** hge.relishgames.com
  5. **
  6. ** hge_tut03 - Using helper classes
  7. */
  8. // Copy the files "particles.png", "menu.wav",
  9. // "font1.fnt", "font1.png" and "trail.psi" from
  10. // the folder "precompiled" to the folder with
  11. // executable file. Also copy hge.dll and bass.dll
  12. // to the same folder.
  13. #include "....includehge.h"
  14. #include "....includehgesprite.h"
  15. #include "....includehgefont.h"
  16. #include "....includehgeparticle.h"
  17. // Pointer to the HGE interface.
  18. // Helper classes require this to work.
  19. HGE *hge=0;
  20. // Pointers to the HGE objects we will use
  21. hgeSprite* spr;
  22. hgeSprite* spt;
  23. hgeFont* fnt;
  24. hgeParticleSystem* par;
  25. // Handles for HGE resourcces
  26. HTEXTURE tex;
  27. HEFFECT snd;
  28. // Some "gameplay" variables
  29. float x=100.0f, y=100.0f;
  30. float dx=0.0f, dy=0.0f;
  31. const float speed=90;
  32. const float friction=0.98f;
  33. // Play sound effect
  34. void boom() {
  35. int pan=int((x-400)/4);
  36. float pitch=(dx*dx+dy*dy)*0.0005f+0.2f;
  37. hge->Effect_PlayEx(snd,100,pan,pitch);
  38. }
  39. bool FrameFunc()
  40. {
  41. float dt=hge->Timer_GetDelta();
  42. // Process keys
  43. if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true;
  44. if (hge->Input_GetKeyState(HGEK_LEFT)) dx-=speed*dt;
  45. if (hge->Input_GetKeyState(HGEK_RIGHT)) dx+=speed*dt;
  46. if (hge->Input_GetKeyState(HGEK_UP)) dy-=speed*dt;
  47. if (hge->Input_GetKeyState(HGEK_DOWN)) dy+=speed*dt;
  48. // Do some movement calculations and collision detection
  49. dx*=friction; dy*=friction; x+=dx; y+=dy;
  50. if(x>784) {x=784-(x-784);dx=-dx;boom();}
  51. if(x<16) {x=16+16-x;dx=-dx;boom();}
  52. if(y>584) {y=584-(y-584);dy=-dy;boom();}
  53. if(y<16) {y=16+16-y;dy=-dy;boom();}
  54. // Update particle system
  55. par->info.nEmission=(int)(dx*dx+dy*dy)*2;
  56. par->MoveTo(x,y);
  57. par->Update(dt);
  58. return false;
  59. }
  60. bool RenderFunc()
  61. {
  62. // Render graphics
  63. hge->Gfx_BeginScene();
  64. hge->Gfx_Clear(0);
  65. par->Render();
  66. spr->Render(x, y);
  67. fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3fnFPS:%d (constant)", hge->Timer_GetDelta(), hge->Timer_GetFPS());
  68. hge->Gfx_EndScene();
  69. return false;
  70. }
  71. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  72. {
  73. hge = hgeCreate(HGE_VERSION);
  74. hge->System_SetState(HGE_LOGFILE, "hge_tut03.log");
  75. hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
  76. hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
  77. hge->System_SetState(HGE_TITLE, "HGE Tutorial 03 - Using helper classes");
  78. hge->System_SetState(HGE_FPS, 100);
  79. hge->System_SetState(HGE_WINDOWED, true);
  80. hge->System_SetState(HGE_SCREENWIDTH, 800);
  81. hge->System_SetState(HGE_SCREENHEIGHT, 600);
  82. hge->System_SetState(HGE_SCREENBPP, 32);
  83. if(hge->System_Initiate()) {
  84. // Load sound and texture
  85. snd=hge->Effect_Load("menu.wav");
  86. tex=hge->Texture_Load("particles.png");
  87. if(!snd || !tex)
  88. {
  89. // If one of the data files is not found, display
  90. // an error message and shutdown.
  91. MessageBox(NULL, "Can't load one of the following files:nMENU.WAV, PARTICLES.PNG, FONT1.FNT, FONT1.PNG, TRAIL.PSI", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
  92. hge->System_Shutdown();
  93. hge->Release();
  94. return 0;
  95. }
  96. // Create and set up a sprite
  97. spr=new hgeSprite(tex, 96, 64, 32, 32);
  98. spr->SetColor(0xFFFFA000);
  99. spr->SetHotSpot(16,16);
  100. // Load a font
  101. fnt=new hgeFont("font1.fnt");
  102. // Create and set up a particle system
  103. spt=new hgeSprite(tex, 32, 32, 32, 32);
  104. spt->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
  105. spt->SetHotSpot(16,16);
  106. par=new hgeParticleSystem("trail.psi",spt);
  107. par->Fire();
  108. // Let's rock now!
  109. hge->System_Start();
  110. // Delete created objects and free loaded resources
  111. delete par;
  112. delete fnt;
  113. delete spt;
  114. delete spr;
  115. hge->Texture_Free(tex);
  116. hge->Effect_Free(snd);
  117. }
  118. // Clean up and shutdown
  119. hge->System_Shutdown();
  120. hge->Release();
  121. return 0;
  122. }