work on WindowSDL3

This commit is contained in:
2025-05-04 17:59:14 -05:00
parent ff2a3337b4
commit 6f4b9b68fd
3 changed files with 137 additions and 12 deletions

View File

@@ -5,6 +5,7 @@
#include "utils/Renderer/Renderer.h"
#include "WindowGLFW/WindowGLFW.h"
#include "WindowSDL3/WindowSDL3.h"
namespace Archimedes {

View File

@@ -0,0 +1,82 @@
#include "pch.hpp"
#ifdef WINDOW_SDL3
#undef WINDOW_SDL3
#include "utils/Window/WindowEvents.h"
#include <SDL3/SDL.h>
#include <SDL3/SDL_opengl.h>
namespace Archimedes {
class Window;
class WindowSDL3 {
public:
WindowSDL3(Window* p, const std::function<void(Event*)>& sendEvent) {
data.window = p;
data.sendEvent = sendEvent;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
Uint32 window_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
SDL_Window* window = SDL_CreateWindow("Archimedes", 1280, 720, window_flags);
if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
std::abort();
}
SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
gl_context = SDL_GL_CreateContext(window);
if (gl_context == nullptr)
{
printf("Error: SDL_GL_CreateContext(): %s\n", SDL_GetError());
std::abort();
}
SDL_GL_MakeCurrent(window, gl_context);
SDL_GL_SetSwapInterval(1); // Enable vsync
SDL_ShowWindow(window);
}
~WindowSDL3() {
}
void doFrame() { restoreContext(); SDL_GL_SwapWindow(w); }
void pollEvents() {
SDL_PollEvent(&e);
switch(e.type) {
}
}
void restoreContext() { SDL_GLMakeCurrent(window, gl_context); }
void getSize(int& w, int& h) {
}
SDL_Window* getWindow() { return w; }
WindowData data;
private:
SDL_Window* w;
SDL_GLContext gl_context;
SDL_Event e;
};
typedef WindowSDL3 WindowImpl;
}
#endif

View File

@@ -1,10 +1,52 @@
#include "ImguiModule.h"
#include "backends/imgui_impl_glfw.h"
#define RENDERER_OPENGL
#define WINDOW_GLFW
#ifdef RENDERER_OPENGL
#include "backends/imgui_impl_opengl3.h"
#define RENDERER_INIT() ImGui_ImplOpenGL3_Init("#version 330")
#define RENDERER_SHUTDOWN() ImGui_ImplOpenGL3_Shutdown()
#define RENDERER_NEWFRAME() ImGui_ImplOpenGL3_NewFrame()
#define RENDERER_RENDERDRAWDATA(x) ImGui_ImplOpenGL3_RenderDrawData(x)
#endif
#ifdef WINDOW_GLFW
#include "backends/imgui_impl_glfw.h"
#include <GLFW/glfw3.h>
#ifdef RENDERER_OPENGL
#define WINDOW_INIT() ImGui_ImplGlfw_InitForOpenGL(window->getWindowImpl().getWindow(), true)
#endif
#define WINDOW_SHUTDOWN() ImGui_ImplGlfw_Shutdown()
#define WINDOW_NEWFRAME() ImGui_ImplGlfw_NewFrame()
#endif
#ifdef WINDOW_SDL3
#include "backends/imgui_impl_sdl3.h"
#include <SDL3/SDL.h>
#ifdef RENDERER_OPENGL
#define WINDOW_INIT() ImGui_ImplSDL3_InitForOpenGL(window->getWindowImpl().getWindow(), window->getWindowImpl().getContext())
#endif
#define WINDOW_SHUTDOWN() ImGui_ImplSDL3_Shutdown()
#define WINDOW_NEWFRAME() ImGui_ImplSDL3_NewFrame()
#define WINDOW_PROCESSEVENT() ImGui_ImplSDL3_ProcessEvent(&e)
#endif
#include "pch.hpp"
ImguiModule::ImguiModule(Archimedes::App* a, void* h = nullptr) : Archimedes::Module(a, h) {
@@ -22,8 +64,8 @@ ImguiModule::~ImguiModule() {
wm->getRenderer()->getCmdList().erase(rcmd_it);
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
RENDERER_SHUTDOWN();
WINDOW_SHUTDOWN();
ImGui::DestroyContext();
wm->releaseWindow(window);
@@ -58,28 +100,28 @@ void ImguiModule::onLoad() {
//ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
if(!ImGui_ImplGlfw_InitForOpenGL(window->getWindowImpl().getWindow(), true))
std::cout << "GLFWImpl failed\n";
if(!WINDOW_INIT())
std::cout << "WINDOW_INIT failed\n";
if(!ImGui_ImplOpenGL3_Init("#version 330")) {
std::cout << "ImGui_ImplOpenGL3_Init failed!\n" << std::endl;
if(!RENDERER_INIT()) {
std::cout << "RENDERER_INIT failed!\n" << std::endl;
}
wm->getRenderer()->getCmdList().push_back([](){
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
RENDERER_RENDERDRAWDATA(ImGui::GetDrawData());
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
RENDERER_NEWFRAME();
WINDOW_NEWFRAME();
ImGui::NewFrame();
});
rcmd_it = --wm->getRenderer()->getCmdList().end()++;
//Compute first frame ahead of first WindowModule->run()
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
RENDERER_NEWFRAME();
WINDOW_NEWFRAME();
ImGui::NewFrame();
}