Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * 2D Game Engine
  3. * Game.cpp: Base Game engine class
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All Right Reserved
  6. *
  7. * Created by Manoël Trapier on 09/02/2021.
  8. */
  9. #include <stdlib.h>
  10. #include <SDL.h>
  11. #include <SDL_image.h>
  12. #include <glm/glm.hpp>
  13. #include <imgui.h>
  14. #include <imgui_sdl.h>
  15. #include <backends/imgui_impl_sdl.h>
  16. #include <memory>
  17. #include <fstream>
  18. #include <Game.h>
  19. #include <Logger.h>
  20. #include <ECS.h>
  21. #include <Components/Animation.h>
  22. #include <Components/Transform.h>
  23. #include <Components/RigidBody.h>
  24. #include <Components/Sprite.h>
  25. #include <Components/BoxCollider.h>
  26. #include <Components/KeyboardControl.h>
  27. #include <Components/CameraFollow.h>
  28. #include <Components/ProjectileEmitter.h>
  29. #include <Components/Heakth.h>
  30. #include <Components/TextLabel.h>
  31. #include <Systems/Movement.h>
  32. #include <Systems/Render.h>
  33. #include <Systems/Animation.h>
  34. #include <Systems/Collision.h>
  35. #include <Systems/Damage.h>
  36. #include <Systems/KeyboardMovement.h>
  37. #include <Systems/CameraMovement.h>
  38. #include <Systems/ProjectileEmit.h>
  39. #include <Systems/ProjectileLifeCycle.h>
  40. #include <Systems/RenderText.h>
  41. #include <Systems/RenderHealth.h>
  42. uint32_t Game::windowsWidth = 0;
  43. uint32_t Game::windowsHeight = 0;
  44. uint32_t Game::mapWidth = 0;
  45. uint32_t Game::mapHeight = 0;
  46. Game::Game()
  47. {
  48. this->isRunning = false;
  49. this->isDebug = false;
  50. this->millisecsPerviousFrame = 0;
  51. this->registry = std::make_unique<Registry>();
  52. this->assetStore = std::make_unique<AssetStore>();
  53. this->eventBus = std::make_unique<EventBus>();
  54. }
  55. Game::~Game()
  56. {
  57. }
  58. void Game::Initialize()
  59. {
  60. int ret;
  61. SDL_DisplayMode displayMode;
  62. ret = SDL_Init(SDL_INIT_EVERYTHING);
  63. if (ret)
  64. {
  65. Logger::Critical("SDL Initialisation error.");
  66. return;
  67. }
  68. ret = TTF_Init();
  69. if (ret)
  70. {
  71. Logger::Critical("SDL_ttf Initialisation error.");
  72. return;
  73. }
  74. SDL_GetCurrentDisplayMode(0, &displayMode);
  75. Game::windowsWidth = 800; //displayMode.w;
  76. Game::windowsHeight = 600; //displayMode.h;
  77. this->window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  78. Game::windowsWidth, Game::windowsHeight, SDL_WINDOW_BORDERLESS);
  79. if (!window)
  80. {
  81. Logger::Critical("SDL window creation error.");
  82. return;
  83. }
  84. this->renderer = SDL_CreateRenderer(this->window, -1, 0);
  85. if (!renderer)
  86. {
  87. Logger::Critical("SDL renderer creation error.");
  88. return;
  89. }
  90. //SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
  91. ImGui::CreateContext();
  92. ImGuiSDL::Initialize(this->renderer, Game::windowsWidth, Game::windowsHeight);
  93. this->camera.x = 0;
  94. this->camera.y = 0;
  95. this->camera.w = Game::windowsWidth;
  96. this->camera.h = Game::windowsHeight;
  97. this->isRunning = true;
  98. }
  99. void Game::Destroy()
  100. {
  101. ImGuiSDL::Deinitialize();
  102. ImGui::DestroyContext();
  103. if (this->renderer)
  104. {
  105. SDL_DestroyRenderer(this->renderer);
  106. }
  107. if (this->window)
  108. {
  109. SDL_DestroyWindow(this->window);
  110. }
  111. SDL_Quit();
  112. }
  113. void Game::Run()
  114. {
  115. this->Setup();
  116. #if 0
  117. for(int i = 0 ; i < 100; i++)
  118. {
  119. this->ProcessInput();
  120. this->Update();
  121. this->Render();
  122. }
  123. #else
  124. while(this->isRunning)
  125. {
  126. this->ProcessInput();
  127. this->Update();
  128. this->Render();
  129. }
  130. #endif
  131. }
  132. void Game::LoadLevel(int level)
  133. {
  134. this->registry->addSystem<MovementSystem>();
  135. this->registry->addSystem<RenderSystem>();
  136. this->registry->addSystem<AnimationSystem>();
  137. this->registry->addSystem<CollisionSystem>();
  138. this->registry->addSystem<DamageSystem>();
  139. this->registry->addSystem<KeyboardMovementSystem>();
  140. this->registry->addSystem<CameraMovementSystem>();
  141. this->registry->addSystem<ProjectileEmitSystem>();
  142. this->registry->addSystem<ProjectileLifeCycleSystem>();
  143. this->registry->addSystem<RenderTextSystem>();
  144. this->registry->addSystem<RenderHealthSystem>();
  145. this->assetStore->addTexture(this->renderer, "tank-image", "assets/images/tank-panther-right.png");
  146. this->assetStore->addTexture(this->renderer, "truck-image", "assets/images/truck-ford-right.png");
  147. this->assetStore->addTexture(this->renderer, "chopper-image", "assets/images/chopper-spritesheet.png");
  148. this->assetStore->addTexture(this->renderer, "main-tileset", "assets/tilemaps/jungle.png");
  149. this->assetStore->addTexture(this->renderer, "radar-image", "assets/images/radar.png");
  150. this->assetStore->addTexture(this->renderer, "bullet-image", "assets/images/bullet.png");
  151. this->assetStore->addFont("charriot-font", "assets/fonts/charriot.ttf", 20);
  152. this->assetStore->addFont("arial-font", "assets/fonts/charriot.ttf", 20);
  153. this->assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 10);
  154. Logger::Debug("struct SpriteComponent size is %d", sizeof(struct SpriteComponent));
  155. // jungle.map
  156. int tileSize = 32;
  157. double tileScale = 2.0;
  158. int mapNumCols = 25;
  159. int mapNumRows = 20;
  160. std::fstream mapFile;
  161. mapFile.open("assets/tilemaps/jungle.map");
  162. assert(mapFile.is_open());
  163. for (int y = 0; y < mapNumRows; y++)
  164. {
  165. for (int x = 0; x < mapNumCols; x++)
  166. {
  167. char ch[2] = {0, 0};
  168. mapFile.get(ch[0]);
  169. int srcRectY = atoi(ch) * tileSize;
  170. mapFile.get(ch[0]);
  171. int srcRectX = atoi(ch) * tileSize;
  172. mapFile.ignore();
  173. Entity tile = registry->createEntity();
  174. tile.addComponent<TransformComponent>(glm::vec2(x * (tileScale * tileSize), y * (tileScale * tileSize)), glm::vec2(tileScale, tileScale), 0.0);
  175. tile.addComponent<SpriteComponent>("main-tileset", 0, tileSize, tileSize, false, srcRectX, srcRectY);
  176. tile.group("tiles");
  177. }
  178. }
  179. mapFile.close();
  180. Game::mapWidth = mapNumCols * tileSize * tileScale;
  181. Game::mapHeight = mapNumRows * tileSize * tileScale;
  182. Entity chopper = this->registry->createEntity();
  183. chopper.addComponent<TransformComponent>(glm::vec2(240, 110), glm::vec2(1, 1), 0);
  184. chopper.addComponent<SpriteComponent>("chopper-image", 1, 32, 32);
  185. chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  186. chopper.addComponent<BoxColliderComponent>(32, 32);
  187. chopper.addComponent<AnimationComponent>(2, 15, true);
  188. chopper.addComponent<KeyboardControlComponent>(glm::vec2(0, -80), glm::vec2(80, 0),
  189. glm::vec2(0, 80), glm::vec2(-80, 0));
  190. chopper.addComponent<ProjectileEmitterComponent>(glm::vec2(180, 180), 0, 10000, 10, true);
  191. chopper.addComponent<HealthComponent>(100);
  192. chopper.addComponent<CameraFollowComponent>();
  193. chopper.tag("player");
  194. Entity radar = this->registry->createEntity();
  195. radar.addComponent<TransformComponent>(glm::vec2(this->windowsWidth - 75, 10), glm::vec2(1, 1), 0);
  196. radar.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  197. radar.addComponent<SpriteComponent>("radar-image", 3, 64, 64, true);
  198. radar.addComponent<AnimationComponent>(8, 5, true);
  199. Entity tank = this->registry->createEntity();
  200. tank.addComponent<TransformComponent>(glm::vec2(500, 500), glm::vec2(1, 1), 0);
  201. tank.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  202. tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
  203. tank.addComponent<BoxColliderComponent>(32, 32);
  204. tank.addComponent<ProjectileEmitterComponent>(glm::vec2(100, 0), 3000, 5000, 10, false);
  205. tank.addComponent<HealthComponent>(100);
  206. tank.group("enemies");
  207. Entity truck = this->registry->createEntity();
  208. truck.addComponent<TransformComponent>(glm::vec2(120, 500), glm::vec2(1, 1), 0);
  209. truck.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  210. truck.addComponent<SpriteComponent>("truck-image", 2, 32, 32);
  211. truck.addComponent<BoxColliderComponent>(32, 32);
  212. truck.addComponent<ProjectileEmitterComponent>(glm::vec2(0, -100), 2000, 5000, 10, false);
  213. truck.addComponent<HealthComponent>(100);
  214. truck.group("enemies");
  215. SDL_Color green = { 64, 255, 127, 255 };
  216. Entity label = this->registry->createEntity();
  217. label.addComponent<TextLabelComponent>(glm::vec2(this->windowsWidth/2 - 60, 10), "SUPER CHOPPER BROS 1.0", "charriot-font", green);
  218. }
  219. void Game::Setup()
  220. {
  221. LoadLevel(1);
  222. }
  223. void Game::Update()
  224. {
  225. uint32_t timeToWait = MILLISECS_PER_FRAME - (SDL_GetTicks() - this->millisecsPerviousFrame);
  226. if (timeToWait > 0 && timeToWait <= MILLISECS_PER_FRAME)
  227. {
  228. SDL_Delay(timeToWait);
  229. }
  230. double deltaTime = (SDL_GetTicks() - this->millisecsPerviousFrame) / 1000.0;
  231. this->millisecsPerviousFrame = SDL_GetTicks();
  232. eventBus->reset();
  233. registry->getSystem<DamageSystem>().subscriptToEvents(eventBus);
  234. registry->getSystem<KeyboardMovementSystem>().subscriptToEvents(eventBus);
  235. registry->getSystem<ProjectileEmitSystem>().subscriptToEvents(eventBus);
  236. registry->update();
  237. registry->getSystem<MovementSystem>().update(deltaTime);
  238. registry->getSystem<AnimationSystem>().update();
  239. registry->getSystem<CollisionSystem>().update(this->eventBus);
  240. registry->getSystem<ProjectileEmitSystem>().update(this->registry);
  241. registry->getSystem<ProjectileLifeCycleSystem>().update();
  242. registry->getSystem<CameraMovementSystem>().update(this->camera);
  243. }
  244. void Game::ProcessInput()
  245. {
  246. SDL_Event event;
  247. while(SDL_PollEvent(&event))
  248. {
  249. switch(event.type)
  250. {
  251. case SDL_QUIT:
  252. this->isRunning = false;
  253. break;
  254. case SDL_KEYDOWN:
  255. if (event.key.keysym.sym == SDLK_ESCAPE)
  256. {
  257. this->isRunning = false;
  258. }
  259. else if (event.key.keysym.sym == SDLK_d)
  260. {
  261. this->isDebug = !this->isDebug;
  262. }
  263. else
  264. {
  265. this->eventBus->emitEvent<KeyPressedEvent>(event.key.keysym.sym);
  266. }
  267. break;
  268. default:
  269. break;
  270. }
  271. }
  272. }
  273. void Game::Render()
  274. {
  275. SDL_SetRenderDrawColor(this->renderer, 21, 21, 21, 255);
  276. SDL_RenderClear(this->renderer);
  277. registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore, this->camera);
  278. registry->getSystem<RenderTextSystem>().update(this->renderer, this->assetStore, this->camera);
  279. registry->getSystem<RenderHealthSystem>().update(this->renderer, this->assetStore, this->camera);
  280. if (this->isDebug)
  281. {
  282. registry->getSystem<CollisionSystem>().debugRender(this->renderer, this->camera);
  283. ImGui::NewFrame();
  284. ImGui::ShowDemoWindow();
  285. ImGui::Render();
  286. ImGuiSDL::Render(ImGui::GetDrawData());
  287. }
  288. SDL_RenderPresent(this->renderer);
  289. }