Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 <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. #include <Systems/RenderGUI.h>
  43. uint32_t Game::windowsWidth = 0;
  44. uint32_t Game::windowsHeight = 0;
  45. uint32_t Game::mapWidth = 0;
  46. uint32_t Game::mapHeight = 0;
  47. Game::Game()
  48. {
  49. this->isRunning = false;
  50. this->isDebug = false;
  51. this->millisecsPerviousFrame = 0;
  52. this->registry = std::make_unique<Registry>();
  53. this->assetStore = std::make_unique<AssetStore>();
  54. this->eventBus = std::make_unique<EventBus>();
  55. }
  56. Game::~Game()
  57. {
  58. }
  59. void Game::Initialize()
  60. {
  61. int ret;
  62. SDL_DisplayMode displayMode;
  63. ret = SDL_Init(SDL_INIT_EVERYTHING);
  64. if (ret)
  65. {
  66. Logger::Critical("SDL Initialisation error.");
  67. return;
  68. }
  69. ret = TTF_Init();
  70. if (ret)
  71. {
  72. Logger::Critical("SDL_ttf Initialisation error.");
  73. return;
  74. }
  75. SDL_GetCurrentDisplayMode(0, &displayMode);
  76. Game::windowsWidth = 800; //displayMode.w;
  77. Game::windowsHeight = 600; //displayMode.h;
  78. this->window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  79. Game::windowsWidth, Game::windowsHeight, SDL_WINDOW_BORDERLESS);
  80. if (!window)
  81. {
  82. Logger::Critical("SDL window creation error.");
  83. return;
  84. }
  85. this->renderer = SDL_CreateRenderer(this->window, -1, 0);
  86. if (!renderer)
  87. {
  88. Logger::Critical("SDL renderer creation error.");
  89. return;
  90. }
  91. //SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
  92. ImGui::CreateContext();
  93. ImGuiSDL::Initialize(this->renderer, Game::windowsWidth, Game::windowsHeight);
  94. this->camera.x = 0;
  95. this->camera.y = 0;
  96. this->camera.w = Game::windowsWidth;
  97. this->camera.h = Game::windowsHeight;
  98. this->isRunning = true;
  99. }
  100. void Game::Destroy()
  101. {
  102. ImGuiSDL::Deinitialize();
  103. ImGui::DestroyContext();
  104. if (this->renderer)
  105. {
  106. SDL_DestroyRenderer(this->renderer);
  107. }
  108. if (this->window)
  109. {
  110. SDL_DestroyWindow(this->window);
  111. }
  112. SDL_Quit();
  113. }
  114. void Game::Run()
  115. {
  116. this->Setup();
  117. #if 0
  118. for(int i = 0 ; i < 100; i++)
  119. {
  120. this->ProcessInput();
  121. this->Update();
  122. this->Render();
  123. }
  124. #else
  125. while(this->isRunning)
  126. {
  127. this->ProcessInput();
  128. this->Update();
  129. this->Render();
  130. }
  131. #endif
  132. }
  133. void Game::LoadLevel(int level)
  134. {
  135. this->registry->addSystem<MovementSystem>();
  136. this->registry->addSystem<RenderSystem>();
  137. this->registry->addSystem<AnimationSystem>();
  138. this->registry->addSystem<CollisionSystem>();
  139. this->registry->addSystem<DamageSystem>();
  140. this->registry->addSystem<KeyboardMovementSystem>();
  141. this->registry->addSystem<CameraMovementSystem>();
  142. this->registry->addSystem<ProjectileEmitSystem>();
  143. this->registry->addSystem<ProjectileLifeCycleSystem>();
  144. this->registry->addSystem<RenderTextSystem>();
  145. this->registry->addSystem<RenderHealthSystem>();
  146. this->registry->addSystem<RenderGUISystem>();
  147. this->assetStore->addTexture(this->renderer, "tank-image", "assets/images/tank-panther-right.png");
  148. this->assetStore->addTexture(this->renderer, "truck-image", "assets/images/truck-ford-right.png");
  149. this->assetStore->addTexture(this->renderer, "chopper-image", "assets/images/chopper-spritesheet.png");
  150. this->assetStore->addTexture(this->renderer, "main-tileset", "assets/tilemaps/jungle.png");
  151. this->assetStore->addTexture(this->renderer, "radar-image", "assets/images/radar.png");
  152. this->assetStore->addTexture(this->renderer, "bullet-image", "assets/images/bullet.png");
  153. this->assetStore->addFont("charriot-font", "assets/fonts/charriot.ttf", 20);
  154. this->assetStore->addFont("arial-font", "assets/fonts/charriot.ttf", 20);
  155. this->assetStore->addFont("pico8-font", "assets/fonts/charriot.ttf", 10);
  156. Logger::Debug("struct SpriteComponent size is %d", sizeof(struct SpriteComponent));
  157. // jungle.map
  158. int tileSize = 32;
  159. double tileScale = 2.0;
  160. int mapNumCols = 25;
  161. int mapNumRows = 20;
  162. std::fstream mapFile;
  163. mapFile.open("assets/tilemaps/jungle.map");
  164. assert(mapFile.is_open());
  165. for (int y = 0; y < mapNumRows; y++)
  166. {
  167. for (int x = 0; x < mapNumCols; x++)
  168. {
  169. char ch[2] = {0, 0};
  170. mapFile.get(ch[0]);
  171. int srcRectY = atoi(ch) * tileSize;
  172. mapFile.get(ch[0]);
  173. int srcRectX = atoi(ch) * tileSize;
  174. mapFile.ignore();
  175. Entity tile = registry->createEntity();
  176. tile.addComponent<TransformComponent>(glm::vec2(x * (tileScale * tileSize), y * (tileScale * tileSize)), glm::vec2(tileScale, tileScale), 0.0);
  177. tile.addComponent<SpriteComponent>("main-tileset", 0, tileSize, tileSize, false, srcRectX, srcRectY);
  178. tile.group("tiles");
  179. }
  180. }
  181. mapFile.close();
  182. Game::mapWidth = mapNumCols * tileSize * tileScale;
  183. Game::mapHeight = mapNumRows * tileSize * tileScale;
  184. Entity chopper = this->registry->createEntity();
  185. chopper.addComponent<TransformComponent>(glm::vec2(240, 110), glm::vec2(1, 1), 0);
  186. chopper.addComponent<SpriteComponent>("chopper-image", 1, 32, 32);
  187. chopper.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  188. chopper.addComponent<BoxColliderComponent>(32, 25, glm::vec2(0, 5));
  189. chopper.addComponent<AnimationComponent>(2, 15, true);
  190. chopper.addComponent<KeyboardControlComponent>(glm::vec2(0, -80), glm::vec2(80, 0),
  191. glm::vec2(0, 80), glm::vec2(-80, 0));
  192. chopper.addComponent<ProjectileEmitterComponent>(glm::vec2(180, 180), 0, 10000, 10, true);
  193. chopper.addComponent<HealthComponent>(100);
  194. chopper.addComponent<CameraFollowComponent>();
  195. chopper.tag("player");
  196. Entity radar = this->registry->createEntity();
  197. radar.addComponent<TransformComponent>(glm::vec2(this->windowsWidth - 75, 10), glm::vec2(1, 1), 0);
  198. radar.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  199. radar.addComponent<SpriteComponent>("radar-image", 3, 64, 64, true);
  200. radar.addComponent<AnimationComponent>(8, 5, true);
  201. Entity tank = this->registry->createEntity();
  202. tank.addComponent<TransformComponent>(glm::vec2(500, 500), glm::vec2(1, 1), 0);
  203. tank.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  204. tank.addComponent<SpriteComponent>("tank-image", 1, 32, 32);
  205. tank.addComponent<BoxColliderComponent>(25, 18, glm::vec2(5, 7));
  206. tank.addComponent<ProjectileEmitterComponent>(glm::vec2(100, 0), 3000, 5000, 10, false);
  207. tank.addComponent<HealthComponent>(100);
  208. tank.group("enemies");
  209. Entity truck = this->registry->createEntity();
  210. truck.addComponent<TransformComponent>(glm::vec2(120, 500), glm::vec2(1, 1), 0);
  211. truck.addComponent<RigidBodyComponent>(glm::vec2(0, 0));
  212. truck.addComponent<SpriteComponent>("truck-image", 2, 32, 32);
  213. truck.addComponent<BoxColliderComponent>(25, 20, glm::vec2(5, 5));
  214. truck.addComponent<ProjectileEmitterComponent>(glm::vec2(0, -100), 2000, 5000, 10, false);
  215. truck.addComponent<HealthComponent>(100);
  216. truck.group("enemies");
  217. SDL_Color green = { 64, 255, 127, 255 };
  218. Entity label = this->registry->createEntity();
  219. label.addComponent<TextLabelComponent>(glm::vec2(this->windowsWidth/2 - 60, 10), "SUPER CHOPPER BROS 1.0", "charriot-font", green);
  220. }
  221. void Game::Setup()
  222. {
  223. LoadLevel(1);
  224. }
  225. void Game::Update()
  226. {
  227. uint32_t timeToWait = MILLISECS_PER_FRAME - (SDL_GetTicks() - this->millisecsPerviousFrame);
  228. if (timeToWait > 0 && timeToWait <= MILLISECS_PER_FRAME)
  229. {
  230. SDL_Delay(timeToWait);
  231. }
  232. double deltaTime = (SDL_GetTicks() - this->millisecsPerviousFrame) / 1000.0;
  233. this->millisecsPerviousFrame = SDL_GetTicks();
  234. eventBus->reset();
  235. registry->getSystem<DamageSystem>().subscriptToEvents(eventBus);
  236. registry->getSystem<KeyboardMovementSystem>().subscriptToEvents(eventBus);
  237. registry->getSystem<ProjectileEmitSystem>().subscriptToEvents(eventBus);
  238. registry->update();
  239. registry->getSystem<MovementSystem>().update(deltaTime);
  240. registry->getSystem<AnimationSystem>().update();
  241. registry->getSystem<CollisionSystem>().update(this->eventBus);
  242. registry->getSystem<ProjectileEmitSystem>().update(this->registry);
  243. registry->getSystem<ProjectileLifeCycleSystem>().update();
  244. registry->getSystem<CameraMovementSystem>().update(this->camera);
  245. }
  246. void Game::ProcessInput()
  247. {
  248. SDL_Event event;
  249. while(SDL_PollEvent(&event))
  250. {
  251. int mouseX, mouseY, buttons;
  252. ImGui_ImplSDL2_ProcessEvent(&event);
  253. ImGuiIO &io = ImGui::GetIO();
  254. buttons = SDL_GetMouseState(&mouseX, &mouseY);
  255. io.MousePos = ImVec2(mouseX, mouseY);
  256. io.MouseDown[0] = buttons & SDL_BUTTON(SDL_BUTTON_LEFT);
  257. io.MouseDown[1] = buttons & SDL_BUTTON(SDL_BUTTON_RIGHT);
  258. io.MouseDown[2] = buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE);
  259. switch(event.type)
  260. {
  261. case SDL_QUIT:
  262. this->isRunning = false;
  263. break;
  264. case SDL_KEYDOWN:
  265. if (event.key.keysym.sym == SDLK_ESCAPE)
  266. {
  267. this->isRunning = false;
  268. }
  269. else if (event.key.keysym.sym == SDLK_d)
  270. {
  271. this->isDebug = !this->isDebug;
  272. }
  273. else
  274. {
  275. this->eventBus->emitEvent<KeyPressedEvent>(event.key.keysym.sym);
  276. }
  277. break;
  278. default:
  279. break;
  280. }
  281. }
  282. }
  283. void Game::Render()
  284. {
  285. SDL_SetRenderDrawColor(this->renderer, 21, 21, 21, 255);
  286. SDL_RenderClear(this->renderer);
  287. registry->getSystem<RenderSystem>().update(this->renderer, this->assetStore, this->camera);
  288. registry->getSystem<RenderTextSystem>().update(this->renderer, this->assetStore, this->camera);
  289. registry->getSystem<RenderHealthSystem>().update(this->renderer, this->assetStore, this->camera);
  290. if (this->isDebug)
  291. {
  292. registry->getSystem<CollisionSystem>().debugRender(this->renderer, this->camera);
  293. registry->getSystem<RenderGUISystem>().update(this->registry, this->camera);
  294. }
  295. SDL_RenderPresent(this->renderer);
  296. }