Created
August 23, 2017 11:35
-
-
Save 0x0ade/da410a9c1d1bce19efe069710cdf0c4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Numerics; | |
using SDL2; | |
using ImGuiNET; | |
using ImGuiSDL2CS; | |
namespace YourGameNamespace { | |
public class YourGameWindow : ImGuiSDL2CSWindow { | |
private TextInputBuffer[] _TextInputBuffers; | |
public YourGameWindow() | |
: base("Your Game Window Title") { | |
//////// OPTIONAL //////// | |
// This affects the "underlying" SDL2Window and can be used for a quick game loop sketch. | |
// Don't set those and only ImGui gets rendered / handled. | |
// They're delegate fields so that one can override those from outside. | |
OnEvent = MyEventHandler; | |
OnLoop = MyGameLoop; | |
} | |
// Create any possibly unmanaged resources (textures, buffers) here. | |
protected override void Create() { | |
base.Create(); | |
_TextInputBuffers = TextInputBuffer.CreateBuffers(2); | |
} | |
// Dispose any possibly unmanaged resources (textures, buffers) here. | |
protected override void Dispose(bool disposing) { | |
TextInputBuffer.DisposeBuffers(_TextInputBuffers); | |
base.Dispose(disposing); | |
} | |
// This runs between ImGuiSDL2CSHelper.NewFrame and ImGuiSDL2CSHelper.Render. | |
public unsafe override void ImGuiLayout() { | |
ImGui.BeginWindow("Window 1"); | |
ImGui.LabelText("Label", "Text"); | |
ImGui.InputText("Some text input", _TextInputBuffers[0].Buffer, _TextInputBuffers[0].Length, InputTextFlags.Default, ImGuiSDL2CSHelper.OnTextEdited); | |
ImGui.EndWindow(); | |
ImGui.BeginWindow("Window 2"); | |
ImGui.LabelText("Label", "Text"); | |
ImGui.InputText("Some other text input", _TextInputBuffers[1].Buffer, _TextInputBuffers[1].Length, InputTextFlags.Default, ImGuiSDL2CSHelper.OnTextEdited); | |
ImGui.EndWindow(); | |
} | |
//////// OPTIONAL //////// | |
// Processs any SDL2 events manually if required. | |
// Return false to not allow the default event handler to process it. | |
public bool MyEventHandler(SDL2Window _self, SDL.SDL_Event e) { | |
// We're replacing OnEvent and thus call ImGuiSDL2CSHelper.OnEvent manually. | |
if (!ImGuiSDL2CSHelper.OnEvent(e, ref g_MouseWheel, g_MousePressed)) | |
return false; | |
// Any custom event handling can happen here. | |
return true; | |
} | |
// Any custom game loop should end up here. | |
// Setting the window.IsActive = false stops the loop. | |
public void MyGameLoop(SDL2Window _self) { | |
// This is the default implementation. | |
// Using MiniTK (not OpenTK) to provide access to OpenGL methods. | |
// Alternatively, use SDL_GL_GetProcAddress on your own. | |
OpenTK.Graphics.OpenGL.GL.ClearColor(0.1f, 0.125f, 0.15f, 1f); | |
OpenTK.Graphics.OpenGL.GL.Clear(OpenTK.Graphics.OpenGL.ClearBufferMask.ColorBufferBit); | |
// This calls ImGuiSDL2CSHelper.NewFrame, the overridden ImGuiLayout, ImGuiSDL2CSHelper.Render and renders it. | |
// ImGuiSDL2CSHelper.NewFrame properly sets up ImGuiIO and ImGuiSDL2CSHelper.Render renders the draw data. | |
ImGuiRender(); | |
// Finally, swap. | |
Swap(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment