Add SDL3 surface backend for ImGui example
- Created imgui_impl_sdlsurface3.h and corresponding implementation file. - Added Makefile for building the SDL3 surface example on Linux and macOS. - Included README.md with build instructions for Windows, Linux, and macOS. - Added build script for Windows (build_win32.bat) and Visual Studio project files. - Implemented main application logic in main.cpp to demonstrate ImGui with SDL3 and surface rendering.
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_sdl3.h"
|
||||
#include "imgui_impl_sdlsurface3.h"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
|
||||
{
|
||||
printf("Error: SDL_Init(): %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
||||
SDL_WindowFlags window_flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY;
|
||||
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL3+Surface Example", (int)(1280 * main_scale), (int)(800 * main_scale), 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_ShowWindow(window);
|
||||
|
||||
SDL_Surface* window_surface = SDL_GetWindowSurface(window);
|
||||
if (window_surface == nullptr)
|
||||
{
|
||||
printf("Error: SDL_GetWindowSurface(): %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_SetWindowSurfaceVSync(window, 1);
|
||||
|
||||
int framebuffer_w = window_surface->w;
|
||||
int framebuffer_h = window_surface->h;
|
||||
SDL_Surface* framebuffer = SDL_CreateSurface(framebuffer_w, framebuffer_h, SDL_PIXELFORMAT_RGBA32);
|
||||
if (framebuffer == nullptr)
|
||||
{
|
||||
printf("Error: SDL_CreateSurface(): %s\n", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 1;
|
||||
}
|
||||
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
|
||||
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
ImGuiStyle& style = ImGui::GetStyle();
|
||||
style.ScaleAllSizes(main_scale);
|
||||
style.FontScaleDpi = main_scale;
|
||||
|
||||
ImGui_ImplSDL3_InitForOther(window);
|
||||
ImGui_ImplSDLSurface3_Init(framebuffer);
|
||||
|
||||
bool show_demo_window = true;
|
||||
bool show_another_window = false;
|
||||
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||
bool done = false;
|
||||
|
||||
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;
|
||||
if ((event.type == SDL_EVENT_WINDOW_RESIZED || event.type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) && event.window.windowID == SDL_GetWindowID(window))
|
||||
{
|
||||
window_surface = SDL_GetWindowSurface(window);
|
||||
if (window_surface != nullptr)
|
||||
{
|
||||
int new_w = window_surface->w;
|
||||
int new_h = window_surface->h;
|
||||
if (new_w != framebuffer_w || new_h != framebuffer_h)
|
||||
{
|
||||
SDL_DestroySurface(framebuffer);
|
||||
framebuffer = SDL_CreateSurface(new_w, new_h, SDL_PIXELFORMAT_RGBA32);
|
||||
if (framebuffer == nullptr)
|
||||
{
|
||||
printf("Error: SDL_CreateSurface(): %s\n", SDL_GetError());
|
||||
done = true;
|
||||
break;
|
||||
}
|
||||
framebuffer_w = new_w;
|
||||
framebuffer_h = new_h;
|
||||
ImGui_ImplSDLSurface3_Shutdown();
|
||||
ImGui_ImplSDLSurface3_Init(framebuffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (done)
|
||||
break;
|
||||
|
||||
if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)
|
||||
{
|
||||
SDL_Delay(10);
|
||||
continue;
|
||||
}
|
||||
|
||||
ImGui_ImplSDLSurface3_NewFrame();
|
||||
ImGui_ImplSDL3_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
if (show_demo_window)
|
||||
ImGui::ShowDemoWindow(&show_demo_window);
|
||||
|
||||
{
|
||||
static float f = 0.0f;
|
||||
static int counter = 0;
|
||||
|
||||
ImGui::Begin("Hello, world!");
|
||||
ImGui::Text("This is some useful text.");
|
||||
ImGui::Checkbox("Demo Window", &show_demo_window);
|
||||
ImGui::Checkbox("Another Window", &show_another_window);
|
||||
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
|
||||
ImGui::ColorEdit3("clear color", (float*)&clear_color);
|
||||
if (ImGui::Button("Button"))
|
||||
counter++;
|
||||
ImGui::SameLine();
|
||||
ImGui::Text("counter = %d", counter);
|
||||
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
if (show_another_window)
|
||||
{
|
||||
ImGui::Begin("Another Window", &show_another_window);
|
||||
ImGui::Text("Hello from another window!");
|
||||
if (ImGui::Button("Close Me"))
|
||||
show_another_window = false;
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
ImGui::Render();
|
||||
|
||||
SDL_FillSurfaceRect(framebuffer, nullptr, SDL_MapSurfaceRGBA(framebuffer,
|
||||
(Uint8)(clear_color.x * 255.0f),
|
||||
(Uint8)(clear_color.y * 255.0f),
|
||||
(Uint8)(clear_color.z * 255.0f),
|
||||
(Uint8)(clear_color.w * 255.0f)));
|
||||
|
||||
ImGui_ImplSDLSurface3_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
window_surface = SDL_GetWindowSurface(window);
|
||||
if (window_surface != nullptr)
|
||||
{
|
||||
SDL_BlitSurface(framebuffer, nullptr, window_surface, nullptr);
|
||||
SDL_UpdateWindowSurface(window);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui_ImplSDLSurface3_Shutdown();
|
||||
ImGui_ImplSDL3_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
SDL_DestroySurface(framebuffer);
|
||||
SDL_DestroyWindowSurface(window);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user