From 3bad2eefdc59c9b89c0556ba438c054d54d95bb3 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Sat, 28 Dec 2013 01:17:56 +0100 Subject: [PATCH] [Examples] Added ExternalContext test This test uses SDL2 to create a window and an OpenGL context. It then uses OpenTK to render into the external SDL2 context. If everything is working correctly, a black window should appear and gradually turn white before disappearing. --- .../Examples/OpenTK/Test/ExternalContext.cs | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 Source/Examples/OpenTK/Test/ExternalContext.cs diff --git a/Source/Examples/OpenTK/Test/ExternalContext.cs b/Source/Examples/OpenTK/Test/ExternalContext.cs new file mode 100644 index 00000000..cdb6d156 --- /dev/null +++ b/Source/Examples/OpenTK/Test/ExternalContext.cs @@ -0,0 +1,81 @@ +// This code was written for the OpenTK library and has been released +// to the Public Domain. +// It is provided "as is" without express or implied warranty of any kind. + +using System; +using System.Runtime.InteropServices; +using OpenTK; +using OpenTK.Graphics; +using OpenTK.Graphics.OpenGL; + +namespace Examples.Tests +{ + [Example("External Context Test", ExampleCategory.OpenTK, "OpenGL")] + class ExternalContext + { + public static void Main() + { + using (Toolkit.Init(new ToolkitOptions { Backend = PlatformBackend.PreferNative })) + { + var window = Sdl2.CreateWindow("Test", 0, 0, 640, 480, WindowFlags.AllowHighDpi | WindowFlags.OpenGL); + var context = Sdl2.CreateContext(window); + Sdl2.MakeCurrent(window, context); + + using (var dummy = new GraphicsContext(new ContextHandle(context), OpenTK.Platform.Utilities.CreateDummyWindowInfo())) + { + for (int i = 0; i < 100; i++) + { + Sdl2.PumpEvents(); + GL.ClearColor(i / 100.0f, i / 100.0f, i / 100.0f, i / 100.0f); + GL.Clear(ClearBufferMask.ColorBufferBit); + + Sdl2.SwapWindow(window); + } + + Sdl2.DestroyWindow(window); + } + } + } + } + + #region SDL2 bindings + + public enum WindowFlags + { + Default = 0, + OpenGL = 0x00000002, + AllowHighDpi = 0x00002000, + } + + static class Sdl2 + { + const string lib = "SDL2.dll"; + + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_CreateWindow", ExactSpelling = true)] + public static extern IntPtr CreateWindow(string title, int x, int y, int w, int h, WindowFlags flags); + + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_GL_CreateContext", ExactSpelling = true)] + public static extern IntPtr CreateContext(IntPtr window); + + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_DestroyWindow", ExactSpelling = true)] + public static extern void DestroyWindow(IntPtr window); + + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_GL_GetCurrentContext", ExactSpelling = true)] + public static extern IntPtr GetCurrentContext(); + + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_GL_GetProcAddress", ExactSpelling = true)] + public static extern IntPtr GetAddress(string name); + + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_GL_MakeCurrent", ExactSpelling = true)] + public static extern int MakeCurrent(IntPtr window, IntPtr context); + + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_PumpEvents", ExactSpelling = true)] + public static extern void PumpEvents(); + + [DllImport(lib, CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_GL_SwapWindow", ExactSpelling = true)] + public static extern void SwapWindow(IntPtr window); + } + + #endregion + +}