This commit is contained in:
142
src/loader.cpp
142
src/loader.cpp
@@ -1,4 +1,5 @@
|
||||
#include "loader.h"
|
||||
#include "loader.hpp"
|
||||
#include "util/imgui/widget.hpp"
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <GLES3/gl3.h>
|
||||
@@ -8,45 +9,69 @@
|
||||
|
||||
#include <backends/imgui_impl_opengl3.h>
|
||||
#include <backends/imgui_impl_sdl3.h>
|
||||
#include <format>
|
||||
#include <imgui.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "util/math_.h"
|
||||
#include "log.hpp"
|
||||
#include "util/math.hpp"
|
||||
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
|
||||
#include <physfs.h>
|
||||
|
||||
#include "resource/audio.hpp"
|
||||
#include "resource/xml/settings.hpp"
|
||||
#include "util/imgui/style.hpp"
|
||||
#include "util/preferences.hpp"
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
|
||||
#include "util/web_filesystem.hpp"
|
||||
|
||||
constexpr auto GL_VERSION_MAJOR = 3;
|
||||
constexpr auto GL_VERSION_MINOR = 0;
|
||||
constexpr auto GLSL_VERSION = "#version 300 es";
|
||||
|
||||
#else
|
||||
constexpr auto GL_VERSION_MAJOR = 3;
|
||||
constexpr auto GL_VERSION_MINOR = 3;
|
||||
constexpr auto GLSL_VERSION = "#version 330";
|
||||
#endif
|
||||
|
||||
constexpr auto WINDOW_ROUNDING = 6.0f;
|
||||
constexpr auto WINDOW_COLOR = ImVec4(0.03f, 0.25f, 0.06f, 1.0f);
|
||||
constexpr auto WINDOW_BACKGROUND_COLOR = ImVec4(0.02f, 0.08f, 0.03f, 0.96f);
|
||||
constexpr auto ACCENT_COLOR = ImVec4(0.05f, 0.32f, 0.12f, 1.0f);
|
||||
constexpr auto ACCENT_COLOR_HOVERED = ImVec4(0.07f, 0.4f, 0.15f, 1.0f);
|
||||
constexpr auto ACCENT_COLOR_ACTIVE = ImVec4(0.09f, 0.5f, 0.2f, 1.0f);
|
||||
constexpr auto TAB_UNFOCUSED_COLOR = ImVec4(0.03f, 0.2f, 0.07f, 0.9f);
|
||||
|
||||
using namespace game::util;
|
||||
|
||||
namespace game
|
||||
{
|
||||
Loader::Loader()
|
||||
|
||||
Loader::Loader(int argc, const char** argv)
|
||||
{
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
#ifdef __EMSCRIPTEN__
|
||||
util::web_filesystem::init_and_wait();
|
||||
#endif
|
||||
|
||||
settings = resource::xml::Settings(preferences::path() / "settings.xml");
|
||||
|
||||
logger.info("Initializing...");
|
||||
|
||||
if (!PHYSFS_init((argc > 0 && argv && argv[0]) ? argv[0] : "snivy"))
|
||||
{
|
||||
std::cout << "Failed to initialize SDL: " << SDL_GetError();
|
||||
logger.fatal(std::format("Failed to initialize PhysicsFS: {}", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode())));
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Initialized SDL" << "\n";
|
||||
PHYSFS_setWriteDir(nullptr);
|
||||
|
||||
logger.info("Initialized PhysFS");
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
logger.fatal(std::format("Failed to initialize SDL: {}", SDL_GetError()));
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info("Initialized SDL");
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, GL_VERSION_MAJOR);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, GL_VERSION_MINOR);
|
||||
@@ -57,30 +82,49 @@ namespace game
|
||||
#endif
|
||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
window = SDL_CreateWindow("Snivy", SIZE.x, SIZE.y, SDL_WINDOW_OPENGL);
|
||||
#else
|
||||
|
||||
SDL_PropertiesID windowProperties = SDL_CreateProperties();
|
||||
|
||||
SDL_SetStringProperty(windowProperties, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "Snivy");
|
||||
SDL_SetNumberProperty(windowProperties, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, settings.windowSize.x);
|
||||
SDL_SetNumberProperty(windowProperties, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, settings.windowSize.y);
|
||||
|
||||
SDL_SetNumberProperty(windowProperties, SDL_PROP_WINDOW_CREATE_X_NUMBER,
|
||||
settings.windowPosition.x == 0 ? SDL_WINDOWPOS_CENTERED : settings.windowPosition.x);
|
||||
SDL_SetNumberProperty(windowProperties, SDL_PROP_WINDOW_CREATE_Y_NUMBER,
|
||||
settings.windowPosition.y == 0 ? SDL_WINDOWPOS_CENTERED : settings.windowPosition.y);
|
||||
|
||||
SDL_SetBooleanProperty(windowProperties, SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, true);
|
||||
SDL_SetBooleanProperty(windowProperties, SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, true);
|
||||
SDL_SetBooleanProperty(windowProperties, SDL_PROP_WINDOW_CREATE_HIGH_PIXEL_DENSITY_BOOLEAN, true);
|
||||
|
||||
window = SDL_CreateWindowWithProperties(windowProperties);
|
||||
#endif
|
||||
|
||||
if (!window)
|
||||
{
|
||||
std::cout << "Failed to initialize window: " << SDL_GetError();
|
||||
;
|
||||
logger.fatal(std::format("Failed to initialize window: {}", SDL_GetError()));
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Initialized window" << "\n";
|
||||
logger.info("Initialized window");
|
||||
|
||||
context = SDL_GL_CreateContext(window);
|
||||
|
||||
if (!context)
|
||||
{
|
||||
std::cout << "Failed to initialize GL context: " << SDL_GetError();
|
||||
logger.fatal(std::format("Failed to initialize GL context: {}", SDL_GetError()));
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!SDL_GL_MakeCurrent(window, context))
|
||||
{
|
||||
std::cout << "Failed to make GL context current: " << SDL_GetError();
|
||||
logger.fatal(std::format("Failed to make GL context current: {}", SDL_GetError()));
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
@@ -88,12 +132,12 @@ namespace game
|
||||
#ifndef __EMSCRIPTEN__
|
||||
if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
|
||||
{
|
||||
std::cout << "Failed to initialize GLAD" << "\n";
|
||||
logger.fatal("Failed to initialize GLAD");
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Initialized GLAD" << "\n";
|
||||
logger.info("Initialized GLAD");
|
||||
#endif
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
@@ -101,90 +145,68 @@ namespace game
|
||||
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
std::cout << "Initialized GL context: " << glGetString(GL_VERSION) << "\n";
|
||||
logger.info(std::format("Initialized GL context: {}", (const char*)glGetString(GL_VERSION)));
|
||||
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
SDL_GL_MakeCurrent(window, context);
|
||||
|
||||
if (!MIX_Init())
|
||||
{
|
||||
std::cout << "Failed to initialize SDL mixer: " << SDL_GetError();
|
||||
logger.fatal(std::format("Failed to initialize SDL mixer: {}", SDL_GetError()));
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Initialized SDL mixer" << "\n";
|
||||
logger.info("Initialized SDL mixer");
|
||||
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGuiContext* imguiContext = ImGui::CreateContext();
|
||||
|
||||
if (!imguiContext)
|
||||
{
|
||||
std::cout << "Failed to initialize Dear ImGui" << "\n";
|
||||
logger.fatal("Failed to initialize Dear ImGui");
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Initialized Dear ImGui" << "\n";
|
||||
logger.info("Initialized Dear ImGui");
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.IniFilename = nullptr;
|
||||
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
ImGui::StyleColorsDark();
|
||||
style.Colors[ImGuiCol_TitleBg] = WINDOW_COLOR;
|
||||
style.Colors[ImGuiCol_TitleBgActive] = WINDOW_COLOR;
|
||||
style.Colors[ImGuiCol_TitleBgCollapsed] = WINDOW_COLOR;
|
||||
style.Colors[ImGuiCol_Header] = ACCENT_COLOR;
|
||||
style.Colors[ImGuiCol_HeaderHovered] = ACCENT_COLOR_HOVERED;
|
||||
style.Colors[ImGuiCol_HeaderActive] = ACCENT_COLOR_ACTIVE;
|
||||
style.Colors[ImGuiCol_FrameBg] = ACCENT_COLOR;
|
||||
style.Colors[ImGuiCol_FrameBgActive] = ACCENT_COLOR_ACTIVE;
|
||||
style.Colors[ImGuiCol_FrameBgHovered] = ACCENT_COLOR_HOVERED;
|
||||
style.Colors[ImGuiCol_Button] = ACCENT_COLOR;
|
||||
style.Colors[ImGuiCol_ButtonHovered] = ACCENT_COLOR_HOVERED;
|
||||
style.Colors[ImGuiCol_ButtonActive] = ACCENT_COLOR_ACTIVE;
|
||||
style.Colors[ImGuiCol_CheckMark] = ACCENT_COLOR_ACTIVE;
|
||||
style.Colors[ImGuiCol_SliderGrab] = ACCENT_COLOR;
|
||||
style.Colors[ImGuiCol_SliderGrabActive] = ACCENT_COLOR_ACTIVE;
|
||||
style.Colors[ImGuiCol_ResizeGrip] = ACCENT_COLOR;
|
||||
style.Colors[ImGuiCol_ResizeGripHovered] = ACCENT_COLOR_HOVERED;
|
||||
style.Colors[ImGuiCol_ResizeGripActive] = ACCENT_COLOR_ACTIVE;
|
||||
style.Colors[ImGuiCol_PlotLines] = ACCENT_COLOR;
|
||||
style.Colors[ImGuiCol_PlotLinesHovered] = ACCENT_COLOR_HOVERED;
|
||||
style.Colors[ImGuiCol_PlotHistogram] = ACCENT_COLOR_ACTIVE;
|
||||
style.Colors[ImGuiCol_PlotHistogramHovered] = ACCENT_COLOR_HOVERED;
|
||||
style.Colors[ImGuiCol_Tab] = ACCENT_COLOR;
|
||||
style.Colors[ImGuiCol_TabHovered] = ACCENT_COLOR_HOVERED;
|
||||
style.Colors[ImGuiCol_TabActive] = ACCENT_COLOR_ACTIVE;
|
||||
style.Colors[ImGuiCol_TabUnfocused] = TAB_UNFOCUSED_COLOR;
|
||||
style.Colors[ImGuiCol_TabUnfocusedActive] = ACCENT_COLOR;
|
||||
|
||||
if (!ImGui_ImplSDL3_InitForOpenGL(window, context))
|
||||
{
|
||||
std::cout << "Failed to initialize Dear ImGui SDL3 backend" << "\n";
|
||||
logger.fatal("Failed to initialize Dear ImGui SDL3 backend");
|
||||
ImGui::DestroyContext(imguiContext);
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Initialize Dear ImGui SDL3 backend" << "\n";
|
||||
logger.info("Initialized Dear ImGui SDL3 backend");
|
||||
|
||||
if (!ImGui_ImplOpenGL3_Init(GLSL_VERSION))
|
||||
{
|
||||
std::cout << "Failed to initialize Dear ImGui OpenGL backend" << "\n";
|
||||
logger.fatal("Failed to initialize Dear ImGui OpenGL backend");
|
||||
ImGui_ImplSDL3_Shutdown();
|
||||
ImGui::DestroyContext(imguiContext);
|
||||
isError = true;
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << "Initialize Dear ImGui OpenGL backend" << "\n";
|
||||
logger.info("Initialized Dear ImGui OpenGL backend");
|
||||
|
||||
imgui::style::color_set(settings.color);
|
||||
imgui::style::rounding_set();
|
||||
math::random_seed_set();
|
||||
resource::Audio::volume_set((float)settings.volume / 100);
|
||||
}
|
||||
|
||||
Loader::~Loader()
|
||||
{
|
||||
PHYSFS_deinit();
|
||||
|
||||
if (ImGui::GetCurrentContext())
|
||||
{
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
@@ -196,6 +218,6 @@ namespace game
|
||||
if (window) SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
std::cout << "Exiting..." << "\n";
|
||||
logger.info("Exiting...");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user