From 7be851a68a91cd06f2ca7da06ba652d191c69424 Mon Sep 17 00:00:00 2001 From: the_fiddler Date: Sun, 11 Nov 2007 18:44:10 +0000 Subject: [PATCH] Added ExampleAttribute to all examples. Improved Textures example. Modified several other examples. --- Source/Examples/ExampleAttribute.cs | 22 ++- Source/Examples/ExampleLauncher.cs | 143 ++++++++---------- Source/Examples/Tutorial/Fonts.cs | 51 ++++++- Source/Examples/Tutorial/T01_Simple_Window.cs | 25 +-- Source/Examples/Tutorial/T02_Vertex_Arrays.cs | 4 +- .../Tutorial/T03_Immediate_Mode_Cube.cs | 50 +++--- .../Tutorial/T07_Display_Lists_Flower.cs | 25 +-- Source/Examples/Tutorial/T10_GLSL_Cube.cs | 43 +++--- Source/Examples/Tutorial/Text.cs | 23 ++- Source/Examples/Tutorial/Textures.cs | 56 ++++--- Source/Examples/WinForms/W01_First_Window.cs | 25 ++- .../W02_Immediate_Mode_Cube.Designer.cs | 4 +- .../WinForms/W02_Immediate_Mode_Cube.cs | 79 +++++----- Source/Examples/WinForms/W03_Extensions.cs | 58 ++++--- 14 files changed, 348 insertions(+), 260 deletions(-) diff --git a/Source/Examples/ExampleAttribute.cs b/Source/Examples/ExampleAttribute.cs index d905c728..4ca30b55 100644 --- a/Source/Examples/ExampleAttribute.cs +++ b/Source/Examples/ExampleAttribute.cs @@ -15,13 +15,27 @@ namespace Examples { public readonly string Title; public readonly ExampleCategory Category; - public readonly int Order; + public readonly int Difficulty; + public readonly bool Visible = true; - public ExampleAttribute(string title, ExampleCategory category, int order) + public ExampleAttribute(string title, ExampleCategory category, int difficulty) { this.Title = title; this.Category = category; - this.Order = order; + this.Difficulty = difficulty; + } + + public ExampleAttribute(string title, ExampleCategory category, int difficulty, bool visible) + { + this.Title = title; + this.Category = category; + this.Difficulty = difficulty; + this.Visible = visible; + } + + public override string ToString() + { + return String.Format("{0} {1}: {2}", Category, Difficulty, Title); } } @@ -29,7 +43,7 @@ namespace Examples { OpenGL, OpenAL, - OpenTK, + Tutorial, GLSL, WinForms, Test, diff --git a/Source/Examples/ExampleLauncher.cs b/Source/Examples/ExampleLauncher.cs index d47bcf72..fbf1ff64 100644 --- a/Source/Examples/ExampleLauncher.cs +++ b/Source/Examples/ExampleLauncher.cs @@ -36,20 +36,18 @@ namespace Examples /// class ExampleInfo { - public string Name; - public string Description; public Type Example; + public ExampleAttribute Attributes; - public ExampleInfo(string name, string description, Type example) + public ExampleInfo(Type example, ExampleAttribute attr) { - Name = name; - Description = description; Example = example; + Attributes = attr; } public override string ToString() { - return Name; + return Attributes.ToString(); } } @@ -88,29 +86,24 @@ namespace Examples StringBuilder sb = new StringBuilder(); foreach (Type type in types) { - if (type.GetInterface("IExample") != null) - { - sb.Append(type.Namespace); - sb.Replace("Examples.", String.Empty); - sb.Append(" "); - int order; - try - { - FieldInfo info = type.GetField("order"); - order = (int)info.GetValue(null); - } - catch (NullReferenceException) - { - Debug.Print("Example {0} does not have ordering info", type.FullName); - order = 0; - } - if (order < 10) - sb.Append("0"); // To keep items sorted nicely. - sb.Append(order.ToString()); - sb.Append(": "); - sb.Append(type.Name); + object[] attributes = type.GetCustomAttributes(false); + ExampleAttribute example = null; + foreach (object attr in attributes) + if (attr is ExampleAttribute) + example = (ExampleAttribute)attr; - listBox1.Items.Add(new ExampleInfo(sb.ToString(), "", type)); + if (example != null && example.Visible == true) + { + sb.Append(example.Category); + sb.Append(" "); + if (example.Difficulty < 10) + sb.Append("0"); // To keep items nicely sorted. + sb.Append(example.Difficulty); + sb.Append(": "); + //sb.Append(type.Name); + sb.Append(example.Title); + + listBox1.Items.Add(new ExampleInfo(type, example)); // Clean the StringBuilder for the next pass. sb.Remove(0, sb.Length); @@ -123,62 +116,58 @@ namespace Examples #endregion + #region private void RunExample() + private void RunExample() { if (listBox1.SelectedItem != null) { - Type example = (listBox1.SelectedItem as ExampleInfo).Example; - - Debug.Print("Launching example: {0}", example.ToString()); - this.Visible = false; - try { - if (example.BaseType == typeof(GameWindow)) - { - using (GameWindow gw = (GameWindow)(example.GetConstructor(Type.EmptyTypes).Invoke(null))) - { - (gw as IExample).Launch(); - } - } - else if (example.BaseType == typeof(Form)) - { - using (Form f = (Form)example.GetConstructor(Type.EmptyTypes).Invoke(null)) - { - f.ShowDialog(this); - } - } - else - { - // Console application. - IExample ex = (IExample)example.GetConstructor(Type.EmptyTypes).Invoke(null); - ex.Launch(); - } + ExampleInfo info = (ExampleInfo)listBox1.SelectedItem; + Type example = info.Example; + + Debug.Print("Launching example: {0}", example.ToString()); + this.Visible = false; + + example.GetMethod("Main").Invoke(null, null); + + GC.Collect(); + GC.WaitForPendingFinalizers(); + GC.Collect(); } - catch (Exception expt) + catch (TargetInvocationException expt) { + string ex_info; + if (expt.InnerException != null) + ex_info = expt.InnerException.ToString(); + else + ex_info = expt.ToString(); + MessageBox.Show(ex_info, "An OpenTK example encountered an error.", MessageBoxButtons.OK, MessageBoxIcon.Warning); + + Debug.Print(expt.ToString()); #if DEBUG throw; -#else - if (expt.InnerException != null) - MessageBox.Show(expt.InnerException.ToString(), "An error has occured: \"" + - expt.InnerException.Message + "\"", MessageBoxButtons.OK, MessageBoxIcon.Warning); - else - MessageBox.Show(expt.ToString(), "An error has occured: \"" + expt.Message + "\"", - MessageBoxButtons.OK, MessageBoxIcon.Warning); #endif - } - - GC.Collect(); - GC.WaitForPendingFinalizers(); - GC.Collect(); - - this.Visible = true; + catch (NullReferenceException expt) + { + MessageBox.Show(expt.ToString(), "The Example launcher failed to load the example.", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } + finally + { + this.Visible = true; + this.TopMost = true; // Bring the ExampleLauncher window to front + this.TopMost = false; // but don't allow the user to cover it with other windows. + } } } + #endregion + + #region Launcher events + private void listBox1_DoubleClick(object sender, EventArgs e) { RunExample(); @@ -199,6 +188,16 @@ namespace Examples RunExample(); } + private void ExampleLauncher_FormClosing(object sender, FormClosingEventArgs e) + { + Debug.Flush(); + Debug.Close(); + Trace.Flush(); + Trace.Close(); + } + + #endregion + /// /// The main entry point for the application. /// @@ -211,13 +210,5 @@ namespace Examples Application.Run(exampleLauncher); } } - - private void ExampleLauncher_FormClosing(object sender, FormClosingEventArgs e) - { - Debug.Flush(); - Debug.Close(); - Trace.Flush(); - Trace.Close(); - } } } diff --git a/Source/Examples/Tutorial/Fonts.cs b/Source/Examples/Tutorial/Fonts.cs index c7e0a098..10964284 100644 --- a/Source/Examples/Tutorial/Fonts.cs +++ b/Source/Examples/Tutorial/Fonts.cs @@ -22,15 +22,22 @@ namespace Examples.Tutorial /// /// Tests Font loading and rendering. /// - class Fonts : GameWindow, IExample + [Example("Fonts", ExampleCategory.Tutorial, 3)] + class Fonts : GameWindow { - public Fonts() : base(new DisplayMode(800, 600), String.Format("OpenTK | Tutorial {0}: Fonts", order)) + public Fonts() : base(new DisplayMode(800, 600)) { } + #region --- Fields --- + ITextPrinter printer = new TextPrinter(); const string text = "Hello, world!"; + TextHandle[] handles; // Used to cache the strings we want to print. + // Load some different TextureFont sizes to compare their quality. + // You'll never need to load that many fonts in your application, + // 3 or 4 should be more than enough. TextureFont[] fonts = new TextureFont[] { new TextureFont(new Font(FontFamily.GenericSerif, 8.0f)), @@ -73,7 +80,10 @@ namespace Examples.Tutorial new TextureFont(new Font(FontFamily.GenericSansSerif, 36.0f)), new TextureFont(new Font(FontFamily.GenericSansSerif, 38.0f)), }; - TextHandle[] handles; // Used to cache the strings we want to print. + + #endregion + + #region OnLoad /// /// To maintain high rendering performance, we need to cache the text @@ -95,6 +105,10 @@ namespace Examples.Tutorial printer.Prepare(text, fonts[i], out handles[i]); } + #endregion + + #region OnUnload + /// /// It is important that we need to call the Dispose() methods to reclaim of /// each and every TextHandle and TextureFont to reclaim the unamanged @@ -109,17 +123,29 @@ namespace Examples.Tutorial f.Dispose(); } + #endregion + + #region OnResize + protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) { GL.Viewport(0, 0, Width, Height); } + #endregion + + #region OnUpdateFrame + public override void OnUpdateFrame(UpdateFrameEventArgs e) { if (Keyboard[Key.Escape]) this.Exit(); } + #endregion + + #region OnRenderFrame + /// /// To render pixel-perfect text, we have to setup a 2d display projection /// with a width/height that corresponds to our current Viewport. @@ -167,14 +193,23 @@ namespace Examples.Tutorial SwapBuffers(); } + #endregion - #region IExample Members + #region public static void Main() - public static readonly int order = 6; - - public void Launch() + /// + /// Entry point of this example. + /// + [STAThread] + public static void Main() { - Run(30.0, 0.0); + using (Fonts example = new Fonts()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.Run(30.0, 0.0); + } } #endregion diff --git a/Source/Examples/Tutorial/T01_Simple_Window.cs b/Source/Examples/Tutorial/T01_Simple_Window.cs index 9b6736e4..a7faaa8b 100644 --- a/Source/Examples/Tutorial/T01_Simple_Window.cs +++ b/Source/Examples/Tutorial/T01_Simple_Window.cs @@ -20,9 +20,10 @@ namespace Examples.Tutorial /// /// Demonstrates the GameWindow class. /// - public class T01_Simple_Window : GameWindow, IExample + [Example("Simple Window", ExampleCategory.Tutorial, 1)] + public class T01_Simple_Window : GameWindow { - public T01_Simple_Window() : base(new DisplayMode(800, 600), "OpenTK | Tutorial 1: Simple Window") + public T01_Simple_Window() : base(new DisplayMode(800, 600)) { } #region OnLoad @@ -101,20 +102,24 @@ namespace Examples.Tutorial } #endregion - - #region IExample Members + + #region public static void Main() /// - /// Only used by the ExampleLauncher application, no need to add a Launch() method in your code. - /// Add a call to Run() in your Main() function instead. + /// Entry point of this example. /// - public void Launch() + [STAThread] + public static void Main() { - this.Run(30.0, 5.0); + using (T01_Simple_Window example = new T01_Simple_Window()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.Run(30.0, 0.0); + } } - public static readonly int order = 1; - #endregion } } diff --git a/Source/Examples/Tutorial/T02_Vertex_Arrays.cs b/Source/Examples/Tutorial/T02_Vertex_Arrays.cs index 49a9183a..03e081cb 100644 --- a/Source/Examples/Tutorial/T02_Vertex_Arrays.cs +++ b/Source/Examples/Tutorial/T02_Vertex_Arrays.cs @@ -21,8 +21,10 @@ namespace Examples.Tutorial { /// /// Demonstrates Vertex Arrays (in system memory). Example is incomplete (documentation). + /// Broken since release 0.3.12. /// - class T02_Vertex_Arrays : GameWindow, IExample + [Example("Vertex Arrays", ExampleCategory.OpenGL, 5, false)] + class T02_Vertex_Arrays : GameWindow { float rotation_speed = 3.0f; float angle = 0.0f; diff --git a/Source/Examples/Tutorial/T03_Immediate_Mode_Cube.cs b/Source/Examples/Tutorial/T03_Immediate_Mode_Cube.cs index 27b94893..eb26ad76 100644 --- a/Source/Examples/Tutorial/T03_Immediate_Mode_Cube.cs +++ b/Source/Examples/Tutorial/T03_Immediate_Mode_Cube.cs @@ -24,20 +24,19 @@ namespace Examples.Tutorial /// /// Demonstrates immediate mode rendering. Example is incomplete, and will probably go away in the future. /// - public class T03_Immediate_Mode_Cube : OpenTK.GameWindow, IExample + [Example("Immediate mode", ExampleCategory.Tutorial, 2)] + public class T03_Immediate_Mode_Cube : GameWindow { #region --- Fields --- - /// - /// Denotes the cube rotation. - /// - float angle = 0.0f; + float rotation_speed = 3.0f; + float angle; #endregion - #region --- Constructors --- + #region --- Constructor --- - public T03_Immediate_Mode_Cube() : base(new DisplayMode(800, 600), "OpenTK Tutorial: Immediate mode rendering") + public T03_Immediate_Mode_Cube() : base(new DisplayMode(800, 600)) { } #endregion @@ -48,7 +47,7 @@ namespace Examples.Tutorial { base.OnLoad(e); - GL.ClearColor(Color.MidnightBlue); + GL.ClearColor(Color.SteelBlue); GL.Enable(EnableCap.DepthTest); } @@ -89,22 +88,11 @@ namespace Examples.Tutorial /// public override void OnUpdateFrame(UpdateFrameEventArgs e) { - /* if (Keyboard[OpenTK.Input.Key.Escape]) { this.Exit(); return; } - - if ((Keyboard[OpenTK.Input.Key.AltLeft] || Keyboard[OpenTK.Input.Key.AltRight]) && - Keyboard[OpenTK.Input.Key.Enter]) - { - Fullscreen = !Fullscreen; - } - */ - angle += 3.0f; - if (angle > 720.0f) - angle -= 720.0f; } #endregion @@ -124,12 +112,12 @@ namespace Examples.Tutorial 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); + angle += rotation_speed * (float)e.ScaleFactor; GL.Rotate(angle, 0.0f, 1.0f, 0.0f); DrawCube(); - Context.SwapBuffers(); - Thread.Sleep(0); + this.SwapBuffers(); } #endregion @@ -182,21 +170,23 @@ namespace Examples.Tutorial #endregion - #region public void Launch() + #region public static void Main() /// - /// Launches this example. + /// Entry point of this example. /// - /// - /// Provides a simple way for the example launcher to launch the examples. - /// - public void Launch() + [STAThread] + public static void Main() { - Run(85.0, 0.0); + using (T03_Immediate_Mode_Cube example = new T03_Immediate_Mode_Cube()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.Run(30.0, 0.0); + } } #endregion - - public static readonly int order = 3; } } diff --git a/Source/Examples/Tutorial/T07_Display_Lists_Flower.cs b/Source/Examples/Tutorial/T07_Display_Lists_Flower.cs index 1f9587d0..4380ee05 100644 --- a/Source/Examples/Tutorial/T07_Display_Lists_Flower.cs +++ b/Source/Examples/Tutorial/T07_Display_Lists_Flower.cs @@ -23,7 +23,8 @@ using OpenTK.OpenGL.Enums; namespace Examples.Tutorial { - public class T07_Display_Lists_Flower : GameWindow, IExample + [Example("Display Lists", ExampleCategory.Tutorial, 3)] + public class T07_Display_Lists_Flower : GameWindow { #region --- Fields --- @@ -35,7 +36,7 @@ namespace Examples.Tutorial #region --- Constructors --- public T07_Display_Lists_Flower() - : base(new DisplayMode(800, 600), "OpenTK | T07: Display Lists") + : base(new DisplayMode(800, 600)) { } @@ -141,21 +142,23 @@ namespace Examples.Tutorial #endregion - #region public void Launch() + #region public static void Main() /// - /// Launches this example. + /// Entry point of this example. /// - /// - /// Provides a simple way for the example launcher to launch the examples. - /// - public void Launch() + [STAThread] + public static void Main() { - this.Run(30.0, 5.0); + using (T07_Display_Lists_Flower example = new T07_Display_Lists_Flower()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.Run(30.0, 0.0); + } } #endregion - - public static readonly int order = 7; } } \ No newline at end of file diff --git a/Source/Examples/Tutorial/T10_GLSL_Cube.cs b/Source/Examples/Tutorial/T10_GLSL_Cube.cs index f7197cab..52678b81 100644 --- a/Source/Examples/Tutorial/T10_GLSL_Cube.cs +++ b/Source/Examples/Tutorial/T10_GLSL_Cube.cs @@ -27,8 +27,8 @@ namespace Examples.Tutorial /// /// Demonstrates how to load and use a simple OpenGL shader program. Example is incomplete (documentation). /// - [Example("OpenTK | GLSL Example 1", ExampleCategory.GLSL, 1)] - public class T10_GLSL_Cube : GameWindow, IExample + [Example("First shader", ExampleCategory.GLSL, 1)] + public class T10_GLSL_Cube : GameWindow { #region --- Fields --- @@ -43,10 +43,8 @@ namespace Examples.Tutorial #region --- Constructors --- public T10_GLSL_Cube() - : base(new DisplayMode(800, 600), T10_GLSL_Cube.Title) - { - - } + : base(new DisplayMode(800, 600)) + { } #endregion @@ -81,6 +79,10 @@ namespace Examples.Tutorial out shader_program); } + #endregion + + #region CreateShaders + void CreateShaders(string vs, string fs, out int vertexObject, out int fragmentObject, out int program) @@ -117,6 +119,8 @@ namespace Examples.Tutorial GL.UseProgram(program); } + #endregion + #region private void CreateVBO() void CreateVBO() @@ -151,8 +155,6 @@ namespace Examples.Tutorial #endregion - #endregion - #region OnUnload public override void OnUnload(EventArgs e) @@ -255,26 +257,23 @@ namespace Examples.Tutorial #endregion - #region Example members - - #region public void Launch() + #region public static void Main() /// - /// Launches this example. + /// Entry point of this example. /// - /// - /// Provides a simple way for the example launcher to launch the examples. - /// - public void Launch() + [STAThread] + public static void Main() { - Run(30.0, 0.0); + using (T10_GLSL_Cube example = new T10_GLSL_Cube()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.Run(30.0, 0.0); + } } #endregion - - static readonly ExampleAttribute info = typeof(T10_GLSL_Cube).GetCustomAttributes(false)[0] as ExampleAttribute; - static readonly string Title = info.Title; - - #endregion } } diff --git a/Source/Examples/Tutorial/Text.cs b/Source/Examples/Tutorial/Text.cs index c7bda79d..4357559b 100644 --- a/Source/Examples/Tutorial/Text.cs +++ b/Source/Examples/Tutorial/Text.cs @@ -22,13 +22,14 @@ namespace Examples.Tutorial /// /// Shows how to render and scroll large amounts of text. /// - public class Text : GameWindow, IExample + [Example("Text", ExampleCategory.Tutorial, 4)] + public class Text : GameWindow { TextureFont serif = new TextureFont(new Font(FontFamily.GenericSerif, 24.0f)); TextHandle poem_handle; ITextPrinter text = new TextPrinter(); - public Text() : base(new DisplayMode(800, 600), String.Format("OpenTK | Tutorial {0}: Text", order)) + public Text() : base(new DisplayMode(800, 600)) { } string poem = new StreamReader("Data/Poem.txt").ReadToEnd(); @@ -138,13 +139,21 @@ namespace Examples.Tutorial #endregion - #region --- IExample Members --- + #region public static void Main() - public static readonly int order = 7; - - public void Launch() + /// + /// Entry point of this example. + /// + [STAThread] + public static void Main() { - Run(30.0, 0.0); + using (Text example = new Text()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.Run(30.0, 0.0); + } } #endregion diff --git a/Source/Examples/Tutorial/Textures.cs b/Source/Examples/Tutorial/Textures.cs index cf53f7fc..20847c8c 100644 --- a/Source/Examples/Tutorial/Textures.cs +++ b/Source/Examples/Tutorial/Textures.cs @@ -23,13 +23,13 @@ namespace Examples.Tutorial /// /// Demonstrates simple OpenGL Texturing. /// - public class Textures : GameWindow, IExample + [Example("Texture mapping", ExampleCategory.Tutorial, 5)] + public class Textures : GameWindow { - Bitmap bitmap = new Bitmap("Data\\metal.jpg"); + Bitmap bitmap = new Bitmap("Data\\logo-dark.jpg"); int texture; - TextureFont sans = new TextureFont(new Font(FontFamily.GenericSansSerif, 24.0f)); - public Textures() : base(new DisplayMode(800, 600), "OpenTK | Tutorial 5: Texturing") { } + public Textures() : base(new DisplayMode(800, 600)) { } #region OnLoad @@ -39,18 +39,16 @@ namespace Examples.Tutorial /// Not used. public override void OnLoad(EventArgs e) { - GL.ClearColor(Color.MidnightBlue); + GL.ClearColor(Color.SteelBlue); GL.Enable(EnableCap.Texture2d); GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); - //bitmap = new OpenTK.Fonts.Renderer().bmp; - //bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); GL.GenTextures(1, out texture); GL.BindTexture(TextureTarget.Texture2d, texture); BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), - ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb); + ImageLockMode.ReadOnly, bitmap.PixelFormat); GL.TexImage2D(TextureTarget.Texture2d, 0, PixelInternalFormat.Three, bitmap.Width, bitmap.Height, 0, OpenTK.OpenGL.Enums.PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0); @@ -63,6 +61,15 @@ namespace Examples.Tutorial #endregion + #region OnUnload + + public override void OnUnload(EventArgs e) + { + GL.DeleteTextures(1, ref texture); + } + + #endregion + #region OnResize /// @@ -72,7 +79,7 @@ namespace Examples.Tutorial /// There is no need to call the base implementation. protected override void OnResize(OpenTK.Platform.ResizeEventArgs e) { - GL.Viewport(0, 0, e.Width, e.Height); + GL.Viewport(0, 0, Width, Height); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); @@ -107,40 +114,41 @@ namespace Examples.Tutorial { GL.Clear(ClearBufferMask.ColorBufferBit); + GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); GL.BindTexture(TextureTarget.Texture2d, texture); GL.Begin(BeginMode.Quads); - GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-0.8f + 0.0375f, -0.8f + 0.0375f); - GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(0.8f + 0.0375f, -0.8f + 0.0375f); - GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(0.8f + 0.0375f, 0.8f + 0.0375f); - GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-0.8f + 0.0375f, 0.8f + 0.0375f); + GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(-0.6f, -0.4f); + GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(0.6f, -0.4f); + GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(0.6f, 0.4f); + GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(-0.6f, 0.4f); GL.End(); - //sans. - GL.Scale(2.0f / Width, 2.0f / Height, 1.0f); - //sans.Print('A'); - SwapBuffers(); } #endregion - #region IExample Members + #region public static void Main() /// - /// Only used by the ExampleLauncher application, no need to add a Launch() method in your code. - /// Add a call to Run() in your Main() function instead. + /// Entry point of this example. /// - public void Launch() + [STAThread] + public static void Main() { - this.Run(10.0, 5.0); + using (Textures example = new Textures()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)typeof(Textures).GetCustomAttributes(false)[0]); + example.Title = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.Run(30.0, 0.0); + } } #endregion - - public static readonly int order = 5; } } diff --git a/Source/Examples/WinForms/W01_First_Window.cs b/Source/Examples/WinForms/W01_First_Window.cs index b24fcdc4..c0f59f43 100644 --- a/Source/Examples/WinForms/W01_First_Window.cs +++ b/Source/Examples/WinForms/W01_First_Window.cs @@ -22,7 +22,8 @@ using OpenTK.OpenGL.Enums; namespace Examples.WinForms { - public partial class W01_First_Window : Form, IExample + [Example("Simple GLControl", ExampleCategory.WinForms, 1)] + public partial class W01_First_Window : Form { public W01_First_Window() { @@ -38,6 +39,8 @@ namespace Examples.WinForms } } + #region Events + protected override void OnLoad(EventArgs e) { base.OnLoad(e); @@ -87,15 +90,25 @@ namespace Examples.WinForms } } - #region IExample Members + #endregion - public void Launch() + #region public static void Main() + + /// + /// Entry point of this example. + /// + [STAThread] + public static void Main() { - + using (W01_First_Window example = new W01_First_Window()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Text = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.ShowDialog(); + } } - public static readonly int order = 1; - #endregion } } diff --git a/Source/Examples/WinForms/W02_Immediate_Mode_Cube.Designer.cs b/Source/Examples/WinForms/W02_Immediate_Mode_Cube.Designer.cs index 5dda0eaf..4ed51a07 100644 --- a/Source/Examples/WinForms/W02_Immediate_Mode_Cube.Designer.cs +++ b/Source/Examples/WinForms/W02_Immediate_Mode_Cube.Designer.cs @@ -37,7 +37,7 @@ this.glControl.Dock = System.Windows.Forms.DockStyle.Fill; this.glControl.Location = new System.Drawing.Point(0, 0); this.glControl.Name = "glControl"; - this.glControl.Size = new System.Drawing.Size(624, 444); + this.glControl.Size = new System.Drawing.Size(784, 564); this.glControl.TabIndex = 0; this.glControl.VSync = false; this.glControl.Layout += new System.Windows.Forms.LayoutEventHandler(this.glControl_Layout); @@ -46,7 +46,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(624, 444); + this.ClientSize = new System.Drawing.Size(784, 564); this.Controls.Add(this.glControl); this.Name = "W02_Immediate_Mode_Cube"; this.Text = "Cube"; diff --git a/Source/Examples/WinForms/W02_Immediate_Mode_Cube.cs b/Source/Examples/WinForms/W02_Immediate_Mode_Cube.cs index c272a93c..fba07ac3 100644 --- a/Source/Examples/WinForms/W02_Immediate_Mode_Cube.cs +++ b/Source/Examples/WinForms/W02_Immediate_Mode_Cube.cs @@ -25,7 +25,8 @@ using OpenTK.OpenGL.Enums; namespace Examples.WinForms { - public partial class W02_Immediate_Mode_Cube : Form, IExample + [Example("GLControl game loop", ExampleCategory.WinForms, 2)] + public partial class W02_Immediate_Mode_Cube : Form { static float angle = 0.0f; @@ -86,29 +87,6 @@ namespace Examples.WinForms #endregion - #region private void Render() - - private void Render() - { - GL.MatrixMode(MatrixMode.Modelview); - GL.LoadIdentity(); - Glu.LookAt( - 0.0, 5.0, 5.0, - 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0 - ); - GL.Rotate(angle, 0.0f, 1.0f, 0.0f); - angle += 0.5f; - - GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - - DrawCube(); - - glControl.SwapBuffers(); - } - - #endregion - #region GLControl.Resize event handler void glControl_Resize(object sender, EventArgs e) @@ -151,6 +129,29 @@ namespace Examples.WinForms Render(); } + #endregion + + #region private void Render() + + private void Render() + { + GL.MatrixMode(MatrixMode.Modelview); + GL.LoadIdentity(); + Glu.LookAt( + 0.0, 5.0, 5.0, + 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0 + ); + GL.Rotate(angle, 0.0f, 1.0f, 0.0f); + angle += 0.5f; + + GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); + + DrawCube(); + + glControl.SwapBuffers(); + } + #endregion #region private void DrawCube() @@ -199,22 +200,30 @@ namespace Examples.WinForms GL.End(); } - #endregion - - #region IExample Members - - public void Launch() - { - - } - - public static readonly int order = 2; - #endregion private void glControl_Layout(object sender, LayoutEventArgs e) { glControl_Resize(sender, EventArgs.Empty); } + + #region public static void Main() + + /// + /// Entry point of this example. + /// + [STAThread] + public static void Main() + { + using (W02_Immediate_Mode_Cube example = new W02_Immediate_Mode_Cube()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Text = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.ShowDialog(); + } + } + + #endregion } } diff --git a/Source/Examples/WinForms/W03_Extensions.cs b/Source/Examples/WinForms/W03_Extensions.cs index 9cca7d00..57e150f0 100644 --- a/Source/Examples/WinForms/W03_Extensions.cs +++ b/Source/Examples/WinForms/W03_Extensions.cs @@ -20,10 +20,11 @@ using OpenTK.OpenGL.Enums; namespace Examples.WinForms { - public partial class W03_Extensions : Form, IExample + [Example("Extensions", ExampleCategory.WinForms, 3)] + public partial class W03_Extensions : Form { - GLControl glControl = new GLControl(); - Assembly assembly; + //GLControl glControl = new GLControl(); + GLContext context; Type glClass; Type delegatesClass; Type importsClass; @@ -34,18 +35,22 @@ namespace Examples.WinForms { InitializeComponent(); - assembly = Assembly.Load("OpenTK"); - glClass = assembly.GetType("OpenTK.OpenGL.GL"); + glClass = typeof(GL); delegatesClass = glClass.GetNestedType("Delegates", BindingFlags.Static | BindingFlags.NonPublic); importsClass = glClass.GetNestedType("Imports", BindingFlags.Static | BindingFlags.NonPublic); - glControl.Load += new EventHandler(glControl_Load); - glControl.CreateControl(); + //glControl.CreateControl(); + Application.Idle += StartAsync; } - void glControl_Load(object sender, EventArgs e) + void StartAsync(object sender, EventArgs e) { - Application.Idle += StartAsync; + Application.Idle -= StartAsync; + + context = new GLContext(new DisplayMode(), new OpenTK.Platform.WindowInfo(this)); + context.CreateContext(); + + //while (!glControl.Created) driver = GL.GetString(StringName.Vendor) + " " + @@ -54,11 +59,6 @@ namespace Examples.WinForms all = delegatesClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic).Length; this.Text = String.Format("Loading {0} functions...", all); - } - - void StartAsync(object sender, EventArgs e) - { - Application.Idle -= StartAsync; this.backgroundWorker1.RunWorkerAsync(); } @@ -74,9 +74,7 @@ namespace Examples.WinForms { object d = f.GetValue(delegatesClass); if (d != null) - { ++supported; - } backgroundWorker1.ReportProgress((int)(((float)i / all) * 100.0f), String.Format("({0}/{1}) {2}:\t{3}", (++i).ToString(), all, d != null ? "ok" : "failed", f.Name)); @@ -88,6 +86,7 @@ namespace Examples.WinForms throw; } } + private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { listBox1.Items.Add(e.UserState as string); @@ -105,18 +104,29 @@ namespace Examples.WinForms */ } - #region IExample Members - - public void Launch() { } - - public static readonly int order = 3; - - #endregion - private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { this.Text = String.Format("{0}: {1}/{2} OpenGL functions supported.", driver, supported, all); } + + #region public static void Main() + + /// + /// Entry point of this example. + /// + [STAThread] + public static void Main() + { + using (W03_Extensions example = new W03_Extensions()) + { + // Get the title and category of this example using reflection. + ExampleAttribute info = ((ExampleAttribute)example.GetType().GetCustomAttributes(false)[0]); + example.Text = String.Format("OpenTK | {0} {1}: {2}", info.Category, info.Difficulty, info.Title); + example.ShowDialog(); + } + } + + #endregion } } \ No newline at end of file