diff --git a/SakuraCore/src/Application.cpp b/SakuraCore/src/Application.cpp index 04b68b3..2e7c611 100644 --- a/SakuraCore/src/Application.cpp +++ b/SakuraCore/src/Application.cpp @@ -13,7 +13,10 @@ 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(); } bool Application::Init() { @@ -24,7 +27,7 @@ bool Application::Init() { SakuraVNE::Log::Init(); 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 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); - 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) { LOG_ERROR("SDL window could not be created! {0}", SDL_GetError()); @@ -45,11 +48,11 @@ bool Application::Init() { LOG_INFO("SDl window created"); } - if (GetWindowData().pos_x != -1 && GetWindowData().pos_y != -1) { - if (!SDL_SetWindowPosition(m_Window, GetWindowData().pos_x, GetWindowData().pos_y)) { + if (GetAppData().windowdata.pos_x != -1 && GetAppData().windowdata.pos_y != -1) { + if (!SDL_SetWindowPosition(m_Window, GetAppData().windowdata.pos_x, GetAppData().windowdata.pos_y)) { LOG_ERROR("Failed to set SDL_Window position {0}", SDL_GetError()); } 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 { LOG_WARN("SDL window position not set. Will not attempt to set window position."); @@ -77,6 +80,10 @@ bool Application::Init() { } void Application::Run() { + if (!m_initResult) { + return; + } + ImGuiIO &io = ImGui::GetIO(); Uint64 oldTime = SDL_GetTicks(); @@ -109,7 +116,7 @@ void Application::Run() { } 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())); } diff --git a/SakuraCore/src/Application.h b/SakuraCore/src/Application.h index 8d40ae7..de04075 100644 --- a/SakuraCore/src/Application.h +++ b/SakuraCore/src/Application.h @@ -2,22 +2,26 @@ #include "Layer.h" #include "LayerStack.h" -#include "SDL3/SDL.h" +#include "SDL3/SDL_render.h" +#include "SDL3/SDL_video.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 { int width = 1280; int height = 720; int pos_x = -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 { public: - Application(); + Application(const AppData &appdata = AppData()); ~Application(); bool Init(); @@ -27,7 +31,7 @@ public: void PushLayer(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_Renderer *GetSDLRenderer() { return m_Renderer; } inline SDL_Surface *GetSDLWindowSurface() { return m_Surface; } @@ -39,16 +43,17 @@ public: static Application &Get() { return *s_Instance; } private: + bool m_isRunning; + bool m_initResult; + SDL_Window *m_Window; SDL_Surface *m_Surface; SDL_Renderer *m_Renderer; - WindowData m_WindowData; - - bool m_isRunning; + AppData m_AppData; SakuraVNE::LayerStack m_LayerStack; + SakuraVNE::ImGuiLayer *m_ImGuiLayer; static Application *s_Instance; - SakuraVNE::ImGuiLayer *m_ImGuiLayer; }; diff --git a/SakuraCore/src/imguilayer.cpp b/SakuraCore/src/imguilayer.cpp index 5427877..322aaa6 100644 --- a/SakuraCore/src/imguilayer.cpp +++ b/SakuraCore/src/imguilayer.cpp @@ -16,6 +16,7 @@ void ImGuiLayer::OnAttach() { ImGuiIO &io = ImGui::GetIO(); io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; + io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; ImGui::StyleColorsDark(); @@ -36,13 +37,15 @@ void ImGuiLayer::Begin() { ImGui_ImplSDLRenderer3_NewFrame(); ImGui_ImplSDL3_NewFrame(); ImGui::NewFrame(); + // for docking the imgui windows to the edge of the sdl window + ImGui::DockSpaceOverViewport(); } void ImGuiLayer::End() { ImGuiIO &io = ImGui::GetIO(); 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_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), Application::Get().GetSDLRenderer()); @@ -52,7 +55,7 @@ void ImGuiLayer::End() { void ImGuiLayer::OnImGuiRender() { ImGuiIO &io = ImGui::GetIO(); bool demoWindow = true; - ImGui::Begin("Test window"); + ImGui::Begin("Framerate"); ImGui::ShowDemoWindow(&demoWindow); ImGui::Text("Application avg %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);