2024-06-23 20:42:33 +02:00
|
|
|
#include "Application.h"
|
|
|
|
|
#include "Event.h"
|
2026-02-10 13:07:38 +01:00
|
|
|
#include "Log.h"
|
2026-03-10 10:18:40 +01:00
|
|
|
#include "SDL3/SDL_render.h"
|
2024-06-23 20:42:33 +02:00
|
|
|
#include "imgui.h"
|
2026-02-10 13:07:38 +01:00
|
|
|
#include "imgui_impl_sdl3.h"
|
2026-02-26 15:34:52 +01:00
|
|
|
#include "imgui_impl_sdlrenderer3.h"
|
2024-06-23 20:42:33 +02:00
|
|
|
|
2026-03-09 21:45:23 +01:00
|
|
|
Application::Application() : m_Window(nullptr), m_Renderer(nullptr), m_Surface(nullptr), m_isRunning(false) {}
|
2026-02-10 13:07:38 +01:00
|
|
|
Application::~Application() { Shutdown(); }
|
2024-06-23 20:42:33 +02:00
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
bool Application::Init() {
|
2024-06-23 20:42:33 +02:00
|
|
|
SetRunningState(true);
|
|
|
|
|
|
|
|
|
|
SakuraVNE::Log::Init();
|
|
|
|
|
LOG_INFO("Initialized logger library");
|
|
|
|
|
|
|
|
|
|
LOG_INFO("window width: {0}, height: {1}", GetWindowData().width, GetWindowData().height);
|
|
|
|
|
|
|
|
|
|
// Init sdl
|
2026-03-09 21:29:12 +01:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) == 0) {
|
2024-06-23 20:42:33 +02:00
|
|
|
LOG_ERROR("SDL could not be initialized! {0}", SDL_GetError());
|
|
|
|
|
Shutdown();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 21:31:23 +01:00
|
|
|
SDL_WindowFlags windowFlags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE);
|
|
|
|
|
m_Window = SDL_CreateWindow(GetWindowData().title, GetWindowData().width, GetWindowData().height, windowFlags);
|
2024-06-23 20:42:33 +02:00
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
if (!m_Window) {
|
2024-06-23 20:42:33 +02:00
|
|
|
LOG_ERROR("SDL window could not be created! {0}", SDL_GetError());
|
|
|
|
|
Shutdown();
|
|
|
|
|
|
|
|
|
|
return false;
|
2026-02-10 13:07:38 +01:00
|
|
|
} else {
|
2024-06-23 20:42:33 +02:00
|
|
|
LOG_INFO("SDl window created");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 15:26:11 +01:00
|
|
|
if (GetWindowData().pos_x != -1 && GetWindowData().pos_y != -1) {
|
|
|
|
|
if (!SDL_SetWindowPosition(m_Window, GetWindowData().pos_x, GetWindowData().pos_y)) {
|
2026-03-09 21:29:36 +01:00
|
|
|
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);
|
2026-03-09 15:26:11 +01:00
|
|
|
}
|
2026-03-10 10:20:06 +01:00
|
|
|
} else {
|
|
|
|
|
LOG_WARN("SDL window position not set. Will not attempt to set window position.");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-10 10:18:40 +01:00
|
|
|
LOG_INFO("Available renderer drivers:");
|
|
|
|
|
for (int i = 0; i < SDL_GetNumRenderDrivers(); i++) {
|
|
|
|
|
LOG_INFO("{0}. {1}", i + 1, SDL_GetRenderDriver(i));
|
2026-03-09 15:26:11 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-26 15:34:52 +01:00
|
|
|
m_Renderer = SDL_CreateRenderer(m_Window, nullptr);
|
2026-02-10 13:07:38 +01:00
|
|
|
if (!m_Renderer) {
|
2024-06-23 20:42:33 +02:00
|
|
|
LOG_ERROR("Renderer could not be created! {0}", SDL_GetError());
|
2026-02-10 13:07:38 +01:00
|
|
|
} else {
|
2024-06-23 20:42:33 +02:00
|
|
|
LOG_INFO("SDL renderer created");
|
2026-03-10 10:18:40 +01:00
|
|
|
LOG_INFO("Renderer: {0}", SDL_GetRendererName(m_Renderer));
|
2024-06-23 20:42:33 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-09 21:30:25 +01:00
|
|
|
SDL_SetRenderVSync(m_Renderer, 1);
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
// Imgui init
|
2024-06-23 20:42:33 +02:00
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
|
ImGui::CreateContext();
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
ImGuiIO &io = ImGui::GetIO();
|
2024-06-23 20:42:33 +02:00
|
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
|
|
|
|
|
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
|
|
2026-02-26 15:34:52 +01:00
|
|
|
ImGui_ImplSDL3_InitForSDLRenderer(m_Window, m_Renderer);
|
|
|
|
|
ImGui_ImplSDLRenderer3_Init(m_Renderer);
|
2024-06-23 20:42:33 +02:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
void Application::Run() {
|
2026-03-09 21:31:23 +01:00
|
|
|
bool demoWindowShow = false;
|
2024-06-23 20:42:33 +02:00
|
|
|
bool showOtherWindow = false;
|
|
|
|
|
ImVec4 clearColor = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
ImGuiIO &io = ImGui::GetIO();
|
2024-06-23 20:42:33 +02:00
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
while (GetRunningState()) {
|
2024-06-24 19:53:47 +02:00
|
|
|
SDL_Event event;
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
while (SDL_PollEvent(&event)) {
|
2026-02-26 15:34:52 +01:00
|
|
|
ImGui_ImplSDL3_ProcessEvent(&event);
|
2024-06-24 20:06:35 +02:00
|
|
|
|
2026-02-26 15:34:52 +01:00
|
|
|
if (event.type == SDL_EVENT_QUIT) {
|
2024-06-24 19:53:47 +02:00
|
|
|
SetRunningState(false);
|
|
|
|
|
LOG_INFO("Running state: {0}", GetRunningState());
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 15:34:52 +01:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(GetSDLWindow())) {
|
2024-06-24 19:53:47 +02:00
|
|
|
SetRunningState(false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-26 15:34:52 +01:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_RESIZED) {
|
2024-06-24 19:53:47 +02:00
|
|
|
SDL_GetWindowSize(GetSDLWindow(), &GetWindowData().width, &GetWindowData().height);
|
|
|
|
|
|
|
|
|
|
SetSDLWindowSurface(SDL_GetWindowSurface(GetSDLWindow()));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-23 20:42:33 +02:00
|
|
|
|
2026-02-26 15:34:52 +01:00
|
|
|
ImGui_ImplSDLRenderer3_NewFrame();
|
|
|
|
|
ImGui_ImplSDL3_NewFrame();
|
2024-06-23 20:42:33 +02:00
|
|
|
ImGui::NewFrame();
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
if (demoWindowShow) {
|
2024-06-23 20:42:33 +02:00
|
|
|
ImGui::ShowDemoWindow(&demoWindowShow);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
// imgui demo window stuff or frame stuff
|
2024-06-23 20:42:33 +02:00
|
|
|
static float f = 0.0f;
|
|
|
|
|
static int counter = 0;
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
ImGui::Begin("Hello World!"); // creates window and add later stuff to it
|
|
|
|
|
|
2024-06-23 20:42:33 +02:00
|
|
|
ImGui::Text("text stuff");
|
|
|
|
|
ImGui::Checkbox("Demo checkbox", &showOtherWindow);
|
|
|
|
|
|
|
|
|
|
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
|
2026-02-10 13:07:38 +01:00
|
|
|
ImGui::ColorEdit3("clear color edit", (float *)&clearColor);
|
2024-06-23 20:42:33 +02:00
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
if (ImGui::Button("Button"))
|
|
|
|
|
counter++;
|
2024-06-23 20:42:33 +02:00
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
ImGui::Text("counter = %d", counter);
|
|
|
|
|
|
|
|
|
|
ImGui::Text("Application avg %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
2026-03-04 20:10:35 +01:00
|
|
|
|
|
|
|
|
if (ImGui::Button("Quit")) {
|
|
|
|
|
SetRunningState(false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-23 20:42:33 +02:00
|
|
|
ImGui::End();
|
2026-02-10 13:07:38 +01:00
|
|
|
|
|
|
|
|
// Rendering
|
2024-06-23 20:42:33 +02:00
|
|
|
ImGui::Render();
|
2026-02-26 15:34:52 +01:00
|
|
|
SDL_SetRenderScale(m_Renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
|
2024-06-23 20:42:33 +02:00
|
|
|
SDL_SetRenderDrawColor(m_Renderer, (Uint8)(clearColor.x * 255), (Uint8)(clearColor.y * 255), (Uint8)(clearColor.z * 255), (Uint8)(clearColor.w * 255));
|
|
|
|
|
SDL_RenderClear(m_Renderer);
|
2026-02-26 15:34:52 +01:00
|
|
|
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), m_Renderer);
|
2024-06-23 20:42:33 +02:00
|
|
|
SDL_RenderPresent(m_Renderer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 13:07:38 +01:00
|
|
|
void Application::Shutdown() {
|
2024-06-23 20:42:33 +02:00
|
|
|
LOG_WARN("Shutting down the application!");
|
|
|
|
|
|
2026-02-26 15:34:52 +01:00
|
|
|
ImGui_ImplSDLRenderer3_Shutdown();
|
|
|
|
|
ImGui_ImplSDL3_Shutdown();
|
2024-06-23 20:42:33 +02:00
|
|
|
ImGui::DestroyContext();
|
|
|
|
|
|
|
|
|
|
SDL_DestroyRenderer(m_Renderer);
|
|
|
|
|
|
|
|
|
|
// Destroy window
|
|
|
|
|
SDL_DestroyWindow(m_Window);
|
|
|
|
|
|
|
|
|
|
// Quit SDL subsystems
|
|
|
|
|
SDL_Quit();
|
2026-02-10 13:07:38 +01:00
|
|
|
}
|