From 7d5087f1f82a073655ff1c13b174c8d48afad7ae Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Mon, 20 Aug 2007 14:12:57 +0000 Subject: [PATCH] Improved GLControl event handling on initialization sequence (Resize event is now raised after loading the GL class). Improved WinForms.Cube example to hook the GLControl events instead of the parent Form's ones. Improved Debug output in X11GLContext and X11GLControl --- Source/Examples/WinForms/Cube.cs | 56 +++++++++----------- Source/Examples/WinForms/W01_First_Window.cs | 23 ++++---- Source/OpenTK/GLControl.cs | 12 ++--- Source/OpenTK/Platform/X11/X11GLContext.cs | 2 +- Source/OpenTK/Platform/X11/X11GLControl.cs | 5 +- 5 files changed, 49 insertions(+), 49 deletions(-) diff --git a/Source/Examples/WinForms/Cube.cs b/Source/Examples/WinForms/Cube.cs index 8e62ba55..458026ab 100644 --- a/Source/Examples/WinForms/Cube.cs +++ b/Source/Examples/WinForms/Cube.cs @@ -33,8 +33,6 @@ namespace Examples.WinForms public Cube() { InitializeComponent(); - - Application.Idle += Application_Idle; this.ShowDialog(); } @@ -94,6 +92,12 @@ namespace Examples.WinForms { base.OnLoad(e); + glControl.KeyDown += new KeyEventHandler(glControl_KeyDown); + glControl.Resize += new EventHandler(glControl_Resize); + glControl.Paint += new PaintEventHandler(glControl_Paint); + + glControl.CreateContext(); + Text = GL.GetString(GL.Enums.StringName.VENDOR) + " " + GL.GetString(GL.Enums.StringName.RENDERER) + " " + @@ -102,30 +106,24 @@ namespace Examples.WinForms GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); GL.Enable(GL.Enums.EnableCap.DEPTH_TEST); - glControl.KeyDown += new KeyEventHandler(Cube_KeyDown); - - OnResize(e); + Application.Idle += Application_Idle; } - + #endregion - #region Resize event handler + #region GLControl.Resize event handler - protected override void OnResize(EventArgs e) + void glControl_Resize(object sender, EventArgs e) { - base.OnResize(e); + OpenTK.GLControl c = sender as OpenTK.GLControl; - if (ClientSize.Height == 0) - ClientSize = new System.Drawing.Size(ClientSize.Width, 1); + if (c.ClientSize.Height == 0) + c.ClientSize = new System.Drawing.Size(c.ClientSize.Width, 1); - GL.Viewport(0, 0, ClientSize.Width, ClientSize.Height); + GL.Viewport(0, 0, c.ClientSize.Width, c.ClientSize.Height); double ratio = 0.0; - ratio = ClientSize.Width / (double)ClientSize.Height; - //if (ClientSize.Width > ClientSize.Height) - // ratio = ClientSize.Width / (double)ClientSize.Height; - //else - // ratio = ClientSize.Height / (double)ClientSize.Width; + ratio = c.ClientSize.Width / (double)c.ClientSize.Height; GL.MatrixMode(GL.Enums.MatrixMode.PROJECTION); GL.LoadIdentity(); @@ -134,20 +132,9 @@ namespace Examples.WinForms #endregion - #region Paint event handler + #region GLControl.KeyDown event handler - protected override void OnPaint(PaintEventArgs e) - { - base.OnPaint(e); - - Render(); - } - - #endregion - - #region KeyDown event handler - - void Cube_KeyDown(object sender, KeyEventArgs e) + void glControl_KeyDown(object sender, KeyEventArgs e) { if (e.Alt && e.Shift) { @@ -163,6 +150,15 @@ namespace Examples.WinForms } } + #endregion + + #region GLControl.Paint event handler + + void glControl_Paint(object sender, PaintEventArgs e) + { + Render(); + } + #endregion #region private void DrawCube() diff --git a/Source/Examples/WinForms/W01_First_Window.cs b/Source/Examples/WinForms/W01_First_Window.cs index 38065cfa..e5c2659a 100644 --- a/Source/Examples/WinForms/W01_First_Window.cs +++ b/Source/Examples/WinForms/W01_First_Window.cs @@ -39,34 +39,35 @@ namespace Examples.WinForms private void redButton_Click(object sender, EventArgs e) { - //GL.ClearColor(0.7f, 0.0f, 0.0f, 0.0f); - //glControl1.Invalidate(); + GL.ClearColor(0.7f, 0.0f, 0.0f, 0.0f); + glControl1.Invalidate(); } private void greenButton_Click(object sender, EventArgs e) { - //GL.ClearColor(0.0f, 0.5f, 0.0f, 0.0f); - //glControl1.Invalidate(); + GL.ClearColor(0.0f, 0.5f, 0.0f, 0.0f); + glControl1.Invalidate(); } private void blueButton_Click(object sender, EventArgs e) { - //GL.ClearColor(0.0f, 0.0f, 0.7f, 0.0f); - //glControl1.Invalidate(); + GL.ClearColor(0.0f, 0.0f, 0.7f, 0.0f); + glControl1.Invalidate(); } private void glControl1_Paint(object sender, PaintEventArgs e) { - //GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT); - //glControl1.SwapBuffers(); + System.Diagnostics.Debug.Print("Paint"); + GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT); + glControl1.SwapBuffers(); } private void glControl1_Resize(object sender, OpenTK.Platform.ResizeEventArgs e) { - //if (glControl1.ClientSize.Height == 0) - // glControl1.ClientSize = new System.Drawing.Size(glControl1.ClientSize.Width, 1); + if (glControl1.ClientSize.Height == 0) + glControl1.ClientSize = new System.Drawing.Size(glControl1.ClientSize.Width, 1); - //GL.Viewport(0, 0, glControl1.ClientSize.Width, glControl1.ClientSize.Height); + GL.Viewport(0, 0, glControl1.ClientSize.Width, glControl1.ClientSize.Height); } private void glControl1_KeyDown(object sender, KeyEventArgs e) diff --git a/Source/OpenTK/GLControl.cs b/Source/OpenTK/GLControl.cs index 223bfb87..e4e010fd 100644 --- a/Source/OpenTK/GLControl.cs +++ b/Source/OpenTK/GLControl.cs @@ -23,7 +23,7 @@ namespace OpenTK // TODO: Document the GLControl class. /// - /// + /// Defines a UserControl with opengl rendering capabilities. /// public partial class GLControl : UserControl, IGLControl { @@ -43,6 +43,10 @@ namespace OpenTK { } + /// + /// Constructs a new GLControl, with the specified DisplayMode. + /// + /// The DisplayMode of the control. public GLControl(DisplayMode mode) { InitializeComponent(); @@ -88,9 +92,6 @@ namespace OpenTK /// public void CreateContext() { - Debug.Print("Creating opengl context"); - Debug.Indent(); - if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) { @@ -109,8 +110,7 @@ namespace OpenTK } OpenTK.OpenGL.GL.LoadAll(); - - Debug.Unindent(); + this.OnResize(EventArgs.Empty); } /// diff --git a/Source/OpenTK/Platform/X11/X11GLContext.cs b/Source/OpenTK/Platform/X11/X11GLContext.cs index ea811cc2..9b509f34 100644 --- a/Source/OpenTK/Platform/X11/X11GLContext.cs +++ b/Source/OpenTK/Platform/X11/X11GLContext.cs @@ -142,10 +142,10 @@ visual = Glx.ChooseVisual(windowInfo.Display, windowInfo.Screen, attrib); Debug.WriteLine(direct ? "Context is direct." : "Context is indirect."); context = Glx.CreateContext(windowInfo.Display, visual, shareHandle, direct); + Debug.Unindent(); if (context != IntPtr.Zero) { Debug.WriteLine(String.Format("New opengl context created. (id: {0})", context)); - Debug.Unindent(); } else { diff --git a/Source/OpenTK/Platform/X11/X11GLControl.cs b/Source/OpenTK/Platform/X11/X11GLControl.cs index 2fb57cd7..d7ac8577 100644 --- a/Source/OpenTK/Platform/X11/X11GLControl.cs +++ b/Source/OpenTK/Platform/X11/X11GLControl.cs @@ -97,6 +97,8 @@ namespace OpenTK.Platform.X11 */ //glContext.MakeCurrent(); //OpenTK.OpenGL.GL.LoadAll(); + + Debug.Unindent(); } void c_HandleCreated(object sender, EventArgs e) @@ -104,11 +106,12 @@ namespace OpenTK.Platform.X11 UserControl c = (sender as UserControl); Debug.Print("GLControl handle created, creating X11GLContext."); Debug.Indent(); - glContext.windowInfo.Handle = info.Handle = (sender as UserControl).Handle; try { + glContext.windowInfo.Handle = info.Handle = (sender as UserControl).Handle; glContext.CreateContext(null, true); + glContext.MakeCurrent(); } catch (ApplicationException expt) {