Game.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <SDL.h>
  10. #include <SDL_image.h>
  11. #include <glm/glm.hpp>
  12. #include <Game.h>
  13. #include <Logger.h>
  14. #include <ECS.h>
  15. Game::Game()
  16. {
  17. this->isRunning = false;
  18. this->windowsHeight = 0;
  19. this->windowsWidth = 0;
  20. }
  21. Game::~Game()
  22. {
  23. }
  24. void Game::Initialize()
  25. {
  26. int ret;
  27. SDL_DisplayMode displayMode;
  28. ret = SDL_Init(SDL_INIT_EVERYTHING);
  29. if (ret)
  30. {
  31. Logger::Error("SDL Initialisation error.");
  32. return;
  33. }
  34. SDL_GetCurrentDisplayMode(0, &displayMode);
  35. this->windowsWidth = displayMode.w;
  36. this->windowsHeight = displayMode.h;
  37. this->window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  38. this->windowsWidth, this->windowsHeight, SDL_WINDOW_BORDERLESS);
  39. if (!window)
  40. {
  41. Logger::Error("SDL window creation error.");
  42. return;
  43. }
  44. this->renderer = SDL_CreateRenderer(window, -1, 0);
  45. if (!renderer)
  46. {
  47. Logger::Error("SDL renderer creation error.");
  48. return;
  49. }
  50. SDL_SetWindowFullscreen(this->window, SDL_WINDOW_FULLSCREEN);
  51. this->isRunning = true;
  52. }
  53. void Game::Destroy()
  54. {
  55. if (this->renderer)
  56. {
  57. SDL_DestroyRenderer(this->renderer);
  58. }
  59. if (this->window)
  60. {
  61. SDL_DestroyWindow(this->window);
  62. }
  63. SDL_Quit();
  64. }
  65. void Game::Run()
  66. {
  67. this->Setup();
  68. while(this->isRunning)
  69. {
  70. this->ProcessInput();
  71. this->Update();
  72. this->Render();
  73. }
  74. }
  75. void Game::Setup()
  76. {
  77. }
  78. void Game::Update()
  79. {
  80. int32_t timeToWait = MILLISECS_PER_FRAME - (SDL_GetTicks() - this->millisecsPerviousFrame);
  81. if (timeToWait > 0 && timeToWait <= MILLISECS_PER_FRAME)
  82. {
  83. SDL_Delay(timeToWait);
  84. }
  85. double deltaTime = (SDL_GetTicks() - this->millisecsPerviousFrame) / 1000.0;
  86. this->millisecsPerviousFrame = SDL_GetTicks();
  87. }
  88. void Game::ProcessInput()
  89. {
  90. SDL_Event event;
  91. while(SDL_PollEvent(&event))
  92. {
  93. switch(event.type)
  94. {
  95. case SDL_QUIT:
  96. this->isRunning = false;
  97. break;
  98. case SDL_KEYDOWN:
  99. if (event.key.keysym.sym == SDLK_ESCAPE)
  100. {
  101. this->isRunning = false;
  102. }
  103. break;
  104. default:
  105. break;
  106. }
  107. }
  108. }
  109. void Game::Render()
  110. {
  111. SDL_SetRenderDrawColor(this->renderer, 21, 21, 21, 255);
  112. SDL_RenderClear(this->renderer);
  113. SDL_RenderPresent(this->renderer);
  114. }