AronaCore/core/application/application.cpp
2024-02-28 11:20:18 +08:00

131 lines
3.7 KiB
C++

#include "application.h"
#include <iostream>
#include "command_line.h"
#include "imgui_impl_sdl3.h"
#include "imgui_internal.h"
#include "window_manager.h"
#include "filesystem/stb_image.h"
#include "rhi/texture.h"
#include "rhi/renderer.h"
#include "spdlog/async.h"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/basic_file_sink.h"
bool g_is_running = true;
bool g_exit_requested = false;
application* g_app_instance = nullptr;
static void glfw_error_callback(int error, const char* description) {
spdlog::error("Glfw Error {}: {}", error, description);
}
void application::init(const window_params& in_window_params, int argc, char** argv) {
try {
async_spdlog_ = spdlog::basic_logger_mt<spdlog::async_factory>("async_file_logger", "logs/log.txt");
} catch (const spdlog::spdlog_ex& ex) {
std::cout << "Log init failed: " << ex.what() << std::endl;
}
command_line::instance().init(argc, argv);
init_glfw();
init_imgui();
renderer_ = new renderer();
renderer_->pre_init();
const auto window = g_window_manager.create_main_window(in_window_params.title.c_str(), in_window_params.width, in_window_params.height);
// new glfw window
if (!window) {
spdlog::error("Failed to create glfw window");
return;
}
renderer_->init(window);
g_is_running = true;
}
int application::run() {
const ImGuiIO& io = ImGui::GetIO();
while (!g_exit_requested) {
g_window_manager.tick();
g_exit_requested = g_exit_requested || g_window_manager.should_close();
tick(io.DeltaTime);
renderer_->new_frame(g_window_manager.get_main_window());
draw_gui();
renderer_->end_frame(g_window_manager.get_main_window());
}
shutdown();
return 0;
}
void application::shutdown() {
renderer_->shutdown();
destroy_imgui();
delete renderer_;
renderer_ = nullptr;
destroy_glfw();
g_is_running = false;
}
void application::request_exit() {
g_window_manager.request_exit();
g_exit_requested = true;
}
std::shared_ptr<texture> application::load_texture(const std::string& path, vk::Format format) {
int width = 0;
int height = 0;
uint8_t* image_data = stbi_load(path.c_str(), &width, &height, nullptr, 4);
if (!image_data) {
spdlog::error("Failed to load texture: {}", path.c_str());
return nullptr;
}
const auto texture = renderer::create_texture(image_data, width, height, format);
stbi_image_free(image_data);
return texture;
}
std::shared_ptr<texture> application::create_texture(const unsigned char* data, const int width, const int height, vk::Format format) {
return renderer::create_texture(data, width, height, format);
}
void application::init_glfw() {
glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit()) {
spdlog::error("Failed to initialize GLFW");
return;
}
// glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
}
void application::init_imgui() {
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
init_imgui(ImGui::CreateContext());
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
// io.ConfigViewportsNoAutoMerge = true;
// io.ConfigViewportsNoTaskBarIcon = false;
ImGui::StyleColorsDark();
//ImGui::StyleColorsLight();
}
void application::destroy_glfw() {
glfwTerminate();
}
void application::destroy_imgui() {
init_imgui(nullptr);
ImGui::DestroyContext();
}