reworked app init before major layer rework

This commit is contained in:
2026-04-01 18:23:51 +02:00
parent cc3f592c67
commit fb06a38c83
3 changed files with 34 additions and 19 deletions

View File

@@ -13,7 +13,10 @@
Application *Application::s_Instance = nullptr; Application *Application::s_Instance = nullptr;
Application::Application() : m_Window(nullptr), m_Renderer(nullptr), m_Surface(nullptr), m_isRunning(false) { SDL_SetMainReady(); } Application::Application(const AppData &appdata) : m_Window(nullptr), m_Renderer(nullptr), m_Surface(nullptr), m_isRunning(false), m_AppData(appdata) {
SDL_SetMainReady();
m_initResult = Init();
}
Application::~Application() { Shutdown(); } Application::~Application() { Shutdown(); }
bool Application::Init() { bool Application::Init() {
@@ -24,7 +27,7 @@ bool Application::Init() {
SakuraVNE::Log::Init(); SakuraVNE::Log::Init();
LOG_INFO("Initialized logger library"); LOG_INFO("Initialized logger library");
LOG_INFO("window width: {0}, height: {1}", GetWindowData().width, GetWindowData().height); LOG_INFO("window width: {0}, height: {1}", GetAppData().windowdata.width, GetAppData().windowdata.height);
// Init sdl // Init sdl
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) == 0) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) == 0) {
@@ -34,7 +37,7 @@ bool Application::Init() {
} }
SDL_WindowFlags windowFlags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE); SDL_WindowFlags windowFlags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE);
m_Window = SDL_CreateWindow(GetWindowData().title, GetWindowData().width, GetWindowData().height, windowFlags); m_Window = SDL_CreateWindow(GetAppData().windowdata.title.c_str(), GetAppData().windowdata.width, GetAppData().windowdata.height, windowFlags);
if (!m_Window) { if (!m_Window) {
LOG_ERROR("SDL window could not be created! {0}", SDL_GetError()); LOG_ERROR("SDL window could not be created! {0}", SDL_GetError());
@@ -45,11 +48,11 @@ bool Application::Init() {
LOG_INFO("SDl window created"); LOG_INFO("SDl window created");
} }
if (GetWindowData().pos_x != -1 && GetWindowData().pos_y != -1) { if (GetAppData().windowdata.pos_x != -1 && GetAppData().windowdata.pos_y != -1) {
if (!SDL_SetWindowPosition(m_Window, GetWindowData().pos_x, GetWindowData().pos_y)) { if (!SDL_SetWindowPosition(m_Window, GetAppData().windowdata.pos_x, GetAppData().windowdata.pos_y)) {
LOG_ERROR("Failed to set SDL_Window position {0}", SDL_GetError()); LOG_ERROR("Failed to set SDL_Window position {0}", SDL_GetError());
} else { } else {
LOG_INFO("SDL window position set to the initial value: x {0}, y {1}", GetWindowData().pos_x, GetWindowData().pos_y); LOG_INFO("SDL window position set to the initial value: x {0}, y {1}", GetAppData().windowdata.pos_x, GetAppData().windowdata.pos_y);
} }
} else { } else {
LOG_WARN("SDL window position not set. Will not attempt to set window position."); LOG_WARN("SDL window position not set. Will not attempt to set window position.");
@@ -77,6 +80,10 @@ bool Application::Init() {
} }
void Application::Run() { void Application::Run() {
if (!m_initResult) {
return;
}
ImGuiIO &io = ImGui::GetIO(); ImGuiIO &io = ImGui::GetIO();
Uint64 oldTime = SDL_GetTicks(); Uint64 oldTime = SDL_GetTicks();
@@ -109,7 +116,7 @@ void Application::Run() {
} }
if (event.type == SDL_EVENT_WINDOW_RESIZED) { if (event.type == SDL_EVENT_WINDOW_RESIZED) {
SDL_GetWindowSize(GetSDLWindow(), &GetWindowData().width, &GetWindowData().height); SDL_GetWindowSize(GetSDLWindow(), &GetAppData().windowdata.width, &GetAppData().windowdata.height);
SetSDLWindowSurface(SDL_GetWindowSurface(GetSDLWindow())); SetSDLWindowSurface(SDL_GetWindowSurface(GetSDLWindow()));
} }

View File

@@ -2,22 +2,26 @@
#include "Layer.h" #include "Layer.h"
#include "LayerStack.h" #include "LayerStack.h"
#include "SDL3/SDL.h" #include "SDL3/SDL_render.h"
#include "SDL3/SDL_video.h"
#include "imguilayer.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;
int pos_x = -1; int pos_x = -1;
int pos_y = -1; int pos_y = -1;
const char *title = "Sakura Visual Novel Engine"; std::string title = "Window Title";
};
struct AppData {
std::string name = "Application";
WindowData windowdata;
}; };
class Application { class Application {
public: public:
Application(); Application(const AppData &appdata = AppData());
~Application(); ~Application();
bool Init(); bool Init();
@@ -27,7 +31,7 @@ public:
void PushLayer(SakuraVNE::Layer *); void PushLayer(SakuraVNE::Layer *);
void PushOverlay(SakuraVNE::Layer *); void PushOverlay(SakuraVNE::Layer *);
inline WindowData &GetWindowData() { return m_WindowData; } inline AppData &GetAppData() { return m_AppData; }
inline SDL_Window *GetSDLWindow() { return m_Window; } inline SDL_Window *GetSDLWindow() { return m_Window; }
inline SDL_Renderer *GetSDLRenderer() { return m_Renderer; } inline SDL_Renderer *GetSDLRenderer() { return m_Renderer; }
inline SDL_Surface *GetSDLWindowSurface() { return m_Surface; } inline SDL_Surface *GetSDLWindowSurface() { return m_Surface; }
@@ -39,16 +43,17 @@ public:
static Application &Get() { return *s_Instance; } static Application &Get() { return *s_Instance; }
private: private:
bool m_isRunning;
bool m_initResult;
SDL_Window *m_Window; SDL_Window *m_Window;
SDL_Surface *m_Surface; SDL_Surface *m_Surface;
SDL_Renderer *m_Renderer; SDL_Renderer *m_Renderer;
WindowData m_WindowData; AppData m_AppData;
bool m_isRunning;
SakuraVNE::LayerStack m_LayerStack; SakuraVNE::LayerStack m_LayerStack;
SakuraVNE::ImGuiLayer *m_ImGuiLayer;
static Application *s_Instance; static Application *s_Instance;
SakuraVNE::ImGuiLayer *m_ImGuiLayer;
}; };

View File

@@ -16,6 +16,7 @@ void ImGuiLayer::OnAttach() {
ImGuiIO &io = ImGui::GetIO(); ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
@@ -36,13 +37,15 @@ void ImGuiLayer::Begin() {
ImGui_ImplSDLRenderer3_NewFrame(); ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame(); ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
// for docking the imgui windows to the edge of the sdl window
ImGui::DockSpaceOverViewport();
} }
void ImGuiLayer::End() { void ImGuiLayer::End() {
ImGuiIO &io = ImGui::GetIO(); ImGuiIO &io = ImGui::GetIO();
Application &app = Application::Get(); Application &app = Application::Get();
io.DisplaySize = ImVec2((float)app.GetWindowData().width, (float)app.GetWindowData().height); io.DisplaySize = ImVec2((float)app.GetAppData().windowdata.width, (float)app.GetAppData().windowdata.height);
ImGui::Render(); ImGui::Render();
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), Application::Get().GetSDLRenderer()); ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), Application::Get().GetSDLRenderer());
@@ -52,7 +55,7 @@ void ImGuiLayer::End() {
void ImGuiLayer::OnImGuiRender() { void ImGuiLayer::OnImGuiRender() {
ImGuiIO &io = ImGui::GetIO(); ImGuiIO &io = ImGui::GetIO();
bool demoWindow = true; bool demoWindow = true;
ImGui::Begin("Test window"); ImGui::Begin("Framerate");
ImGui::ShowDemoWindow(&demoWindow); ImGui::ShowDemoWindow(&demoWindow);
ImGui::Text("Application avg %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); ImGui::Text("Application avg %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);