From b12181b1d38e885f323cc63f57a6860234851470 Mon Sep 17 00:00:00 2001 From: ALittlePatate Date: Mon, 23 Oct 2023 13:41:37 +0200 Subject: [PATCH] add: imgui demo --- src/client/Makefile | 1 + src/client/main.cpp | 3 +- src/client/overlay.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++ src/client/overlay.hpp | 3 ++ 4 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/client/overlay.cpp create mode 100644 src/client/overlay.hpp diff --git a/src/client/Makefile b/src/client/Makefile index 879521e..71b748f 100644 --- a/src/client/Makefile +++ b/src/client/Makefile @@ -1,5 +1,6 @@ SRC = main.cpp \ memory.cpp \ + overlay.cpp \ imgui/*.cpp OBJ = $(SRC:.cpp=.o) NAME = Revird diff --git a/src/client/main.cpp b/src/client/main.cpp index e9b9a01..2c0c355 100644 --- a/src/client/main.cpp +++ b/src/client/main.cpp @@ -1,8 +1,9 @@ #include "memory.hpp" +#include "overlay.hpp" #include -#include "imgui/imgui.h" int main() { + run_overlay(); /* if (!open_device()) return -1; diff --git a/src/client/overlay.cpp b/src/client/overlay.cpp new file mode 100644 index 0000000..5b2ce91 --- /dev/null +++ b/src/client/overlay.cpp @@ -0,0 +1,99 @@ +#include "overlay.hpp" +#include +#include "imgui/imgui.h" +#include "imgui/imgui_impl_sdl3.h" +#include "imgui/imgui_impl_opengl3.h" +#include +#include + +int run_overlay(void) +{ + // Setup SDL + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMEPAD) != 0) + { + printf("Error: SDL_Init(): %s\n", SDL_GetError()); + return -1; + } + + const char* glsl_version = "#version 130"; + 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); + + // Enable native IME. + SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1"); + + // 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_HIDDEN); + SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+OpenGL3 example", 1280, 720, window_flags); + if (window == nullptr) + { + printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError()); + return -1; + } + SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + SDL_GLContext gl_context = SDL_GL_CreateContext(window); + SDL_GL_MakeCurrent(window, gl_context); + SDL_GL_SetSwapInterval(1); // Enable vsync + SDL_ShowWindow(window); + + // Setup Dear ImGui context + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO(); (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + + // Setup Dear ImGui style + ImGui::StyleColorsDark(); + + // Setup Platform/Renderer backends + ImGui_ImplSDL3_InitForOpenGL(window, gl_context); + ImGui_ImplOpenGL3_Init(glsl_version); + + // Main loop + bool done = false; + ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); + while (!done) + { + SDL_Event event; + while (SDL_PollEvent(&event)) + { + ImGui_ImplSDL3_ProcessEvent(&event); + if (event.type == SDL_EVENT_QUIT) + done = true; + if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window)) + done = true; + } + + // Start the Dear ImGui frame + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplSDL3_NewFrame(); + ImGui::NewFrame(); + + ImGui::ShowDemoWindow(); + + // Rendering + ImGui::Render(); + glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y); + glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); + glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + SDL_GL_SwapWindow(window); + } + + // Cleanup + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplSDL3_Shutdown(); + ImGui::DestroyContext(); + + SDL_GL_DeleteContext(gl_context); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 1; +} diff --git a/src/client/overlay.hpp b/src/client/overlay.hpp new file mode 100644 index 0000000..cc8f84c --- /dev/null +++ b/src/client/overlay.hpp @@ -0,0 +1,3 @@ +#pragma once + +int run_overlay(void);