feat: canvasengine singleton
parent
23a913c9f1
commit
d1c3205810
|
@ -1,9 +1,11 @@
|
|||
#include "canvas_engine.hpp"
|
||||
#include <cassert>
|
||||
#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>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
|
||||
#ifndef PROJECTNAME
|
||||
|
@ -11,6 +13,8 @@
|
|||
#endif
|
||||
|
||||
namespace ce {
|
||||
CanvasEngine *CanvasEngine::singleton_instance{nullptr};
|
||||
|
||||
CanvasEngine::CanvasEngine() {
|
||||
if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to initialize SDL, SDL error: %s", SDL_GetError());
|
||||
|
@ -39,6 +43,9 @@ CanvasEngine::~CanvasEngine() {
|
|||
}
|
||||
|
||||
void CanvasEngine::run() {
|
||||
assert(CanvasEngine::singleton_instance == nullptr && "Engine singleton instance already assigned, starting another instance is invalid");
|
||||
// register as singleton
|
||||
CanvasEngine::singleton_instance = this;
|
||||
// start tracking time
|
||||
std::timespec_get(&this->startup_ts, TIME_UTC);
|
||||
this->frame_start_ts = this->last_frame_start_ts = this->startup_ts;
|
||||
|
@ -57,6 +64,8 @@ void CanvasEngine::run() {
|
|||
this->last_frame_start_ts = this->frame_start_ts;
|
||||
}
|
||||
}
|
||||
assert(CanvasEngine::singleton_instance == this && "Engine singleton instance changed while game was running");
|
||||
CanvasEngine::singleton_instance = nullptr;
|
||||
}
|
||||
|
||||
void CanvasEngine::request_close() {
|
||||
|
@ -86,7 +95,7 @@ void CanvasEngine::process_event(SDL_Event const &evt) {
|
|||
}
|
||||
}
|
||||
|
||||
void CanvasEngine::tick(double delta_time) {
|
||||
void CanvasEngine::tick(double delta_time [[maybe_unused]]) {
|
||||
}
|
||||
|
||||
void CanvasEngine::draw(SDL_Renderer *render) {
|
||||
|
|
|
@ -1,14 +1,26 @@
|
|||
#ifndef CANVAS_ENGINE_HPP
|
||||
#define CANVAS_ENGINE_HPP
|
||||
|
||||
#include "node.hpp"
|
||||
#include <ctime>
|
||||
#include <cassert>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_events.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
|
||||
#if __has_cpp_attribute(gnu::always_inline)
|
||||
#define forceinline [[gnu::always_inline]] inline
|
||||
#elif __has_cpp_attribute(clang::always_inline)
|
||||
#define forceinline [[clang::always_inline]] inline
|
||||
#else
|
||||
#define forceinline inline
|
||||
#endif
|
||||
|
||||
namespace ce {
|
||||
class CanvasEngine {
|
||||
private:
|
||||
static CanvasEngine *singleton_instance; //!< currently active engine instance
|
||||
SDL_Window *window{nullptr}; //!< primary application window
|
||||
SDL_Renderer *render{nullptr}; //!< primary application window's renderer
|
||||
std::timespec startup_ts{}; //!< time in UTC at start of application (when run() was called)
|
||||
|
@ -20,7 +32,11 @@ private:
|
|||
public:
|
||||
CanvasEngine();
|
||||
~CanvasEngine();
|
||||
|
||||
forceinline CanvasEngine *get_singleton(){
|
||||
assert(CanvasEngine::singleton_instance && "Engine singleton instance not assigned yet, call run() before attempting to access");
|
||||
return CanvasEngine::singleton_instance;
|
||||
}
|
||||
void setup(std::unique_ptr<Node> scene_root);
|
||||
void run();
|
||||
void request_close();
|
||||
void set_target_delta_time(double target);
|
||||
|
|
Loading…
Reference in New Issue