Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImGui in OpenGL Games (Cocoa) (MacOS) #1253

Closed
aKalisch opened this issue Jul 30, 2017 · 4 comments
Closed

ImGui in OpenGL Games (Cocoa) (MacOS) #1253

aKalisch opened this issue Jul 30, 2017 · 4 comments

Comments

@aKalisch
Copy link

aKalisch commented Jul 30, 2017

Hello guys.

I currently try to implement ImGui in CS:GO (MacOS).

Therefor I hooked SDL_GL_SwapWindow from the Game itself. So far so good. All works fine and the hook is executing.

void SDLHook::HookSwapWindow() {
    uintptr_t swapwindowFn = reinterpret_cast<uintptr_t>(dlsym(RTLD_NEXT, "SDL_GL_SwapWindow"));
    swapwindow_ptr = reinterpret_cast<uintptr_t*>(CPatternScanner::Instance()->GetAbsoluteAddress("libSDL2-2.0.0.dylib", swapwindowFn, 0xF, 0x4));

    swapwindow_original = *swapwindow_ptr;

    *swapwindow_ptr = reinterpret_cast<uintptr_t>(&SDLHook::SDLSwapWindow);
}

The code of the hooked method:

void SDLHook::SDLSwapWindow(SDL_Window* window) {
    static oSDL_GL_SwapWindowFn oSDL_GL_SwapWindow = reinterpret_cast<oSDL_GL_SwapWindowFn>(swapwindow_original);

    static SDL_GLContext original_context = SDL_GL_GetCurrentContext();

    static SDL_GLContext user_context = NULL;

    if (!user_context) {
        user_context = SDL_GL_CreateContext(window);

        SDL_GL_MakeCurrent(window, user_context);

        ImGui_ImplSdl_Init(window);
        
    } else {
        SDL_GL_MakeCurrent(window, user_context);
    }

    ImGui_ImplSdl_NewFrame(window);

    ImGui::SetNextWindowPos(ImVec2(10.f, 10.f));
    ImGui::SetNextWindowSize(ImVec2(320.f, 240.f));
    ImGui::Begin("Test Window");
    ImGui::Text("Just a simple text");
    ImGui::End();

    ImGui::Render();

    SDL_GL_MakeCurrent(window, original_context);

    oSDL_GL_SwapWindow(window);
}

There are no crashes and the game is running well.
But it's simply not drawing anything.

When I change the hooked method to:

void SDLHook::SDLSwapWindow(SDL_Window* window) {
    static oSDL_GL_SwapWindowFn oSDL_GL_SwapWindow = reinterpret_cast<oSDL_GL_SwapWindowFn>(swapwindow_original);

    static bool ImGuiInit = false;

    if (!ImGuiInit) {
        ImGuiInit = true;

        ImGui_ImplSdl_Init(window);
    }

    ImGui_ImplSdl_NewFrame(window);

    ImGui::SetNextWindowPos(ImVec2(10.f, 10.f));
    ImGui::SetNextWindowSize(ImVec2(320.f, 240.f));
    ImGui::Begin("Test Window");
    ImGui::Text("Just a simple text");
    ImGui::End();

    ImGui::Render();

    oSDL_GL_SwapWindow(window);
}

The "Test Window" is drawing. But the game freezes.

Does someone have a clue why this happens?

@raresica1234
Copy link

Maybe you are swapping the window before anything from CS:GO can render, therefore Windows sees the game as freezing because there is nothing drawing.

@aKalisch
Copy link
Author

It's not windows... It's macOS ;-)

And no.. The Hook is the SwapWindow function. When ImGui Frame is executed I call the original funtction.

I think the problem is at the cocoa gl side.

@ocornut ocornut added the opengl label Jan 7, 2018
@dwnste
Copy link

dwnste commented Feb 28, 2018

I'm kinda late, but I've found the workaround: https://github.com/dwnste/imgui_sdl_osx

@ocornut
Copy link
Owner

ocornut commented Mar 1, 2018

Sorry but #1586

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants