application refactoring

This commit is contained in:
2026-03-16 20:13:48 +01:00
parent 13286719ab
commit 370e0f9c5a
2 changed files with 45 additions and 56 deletions

View File

@@ -1,17 +1,25 @@
#include "Application.h" #include "Application.h"
#include "Event.h" #include "Event.h"
#include "Layer.h"
#include "Log.h" #include "Log.h"
#include "SDL3/SDL_main.h"
#include "SDL3/SDL_render.h" #include "SDL3/SDL_render.h"
#include "imgui.h" #include "imgui.h"
#include "imgui_impl_sdl3.h" #include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlrenderer3.h" #include "imgui_impl_sdlrenderer3.h"
Application::Application() : m_Window(nullptr), m_Renderer(nullptr), m_Surface(nullptr), m_isRunning(false) {} #define SDL_MAIN_HANDLED 1
Application *Application::s_Instance = nullptr;
Application::Application() : m_Window(nullptr), m_Renderer(nullptr), m_Surface(nullptr), m_isRunning(false) { SDL_SetMainReady(); }
Application::~Application() { Shutdown(); } Application::~Application() { Shutdown(); }
bool Application::Init() { bool Application::Init() {
SetRunningState(true); SetRunningState(true);
s_Instance = this;
SakuraVNE::Log::Init(); SakuraVNE::Log::Init();
LOG_INFO("Initialized logger library"); LOG_INFO("Initialized logger library");
@@ -61,29 +69,23 @@ bool Application::Init() {
SDL_SetRenderVSync(m_Renderer, 1); SDL_SetRenderVSync(m_Renderer, 1);
// Imgui init m_ImGuiLayer = new SakuraVNE::ImGuiLayer();
IMGUI_CHECKVERSION(); PushOverlay(m_ImGuiLayer);
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplSDL3_InitForSDLRenderer(m_Window, m_Renderer);
ImGui_ImplSDLRenderer3_Init(m_Renderer);
return true; return true;
} }
void Application::Run() { void Application::Run() {
bool demoWindowShow = false;
bool showOtherWindow = false;
ImVec4 clearColor = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
ImGuiIO &io = ImGui::GetIO(); ImGuiIO &io = ImGui::GetIO();
while (GetRunningState()) { while (GetRunningState()) {
float time = 0;
for (auto layer : m_LayerStack) {
layer->OnFrame(time);
}
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
@@ -104,45 +106,11 @@ void Application::Run() {
SetSDLWindowSurface(SDL_GetWindowSurface(GetSDLWindow())); SetSDLWindowSurface(SDL_GetWindowSurface(GetSDLWindow()));
} }
} }
//
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
if (demoWindowShow) {
ImGui::ShowDemoWindow(&demoWindowShow);
}
// imgui demo window stuff or frame stuff
static float f = 0.0f;
static int counter = 0;
ImGui::Begin("Hello World!"); // creates window and add later stuff to it
ImGui::Text("text stuff");
ImGui::Checkbox("Demo checkbox", &showOtherWindow);
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color edit", (float *)&clearColor);
if (ImGui::Button("Button"))
counter++;
ImGui::SameLine();
ImGui::Text("counter = %d", counter);
ImGui::Text("Application avg %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
if (ImGui::Button("Quit")) {
SetRunningState(false);
}
ImGui::End();
// Rendering // Rendering
ImGui::Render(); ImGui::Render();
SDL_SetRenderScale(m_Renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); SDL_SetRenderScale(m_Renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
SDL_SetRenderDrawColor(m_Renderer, (Uint8)(clearColor.x * 255), (Uint8)(clearColor.y * 255), (Uint8)(clearColor.z * 255), (Uint8)(clearColor.w * 255)); SDL_SetRenderDrawColor(m_Renderer, (Uint8)255, (Uint8)255, (Uint8)255, (Uint8)255);
SDL_RenderClear(m_Renderer); SDL_RenderClear(m_Renderer);
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), m_Renderer); ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), m_Renderer);
SDL_RenderPresent(m_Renderer); SDL_RenderPresent(m_Renderer);
@@ -152,10 +120,6 @@ void Application::Run() {
void Application::Shutdown() { void Application::Shutdown() {
LOG_WARN("Shutting down the application!"); LOG_WARN("Shutting down the application!");
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
SDL_DestroyRenderer(m_Renderer); SDL_DestroyRenderer(m_Renderer);
// Destroy window // Destroy window
@@ -164,3 +128,12 @@ void Application::Shutdown() {
// Quit SDL subsystems // Quit SDL subsystems
SDL_Quit(); SDL_Quit();
} }
void Application::PushLayer(SakuraVNE::Layer *layer) {
m_LayerStack.PushLayer(layer);
layer->OnAttach();
}
void Application::PushOverlay(SakuraVNE::Layer *layer) {
m_LayerStack.PushOverLay(layer);
layer->OnAttach();
}

View File

@@ -1,7 +1,12 @@
#pragma once #pragma once
#include "Layer.h"
#include "LayerStack.h"
#include "SDL3/SDL.h" #include "SDL3/SDL.h"
#include "imguilayer.h"
// TODO: Move this out to its own class(?) so it can be defined by the application
// maybe this could be used as a fallback if the app did not define one
struct WindowData { struct WindowData {
int width = 1280; int width = 1280;
int height = 720; int height = 720;
@@ -19,14 +24,20 @@ public:
void Run(); void Run();
void Shutdown(); void Shutdown();
void PushLayer(SakuraVNE::Layer *);
void PushOverlay(SakuraVNE::Layer *);
inline WindowData &GetWindowData() { return m_WindowData; } inline WindowData &GetWindowData() { return m_WindowData; }
inline SDL_Window *GetSDLWindow() { return m_Window; } inline SDL_Window *GetSDLWindow() { return m_Window; }
inline SDL_Renderer *GetSDLRenderer() { return m_Renderer; }
inline SDL_Surface *GetSDLWindowSurface() { return m_Surface; } inline SDL_Surface *GetSDLWindowSurface() { return m_Surface; }
inline void SetSDLWindowSurface(SDL_Surface *newSurface) { m_Surface = newSurface; } inline void SetSDLWindowSurface(SDL_Surface *newSurface) { m_Surface = newSurface; }
bool &GetRunningState() { return m_isRunning; } bool &GetRunningState() { return m_isRunning; }
void SetRunningState(bool isRunning) { m_isRunning = isRunning; } void SetRunningState(bool isRunning) { m_isRunning = isRunning; }
static Application &Get() { return *s_Instance; }
private: private:
SDL_Window *m_Window; SDL_Window *m_Window;
SDL_Surface *m_Surface; SDL_Surface *m_Surface;
@@ -35,4 +46,9 @@ private:
WindowData m_WindowData; WindowData m_WindowData;
bool m_isRunning; bool m_isRunning;
SakuraVNE::LayerStack m_LayerStack;
static Application *s_Instance;
SakuraVNE::ImGuiLayer *m_ImGuiLayer;
}; };