11using System . Numerics ;
2+ using System . Runtime . InteropServices ;
23using Hexa . NET . ImGui ;
34using Hexa . NET . ImGui . Backends . GLFW ;
45using Hexa . NET . ImGui . Backends . OpenGL3 ;
@@ -14,10 +15,12 @@ public unsafe class ImGuiWrapper : IDisposable {
1415 private readonly Sdl sdl ;
1516 private readonly Silk . NET . SDL . Window * window ;
1617 private readonly uint windowId ;
17- private readonly void * glContext ;
18+ private readonly NativeContext context ;
19+ private readonly GL gl ;
1820 private readonly ImGuiContext * imguiContext ;
1921
2022 public bool Exiting ;
23+
2124 public Vector2 WindowPos {
2225 get {
2326 int x ;
@@ -69,8 +72,8 @@ public ImGuiWrapper(
6972 }
7073 }
7174
72- this . glContext = this . sdl . GLCreateContext ( this . window ) ;
73- GL . InitApi ( new SdlNativeContext ( this . sdl ) ) ;
75+ this . context = new NativeContext ( this . sdl , this . window ) ;
76+ this . gl = new GL ( this . context ) ;
7477
7578 this . imguiContext = ImGui . CreateContext ( ) ;
7679 ImGui . SetCurrentContext ( this . imguiContext ) ;
@@ -81,19 +84,23 @@ public ImGuiWrapper(
8184 . AddDefaultFont ( )
8285 . SetOption ( config => { config . FontBuilderFlags |= ( uint ) ImGuiFreeTypeBuilderFlags . LoadColor ; } ) ;
8386
87+ var io = ImGui . GetIO ( ) ;
88+ io . IniFilename = ( byte * ) Marshal . StringToHGlobalAnsi ( iniPath + "\0 " ) ;
89+
8490 const string cjkFont = "C:/Windows/Fonts/msyh.ttc" ;
8591 if ( File . Exists ( cjkFont ) ) {
86- var ranges = ImGui . GetIO ( ) . Fonts . GetGlyphRangesJapanese ( ) ;
92+ var ranges = io . Fonts . GetGlyphRangesJapanese ( ) ;
8793 builder . AddFontFromFileTTF ( cjkFont , 13f , ranges ) ;
8894 }
8995
9096 builder . Build ( ) ;
9197
92- ImGuiImplSDL2 . InitForOpenGL ( ( SDLWindow * ) this . window , this . glContext ) ;
98+ ImGuiImplSDL2 . InitForOpenGL ( ( SDLWindow * ) this . window , ( void * ) this . context . Handle ) ;
9399 ImGuiImplGLFW . SetCurrentContext ( ImGui . GetCurrentContext ( ) ) ;
94100
95101 ImGuiImplOpenGL3 . SetCurrentContext ( ImGui . GetCurrentContext ( ) ) ;
96102 ImGuiImplOpenGL3 . Init ( ( string ) null ! ) ;
103+
97104 ImGuiImplOpenGL3 . NewFrame ( ) ;
98105 }
99106
@@ -120,14 +127,14 @@ public void Render(Action draw) {
120127
121128 draw ( ) ;
122129
123- this . sdl . GLMakeCurrent ( this . window , this . glContext ) ;
124- GL . BindFramebuffer ( GLFramebufferTarget . Framebuffer , 0 ) ;
130+ this . sdl . GLMakeCurrent ( this . window , ( void * ) this . context . Handle ) ;
131+ this . gl . BindFramebuffer ( GLFramebufferTarget . Framebuffer , 0 ) ;
125132
126133 const float grey = 40 / 255f ;
127- GL . ClearColor ( grey , grey , grey , 1f ) ;
134+ this . gl . ClearColor ( grey , grey , grey , 1f ) ;
128135
129136 // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
130- GL . Clear ( GLClearBufferMask . ColorBufferBit | GLClearBufferMask . DepthBufferBit ) ;
137+ this . gl . Clear ( GLClearBufferMask . ColorBufferBit | GLClearBufferMask . DepthBufferBit ) ;
131138
132139 ImGui . Render ( ) ;
133140 ImGui . EndFrame ( ) ;
@@ -154,7 +161,7 @@ public void Dispose() {
154161 ImGui . SetCurrentContext ( null ) ;
155162 ImGui . DestroyContext ( this . imguiContext ) ;
156163
157- this . sdl . GLDeleteContext ( this . glContext ) ;
164+ this . context . Dispose ( ) ;
158165 this . sdl . DestroyWindow ( this . window ) ;
159166 this . sdl . Quit ( ) ;
160167 }
@@ -167,39 +174,52 @@ public nint CreateTexture(byte[] data, int width, int height) {
167174 }
168175
169176 fixed ( byte * dataPtr = data ) {
170- var texture = GL . GenTexture ( ) ;
171- GL . BindTexture ( GLTextureTarget . Texture2D , texture ) ;
177+ var texture = this . gl . GenTexture ( ) ;
178+ this . gl . BindTexture ( GLTextureTarget . Texture2D , texture ) ;
172179
173- GL . TexParameteri ( GLTextureTarget . Texture2D , GLTextureParameterName . MinFilter ,
180+ this . gl . TexParameteri ( GLTextureTarget . Texture2D , GLTextureParameterName . MinFilter ,
174181 ( int ) GLTextureMinFilter . Linear ) ;
175- GL . TexParameteri ( GLTextureTarget . Texture2D , GLTextureParameterName . MagFilter ,
182+ this . gl . TexParameteri ( GLTextureTarget . Texture2D , GLTextureParameterName . MagFilter ,
176183 ( int ) GLTextureMagFilter . Linear ) ;
177184
178- GL . PixelStorei ( GLPixelStoreParameter . UnpackRowLength , 0 ) ;
179- GL . TexImage2D ( GLTextureTarget . Texture2D , 0 , GLInternalFormat . Rgba , width , height , 0 , GLPixelFormat . Rgba ,
185+ this . gl . PixelStorei ( GLPixelStoreParameter . UnpackRowLength , 0 ) ;
186+ this . gl . TexImage2D ( GLTextureTarget . Texture2D , 0 , GLInternalFormat . Rgba , width , height , 0 ,
187+ GLPixelFormat . Rgba ,
180188 GLPixelType . UnsignedByte , ( nint ) dataPtr ) ;
181189
182190 return ( nint ) texture ;
183191 }
184192 }
185193
186194 public void DestroyTexture ( nint texture ) {
187- GL . DeleteTexture ( ( uint ) texture ) ;
195+ this . gl . DeleteTexture ( ( uint ) texture ) ;
188196 }
189197
190- public class SdlNativeContext ( Sdl sdl ) : INativeContext {
191- public nint GetProcAddress ( string procName ) {
192- return ( nint ) sdl . GLGetProcAddress ( procName ) ;
198+ private class NativeContext ( Sdl sdl , Silk . NET . SDL . Window * window ) : IGLContext {
199+ private void * glContext = sdl . GLCreateContext ( window ) ;
200+ public nint Handle => ( nint ) this . glContext ;
201+ public bool IsCurrent => sdl . GLGetCurrentContext ( ) == this . glContext ;
202+
203+ public void Dispose ( ) {
204+ if ( this . glContext != null ) {
205+ sdl . GLDeleteContext ( this . glContext ) ;
206+ this . glContext = null ;
207+ }
193208 }
194209
195210 public bool TryGetProcAddress ( string procName , out nint procAddress ) {
196- return ( procAddress = ( nint ) sdl . GLGetProcAddress ( procName ) ) != nint . Zero ;
211+ procAddress = ( nint ) sdl . GLGetProcAddress ( procName ) ;
212+ return procAddress != 0 ;
197213 }
198214
199- public bool IsExtensionSupported ( string extensionName ) {
200- return sdl . GLExtensionSupported ( extensionName ) != 0 ;
201- }
215+ public nint GetProcAddress ( string procName )
216+ => ( nint ) sdl . GLGetProcAddress ( procName ) ;
217+
218+ public bool IsExtensionSupported ( string extensionName )
219+ => sdl . GLExtensionSupported ( extensionName ) != 0 ;
202220
203- public void Dispose ( ) { }
221+ public void MakeCurrent ( ) => sdl . GLMakeCurrent ( window , this . glContext ) ;
222+ public void SwapBuffers ( ) => sdl . GLSwapWindow ( window ) ;
223+ public void SwapInterval ( int interval ) => sdl . GLSetSwapInterval ( interval ) ;
204224 }
205225}
0 commit comments