From af97656cc7d9f59dc3fea99374e861b250ef1efe Mon Sep 17 00:00:00 2001 From: Sara Date: Fri, 15 Nov 2024 11:21:52 +0100 Subject: [PATCH] feat: started work on application --- .gitignore | 1 + .sconsign.dblite | Bin 2228 -> 0 bytes SConstruct | 13 ++++++- src/core/application.cpp | 76 +++++++++++++++++++++++++++++++++++++++ src/core/application.hpp | 33 +++++++++++++++++ src/main.cpp | 11 +++--- 6 files changed, 127 insertions(+), 7 deletions(-) delete mode 100644 .sconsign.dblite create mode 100644 src/core/application.cpp create mode 100644 src/core/application.hpp diff --git a/.gitignore b/.gitignore index e048dfe..e105966 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # binaries *.o game +.sconsign.dblite diff --git a/.sconsign.dblite b/.sconsign.dblite deleted file mode 100644 index 82a4aa582599ac86dfddb17184ba0d4cbf128da4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2228 zcmd^AO>Y}T7)~3PxJg5ytq=ksZ4)1Ws?N^m&K_gqlsJh~IYw#^RWx7g-72vyuZ;u} zQn>F4rvHEo7sQn-;y1t*iNC>tnf)+M5rhj|y0UiWeaG|eJJ0*P&s@}Ad|O}1pD)b& z*|nx=soKiQMeW5?JGjTW*H7BxQ97H><;-NS=SDMsC&Q7+Hmhl8G@E{AzBV6xY22`43t*DCj>_{3L(^-lQ5!?xkwimzeP6E_rKp>M7o=KKBFw8B8UJ(L=nIP09;%1bGYC7Xun^rTPvTM_-$9S9ZQ#;;e!YE8wIZKR+za%Vx%OI zHZPz!!5|VoReGC_bDCvz79AI1E`UX zc*qe$fcrr}eU4;~=N-#~|FSYf0xYyt0kkEhkouToEJBJ24OkeWzo|@2{@0>7P<#oQ zVl+S?Pyzr-1F9sH8Zf3@QA(s|=>vih z)Kqc|1+wi82-66%fH<;87-K7JOS<%xRf +#include +#include + +#ifndef PROJECTNAME +#define PROJECTNAME "application" +#endif + +Application::Application() { + if(SDL_Init(SDL_INIT_EVERYTHING) != 0) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to initialize SDL, SDL error: %s", SDL_GetError()); + return; + } + this->window = SDL_CreateWindow(PROJECTNAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 800, SDL_WINDOW_FULLSCREEN); + if(this->window == nullptr) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create window, SDL error: %s", SDL_GetError()); + SDL_Quit(); + return; + } + this->render = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if(this->render == nullptr) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to initialize renderer, SDL error: %s", SDL_GetError()); + SDL_DestroyWindow(this->window); + SDL_Quit(); + return; + } + this->stay_open = true; +} + +Application::~Application() { + SDL_DestroyRenderer(this->render); + SDL_DestroyWindow(this->window); + SDL_Quit(); +} + +void Application::run() { + // start tracking time + std::timespec_get(&this->startup_ts, TIME_UTC); + this->frame_start_ts = this->last_frame_start_ts = this->startup_ts; + // main application loop + while(stay_open) { + // track frame time + std::timespec_get(&this->frame_start_ts, TIME_UTC); + this->delta_time = double(this->last_frame_start_ts.tv_nsec - this->frame_start_ts.tv_nsec) / 1000000000.f; + // process events + this->process_events(); + // update application implementation + if(this->delta_time > this->target_delta_time) { + this->tick(this->delta_time); + this->draw(render); + this->last_frame_start_ts = this->frame_start_ts; + } + } +} + +void Application::request_close() { + this->stay_open = false; +} + +void Application::set_target_delta_time(double target) { + this->target_delta_time = target; +} + +void Application::process_events() { + SDL_Event evt{}; + while(SDL_PollEvent(&evt)) { + switch(evt.type) { + case SDL_QUIT: + this->request_close(); + break; + default:break; + } + this->process_event(evt); + } +} diff --git a/src/core/application.hpp b/src/core/application.hpp new file mode 100644 index 0000000..09497f8 --- /dev/null +++ b/src/core/application.hpp @@ -0,0 +1,33 @@ +#ifndef CORE_APPLICATION_HPP +#define CORE_APPLICATION_HPP + +#include +#include +#include +#include + +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); +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 +}; + +#endif // !CORE_APPLICATION_HPP diff --git a/src/main.cpp b/src/main.cpp index bd91c67..dc5b5a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,13 +18,12 @@ int main(int argc, char* argv[]) { while(keep_open) { SDL_PollEvent(&event); switch(event.type) { - case SDL_QUIT: - keep_open = false; - break; - default: - break; + case SDL_QUIT: + keep_open = false; + break; + default: + break; } - SDL_RenderClear(render); SDL_RenderPresent(render); }