Skip to content

Instantly share code, notes, and snippets.

@leiradel
Created August 7, 2019 21:22
Show Gist options
  • Save leiradel/658d2225898b12c0be399dcd4af1d4a8 to your computer and use it in GitHub Desktop.
Save leiradel/658d2225898b12c0be399dcd4af1d4a8 to your computer and use it in GitHub Desktop.
Dear ImGui docking bug
#include <imgui.h>
#include <imgui_impl_sdl.h>
#include <imgui_impl_opengl2.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <string>
class Application
{
protected:
SDL_Window* _window;
SDL_GLContext _glContext;
public:
Application() = default;
bool init(std::string const& title, int width, int height)
{
// Setup SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
return false;
}
// Setup window
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
if (_window == NULL)
{
return false;
}
_glContext = SDL_GL_CreateContext(_window);
if (_glContext == NULL)
{
SDL_DestroyWindow(_window);
return false;
}
SDL_GL_MakeCurrent(_window, _glContext);
SDL_GL_SetSwapInterval(1);
// Setup ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
ImGui::StyleColorsDark();
ImGui_ImplSDL2_InitForOpenGL(_window, _glContext);
ImGui_ImplOpenGL2_Init();
return true;
}
void destroy()
{
ImGui_ImplOpenGL2_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(_glContext);
SDL_DestroyWindow(_window);
SDL_Quit();
}
void run()
{
bool done = false;
do
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (!ImGui_ImplSDL2_ProcessEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
case SDL_CONTROLLERBUTTONUP:
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERAXISMOTION:
break;
}
}
}
ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplSDL2_NewFrame(_window);
ImGui::NewFrame();
draw();
ImGui::Render();
glViewport(0, 0, (int)ImGui::GetIO().DisplaySize.x, (int)ImGui::GetIO().DisplaySize.y);
glClearColor(0.05f, 0.05f, 0.05f, 0);
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
SDL_GL_SwapWindow(_window);
SDL_Delay(1);
}
while (!done);
}
void draw()
{
const float oldWindowRounding = ImGui::GetStyle().WindowRounding;
ImGui::GetStyle().WindowRounding = 0;
const ImVec2 oldWindowPadding = ImGui::GetStyle().WindowPadding;
// Uncomment the following line to force the bug.
ImGui::GetStyle().WindowPadding = ImVec2(0.0f, 0.0f);
ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowSize(ImGui::GetIO().DisplaySize);
ImGui::SetNextWindowBgAlpha(1.0f);
const ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoInputs
| ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoSavedSettings;
ImGui::Begin("dock", nullptr, flags);
ImGui::GetStyle().WindowRounding = oldWindowRounding;
ImGui::GetStyle().WindowPadding = oldWindowPadding;
ImGui::DockSpace(ImGui::GetID("dock"));
ImGui::End();
if (ImGui::Begin("Test 1")) {}
ImGui::End();
if (ImGui::Begin("Test 2")) {}
ImGui::End();
}
};
int main(int, char**)
{
Application app;
if (!app.init("Docking bug", 1024, 640))
{
return 1;
}
app.run();
app.destroy();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment