Compare commits

...

3 Commits

Author SHA1 Message Date
370e0f9c5a application refactoring 2026-03-16 20:13:48 +01:00
13286719ab imgui layer 2026-03-16 20:13:33 +01:00
4bc043e5a1 layerstack iterator functions 2026-03-16 20:13:27 +01:00
5 changed files with 123 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;
}; };

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Layer.h" #include "Layer.h"
#include <iterator>
#include <vector> #include <vector>
namespace SakuraVNE { namespace SakuraVNE {
@@ -14,6 +15,16 @@ public:
void PushOverLay(Layer *layer); void PushOverLay(Layer *layer);
void PopOverlay(Layer *layer); void PopOverlay(Layer *layer);
std::vector<Layer *>::iterator begin() { return m_LayerStack.begin(); }
std::vector<Layer *>::iterator end() { return m_LayerStack.end(); }
std::vector<Layer *>::reverse_iterator rbegin() { return m_LayerStack.rbegin(); }
std::vector<Layer *>::reverse_iterator rend() { return m_LayerStack.rend(); }
std::vector<Layer *>::const_iterator begin() const { return m_LayerStack.begin(); }
std::vector<Layer *>::const_iterator end() const { return m_LayerStack.end(); }
std::vector<Layer *>::const_reverse_iterator rbegin() const { return m_LayerStack.rbegin(); }
std::vector<Layer *>::const_reverse_iterator rend() const { return m_LayerStack.rend(); }
#ifdef DEBUG #ifdef DEBUG
// this is only used for the tests for now, so it will be taken out of the release build // this is only used for the tests for now, so it will be taken out of the release build
inline const std::vector<Layer *> &GetLayers() const { return m_LayerStack; } inline const std::vector<Layer *> &GetLayers() const { return m_LayerStack; }

View File

@@ -0,0 +1,46 @@
#include "imguilayer.h"
#include "Application.h"
#include "Log.h"
#include "imgui.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlrenderer3.h"
namespace SakuraVNE {
ImGuiLayer::ImGuiLayer() : Layer("ImGuiLayer") {}
void ImGuiLayer::OnAttach() {
// Imgui init
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
ImGui::StyleColorsDark();
ImGui_ImplSDL3_InitForSDLRenderer(Application::Get().GetSDLWindow(), Application::Get().GetSDLRenderer());
ImGui_ImplSDLRenderer3_Init(Application::Get().GetSDLRenderer());
LOG_INFO("Imgui layer OnAttach ran");
}
void ImGuiLayer::OnDetach() {
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
LOG_WARN("Imgui on detach ran");
}
void ImGuiLayer::Begin() {
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
}
void ImGuiLayer::End() {
ImGuiIO &io = ImGui::GetIO();
Application &app = Application::Get();
io.DisplaySize = ImVec2((float)app.GetWindowData().width, (float)app.GetWindowData().height);
}
} // namespace SakuraVNE

View File

@@ -0,0 +1,21 @@
#pragma once
#include "Layer.h"
namespace SakuraVNE {
class ImGuiLayer : public Layer {
public:
ImGuiLayer();
~ImGuiLayer() = default;
// virtual void OnStart() override;
// virtual void OnFrame(float timestamp) override;
// virtual void OnEnd() override;
virtual void OnAttach() override;
virtual void OnDetach() override;
// virtual void OnEvent() override;
void Begin();
void End();
};
} // namespace SakuraVNE