22 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
23 std::cout <<
"SDL_Init Error: " << SDL_GetError() << std::endl;
26 std::cout <<
"SDL_Init Success!" << std::endl;
29 const char* ctitle = title.c_str();
30 m_window = SDL_CreateWindow(ctitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
32 std::cout <<
"SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
49 std::cout <<
"TTF_Init Error: " << TTF_GetError() << std::endl;
70 std::string font =
"../Assets/Fonts/BruceForever.ttf";
74 SDL_Color color = {255, 255, 255, 255};
77 SDL_Rect rect = {5, 0, 80, 25};
81 color = {255, 0, 0, 255};
82 std::string numEnemies = std::to_string(sceneTree->findGameObjectsByTag(
"Warrior").size());
84 rect = {5, 15, 160, 25};
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();
93 rect = {5, 30, 120, 25};
97 color = {0, 255, 0, 255};
98 auto players = sceneTree->findGameObjectsByTag(
"Player");
101 auto player = players[0];
105 rect = {5, 45, 80, 25};
106 SDL_RenderCopy(
m_renderer, text, NULL, &rect);
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};
114 rect = {640 - 120, 10, 100, 25};
116 color = {255, 0, 255, 255};
117 SDL_SetRenderDrawColor(
m_renderer, color.r, color.g, color.b, color.a);
121 SDL_DestroyTexture(text);
128 while (SDL_PollEvent(&event)) {
130 if (event.type == SDL_QUIT) {
143 SDL_SetRenderDrawColor(
m_renderer, 100, 190, 255, SDL_ALPHA_OPAQUE);
161 int frameDelayInMs = 1000 / targetFPS;
164 int lastFrameNumber = 0;
168 Uint32 frameStartTime = SDL_GetTicks();
173 int frameTimeDurationInMs = SDL_GetTicks() - frameStartTime;
174 if (frameDelayInMs > frameTimeDurationInMs) {
175 SDL_Delay(frameDelayInMs - frameTimeDurationInMs);
178 if (SDL_GetTicks() - msCount >= 1000) {
179 m_FPS = frameNumber - lastFrameNumber;
180 lastFrameNumber = frameNumber;
A component for managing health of game objects.
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,...
SDL_Window * m_window
Pointer to the SDL_Window.
SDL_Renderer * m_renderer
Pointer to the SDL_Renderer used for all rendering in the game.
void advanceFrame()
Advances the game frame, updating timing and FPS calculations.
float m_FPS
Frames per second, updated during the game loop to measure performance.
void render()
Renders the game state to the window. This function should be called once per frame to draw all game ...
void printStats()
Prints current game statistics like FPS.
bool m_gameIsRunning
Flag to control the game loop.
void start(bool demo)
Starts the game application, initializing all necessary resources.
~GameApplication()
Destructor that cleans up SDL resources.
void input()
Processes input from the user. This function should be called in the game loop to handle events like ...
GameApplication(std::string title)
Constructs a GameApplication with a specified window title.
void update()
Updates the game state. This function should be called once per frame to update the state of the game...
Defines the Texture component for managing textures within the Game Engine, encapsulating SDL texture...