Skip to content

Instantly share code, notes, and snippets.

@RogueMacro
Created March 24, 2021 17:00
Show Gist options
  • Save RogueMacro/6cfdc23a3d89f1805fa18771c800aa8c to your computer and use it in GitHub Desktop.
Save RogueMacro/6cfdc23a3d89f1805fa18771c800aa8c to your computer and use it in GitHub Desktop.
SDL2 + ImGui example in Beeflang
using System;
using ImGui;
using SDL2;
namespace Example
{
public class Program
{
public static int Main(String[] args)
{
if (!ImGui.CHECKVERSION())
Runtime.FatalError("ImGui version mismatch");
SDL.Init(.Events);
SDL.Window* window = SDL.CreateWindow("BFVM", .Centered, .Centered, 1080, 720, .OpenGL | .Maximized | .Resizable);
SDL.SDL_GLContext glContext = SDL.GL_CreateContext(window);
SDL.SDL_GL_MakeCurrent(window, glContext);
SDL.GL_SetSwapInterval(1);
ImGui.CreateContext();
ImGui.IO* io = ImGui.GetIO();
io.ConfigFlags |= .DockingEnable | .ViewportsEnable;
io.ConfigViewportsNoDecoration = false;
ImGuiImplOpenGL3.Init();
ImGuiImplSDL2.InitForOpenGL(window, &glContext);
GL.Init((proc) => SDL.SDL_GL_GetProcAddress(proc.Ptr));
bool isRunning = true;
while (isRunning)
{
while (SDL.PollEvent(var event) == 1)
{
ImGuiImplSDL2.ProcessEvent(&event);
if (event.type == .Quit)
isRunning = false;
else if (event.type == .WindowEvent && event.window.windowEvent == .Close && event.window.windowID == SDL.GetWindowID(window))
isRunning = false;
}
SDL.GetWindowSize(window, var width, var height);
io.DisplaySize = .(width, height);
ImGuiImplOpenGL3.NewFrame();
ImGuiImplSDL2.NewFrame(window);
ImGui.NewFrame();
if (ImGui.BeginMainMenuBar())
{
if (ImGui.BeginMenu("File"))
ImGui.EndMenu();
ImGui.EndMainMenuBar();
}
var viewport = ImGui.GetMainViewport();
ImGui.SetNextWindowPos(viewport.Pos);
ImGui.SetNextWindowSize(viewport.Size);
ImGui.SetNextWindowViewport(viewport.ID);
ImGui.PushStyleVar(.WindowPadding, .(0, 0));
ImGui.PushStyleVar(.WindowRounding, 0.0f);
ImGui.PushStyleVar(.WindowBorderSize, 0.0f);
ImGui.WindowFlags windowFlags = .NoResize | .NoMove | .NoBringToFrontOnFocus | .NoNavFocus;
ImGui.Begin("MainDockspaceWindow", null, windowFlags);
ImGui.PopStyleVar(3);
ImGui.ID dockspaceId = ImGui.GetID("MainDockspace");
ImGui.DockSpace(dockspaceId);
if (ImGui.Begin("TEST"))
{
ImGui.End();
}
ImGui.End();
ImGui.Render();
GL.glViewport(0, 0, (.)io.DisplaySize.x, (.)io.DisplaySize.y);
GL.glClearColor(0.2f, 0.2f, 0.2f, 1);
GL.glClear(GL.GL_COLOR_BUFFER_BIT);
ImGuiImplOpenGL3.RenderDrawData(ImGui.GetDrawData());
if (io.ConfigFlags.HasFlag(.ViewportsEnable))
{
SDL.Window* backup_current_window = SDL.GL_GetCurrentWindow();
SDL.SDL_GLContext backup_current_context = SDL.GL_GetCurrentContext();
#if BF_PLATFORM_WINDOWS
ImGui.UpdatePlatformWindows();
ImGui.RenderPlatformWindowsDefault();
#else
#error Only windows is supported
#endif
SDL.SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
}
SDL.GL_SwapWindow(window);
}
ImGuiImplOpenGL3.Shutdown();
ImGuiImplSDL2.Shutdown();
ImGui.DestroyContext();
SDL.GL_DeleteContext(glContext);
SDL.DestroyWindow(window);
SDL.Quit();
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment