From 437d8fe6273e0cb96ca34e088643d934298898ea Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 2 Sep 2007 13:34:44 +0000 Subject: [PATCH] Updated T10_GLSL_Cube to remove NRE exception on Mono 1.2.x. Added debug info to the GL.LoadAll function. Added W03_Extensions.cs example. --- Source/Examples/ExampleLauncher.Designer.cs | 1 + Source/Examples/ExampleLauncher.cs | 52 ++++++------- Source/Examples/Tutorial/T01_Simple_Window.cs | 1 + Source/Examples/Tutorial/T10_GLSL_Cube.cs | 4 +- .../WinForms/W03_Extensions.Designer.cs | 59 ++++++++++++++ Source/Examples/WinForms/W03_Extensions.cs | 77 +++++++++++++++++++ Source/OpenTK/OpenGL/GLHelper.cs | 14 +++- Source/OpenTK/Platform/X11/X11GLNative.cs | 2 +- 8 files changed, 179 insertions(+), 31 deletions(-) create mode 100644 Source/Examples/WinForms/W03_Extensions.Designer.cs create mode 100644 Source/Examples/WinForms/W03_Extensions.cs diff --git a/Source/Examples/ExampleLauncher.Designer.cs b/Source/Examples/ExampleLauncher.Designer.cs index 0ba6bdbc..df491b7a 100644 --- a/Source/Examples/ExampleLauncher.Designer.cs +++ b/Source/Examples/ExampleLauncher.Designer.cs @@ -66,6 +66,7 @@ this.MinimumSize = new System.Drawing.Size(300, 300); this.Name = "ExampleLauncher"; this.Text = "OpenTK Example Launcher"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ExampleLauncher_FormClosing); this.Load += new System.EventHandler(this.ExampleLauncher_Load); this.ResumeLayout(false); diff --git a/Source/Examples/ExampleLauncher.cs b/Source/Examples/ExampleLauncher.cs index 28ccaff5..202d1c5f 100644 --- a/Source/Examples/ExampleLauncher.cs +++ b/Source/Examples/ExampleLauncher.cs @@ -38,6 +38,21 @@ namespace Examples public void ExampleLauncher_Load(object sender, EventArgs e) { + try + { + if (File.Exists("debug.log")) + File.Delete("debug.log"); + } + catch (Exception expt) + { + MessageBox.Show("Could not access debug.log", expt.ToString()); + } + + Debug.Listeners.Clear(); + Debug.Listeners.Add(new TextWriterTraceListener("debug.log")); + Debug.Listeners.Add(new ConsoleTraceListener()); + Debug.AutoFlush = true; + // Get all examples Type[] types = Assembly.GetExecutingAssembly().GetTypes(); foreach (Type type in types) @@ -159,36 +174,19 @@ namespace Examples [STAThread] static void Main() { - try + using (Form exampleLauncher = new ExampleLauncher()) { - if (File.Exists("debug.log")) - File.Delete("debug.log"); - } - catch (Exception e) - { - MessageBox.Show("Could not access debug.log", e.ToString()); + Application.EnableVisualStyles(); + Application.Run(exampleLauncher); } + } - Debug.Listeners.Clear(); - Debug.Listeners.Add(new TextWriterTraceListener("debug.log")); - Debug.Listeners.Add(new ConsoleTraceListener()); - Debug.AutoFlush = true; - - try - { - using (Form exampleLauncher = new ExampleLauncher()) - { - Application.EnableVisualStyles(); - Application.Run(exampleLauncher); - } - } - finally - { - Debug.Flush(); - Debug.Close(); - Trace.Flush(); - Trace.Close(); - } + private void ExampleLauncher_FormClosing(object sender, FormClosingEventArgs e) + { + Debug.Flush(); + Debug.Close(); + Trace.Flush(); + Trace.Close(); } } } diff --git a/Source/Examples/Tutorial/T01_Simple_Window.cs b/Source/Examples/Tutorial/T01_Simple_Window.cs index 7a822e63..762296c8 100644 --- a/Source/Examples/Tutorial/T01_Simple_Window.cs +++ b/Source/Examples/Tutorial/T01_Simple_Window.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Text; using System.Drawing; +using System.Diagnostics; using OpenTK; using OpenTK.OpenGL; diff --git a/Source/Examples/Tutorial/T10_GLSL_Cube.cs b/Source/Examples/Tutorial/T10_GLSL_Cube.cs index f16f0508..50d5354b 100644 --- a/Source/Examples/Tutorial/T10_GLSL_Cube.cs +++ b/Source/Examples/Tutorial/T10_GLSL_Cube.cs @@ -86,7 +86,7 @@ namespace Examples.Tutorial vertex_shader_object = (uint)GL.CreateShader(GL.Enums.VERSION_2_0.VERTEX_SHADER); fragment_shader_object = (uint)GL.CreateShader(GL.Enums.VERSION_2_0.FRAGMENT_SHADER); - GL.ShaderSource(vertex_shader_object, vertex_shader_source.Length, vertex_shader_source, (int[])null); + unsafe { GL.ShaderSource(vertex_shader_object, vertex_shader_source.Length, vertex_shader_source, (int*)null); } GL.CompileShader(vertex_shader_object); GL.GetShader(vertex_shader_object, GL.Enums.VERSION_2_0.COMPILE_STATUS, out status); if (status != (int)GL.Enums.Boolean.TRUE) @@ -99,7 +99,7 @@ namespace Examples.Tutorial throw new Exception(info.ToString()); } - GL.ShaderSource(fragment_shader_object, fragment_shader_source.Length, fragment_shader_source, (int[])null); + unsafe { GL.ShaderSource(fragment_shader_object, fragment_shader_source.Length, fragment_shader_source, (int*)null); } GL.CompileShader(fragment_shader_object); GL.GetShader(fragment_shader_object, GL.Enums.VERSION_2_0.COMPILE_STATUS, out status); if (status != (int)GL.Enums.Boolean.TRUE) diff --git a/Source/Examples/WinForms/W03_Extensions.Designer.cs b/Source/Examples/WinForms/W03_Extensions.Designer.cs new file mode 100644 index 00000000..939c09f5 --- /dev/null +++ b/Source/Examples/WinForms/W03_Extensions.Designer.cs @@ -0,0 +1,59 @@ +namespace Examples.WinForms +{ + partial class W03_Extensions + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.listBox1 = new System.Windows.Forms.ListBox(); + this.SuspendLayout(); + // + // listBox1 + // + this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill; + this.listBox1.FormattingEnabled = true; + this.listBox1.Location = new System.Drawing.Point(0, 0); + this.listBox1.Name = "listBox1"; + this.listBox1.Size = new System.Drawing.Size(284, 264); + this.listBox1.TabIndex = 0; + // + // W03_Extensions + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(284, 264); + this.Controls.Add(this.listBox1); + this.Name = "W03_Extensions"; + this.Text = "W03_Extensions"; + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.ListBox listBox1; + } +} \ No newline at end of file diff --git a/Source/Examples/WinForms/W03_Extensions.cs b/Source/Examples/WinForms/W03_Extensions.cs new file mode 100644 index 00000000..c7564ec6 --- /dev/null +++ b/Source/Examples/WinForms/W03_Extensions.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Text; +using System.Windows.Forms; + +using OpenTK; +using System.Reflection; +using OpenTK.OpenGL; + +namespace Examples.WinForms +{ + public partial class W03_Extensions : Form//, IExample + { + GLControl glControl = new GLControl(); + Assembly assembly; + Type glClass; + Type delegatesClass; + Type importsClass; + + public W03_Extensions() + { + InitializeComponent(); + + assembly = Assembly.Load("OpenTK"); + glClass = assembly.GetType("OpenTK.OpenGL.GL"); + delegatesClass = glClass.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic); + importsClass = glClass.GetNestedType("Imports", BindingFlags.Static | BindingFlags.NonPublic); + } + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + + glControl.CreateContext(); + + FieldInfo[] v = delegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic); + + int i = 0, supported = 0; + + try + { + foreach (FieldInfo f in v) + { + Delegate d = GL.GetDelegate(f.Name, f.FieldType); + + f.SetValue(null, d); + this.listBox1.Items.Add(String.Format("{0}/{1} {2}: {3}", + (++i).ToString(), v.Length, d != null ? "ok" : "failed", f.Name)); + + if (d != null) + { + ++supported; + } + } + + this.Text = String.Format("Supported extensions: {0}", supported); + } + catch (Exception expt) + { + MessageBox.Show("An error occured while loading extensions", "Extension loading failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + throw; + } + } + + #region IExample Members + + public void Launch() + { + + } + + #endregion + } +} \ No newline at end of file diff --git a/Source/OpenTK/OpenGL/GLHelper.cs b/Source/OpenTK/OpenGL/GLHelper.cs index 0f636763..78f433e1 100644 --- a/Source/OpenTK/OpenGL/GLHelper.cs +++ b/Source/OpenTK/OpenGL/GLHelper.cs @@ -12,6 +12,7 @@ using System.Text; using System.Runtime.InteropServices; using System.Reflection; using OpenTK.Platform; +using System.Diagnostics; #endregion @@ -236,10 +237,21 @@ namespace OpenTK.OpenGL public static void LoadAll() { FieldInfo[] v = delegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic); + + int supported = 0; + + Debug.Print("Will now try to load all {0} opengl functions.", v.Length); foreach (FieldInfo f in v) { - f.SetValue(null, GetDelegate(f.Name, f.FieldType)); + Delegate d = GetDelegate(f.Name, f.FieldType); + if (d != null) + { + ++supported; + } + + f.SetValue(null, d); } + Debug.Print("Total supported functions: {0}.", supported); AvailableExtensions.Clear(); rebuildExtensionList = true; diff --git a/Source/OpenTK/Platform/X11/X11GLNative.cs b/Source/OpenTK/Platform/X11/X11GLNative.cs index 76f1acef..80d484c7 100644 --- a/Source/OpenTK/Platform/X11/X11GLNative.cs +++ b/Source/OpenTK/Platform/X11/X11GLNative.cs @@ -56,7 +56,7 @@ namespace OpenTK.Platform.X11 Debug.Print("Native window driver: {0}", this.ToString()); window = new WindowInfo(); - Utilities.ThrowOnX11Error = true; + //Utilities.ThrowOnX11Error = true; } #endregion