IntPtr CreateWindow(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device, IntPtr parentHandle) { // Use win32 to create the native window. // Keep in mind that some construction code runs in the WM_CREATE message handler. // The style of a parent window is different than that of a child window. // Note: the child window should always be visible, even if the parent isn't. WindowStyle style = 0; ExtendedWindowStyle ex_style = 0; if (parentHandle == IntPtr.Zero) { style |= WindowStyle.OverlappedWindow | WindowStyle.ClipChildren; ex_style = ParentStyleEx; } else { style |= WindowStyle.Visible | WindowStyle.Child | WindowStyle.ClipSiblings; ex_style = ChildStyleEx; } // Find out the final window rectangle, after the WM has added its chrome (titlebar, sidebars etc). Win32Rectangle rect = new Win32Rectangle(); rect.left = x; rect.top = y; rect.right = x + width; rect.bottom = y + height; Functions.AdjustWindowRectEx(ref rect, style, false, ex_style); // Create the window class that we will use for this window. // The current approach is to register a new class for each top-level WinGLWindow we create. if (!class_registered) { ExtendedWindowClass wc = new ExtendedWindowClass(); wc.Size = ExtendedWindowClass.SizeInBytes; wc.Style = DefaultClassStyle; wc.Instance = Instance; wc.WndProc = WindowProcedureDelegate; wc.ClassName = ClassName; wc.Icon = Icon != null ? Icon.Handle : IntPtr.Zero; #warning "This seems to resize one of the 'large' icons, rather than using a small icon directly (multi-icon files). Investigate!" wc.IconSm = Icon != null ? new Icon(Icon, 16, 16).Handle : IntPtr.Zero; wc.Cursor = Functions.LoadCursor(CursorName.Arrow); ushort atom = Functions.RegisterClassEx(ref wc); if (atom == 0) { throw new PlatformException(String.Format("Failed to register window class. Error: {0}", Marshal.GetLastWin32Error())); } class_registered = true; } IntPtr window_name = Marshal.StringToHGlobalAuto(title); IntPtr handle = Functions.CreateWindowEx( ex_style, ClassName, window_name, style, rect.left, rect.top, rect.Width, rect.Height, parentHandle, IntPtr.Zero, Instance, IntPtr.Zero); if (handle == IntPtr.Zero) { throw new PlatformException(String.Format("Failed to create window. Error: {0}", Marshal.GetLastWin32Error())); } return(handle); }
IntPtr CreateWindow(int x, int y, int width, int height, string title, GameWindowFlags options, DisplayDevice device, IntPtr parentHandle) { // Use win32 to create the native window. // Keep in mind that some construction code runs in the WM_CREATE message handler. // The style of a parent window is different than that of a child window. // Note: the child window should always be visible, even if the parent isn't. WindowStyle style = 0; ExtendedWindowStyle ex_style = 0; if (parentHandle == IntPtr.Zero) { style |= WindowStyle.OverlappedWindow | WindowStyle.ClipChildren; ex_style = ParentStyleEx; } else { style |= WindowStyle.Visible | WindowStyle.Child | WindowStyle.ClipSiblings; ex_style = ChildStyleEx; } // Find out the final window rectangle, after the WM has added its chrome (titlebar, sidebars etc). Win32Rectangle rect = new Win32Rectangle(); rect.left = x; rect.top = y; rect.right = x + width; rect.bottom = y + height; Functions.AdjustWindowRectEx(ref rect, style, false, ex_style); // Create the window class that we will use for this window. // The current approach is to register a new class for each top-level WinGLWindow we create. if (!class_registered) { ExtendedWindowClass wc = new ExtendedWindowClass(); wc.Size = ExtendedWindowClass.SizeInBytes; wc.Style = DefaultClassStyle; wc.Instance = Instance; wc.WndProc = WindowProcedureDelegate; wc.ClassName = ClassName; wc.Icon = Icon != null ? Icon.Handle : IntPtr.Zero; #warning "This seems to resize one of the 'large' icons, rather than using a small icon directly (multi-icon files). Investigate!" wc.IconSm = Icon != null ? new Icon(Icon, 16, 16).Handle : IntPtr.Zero; wc.Cursor = Functions.LoadCursor(CursorName.Arrow); ushort atom = Functions.RegisterClassEx(ref wc); if (atom == 0) throw new PlatformException(String.Format("Failed to register window class. Error: {0}", Marshal.GetLastWin32Error())); class_registered = true; } IntPtr window_name = Marshal.StringToHGlobalAuto(title); IntPtr handle = Functions.CreateWindowEx( ex_style, ClassName, window_name, style, rect.left, rect.top, rect.Width, rect.Height, parentHandle, IntPtr.Zero, Instance, IntPtr.Zero); if (handle == IntPtr.Zero) throw new PlatformException(String.Format("Failed to create window. Error: {0}", Marshal.GetLastWin32Error())); return handle; }
public WinGLContext(GraphicsMode format, WinWindowInfo window, IGraphicsContext sharedContext, int major, int minor, GraphicsContextFlags flags) { // There are many ways this code can break when accessed by multiple threads. The biggest offender is // the sharedContext stuff, which will only become valid *after* this constructor returns. // The easiest solution is to serialize all context construction - hence the big lock, below. lock (LoadLock) { if (window == null) { throw new ArgumentNullException("window", "Must point to a valid window."); } if (window.Handle == IntPtr.Zero) { throw new ArgumentException("window", "Must be a valid window."); } IntPtr current_context = Wgl.GetCurrentContext(); ExtendedWindowClass wc = new ExtendedWindowClass(); wc.Size = ExtendedWindowClass.SizeInBytes; wc.Style = ClassStyle.OwnDC; wc.Instance = Instance; wc.WndProc = WindowProcedure; wc.ClassName = ClassName; ushort atom = Functions.RegisterClassEx(ref wc); IntPtr window_name = Marshal.StringToHGlobalAuto("temp"); var temp_window = Functions.CreateWindowEx(ExtendedWindowStyle.WindowEdge | ExtendedWindowStyle.ApplicationWindow, ClassName, window_name, WindowStyle.OverlappedWindow | WindowStyle.ClipChildren, 0, 0, 10, 10, IntPtr.Zero, IntPtr.Zero, Instance, IntPtr.Zero); var error = Marshal.GetLastWin32Error(); TemporaryContext temp_context = null; try { if (current_context == IntPtr.Zero) { // Create temporary context to load WGL extensions temp_context = new TemporaryContext(temp_window); current_context = Wgl.GetCurrentContext(); if (current_context != IntPtr.Zero && current_context == temp_context.Context.Handle) { new Wgl().LoadEntryPoints(); } } Debug.Print("OpenGL will be bound to window:{0} on thread:{1}", window.Handle, System.Threading.Thread.CurrentThread.ManagedThreadId); ModeSelector = new WinGraphicsMode(window.DeviceContext); Mode = SetGraphicsModePFD(ModeSelector, format, (WinWindowInfo)window); if (Wgl.SupportsFunction("wglCreateContextAttribsARB")) { try { Debug.Write("Using WGL_ARB_create_context... "); List <int> attributes = new List <int>(); attributes.Add((int)ArbCreateContext.MajorVersion); attributes.Add(major); attributes.Add((int)ArbCreateContext.MinorVersion); attributes.Add(minor); if (flags != 0) { attributes.Add((int)ArbCreateContext.ContextFlags); attributes.Add((int)GetARBContextFlags(flags)); attributes.Add((int)ArbCreateContext.ProfileMask); attributes.Add((int)GetARBContextProfile(flags)); } // According to the docs, " <attribList> specifies a list of attributes for the context. // The list consists of a sequence of <name,value> pairs terminated by the // value 0. [...]" // Is this a single 0, or a <0, 0> pair? (Defensive coding: add two zeroes just in case). attributes.Add(0); attributes.Add(0); Handle = new ContextHandle( Wgl.wglCreateContextAttribs( window.DeviceContext, sharedContext != null ? (sharedContext as IGraphicsContextInternal).Context.Handle : IntPtr.Zero, attributes.ToArray())); if (Handle == ContextHandle.Zero) { Debug.Print("failed. (Error: {0})", Marshal.GetLastWin32Error()); } } catch (Exception e) { Debug.Print(e.ToString()); } } if (Handle == ContextHandle.Zero) { // Failed to create GL3-level context, fall back to GL2. Debug.Write("Falling back to GL2... "); Handle = new ContextHandle(Wgl.CreateContext(window.DeviceContext)); if (Handle == ContextHandle.Zero) { Handle = new ContextHandle(Wgl.CreateContext(window.DeviceContext)); } if (Handle == ContextHandle.Zero) { throw new GraphicsContextException( String.Format("Context creation failed. Wgl.CreateContext() error: {0}.", Marshal.GetLastWin32Error())); } } Debug.WriteLine(String.Format("success! (id: {0})", Handle)); } finally { if (temp_context != null) { temp_context.Dispose(); temp_context = null; } if (temp_window != null) { error = Marshal.GetLastWin32Error(); Functions.DestroyWindow(temp_window); Functions.UnregisterClass(ClassName, Instance); error = Marshal.GetLastWin32Error(); } } } // Todo: is this comment still true? // On intel drivers, wgl entry points appear to change // when creating multiple contexts. As a workaround, // we reload Wgl entry points every time we create a // new context - this solves the issue without any apparent // side-effects (i.e. the old contexts can still be handled // using the new entry points.) // Sigh... MakeCurrent(window); new Wgl().LoadEntryPoints(); if (sharedContext != null) { Marshal.GetLastWin32Error(); Debug.Write(String.Format("Sharing state with context {0}: ", sharedContext)); bool result = Wgl.ShareLists((sharedContext as IGraphicsContextInternal).Context.Handle, Handle.Handle); Debug.WriteLine(result ? "success!" : "failed with win32 error " + Marshal.GetLastWin32Error()); } }
internal static extern ushort RegisterClassEx(ref ExtendedWindowClass window_class);
internal static extern BOOL GetClassInfoEx(HINSTANCE hinst, UIntPtr lpszClass, ref ExtendedWindowClass lpwcx);
internal static extern BOOL GetClassInfoEx(HINSTANCE hinst, [MarshalAs(UnmanagedType.LPTStr)] LPCTSTR lpszClass, ref ExtendedWindowClass lpwcx);
internal static bool GetClassInfoEx(IntPtr hinst, UIntPtr lpszClass, ref ExtendedWindowClass lpwcx);
internal static bool GetClassInfoEx(IntPtr hinst, [MarshalAs(UnmanagedType.LPTStr)] string lpszClass, ref ExtendedWindowClass lpwcx);