#include "SDL.hpp" #include "../Utils.hpp" #ifdef _WIN32 #include "D3D.hpp" #include "DvdSpin/D3DSpin.hpp" #include #include #endif #include #include #include #include #include #include #include namespace Li::Gui { SDL::SDL(std::string windowTitle, int windowWidth, int windowHeight) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { Utils::ShowErrorMessage("Failed to initalize SDL: " + std::string(SDL_GetError())); return; } #ifdef SDL_HINT_IME_SHOW_UI SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); #endif SDL_WindowFlags windowFlags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); this->window = SDL_CreateWindow(windowTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowWidth, windowHeight, windowFlags); SDL_VERSION(&this->wmInfo.version); SDL_GetWindowWMInfo(this->window, &this->wmInfo); #ifdef _WIN32 HWND hwnd = (HWND)this->wmInfo.info.win.window; D3D* d3dObj = new D3D(hwnd, windowWidth, windowHeight); this->renderer = d3dObj; //this->dvdSpinner = new Li::Gui::DvdSpin::D3DSpin(d3dObj); #else #error no renderer for this platform #endif IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); io.IniFilename = nullptr; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls ImGui::StyleColorsDark(); #ifdef _WIN32 ImGui_ImplSDL2_InitForD3D(this->window); this->renderer->InitImgui(); #else #error No imgui renderer backend provided for this platform #endif this->isExiting = false; this->SetMaxFPS(24.0); this->lastRenderTime = SDL_GetTicks(); } bool SDL::IsExiting() { return this->isExiting; } void SDL::PollEvent() { SDL_Event event; while (SDL_PollEvent(&event)) { ImGui_ImplSDL2_ProcessEvent(&event); if (event.type == SDL_QUIT) this->isExiting = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) this->isExiting = true; if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window)) { // Release all outstanding references to the swap chain's buffers before resizing. #ifdef _WIN32 int width; int height; SDL_GetWindowSize(this->window, &width, &height); D3D* d3d = (D3D*)this->renderer; d3d->Resize(width, height); #else #error No resize method provided for this platform #endif } } } Renderer* SDL::Renderer() { return this->renderer; } void SDL::NewFrame() { this->curRenderTime = SDL_GetTicks(); this->renderer->ImGuiNewFrame(); ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); } void SDL::SetMaxFPS(float fpsLimit) { this->maxFps = fpsLimit; this->limit = (uint32_t)((float)1000.0 / this->maxFps); } void SDL::Render() { ImGui::Render(); this->renderer->Render(); this->lastRenderTime = this->curRenderTime; this->delta = (this->curRenderTime - this->lastRenderTime); if (this->delta < this->limit) { uint32_t remain = (this->limit - this->delta); std::this_thread::sleep_for(std::chrono::milliseconds(remain)); } } SDL::~SDL() { #ifdef _WIN32 ImGui_ImplDX11_Shutdown(); #endif ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(); delete this->renderer; SDL_DestroyWindow(this->window); SDL_Quit(); } }