mirror of
https://github.com/ocornut/imgui.git
synced 2026-11-04 20:32:00 +00:00
Unable to get ImGui to show in simple SDL program #935
Labels
No labels
accidentally closed
android
backends
bug
building
clipper
coloredit
color spaces
cplusplus
data types
demo
disabled
doc
docking
dpi
drag drop
drawing/drawlist
dropped
dx12
enhancement
focus
font/text
gallery
help wanted
idle/power saving
ime
in progress
inputs
inputtext
internals
label/id and id stack
language bindings
layout
linux/bsd
menus
multi-contexts
multi-threading
multi-viewports
nav
news/info
opengl
optimization
osx/ios
overlap
plot/graph
popups
question
scrolling
selection
settings
sliders/drags
style
tables/columns
tabs
test wanted
tools
tooltips
tree
tricks & tips
useful widgets
vulkan
web
window pos/depth
window sizing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ocornut/imgui#935
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.cppandimgui_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.cppexample: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?
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.
The GL3 examples are trying to set every GL state they need and restore everything back to how it was at the end. So in theory there shouldn't be a problem and it shouldn't be "hard" to mix them. In practice maybe there's an unsolved issue.
Shouldn't SDL_RenderPresent be called at the very end of your GL frame?
@ocornut I moved SDL_RenderPresent to the very end, after
ImGui::Render();. But ImGui still does not show up.@kuhaut I tried your suggestion to use
sdl_gpu, and I modified the code to be:However, I only see the two primitives being drawn. ImGui still does not show up. Have I used
GPU_FlushBlitBuffer()correctly?What does the ImGui call back for rendering look like? If its using gl then this won't work because you can't mix SDL_Render* and OpenGL in general.
@paulsapps Hey, just as a quick test, I went and removed all code relating to
SDL_Renderer. This leaves just ImGui, and so it should show up right?However, the following just renders an empty SDL window, presumably because there's no renderer now. ImGui does not render at all:
Not sure if this is relevant, but I also modified the
imgui.inifile that came with the demo, so any ImGui window should show at (0,0) (If I understood it correctly).So, now I'm confused. Why is ImGui not showing even after I removed the sdl renderer?
Can't tell what those other functions do but it seems a lot of init code is missing... also
SDL_Event* event = new SDL_Event();isn't right, that should be on the stack.Compare your code above with the SDL+GL3 example in the repository, you are missing code to initialize OpenGL and swap the buffers, etc.
I don't know about SDL myself. If you want to use SDL_RenderXXX functions whatever they do, look for info about mixing them with OpenGL on the internet and maybe you'll find a workaround or understanding of how to do it. If SDL_RenderXXX uses OpenGL I see no reason to not be able to use OpenGL directly yourself. It appears to use the old style fixed pipeline so maybe using GL2 code will be more compatible with it. What appears to be lacking in SDL_Render API is a way to render textured triangles, which would have allowed to make an imgui renderer using only the SDL_Render API.
It shouldn't be so hard to add one nor it should be hard to just use OpenGL assuming your SDL_Render is configured to use OpenGL itself.
Yes, imgui already saves & restores the gl state, i had some problems with this a time ago, at one point it worked on linux nvidia nouveau, but not on windows with proprietary drivers, 'the web' said that sdl2 got it's own mind of the internal gl state, and it's not much plug & play but digging deeper at which point i used sdl_gpu as a opengl intermediate between imgui and sdl2 and other gl commands, which works nice on linux & windows (you however need to use all drawing functions from sdl_gpu). If you want to dig deeper, look into what GPU_FlushBlitBuffer (custom GL start) and GPU_ResetRendererState (custom GL stop) do.
Great library btw ocornut & all contributors, hope i could help a bit.
SDL init thats working for me
Add in the end of the loop
There is no need to initialize SDL_Renderer and SDL_Event
@kuhaut Thanks for your suggestion. Would it be possible to get a working code sample using sdl_gpu? I think it would make a nice addition to examples/. I don't quite understand how sdl, sdl_gpu, and imgui should work together. Do I now have to use sdl_gpu to draw my primitives, instead of using SDL Renderer?
One confusing point is, to initialize an sdl_gpu window, I have to do:
I can then draw random things using:
So, I guess I no longer have to use SDL window or the renderer, and instead I use the sdl_gpu provided functions? But when integrating with ImGui, it is expecting an SDL Window. If I try to pass in the
screen, it fails:Does
imgui_impl_sdl_gl3.cppandimgui_impl_sdl_gl3.hneed to be ported to sdl_gpu in order for this to work?Your not checking if gl3w picked opengl functions. No need to suffer with SDL_GPU really. SDL renderer works with opengl just fine you only have to use compatibility profile
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY)Also check my previous comment.
Yes, oreyg, it's definitely another wrapper around sdl2 drawing, so you might avoid the additional complexity. The lib quality seems to be quite nice however, and since i calculate positions mostly in float coordinates, there's no need to do the int conversion for every sdl2 draw. float to int costs quite a bit cycles, int to float being cheap, so it might aswell be that sdl_gpu rendering is a bit faster than sdl2 draw calls, but i digress. One of the main reasons i use it, is that it solved the problem of mixing easy draw calls with custom opengl & imgui drawing for me.
basic setup would be like this:
this works without problems so far on multiple windows & linux machines for me, but a working sdl2 only example would still be nice.
Moving discussion to #1116