Unable to get ImGui to show in simple SDL program #935

Closed
opened 2016-12-09 16:07:34 +00:00 by slidingboar · 15 comments
slidingboar commented 2016-12-09 16:07:34 +00:00 (Migrated from github.com)

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?

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?
kuhaut commented 2016-12-10 22:29:23 +00:00 (Migrated from github.com)

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.

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.
ocornut commented 2016-12-10 23:39:59 +00:00 (Migrated from github.com)

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.

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.
ocornut commented 2016-12-10 23:41:34 +00:00 (Migrated from github.com)

Shouldn't SDL_RenderPresent be called at the very end of your GL frame?

Shouldn't SDL_RenderPresent be called at the very end of your GL frame?
slidingboar commented 2016-12-11 01:52:09 +00:00 (Migrated from github.com)

@ocornut I moved SDL_RenderPresent to the very end, after ImGui::Render();. But ImGui still does not show up.

@ocornut I moved SDL_RenderPresent to the very end, after `ImGui::Render();`. But ImGui still does not show up.
slidingboar commented 2016-12-11 02:50:03 +00:00 (Migrated from github.com)

@kuhaut I tried your suggestion to use sdl_gpu, and I modified the code to be:

        #include "SDL_gpu.h"

        // ...

        // 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);

        // ** sdl_gpu **
        GPU_FlushBlitBuffer()

        // ImGui
        ImGui_ImplSdlGL3_NewFrame(window);
        ImGui::Begin("hello world");
        ImGui::Text("Hello, I am here");
        ImGui::End();

        // Render ImGui
        ImGui::Render();

However, I only see the two primitives being drawn. ImGui still does not show up. Have I used GPU_FlushBlitBuffer() correctly?

@kuhaut I tried your suggestion to use `sdl_gpu`, and I modified the code to be: ``` #include "SDL_gpu.h" // ... // 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); // ** sdl_gpu ** GPU_FlushBlitBuffer() // ImGui ImGui_ImplSdlGL3_NewFrame(window); ImGui::Begin("hello world"); ImGui::Text("Hello, I am here"); ImGui::End(); // Render ImGui ImGui::Render(); ``` However, I only see the two primitives being drawn. ImGui still does not show up. Have I used `GPU_FlushBlitBuffer()` correctly?
MrSapps commented 2016-12-11 02:53:46 +00:00 (Migrated from github.com)

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.

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.
slidingboar commented 2016-12-11 03:14:43 +00:00 (Migrated from github.com)

@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:

#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();

    bool quit = false;

    // ImGui Initializtion
    gl3wInit();
    ImGui_ImplSdlGL3_Init(window);
    

    // Main loop
    while(!quit && event->type != SDL_QUIT) {
        SDL_PollEvent(event);

        // 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;
            }
        }

        // ImGui
        ImGui_ImplSdlGL3_NewFrame(window);
        ImGui::Begin("hello world");
        ImGui::Text("Hello, I am here");
        ImGui::End();

        // Render ImGui
        ImGui::Render();

    }

    return 0;
}

Not sure if this is relevant, but I also modified the imgui.ini file that came with the demo, so any ImGui window should show at (0,0) (If I understood it correctly).

[Debug]
Pos=0,0
Size=400,400
Collapsed=0

[ImGui Demo]
Pos=0,0
Size=550,680
Collapsed=0

[hello world]
Pos=0,0
Size=127,48
Collapsed=0

So, now I'm confused. Why is ImGui not showing even after I removed the sdl renderer?

@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: ``` #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(); bool quit = false; // ImGui Initializtion gl3wInit(); ImGui_ImplSdlGL3_Init(window); // Main loop while(!quit && event->type != SDL_QUIT) { SDL_PollEvent(event); // 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; } } // ImGui ImGui_ImplSdlGL3_NewFrame(window); ImGui::Begin("hello world"); ImGui::Text("Hello, I am here"); ImGui::End(); // Render ImGui ImGui::Render(); } return 0; } ``` Not sure if this is relevant, but I also modified the `imgui.ini` file that came with the demo, so any ImGui window should show at (0,0) (If I understood it correctly). ``` [Debug] Pos=0,0 Size=400,400 Collapsed=0 [ImGui Demo] Pos=0,0 Size=550,680 Collapsed=0 [hello world] Pos=0,0 Size=127,48 Collapsed=0 ``` So, now I'm confused. Why is ImGui not showing even after I removed the sdl renderer?
MrSapps commented 2016-12-11 03:18:37 +00:00 (Migrated from github.com)

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.

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.
ocornut commented 2016-12-11 11:43:32 +00:00 (Migrated from github.com)

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.

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.
kuhaut commented 2016-12-12 10:49:55 +00:00 (Migrated from github.com)

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.

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.
oreyg commented 2016-12-12 14:42:08 +00:00 (Migrated from github.com)

SDL init thats working for me

    // Init SDL
    if(SDL_Init( SDL_INIT_EVERYTHING ) < 0)
    {
        throw runtime_error("SDL could not be initialized!" + string(SDL_GetError()));
    }

    // Tell SDL what version of OpenGL you need
    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, 2);

    // Create Window
    window = SDL_CreateWindow(
        "ImGui demo",
        SDL_WINDOWPOS_UNDEFINED,
        SDL_WINDOWPOS_UNDEFINED,
        640,
        480,
        SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);

    // Check if window was created
    if(window == 0)
    {
        SDL_Quit();
        throw runtime_error("SDL could not instantiate window. " + string(SDL_GetError()));
    }

    // Create OpenGL context for window
    SDL_GLContext glcontext = SDL_GL_CreateContext(window);
    if (gl3wInit())
    {
        SDL_GL_DeleteContext(glcontext);
        SDL_Quit();
        throw runtime_error("Failed to initialize OpenGL\n");
    }

    // Init ImGui
    ImGui_ImplSdlGL3_Init(window);

Add in the end of the loop

SDL_GL_SwapWindow(window);
glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
glClear(GL_COLOR_BUFFER_BIT);

There is no need to initialize SDL_Renderer and SDL_Event

SDL_Event* event = nullptr;
...
SDL_PollEvent(event);
ImGui_ImplSdlGL3_ProcessEvent(event);
quit = event.type != SDL_QUIT;
SDL init thats working for me ``` // Init SDL if(SDL_Init( SDL_INIT_EVERYTHING ) < 0) { throw runtime_error("SDL could not be initialized!" + string(SDL_GetError())); } // Tell SDL what version of OpenGL you need 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, 2); // Create Window window = SDL_CreateWindow( "ImGui demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); // Check if window was created if(window == 0) { SDL_Quit(); throw runtime_error("SDL could not instantiate window. " + string(SDL_GetError())); } // Create OpenGL context for window SDL_GLContext glcontext = SDL_GL_CreateContext(window); if (gl3wInit()) { SDL_GL_DeleteContext(glcontext); SDL_Quit(); throw runtime_error("Failed to initialize OpenGL\n"); } // Init ImGui ImGui_ImplSdlGL3_Init(window); ``` Add in the end of the loop ``` SDL_GL_SwapWindow(window); glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y); glClear(GL_COLOR_BUFFER_BIT); ``` There is no need to initialize SDL_Renderer and SDL_Event ``` SDL_Event* event = nullptr; ... SDL_PollEvent(event); ImGui_ImplSdlGL3_ProcessEvent(event); quit = event.type != SDL_QUIT; ```
slidingboar commented 2016-12-12 17:40:19 +00:00 (Migrated from github.com)

@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:

GPU_Target* screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS);

I can then draw random things using:

void GPU_Clear(GPU_Target* target);
void GPU_Blit(GPU_Image* image, GPU_Rect* src_rect, GPU_Target* target, float x, float y);

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:

ImGui_ImplSdlGL3_Init(screen);

---
error: cannot convert ‘GPU_Target*’ to ‘SDL_Window*’ for argument ‘1’ to ‘bool ImGui_ImplSdlGL3_Init(SDL_Window*)’

Does imgui_impl_sdl_gl3.cpp and imgui_impl_sdl_gl3.h need to be ported to sdl_gpu in order for this to work?

@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: ``` GPU_Target* screen = GPU_Init(800, 600, GPU_DEFAULT_INIT_FLAGS); ``` I can then draw random things using: ``` void GPU_Clear(GPU_Target* target); void GPU_Blit(GPU_Image* image, GPU_Rect* src_rect, GPU_Target* target, float x, float y); ``` 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: ``` ImGui_ImplSdlGL3_Init(screen); --- error: cannot convert ‘GPU_Target*’ to ‘SDL_Window*’ for argument ‘1’ to ‘bool ImGui_ImplSdlGL3_Init(SDL_Window*)’ ``` Does ` imgui_impl_sdl_gl3.cpp` and ` imgui_impl_sdl_gl3.h` need to be ported to sdl_gpu in order for this to work?
oreyg commented 2016-12-12 19:42:31 +00:00 (Migrated from github.com)

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.

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.
kuhaut commented 2016-12-12 22:12:24 +00:00 (Migrated from github.com)

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:

// init
SDL_Init(sdl_init_flags);
GPU_Target* gpu_screen = GPU_InitRenderer(GPU_RENDERER_OPENGL_3, screen_w, screen_h, sdl2_window_and_sdl_gpu_startup_flags );
// so den sdl windowpointer rauspopeln:
SDL_Window* sdl_window = SDL_GetWindowFromID(gpu_screen->context->windowID);  
GPU_Image* testimage = GPU_LoadImage("test.png");

// input loop
SDL_Event event;
while(SDL_PollEvent(&event)) { ImGui_ImplSdlGL3_ProcessEvent(&event); } // process all events that happened since last frame
ImGui_ImplSdlGL3_NewFrame();

// draw loop
GPU_Clear(gpu_screen);
GPU_Blit(testimage, NULL, gpu_screen, 0.0f, 0.0f);
GPU_FlushBlitBuffer(); // from now on raw opengl commands
ImGui::ShowTestWindow(); ImGui::Render();
GPU_ResetRendererState(); // now sdl_gpu draws over imgui
GPU_BlitScale(testimage, NULL, gpu_screen, 0.0f, 0.0f, 0.1f, 0.1f);
GPU_Flip(gpu_screen);

this works without problems so far on multiple windows & linux machines for me, but a working sdl2 only example would still be nice.

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: ``` // init SDL_Init(sdl_init_flags); GPU_Target* gpu_screen = GPU_InitRenderer(GPU_RENDERER_OPENGL_3, screen_w, screen_h, sdl2_window_and_sdl_gpu_startup_flags ); // so den sdl windowpointer rauspopeln: SDL_Window* sdl_window = SDL_GetWindowFromID(gpu_screen->context->windowID); GPU_Image* testimage = GPU_LoadImage("test.png"); // input loop SDL_Event event; while(SDL_PollEvent(&event)) { ImGui_ImplSdlGL3_ProcessEvent(&event); } // process all events that happened since last frame ImGui_ImplSdlGL3_NewFrame(); // draw loop GPU_Clear(gpu_screen); GPU_Blit(testimage, NULL, gpu_screen, 0.0f, 0.0f); GPU_FlushBlitBuffer(); // from now on raw opengl commands ImGui::ShowTestWindow(); ImGui::Render(); GPU_ResetRendererState(); // now sdl_gpu draws over imgui GPU_BlitScale(testimage, NULL, gpu_screen, 0.0f, 0.0f, 0.1f, 0.1f); GPU_Flip(gpu_screen); ``` this works without problems so far on multiple windows & linux machines for me, but a working sdl2 only example would still be nice.
ocornut commented 2017-05-01 12:38:03 +00:00 (Migrated from github.com)

Moving discussion to #1116

Moving discussion to #1116
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#935
No description provided.