imgui: Unable to get ImGui to show in simple SDL program
Hi, I’m learning SDL and ImGui. I compiled and successfully ran the sdl_opengl3_example project. However, when I write my own sdl program using imgui_impl_sdl_gl3.cpp and imgui_impl_sdl_gl3.h, I cannot get ImGui to show.
The code is simple; it sets up the SDL window and renderer, displays two primitives, and handles the ESC keyboard input. I wrote in the ImGui code based on the main.cpp example:
#include "SDL.h"
#include "imgui.h"
#include "imgui_impl_sdl_gl3.h"
#include "GL/gl3w.h"
int main(int, char**)
{
// SDL initialization
SDL_Window* window = SDL_CreateWindow("test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Event* event = new SDL_Event();
SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
bool quit = false;
// ImGui Initialization
gl3wInit();
ImGui_ImplSdlGL3_Init(window);
// Main loop
while(!quit && event->type != SDL_QUIT) {
SDL_PollEvent(event);
SDL_RenderClear(render);
// ImGui keyboard events
ImGui_ImplSdlGL3_ProcessEvent(event);
if (event->type == SDL_KEYDOWN) {
switch(event->key.keysym.sym) {
case SDLK_ESCAPE:
quit = true;
ImGui_ImplSdlGL3_Shutdown();
break;
default:
break;
}
}
// Draw some primitives
SDL_Rect fillRect = { 640 / 4, 480 / 4, 640 / 2, 480 / 2 };
SDL_SetRenderDrawColor( render, 0xFF, 0x00, 0x00, 0xFF );
SDL_RenderFillRect( render, &fillRect );
SDL_Rect outlineRect = { 640 / 6, 480 / 6, 640 * 2 / 3, 480 * 2 / 3 };
SDL_SetRenderDrawColor( render, 0x00, 0xFF, 0x00, 0xFF );
SDL_RenderDrawRect( render, &outlineRect );
// Update SDL screen
SDL_RenderPresent(render);
// ImGui
ImGui_ImplSdlGL3_NewFrame(window);
ImGui::Begin("hello world");
ImGui::Text("Hello, I am here");
ImGui::End();
// Render ImGui
ImGui::Render();
}
return 0;
}
This only shows the two primitives on the screen. I have drawn ImGui last, so it should be overlayed on top of the primitives. I suspect it’s not because ImGui is using opengl, and I’m using SDL Renderer, but I’m not sure since I think SDL2 uses opengl under the hood anyway. Is it possible to get ImGui to use SDL’s Renderer instead?
Or is the problem something different?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (4 by maintainers)
This is likely not an imgui issue. It’s not easy to mix custom gl code with SDL2 drawing, the basic problem is that sdl manages its own GL state, and imgui uses raw gl* commands which do know nothing of what SDL2 has on it’s GL mind. So you can’t simply use raw gl commands with SDL2 drawing calls mixed. One way that works is to have a look into sdl_gpu and using the GPU_FlushBlitBuffer() after all sdl related drawing has been done to begin imgui drawing and other custom GL calls.