Skip to content
Snippets Groups Projects
Unverified Commit 670d53f3 authored by Leon Niklas Lux's avatar Leon Niklas Lux Committed by Leon Lux
Browse files

SDL Application als Basisklasse für SDL2 Ressourcenmanagement

parent e3b8d307
Branches
Tags
1 merge request!55Abgabe des Projektes
......@@ -17,6 +17,7 @@ set(GAME_SOURCES
src/ContactListener.cpp
src/Enemy.cpp
src/EnemyManager.cpp
src/SDLApplication.cpp
)
......
#include "SDLApplication.hpp"
SDLApplication::SDLApplication(const std::string &title, int width, int height)
: m_Title(title), m_Width(width), m_Height(height), m_Running(true), m_SDLInitialized(false), m_Renderer(nullptr),
m_Window(nullptr)
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
throw std::runtime_error("failed to initialize SDL2: " + std::string(SDL_GetError()));
}
m_SDLInitialized = true;
if (SDL_CreateWindowAndRenderer(m_Width, m_Height, SDL_WINDOW_SHOWN, &m_Window, &m_Renderer))
{
throw std::runtime_error("failed to create window and renderer: " + std::string(SDL_GetError()));
}
}
SDLApplication::~SDLApplication()
{
if (m_Renderer)
{
SDL_DestroyRenderer(m_Renderer);
}
if (m_Window)
{
SDL_DestroyWindow(m_Window);
}
if (m_SDLInitialized)
{
SDL_Quit();
}
}
void SDLApplication::run()
{
while (m_Running)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
m_Running = false;
}
else
{
handleEvent(event);
}
}
SDL_RenderClear(m_Renderer);
render();
SDL_RenderPresent(m_Renderer);
}
}
\ No newline at end of file
#pragma once
#include <SDL2/SDL.h>
#include <stdexcept>
#include <string>
/**
* @brief Base Class for Game managing SDL State.
*/
class SDLApplication
{
public:
/**
* @brief Create a new Application with a Window and Renderer.
* @param title Title shown at top of Window.
* @param width Width of the Window.
* @param height Height of the Window.
* @throw std::runtime_error When Initialization of SDL2 fails.
*/
SDLApplication(const std::string &title, int width, int height);
SDLApplication(const SDLApplication &) = delete;
SDLApplication &operator=(const SDLApplication &) = delete;
virtual ~SDLApplication();
/**
* @brief Handle Events produced.
* @param event The event to handle.
*/
virtual void handleEvent(SDL_Event &event) = 0;
/**
* @brief Update state of the underlying Renderer for drawing.
*/
virtual void render() = 0;
/**
* @brief Run the application until a SDL_QUIT Event is produced.
*/
virtual void run();
private:
/**
* @brief Title of the Window.
*/
std::string m_Title;
/**
* @brief Current Width of the Window.
*/
int m_Width;
/**
* @brief Current Height of the Window.
*/
int m_Height;
/**
* @brief Wether the Application is running or received a Quit event.
*/
bool m_Running;
/**
* @brief Helper to ensure that SDL_Quit is only called once.
*/
bool m_SDLInitialized;
/**
* @brief SDL Window to use as drawing Target.
*/
SDL_Window *m_Window;
/**
* @brief SDL Renderer associated with the Window.
*/
SDL_Renderer *m_Renderer;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment