2025-12-02 03:16:07 +02:00
|
|
|
#include "Application.h"
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <print>
|
2025-12-03 00:11:15 +02:00
|
|
|
#include <stdexcept>
|
2025-12-02 03:16:07 +02:00
|
|
|
|
2025-12-02 21:26:53 +02:00
|
|
|
#include <SDL3/SDL_events.h>
|
2025-12-02 17:57:59 +02:00
|
|
|
#include <SDL3/SDL_init.h>
|
2025-12-04 12:18:26 +02:00
|
|
|
#include <SDL3/SDL_timer.h>
|
2025-12-02 17:57:59 +02:00
|
|
|
#include <SDL3/SDL_video.h>
|
2025-12-02 03:16:07 +02:00
|
|
|
|
2025-12-03 02:31:38 +02:00
|
|
|
#include <imgui_impl_sdl3.h>
|
|
|
|
|
#include <imgui_impl_vulkan.h>
|
|
|
|
|
|
2025-12-04 12:18:26 +02:00
|
|
|
#include "Util.h"
|
2025-12-03 00:11:15 +02:00
|
|
|
#include "VulkanRenderer.h"
|
2025-12-02 17:57:59 +02:00
|
|
|
|
2025-12-02 03:16:07 +02:00
|
|
|
namespace Lunar {
|
|
|
|
|
|
|
|
|
|
Application::Application()
|
2025-12-02 17:57:59 +02:00
|
|
|
{
|
|
|
|
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
|
|
|
|
std::println(std::cerr, "Failed to initialize SDL.");
|
|
|
|
|
throw std::runtime_error("App init fail");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_window = SDL_CreateWindow(
|
|
|
|
|
"Lunar", 1280, 720, SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE);
|
|
|
|
|
if (!m_window) {
|
|
|
|
|
m_logger.err("Failed to create SDL window");
|
|
|
|
|
throw std::runtime_error("App init fail");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 00:11:15 +02:00
|
|
|
m_renderer = std::make_unique<VulkanRenderer>(m_window, m_logger);
|
2025-12-02 17:57:59 +02:00
|
|
|
|
2025-12-06 20:11:59 +02:00
|
|
|
mouse_captured(true);
|
|
|
|
|
|
2025-12-02 17:57:59 +02:00
|
|
|
m_logger.info("App init done!");
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 03:16:07 +02:00
|
|
|
Application::~Application()
|
|
|
|
|
{
|
2025-12-03 00:11:15 +02:00
|
|
|
m_renderer.reset();
|
2025-12-02 21:26:53 +02:00
|
|
|
|
2025-12-02 03:16:07 +02:00
|
|
|
SDL_DestroyWindow(m_window);
|
|
|
|
|
SDL_Quit();
|
|
|
|
|
|
|
|
|
|
m_logger.info("App destroy done!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto Application::run() -> void
|
|
|
|
|
{
|
|
|
|
|
SDL_Event e;
|
|
|
|
|
|
2025-12-04 12:18:26 +02:00
|
|
|
ImGuiIO &io = ImGui::GetIO();
|
|
|
|
|
io.IniFilename = nullptr;
|
|
|
|
|
|
|
|
|
|
uint64_t last { 0 };
|
|
|
|
|
float fps { 0.0f };
|
2025-12-02 03:16:07 +02:00
|
|
|
while (m_running) {
|
2025-12-04 12:18:26 +02:00
|
|
|
uint64_t now { SDL_GetTicks() };
|
|
|
|
|
uint64_t dt { now - last };
|
|
|
|
|
last = now;
|
|
|
|
|
|
|
|
|
|
if (dt > 0)
|
|
|
|
|
fps = 1000.0f / (float)dt;
|
|
|
|
|
|
2025-12-02 03:16:07 +02:00
|
|
|
while (SDL_PollEvent(&e)) {
|
2025-12-02 21:26:53 +02:00
|
|
|
if (e.type == SDL_EVENT_QUIT) {
|
2025-12-02 03:16:07 +02:00
|
|
|
m_running = false;
|
2025-12-02 21:26:53 +02:00
|
|
|
} else if (e.type == SDL_EVENT_WINDOW_RESIZED) {
|
|
|
|
|
int width {}, height {};
|
|
|
|
|
SDL_GetWindowSize(m_window, &width, &height);
|
2025-12-03 00:11:15 +02:00
|
|
|
m_renderer->resize(static_cast<uint32_t>(width),
|
2025-12-03 00:02:17 +02:00
|
|
|
static_cast<uint32_t>(height));
|
2025-12-06 20:11:59 +02:00
|
|
|
} else if (e.type == SDL_EVENT_KEY_DOWN && e.key.repeat == false) {
|
|
|
|
|
if (e.key.key == SDLK_F11 && e.key.mod & SDL_KMOD_LCTRL) {
|
|
|
|
|
mouse_captured(!mouse_captured());
|
|
|
|
|
m_show_imgui = mouse_captured();
|
|
|
|
|
}
|
2025-12-02 21:26:53 +02:00
|
|
|
}
|
2025-12-03 02:31:38 +02:00
|
|
|
|
|
|
|
|
ImGui_ImplSDL3_ProcessEvent(&e);
|
2025-12-02 03:16:07 +02:00
|
|
|
}
|
2025-12-02 17:57:59 +02:00
|
|
|
|
2025-12-03 02:31:38 +02:00
|
|
|
ImGui_ImplSDL3_NewFrame();
|
|
|
|
|
ImGui_ImplVulkan_NewFrame();
|
|
|
|
|
|
|
|
|
|
ImGui::NewFrame();
|
2025-12-04 12:18:26 +02:00
|
|
|
|
2025-12-06 20:11:59 +02:00
|
|
|
if (m_show_imgui) {
|
|
|
|
|
ImGui::ShowDemoWindow();
|
2025-12-04 12:18:26 +02:00
|
|
|
|
2025-12-06 20:11:59 +02:00
|
|
|
ImGui::SetNextWindowSize({ 100, 50 });
|
|
|
|
|
ImGui::SetNextWindowPos({ 0, 0 });
|
|
|
|
|
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4 { 0, 0, 0, 0.5f });
|
|
|
|
|
if (ImGui::Begin("Debug Info", nullptr,
|
|
|
|
|
ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize)) {
|
|
|
|
|
defer(ImGui::End());
|
2025-12-04 12:18:26 +02:00
|
|
|
|
2025-12-06 20:11:59 +02:00
|
|
|
ImGui::Text("%s", std::format("FPS: {:.2f}", fps).c_str());
|
|
|
|
|
}
|
|
|
|
|
ImGui::PopStyleColor();
|
2025-12-04 12:18:26 +02:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 02:31:38 +02:00
|
|
|
ImGui::Render();
|
|
|
|
|
|
2025-12-03 00:11:15 +02:00
|
|
|
m_renderer->render();
|
2025-12-02 03:16:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-06 20:11:59 +02:00
|
|
|
auto Application::mouse_captured(bool new_state) -> void
|
|
|
|
|
{
|
|
|
|
|
SDL_CaptureMouse(new_state);
|
|
|
|
|
|
|
|
|
|
m_mouse_captured = new_state;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 03:16:07 +02:00
|
|
|
} // namespace Lunar
|