Compare commits
3 Commits
e152bf93b3
...
370e0f9c5a
| Author | SHA1 | Date | |
|---|---|---|---|
| 370e0f9c5a | |||
| 13286719ab | |||
| 4bc043e5a1 |
@@ -1,17 +1,25 @@
|
||||
#include "Application.h"
|
||||
#include "Event.h"
|
||||
#include "Layer.h"
|
||||
#include "Log.h"
|
||||
#include "SDL3/SDL_main.h"
|
||||
#include "SDL3/SDL_render.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_sdl3.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(); }
|
||||
|
||||
bool Application::Init() {
|
||||
SetRunningState(true);
|
||||
|
||||
s_Instance = this;
|
||||
|
||||
SakuraVNE::Log::Init();
|
||||
LOG_INFO("Initialized logger library");
|
||||
|
||||
@@ -61,29 +69,23 @@ bool Application::Init() {
|
||||
|
||||
SDL_SetRenderVSync(m_Renderer, 1);
|
||||
|
||||
// Imgui init
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
ImGui_ImplSDL3_InitForSDLRenderer(m_Window, m_Renderer);
|
||||
ImGui_ImplSDLRenderer3_Init(m_Renderer);
|
||||
m_ImGuiLayer = new SakuraVNE::ImGuiLayer();
|
||||
PushOverlay(m_ImGuiLayer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Application::Run() {
|
||||
bool demoWindowShow = false;
|
||||
bool showOtherWindow = false;
|
||||
ImVec4 clearColor = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||
|
||||
ImGuiIO &io = ImGui::GetIO();
|
||||
|
||||
while (GetRunningState()) {
|
||||
|
||||
float time = 0;
|
||||
|
||||
for (auto layer : m_LayerStack) {
|
||||
layer->OnFrame(time);
|
||||
}
|
||||
|
||||
SDL_Event event;
|
||||
|
||||
while (SDL_PollEvent(&event)) {
|
||||
@@ -104,45 +106,11 @@ void Application::Run() {
|
||||
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
|
||||
ImGui::Render();
|
||||
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);
|
||||
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), m_Renderer);
|
||||
SDL_RenderPresent(m_Renderer);
|
||||
@@ -152,10 +120,6 @@ void Application::Run() {
|
||||
void Application::Shutdown() {
|
||||
LOG_WARN("Shutting down the application!");
|
||||
|
||||
ImGui_ImplSDLRenderer3_Shutdown();
|
||||
ImGui_ImplSDL3_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
SDL_DestroyRenderer(m_Renderer);
|
||||
|
||||
// Destroy window
|
||||
@@ -164,3 +128,12 @@ void Application::Shutdown() {
|
||||
// Quit SDL subsystems
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "Layer.h"
|
||||
#include "LayerStack.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 {
|
||||
int width = 1280;
|
||||
int height = 720;
|
||||
@@ -19,14 +24,20 @@ public:
|
||||
void Run();
|
||||
void Shutdown();
|
||||
|
||||
void PushLayer(SakuraVNE::Layer *);
|
||||
void PushOverlay(SakuraVNE::Layer *);
|
||||
|
||||
inline WindowData &GetWindowData() { return m_WindowData; }
|
||||
inline SDL_Window *GetSDLWindow() { return m_Window; }
|
||||
inline SDL_Renderer *GetSDLRenderer() { return m_Renderer; }
|
||||
inline SDL_Surface *GetSDLWindowSurface() { return m_Surface; }
|
||||
inline void SetSDLWindowSurface(SDL_Surface *newSurface) { m_Surface = newSurface; }
|
||||
|
||||
bool &GetRunningState() { return m_isRunning; }
|
||||
void SetRunningState(bool isRunning) { m_isRunning = isRunning; }
|
||||
|
||||
static Application &Get() { return *s_Instance; }
|
||||
|
||||
private:
|
||||
SDL_Window *m_Window;
|
||||
SDL_Surface *m_Surface;
|
||||
@@ -35,4 +46,9 @@ private:
|
||||
WindowData m_WindowData;
|
||||
|
||||
bool m_isRunning;
|
||||
|
||||
SakuraVNE::LayerStack m_LayerStack;
|
||||
|
||||
static Application *s_Instance;
|
||||
SakuraVNE::ImGuiLayer *m_ImGuiLayer;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Layer.h"
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
|
||||
namespace SakuraVNE {
|
||||
@@ -14,6 +15,16 @@ public:
|
||||
void PushOverLay(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
|
||||
// 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; }
|
||||
|
||||
46
SakuraCore/src/imguilayer.cpp
Normal file
46
SakuraCore/src/imguilayer.cpp
Normal 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
|
||||
21
SakuraCore/src/imguilayer.h
Normal file
21
SakuraCore/src/imguilayer.h
Normal 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
|
||||
Reference in New Issue
Block a user