DumpDVD/DumpDVD/Gui/SDL.cpp

136 lines
3.5 KiB
C++
Raw Normal View History

2023-03-14 13:36:58 +00:00
#include "SDL.hpp"
2023-03-21 10:12:28 +00:00
#include "../Utils.hpp"
2023-03-17 08:23:19 +00:00
2023-03-14 13:36:58 +00:00
#ifdef _WIN32
#include "D3D.hpp"
2023-03-20 09:39:23 +00:00
#include "DvdSpin/D3DSpin.hpp"
2023-03-14 13:36:58 +00:00
#include <windows.h>
#include <imgui_impl_dx11.h>
#endif
2023-03-17 08:23:19 +00:00
2023-03-19 02:37:35 +00:00
#include <chrono>
#include <thread>
2023-03-17 08:23:19 +00:00
#include <SDL.h>
2023-03-14 13:36:58 +00:00
#include <imgui_impl_sdl2.h>
#include <imgui.h>
#include <string>
#include <iostream>
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)
{
2023-03-21 10:12:28 +00:00
Utils::ShowErrorMessage("Failed to initalize SDL: " + std::string(SDL_GetError()));
2023-03-14 13:36:58 +00:00
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;
2023-03-20 09:39:23 +00:00
D3D* d3dObj = new D3D(hwnd, windowWidth, windowHeight);
this->renderer = d3dObj;
//this->dvdSpinner = new Li::Gui::DvdSpin::D3DSpin(d3dObj);
2023-03-17 08:23:19 +00:00
#else
#error no renderer for this platform
2023-03-14 13:36:58 +00:00
#endif
IMGUI_CHECKVERSION();
ImGui::CreateContext();
2023-03-15 06:59:59 +00:00
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
2023-03-14 13:36:58 +00:00
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();
2023-03-17 08:23:19 +00:00
#else
#error No imgui renderer backend provided for this platform
2023-03-14 13:36:58 +00:00
#endif
2023-03-19 02:37:35 +00:00
this->isExiting = false;
this->SetMaxFPS(24.0);
this->lastRenderTime = SDL_GetTicks();
2023-03-14 13:36:58 +00:00
}
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)
2023-03-15 06:59:59 +00:00
this->isExiting = true;
2023-03-14 13:36:58 +00:00
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
2023-03-15 06:59:59 +00:00
this->isExiting = true;
2023-03-14 13:36:58 +00:00
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
2023-03-20 09:39:23 +00:00
int width;
int height;
SDL_GetWindowSize(this->window, &width, &height);
2023-03-14 13:36:58 +00:00
D3D* d3d = (D3D*)this->renderer;
2023-03-20 09:39:23 +00:00
d3d->Resize(width, height);
2023-03-17 08:23:19 +00:00
#else
#error No resize method provided for this platform
2023-03-14 13:36:58 +00:00
#endif
}
}
}
2023-03-20 09:39:23 +00:00
Renderer* SDL::Renderer() {
return this->renderer;
}
2023-03-14 13:36:58 +00:00
void SDL::NewFrame() {
2023-03-19 02:37:35 +00:00
this->curRenderTime = SDL_GetTicks();
2023-03-14 13:36:58 +00:00
this->renderer->ImGuiNewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
}
2023-03-19 02:37:35 +00:00
void SDL::SetMaxFPS(float fpsLimit) {
this->maxFps = fpsLimit;
2023-03-20 09:39:23 +00:00
this->limit = (uint32_t)((float)1000.0 / this->maxFps);
2023-03-19 02:37:35 +00:00
}
2023-03-14 13:36:58 +00:00
void SDL::Render() {
ImGui::Render();
2023-03-20 09:39:23 +00:00
this->renderer->Render();
2023-03-19 02:37:35 +00:00
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));
}
2023-03-14 13:36:58 +00:00
}
SDL::~SDL() {
#ifdef _WIN32
ImGui_ImplDX11_Shutdown();
#endif
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
2023-03-20 09:39:23 +00:00
delete this->renderer;
2023-03-14 13:36:58 +00:00
SDL_DestroyWindow(this->window);
SDL_Quit();
}
}