feat: reorganized core systems

main
Sara 2024-11-21 08:35:37 +01:00
parent 72ce2044f4
commit 67ae79d73b
5 changed files with 57 additions and 61 deletions

View File

@ -1,13 +1,17 @@
#include "application.hpp"
#include <SDL2/SDL_log.h>
#include "canvas_engine.hpp"
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_keyboard.h>
#include <SDL2/SDL_log.h>
#include <SDL2/SDL_render.h>
#ifndef PROJECTNAME
#define PROJECTNAME "Pass define for PROJECTNAME to change title"
#endif
Application::Application() {
namespace ce {
CanvasEngine::CanvasEngine() {
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to initialize SDL, SDL error: %s", SDL_GetError());
return;
@ -28,13 +32,13 @@ Application::Application() {
this->stay_open = true;
}
Application::~Application() {
CanvasEngine::~CanvasEngine() {
SDL_DestroyRenderer(this->render);
SDL_DestroyWindow(this->window);
SDL_Quit();
}
void Application::run() {
void CanvasEngine::run() {
// start tracking time
std::timespec_get(&this->startup_ts, TIME_UTC);
this->frame_start_ts = this->last_frame_start_ts = this->startup_ts;
@ -55,23 +59,38 @@ void Application::run() {
}
}
void Application::request_close() {
void CanvasEngine::request_close() {
this->stay_open = false;
}
void Application::set_target_delta_time(double target) {
void CanvasEngine::set_target_delta_time(double target) {
this->target_delta_time = target;
}
void Application::process_events() {
void CanvasEngine::process_events() {
SDL_Event evt{};
while(SDL_PollEvent(&evt)) {
switch(evt.type) {
case SDL_QUIT:
this->request_close();
break;
default:break;
}
while(SDL_PollEvent(&evt))
this->process_event(evt);
}
void CanvasEngine::process_event(SDL_Event const &evt) {
switch(evt.type) {
case SDL_QUIT:
this->request_close();
break;
case SDL_KEYDOWN:
break;
case SDL_KEYUP:
break;
default:
break;
}
}
void CanvasEngine::tick(double delta_time) {
}
void CanvasEngine::draw(SDL_Renderer *render) {
SDL_SetRenderDrawColor(this->render, 0, 0, 0, 255);
SDL_RenderClear(render);
}
}

View File

@ -1,33 +1,35 @@
#ifndef CORE_APPLICATION_HPP
#define CORE_APPLICATION_HPP
#ifndef CANVAS_ENGINE_HPP
#define CANVAS_ENGINE_HPP
#include <ctime>
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_render.h>
#include <ctime>
class Application {
public: Application();
virtual ~Application();
void run();
virtual void process_event(SDL_Event const &event) = 0;
virtual void tick(double delta_time) = 0;
virtual void draw(SDL_Renderer *render) = 0;
void request_close();
void set_target_delta_time(double target);
namespace ce {
class CanvasEngine {
private:
void process_events();
protected:
SDL_Window *window{nullptr}; //!< primary application window
SDL_Renderer *render{nullptr}; //!< primary application window's renderer
private:
std::timespec startup_ts{}; //!< time in UTC at start of application (when run() was called)
std::timespec last_frame_start_ts{}; //!< time at start of last frame
std::timespec frame_start_ts{}; //!< time at start of this frame
double delta_time{0.f};
double target_delta_time{};
bool stay_open{false}; //!< application loop will continue so long as this is true
};
public:
CanvasEngine();
~CanvasEngine();
#endif // !CORE_APPLICATION_HPP
void run();
void request_close();
void set_target_delta_time(double target);
private:
void process_events();
void process_event(SDL_Event const &evt);
void tick(double delta_time);
void draw(SDL_Renderer *render);
};
}
#endif // !CANVAS_ENGINE_HPP

View File

@ -1,12 +0,0 @@
#include "game.hpp"
void Game::process_event(SDL_Event const &evt) {
}
void Game::tick(double delta_time) {
}
void Game::draw(SDL_Renderer *render) {
SDL_SetRenderDrawColor(this->render, 0, 0, 0, 255);
SDL_RenderClear(render);
}

View File

@ -1,13 +0,0 @@
#ifndef GAME_HPP
#define GAME_HPP
#include "core/application.hpp"
class Game : public Application {
public:
virtual void process_event(SDL_Event const &evt) override;
virtual void tick(double delta_time) override;
virtual void draw(SDL_Renderer *render) override;
};
#endif // !GAME_HPP

View File

@ -1,6 +1,6 @@
#include "game.hpp"
#include "core/canvas_engine.hpp"
int main(int argc, char* argv[]) {
Game game{};
game.run();
ce::CanvasEngine engine{};
engine.run();
}