Game.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "Game.h"
  9. #include <algorithm>
  10. #include "Renderer.h"
  11. #include "Actor.h"
  12. #include "SpriteComponent.h"
  13. #include "MeshComponent.h"
  14. #include "CameraActor.h"
  15. #include "PlaneActor.h"
  16. Game::Game()
  17. :mRenderer(nullptr)
  18. ,mIsRunning(true)
  19. ,mUpdatingActors(false)
  20. {
  21. }
  22. bool Game::Initialize()
  23. {
  24. if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) != 0)
  25. {
  26. SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
  27. return false;
  28. }
  29. // Create the renderer
  30. mRenderer = new Renderer(this);
  31. if (!mRenderer->Initialize(1024.0f, 768.0f))
  32. {
  33. SDL_Log("Failed to initialize renderer");
  34. delete mRenderer;
  35. mRenderer = nullptr;
  36. return false;
  37. }
  38. LoadData();
  39. mTicksCount = SDL_GetTicks();
  40. return true;
  41. }
  42. void Game::RunLoop()
  43. {
  44. while (mIsRunning)
  45. {
  46. ProcessInput();
  47. UpdateGame();
  48. GenerateOutput();
  49. }
  50. }
  51. void Game::ProcessInput()
  52. {
  53. SDL_Event event;
  54. while (SDL_PollEvent(&event))
  55. {
  56. switch (event.type)
  57. {
  58. case SDL_QUIT:
  59. mIsRunning = false;
  60. break;
  61. }
  62. }
  63. const Uint8* state = SDL_GetKeyboardState(NULL);
  64. if (state[SDL_SCANCODE_ESCAPE])
  65. {
  66. mIsRunning = false;
  67. }
  68. for (auto actor : mActors)
  69. {
  70. actor->ProcessInput(state);
  71. }
  72. }
  73. void Game::UpdateGame()
  74. {
  75. // Compute delta time
  76. // Wait until 16ms has elapsed since last frame
  77. while (!SDL_TICKS_PASSED(SDL_GetTicks(), mTicksCount + 16))
  78. ;
  79. float deltaTime = (SDL_GetTicks() - mTicksCount) / 1000.0f;
  80. if (deltaTime > 0.05f)
  81. {
  82. deltaTime = 0.05f;
  83. }
  84. mTicksCount = SDL_GetTicks();
  85. // Update all actors
  86. mUpdatingActors = true;
  87. for (auto actor : mActors)
  88. {
  89. actor->Update(deltaTime);
  90. }
  91. mUpdatingActors = false;
  92. // Move any pending actors to mActors
  93. for (auto pending : mPendingActors)
  94. {
  95. pending->ComputeWorldTransform();
  96. mActors.emplace_back(pending);
  97. }
  98. mPendingActors.clear();
  99. // Add any dead actors to a temp vector
  100. std::vector<Actor*> deadActors;
  101. for (auto actor : mActors)
  102. {
  103. if (actor->GetState() == Actor::EDead)
  104. {
  105. deadActors.emplace_back(actor);
  106. }
  107. }
  108. // Delete dead actors (which removes them from mActors)
  109. for (auto actor : deadActors)
  110. {
  111. delete actor;
  112. }
  113. }
  114. void Game::GenerateOutput()
  115. {
  116. mRenderer->Draw();
  117. }
  118. void Game::LoadData()
  119. {
  120. // Create actors
  121. Actor* a = new Actor(this);
  122. a->SetPosition(Vector3(200.0f, 75.0f, 0.0f));
  123. a->SetScale(100.0f);
  124. Quaternion q(Vector3::UnitY, -Math::PiOver2);
  125. q = Quaternion::Concatenate(q, Quaternion(Vector3::UnitZ, Math::Pi + Math::Pi / 4.0f));
  126. a->SetRotation(q);
  127. MeshComponent* mc = new MeshComponent(a);
  128. mc->SetMesh(mRenderer->GetMesh("Assets/Cube.gpmesh"));
  129. a = new Actor(this);
  130. a->SetPosition(Vector3(200.0f, -75.0f, 0.0f));
  131. a->SetScale(3.0f);
  132. mc = new MeshComponent(a);
  133. mc->SetMesh(mRenderer->GetMesh("Assets/Sphere.gpmesh"));
  134. // Setup floor
  135. const float start = -1250.0f;
  136. const float size = 250.0f;
  137. for (int i = 0; i < 10; i++)
  138. {
  139. for (int j = 0; j < 10; j++)
  140. {
  141. a = new PlaneActor(this);
  142. a->SetPosition(Vector3(start + i * size, start + j * size, -100.0f));
  143. }
  144. }
  145. // Left/right walls
  146. q = Quaternion(Vector3::UnitX, Math::PiOver2);
  147. for (int i = 0; i < 10; i++)
  148. {
  149. a = new PlaneActor(this);
  150. a->SetPosition(Vector3(start + i * size, start - size, 0.0f));
  151. a->SetRotation(q);
  152. a = new PlaneActor(this);
  153. a->SetPosition(Vector3(start + i * size, -start + size, 0.0f));
  154. a->SetRotation(q);
  155. }
  156. q = Quaternion::Concatenate(q, Quaternion(Vector3::UnitZ, Math::PiOver2));
  157. // Forward/back walls
  158. for (int i = 0; i < 10; i++)
  159. {
  160. a = new PlaneActor(this);
  161. a->SetPosition(Vector3(start - size, start + i * size, 0.0f));
  162. a->SetRotation(q);
  163. a = new PlaneActor(this);
  164. a->SetPosition(Vector3(-start + size, start + i * size, 0.0f));
  165. a->SetRotation(q);
  166. }
  167. // Setup lights
  168. mRenderer->SetAmbientLight(Vector3(0.2f, 0.2f, 0.2f));
  169. DirectionalLight& dir = mRenderer->GetDirectionalLight();
  170. dir.mDirection = Vector3(0.0f, -0.707f, -0.707f);
  171. dir.mDiffuseColor = Vector3(0.78f, 0.88f, 1.0f);
  172. dir.mSpecColor = Vector3(0.8f, 0.8f, 0.8f);
  173. // Camera actor
  174. mCameraActor = new CameraActor(this);
  175. // UI elements
  176. a = new Actor(this);
  177. a->SetPosition(Vector3(-350.0f, -350.0f, 0.0f));
  178. SpriteComponent* sc = new SpriteComponent(a);
  179. sc->SetTexture(mRenderer->GetTexture("Assets/HealthBar.png"));
  180. a = new Actor(this);
  181. a->SetPosition(Vector3(375.0f, -275.0f, 0.0f));
  182. a->SetScale(0.75f);
  183. sc = new SpriteComponent(a);
  184. sc->SetTexture(mRenderer->GetTexture("Assets/Radar.png"));
  185. }
  186. void Game::UnloadData()
  187. {
  188. // Delete actors
  189. // Because ~Actor calls RemoveActor, have to use a different style loop
  190. while (!mActors.empty())
  191. {
  192. delete mActors.back();
  193. }
  194. if (mRenderer)
  195. {
  196. mRenderer->UnloadData();
  197. }
  198. }
  199. void Game::Shutdown()
  200. {
  201. UnloadData();
  202. if (mRenderer)
  203. {
  204. mRenderer->Shutdown();
  205. }
  206. SDL_Quit();
  207. }
  208. void Game::AddActor(Actor* actor)
  209. {
  210. // If we're updating actors, need to add to pending
  211. if (mUpdatingActors)
  212. {
  213. mPendingActors.emplace_back(actor);
  214. }
  215. else
  216. {
  217. mActors.emplace_back(actor);
  218. }
  219. }
  220. void Game::RemoveActor(Actor* actor)
  221. {
  222. // Is it in pending actors?
  223. auto iter = std::find(mPendingActors.begin(), mPendingActors.end(), actor);
  224. if (iter != mPendingActors.end())
  225. {
  226. // Swap to end of vector and pop off (avoid erase copies)
  227. std::iter_swap(iter, mPendingActors.end() - 1);
  228. mPendingActors.pop_back();
  229. }
  230. // Is it in actors?
  231. iter = std::find(mActors.begin(), mActors.end(), actor);
  232. if (iter != mActors.end())
  233. {
  234. // Swap to end of vector and pop off (avoid erase copies)
  235. std::iter_swap(iter, mActors.end() - 1);
  236. mActors.pop_back();
  237. }
  238. }