Replaces FEXConfig usage of GLFW with SDL2

Sometimes GLFW3 can just consume a full CPU core, spinning on nothing.

Fixes #906
This commit is contained in:
Ryan Houdek committed 2021-03-27 05:23:58 -07:00
1 parent aa67142e6f
commit 8ebef457fc
2 files changed
+69 -34

No files matched your search

+4 -4
View File
@@ -1,24 +1,24 @@
set(NAME FEXConfig)
set(SRCS Main.cpp
../Debugger/Context.cpp
${CMAKE_SOURCE_DIR}/External/imgui/examples/imgui_impl_glfw.cpp
${CMAKE_SOURCE_DIR}/External/imgui/examples/imgui_impl_sdl.cpp
${CMAKE_SOURCE_DIR}/External/imgui/examples/imgui_impl_opengl3.cpp
)
find_library(EPOXY_LIBRARY epoxy)
find_library(GLFW_LIBRARY glfw3)
find_package(SDL2 REQUIRED)
add_definitions(-DIMGUI_IMPL_OPENGL_LOADER_CUSTOM=<epoxy/gl.h>)
add_executable(${NAME} ${SRCS})
target_include_directories(${NAME} PRIVATE ${LLVM_INCLUDE_DIRS})
target_include_directories(${NAME} PRIVATE ${SDL2_INCLUDE_DIRS})
target_include_directories(${NAME} PRIVATE ${CMAKE_SOURCE_DIR}/Source/)
target_include_directories(${NAME} PRIVATE ${CMAKE_SOURCE_DIR}/Source/Tools/Debugger/)
target_include_directories(${NAME} PRIVATE ${CMAKE_SOURCE_DIR}/External/imgui/examples/)
target_link_libraries(${NAME} PRIVATE FEXCore Common CommonCore pthread epoxy glfw X11 EGL imgui tiny-json json-maker)
target_link_libraries(${NAME} PRIVATE FEXCore Common CommonCore pthread epoxy ${SDL2_LIBRARIES} X11 EGL imgui tiny-json json-maker)
install(TARGETS ${NAME}
RUNTIME
+65 -30
View File
@@ -1,19 +1,19 @@
#include "Common/Config.h"
#include "Context.h"
#include <imgui.h>
#define YES_IMGUIFILESYSTEM
#include <addons/imgui_user.h>
#include <epoxy/gl.h>
#include <SDL.h>
#include <SDL_scancode.h>
#include <memory>
#include <GLFW/glfw3.h>
#include <filesystem>
#include <thread>
#include "imgui_impl_glfw.h"
#include "imgui_impl_sdl.h"
#include "imgui_impl_opengl3.h"
namespace {
static double GlobalTime{};
static std::chrono::time_point<std::chrono::high_resolution_clock> GlobalTime{};
static bool ConfigOpen{};
static bool ConfigChanged{};
@@ -358,10 +358,11 @@ namespace {
ImGui::EndTabBar();
}
void DrawUI(GLContext::Context *Context) {
void DrawUI() {
ImGuiIO& io = ImGui::GetIO();
double current_time = glfwGetTime();
io.DeltaTime = GlobalTime > 0.0 ? static_cast<float>(current_time - GlobalTime) : static_cast<float>(1.0f/60.0f);
auto current_time = std::chrono::high_resolution_clock::now();
auto Diff = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - GlobalTime);
io.DeltaTime = Diff.count() > 0 ? Diff.count() : 1.0f/60.0f;
GlobalTime = current_time;
ImGui::NewFrame();
@@ -440,7 +441,7 @@ namespace {
FillConfigWindow();
}
if (ImGui::IsKeyPressed(GLFW_KEY_E) && io.KeyCtrl) {
if (ImGui::IsKeyPressed(SDL_SCANCODE_E) && io.KeyCtrl) {
ImGui::OpenPopup(SavedPopupAppName);
}
@@ -453,7 +454,7 @@ namespace {
ImGui::CloseCurrentPopup();
}
if (ImGui::IsKeyPressed(GLFW_KEY_ESCAPE)) {
if (ImGui::IsKeyPressed(SDL_SCANCODE_ESCAPE)) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
@@ -480,33 +481,33 @@ namespace {
}
if (Selected.Open ||
(ImGui::IsKeyPressed(GLFW_KEY_O) && io.KeyCtrl && !io.KeyShift)) {
(ImGui::IsKeyPressed(SDL_SCANCODE_O) && io.KeyCtrl && !io.KeyShift)) {
SelectedOpenFile = true;
}
if (Selected.OpenDefault ||
(ImGui::IsKeyPressed(GLFW_KEY_O) && io.KeyCtrl && io.KeyShift)) {
(ImGui::IsKeyPressed(SDL_SCANCODE_O) && io.KeyCtrl && io.KeyShift)) {
OpenFile(FEX::Config::GetConfigFileLocation());
}
if (Selected.LoadDefault ||
(ImGui::IsKeyPressed(GLFW_KEY_D) && io.KeyCtrl && io.KeyShift)) {
(ImGui::IsKeyPressed(SDL_SCANCODE_D) && io.KeyCtrl && io.KeyShift)) {
LoadDefaultSettings();
}
if (Selected.Save ||
(ImGui::IsKeyPressed(GLFW_KEY_S) && io.KeyCtrl && !io.KeyShift)) {
(ImGui::IsKeyPressed(SDL_SCANCODE_S) && io.KeyCtrl && !io.KeyShift)) {
SaveFile(ConfigFilename);
}
if (Selected.SaveAs ||
(ImGui::IsKeyPressed(GLFW_KEY_S) && io.KeyCtrl && io.KeyShift)) {
(ImGui::IsKeyPressed(SDL_SCANCODE_S) && io.KeyCtrl && io.KeyShift)) {
SelectedSaveFileAs = true;
}
if (Selected.SaveDefault ||
(ImGui::IsKeyPressed(GLFW_KEY_P) && io.KeyCtrl && io.KeyShift)) {
(ImGui::IsKeyPressed(SDL_SCANCODE_P) && io.KeyCtrl && io.KeyShift)) {
SaveFile(FEX::Config::GetConfigFileLocation());
}
if (Selected.Close ||
(ImGui::IsKeyPressed(GLFW_KEY_W) && io.KeyCtrl && !io.KeyShift)) {
(ImGui::IsKeyPressed(SDL_SCANCODE_W) && io.KeyCtrl && !io.KeyShift)) {
CloseConfig();
}
@@ -537,8 +538,27 @@ namespace {
int main() {
std::string ImGUIConfig = FEX::Config::GetConfigFolder(false) + "FEXConfig_imgui.ini";
auto Context = GLContext::CreateContext();
Context->Create("#FEXConfig");
// Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0)
{
printf("Error: %s\n", SDL_GetError());
return -1;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
// Create window with graphics context
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("#FEXConfig", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 640, window_flags);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
@@ -549,39 +569,54 @@ int main() {
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
io.IniFilename = &ImGUIConfig.at(0);
GLFWwindow *Window = static_cast<GLFWwindow*>(Context->GetWindow());
// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(Window, true);
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
const char* glsl_version = "#version 130";
ImGui_ImplOpenGL3_Init(glsl_version);
while (!glfwWindowShouldClose(Window)) {
glfwWaitEvents();
GlobalTime = std::chrono::high_resolution_clock::now();
bool Done{};
while (!Done) {
SDL_Event event;
if (SDL_WaitEvent(nullptr))
{
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
Done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
Done = true;
}
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
DrawUI(Context.get());
ImGui_ImplSDL2_NewFrame(window);
DrawUI();
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
SDL_Window* backup_current_window = SDL_GL_GetCurrentWindow();
SDL_GLContext backup_current_context = SDL_GL_GetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
}
Context->Swap();
SDL_GL_SwapWindow(window);
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
Context->Shutdown();
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}