The Table Engine
gameapp.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <iomanip>
3 #include <sstream>
4 #include <string>
5 
6 #include "gameapp.h"
7 #include "gameobject.h"
8 #include "resourcemanager.h"
9 #include "scenemanager.h"
10 
11 #include "transform.h"
12 #include "texture.h"
13 #include "health.h"
14 // #include "sound.h"
15 
16 #include "SDL2/SDL.h"
17 #include <player_input_script.h>
18 
19 
20 // Constructor
22  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
23  std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
24  return;
25  } else {
26  std::cout << "SDL_Init Success!" << std::endl;
27  }
28 
29  const char* ctitle = title.c_str();
30  m_window = SDL_CreateWindow(ctitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
31  if (m_window == NULL) {
32  std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
33  return;
34  }
35 
36  m_renderer = SDL_CreateRenderer(m_window,-1,SDL_RENDERER_ACCELERATED);
37 }
38 
39 // Destructor
41  // Destroy our renderer
42  SDL_DestroyRenderer(m_renderer);
43  // Destroy our window
44  SDL_DestroyWindow(m_window);
45 }
46 
47 void GameApplication::start(bool demo=false) {
48  if (TTF_Init() < 0) {
49  std::cout << "TTF_Init Error: " << TTF_GetError() << std::endl;
50  }
51 
53 
55  if (demo) {
57  } else {
58  SceneManager::getInstance().loadScenesFromJSON("../../Engine_GUI/game_json_output/game.json");
59  }
61 
62  // Sound* music = ResourceManager::getInstance().loadSound("../Assets/Sounds/music.wav");
63  // music->play(-1);
64 
65 
66 }
67 
69  // render some stats on top left corner
70  std::string font = "../Assets/Fonts/BruceForever.ttf";
71  auto sceneTree = SceneManager::getInstance().getSceneTree();
72 
73  // print level
74  SDL_Color color = {255, 255, 255, 255};
75  std::string level = "Level: " + std::to_string(SceneManager::getInstance().getSceneIndex() + 1);
76  SDL_Texture* text = ResourceManager::getInstance().loadText(font, level, color, 12);
77  SDL_Rect rect = {5, 0, 80, 25};
78  SDL_RenderCopy(m_renderer, text, NULL, &rect);
79 
80  // number of enemies
81  color = {255, 0, 0, 255};
82  std::string numEnemies = std::to_string(sceneTree->findGameObjectsByTag("Warrior").size());
83  text = ResourceManager::getInstance().loadText(font, "Enemies Left: " + numEnemies, color, 12);
84  rect = {5, 15, 160, 25};
85  SDL_RenderCopy(m_renderer, text, NULL, &rect);
86 
87  // FPS rounded to 2 decimal places
88  color = {0, 0, 255, 255};
89  std::stringstream fpsStream;
90  fpsStream << std::fixed << std::setprecision(2) << m_FPS;
91  std::string fps = fpsStream.str();
92  text = ResourceManager::getInstance().loadText(font, "FPS: " + fps, color, 12);
93  rect = {5, 30, 120, 25};
94  SDL_RenderCopy(m_renderer, text, NULL, &rect);
95 
96  // number of keys collected by player
97  color = {0, 255, 0, 255};
98  auto players = sceneTree->findGameObjectsByTag("Player");
99  if (!players.empty())
100  {
101  auto player = players[0];
102 
103  std::string numKeys = std::to_string(player->getScript<PlayerInputScript>()->getKeysCollected());
104  text = ResourceManager::getInstance().loadText(font, "Keys: " + numKeys, color, 12);
105  rect = {5, 45, 80, 25};
106  SDL_RenderCopy(m_renderer, text, NULL, &rect);
107 
108  color = {255, 0, 0, 255};
109  SDL_SetRenderDrawColor(m_renderer, color.r, color.g, color.b, color.a);
110  int health = player->getComponent<Health>()->getHealth();
111  rect = {640 - 120, 10, (int) (100.0 * health / 1000), 25};
112  SDL_RenderFillRect(m_renderer, &rect);
113  // draw a border around the health bar
114  rect = {640 - 120, 10, 100, 25};
115  // color = purple
116  color = {255, 0, 255, 255};
117  SDL_SetRenderDrawColor(m_renderer, color.r, color.g, color.b, color.a);
118  SDL_RenderDrawRect(m_renderer, &rect);
119  }
120 
121  SDL_DestroyTexture(text);
122 }
123 
124 // Handle input
126  SDL_Event event;
127  // Start our event loop
128  while (SDL_PollEvent(&event)) {
129  // Handle each specific event
130  if (event.type == SDL_QUIT) {
131  m_gameIsRunning = false;
132  }
133 
135  }
136 }
137 
140 }
141 
143  SDL_SetRenderDrawColor(m_renderer, 100, 190, 255, SDL_ALPHA_OPAQUE);
144  SDL_RenderClear(m_renderer);
145 
147  printStats();
148 
149  SDL_RenderPresent(m_renderer);
150 }
151 
153  input();
154  update();
155  render();
156 }
157 
159 {
160  int targetFPS = 60;
161  int frameDelayInMs = 1000 / targetFPS;
162 
163  int frameNumber = 0;
164  int lastFrameNumber = 0;
165  int msCount = -1000;
166 
167  while(m_gameIsRunning) {
168  Uint32 frameStartTime = SDL_GetTicks();
169 
170  advanceFrame();
171  frameNumber++;
172 
173  int frameTimeDurationInMs = SDL_GetTicks() - frameStartTime;
174  if (frameDelayInMs > frameTimeDurationInMs) {
175  SDL_Delay(frameDelayInMs - frameTimeDurationInMs);
176  }
177 
178  if (SDL_GetTicks() - msCount >= 1000) {
179  m_FPS = frameNumber - lastFrameNumber;
180  lastFrameNumber = frameNumber;
181  msCount += 1000;
182  }
183  }
184 }
A component for managing health of game objects.
Definition: health.h:22
Handles player interactions, movements, and animation states based on game input.
int getKeysCollected()
Retrieves the number of keys collected by the player.
void setRenderer(SDL_Renderer *renderer)
Sets the renderer for resource management.
SDL_Texture * loadText(std::string font_path, std::string text, SDL_Color color, int font_size=20)
Loads a text as a texture using a specified TTF font.
static ResourceManager & getInstance()
Retrieves the singleton instance of ResourceManager.
static SceneManager & getInstance()
Retrieves the singleton instance of SceneManager.
SceneTree * getSceneTree()
Retrieves the current scene tree managing the game objects.
void getNextScene()
Advances to the next scene based on the current scene index.
void render()
Renders the current scene.
void input()
Processes input for the current scene.
void loadScenesFromJSON(const std::string &filePath)
void update()
Updates the state of the current scene.
void setRenderer(SDL_Renderer *renderer)
Sets the renderer used for drawing game objects.
Defines the Health class, a component for managing the health of game objects.
Manages resources such as textures, fonts, and potentially sounds for a game engine.
Defines the SceneManager class for managing scenes in the game.
void runLoop()
The main game loop that runs continuously while the game is active. This loop calls the input,...
Definition: gameapp.cpp:158
SDL_Window * m_window
Pointer to the SDL_Window.
Definition: gameapp.h:23
SDL_Renderer * m_renderer
Pointer to the SDL_Renderer used for all rendering in the game.
Definition: gameapp.h:24
void advanceFrame()
Advances the game frame, updating timing and FPS calculations.
Definition: gameapp.cpp:152
float m_FPS
Frames per second, updated during the game loop to measure performance.
Definition: gameapp.h:28
void render()
Renders the game state to the window. This function should be called once per frame to draw all game ...
Definition: gameapp.cpp:142
void printStats()
Prints current game statistics like FPS.
Definition: gameapp.cpp:68
bool m_gameIsRunning
Flag to control the game loop.
Definition: gameapp.h:26
void start(bool demo)
Starts the game application, initializing all necessary resources.
Definition: gameapp.cpp:47
~GameApplication()
Destructor that cleans up SDL resources.
Definition: gameapp.cpp:40
void input()
Processes input from the user. This function should be called in the game loop to handle events like ...
Definition: gameapp.cpp:125
GameApplication(std::string title)
Constructs a GameApplication with a specified window title.
Definition: gameapp.cpp:21
void update()
Updates the game state. This function should be called once per frame to update the state of the game...
Definition: gameapp.cpp:138
Defines the Texture component for managing textures within the Game Engine, encapsulating SDL texture...
Provides the Transform component for positioning and sizing game objects.