2007-08-10 09:27:13 +00:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-10 16:55:24 +00:00
|
|
|
|
#region --- Using Directives ---
|
|
|
|
|
|
2007-08-10 09:27:13 +00:00
|
|
|
|
using System;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading;
|
2007-07-27 12:06:32 +00:00
|
|
|
|
using System.Diagnostics;
|
2007-08-03 00:14:31 +00:00
|
|
|
|
using System.Security;
|
|
|
|
|
using System.Security.Permissions;
|
|
|
|
|
using System.Security.Policy;
|
2007-08-04 12:09:58 +00:00
|
|
|
|
using System.IO;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-10 16:55:24 +00:00
|
|
|
|
using OpenTK;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-23 00:15:18 +00:00
|
|
|
|
namespace Examples
|
|
|
|
|
{
|
|
|
|
|
public partial class ExampleLauncher : Form
|
|
|
|
|
{
|
2007-10-20 10:34:29 +00:00
|
|
|
|
#region class ExampleInfo
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains the information necessary to display and launch an example thorugh the ExampleLauncer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
class ExampleInfo
|
|
|
|
|
{
|
|
|
|
|
public string Name;
|
|
|
|
|
public string Description;
|
|
|
|
|
public Type Example;
|
|
|
|
|
|
|
|
|
|
public ExampleInfo(string name, string description, Type example)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
Description = description;
|
|
|
|
|
Example = example;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Constructor ---
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-05 13:42:31 +00:00
|
|
|
|
public ExampleLauncher()
|
2007-08-04 12:09:58 +00:00
|
|
|
|
{
|
2007-08-05 13:42:31 +00:00
|
|
|
|
InitializeComponent();
|
2007-08-04 12:09:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-10-20 10:34:29 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
public void ExampleLauncher_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
2007-09-02 13:34:44 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
// Get all examples
|
|
|
|
|
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
|
2007-10-20 10:34:29 +00:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2007-08-20 12:25:48 +00:00
|
|
|
|
foreach (Type type in types)
|
|
|
|
|
{
|
|
|
|
|
if (type.GetInterface("IExample") != null)
|
|
|
|
|
{
|
2007-10-20 10:34:29 +00:00
|
|
|
|
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 nre)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
listBox1.Items.Add(new ExampleInfo(sb.ToString(), "", type));
|
|
|
|
|
|
|
|
|
|
// Clean the StringBuilder for the next pass.
|
|
|
|
|
sb.Remove(0, sb.Length);
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Select first item
|
|
|
|
|
if (listBox1.Items.Count > 0)
|
|
|
|
|
this.listBox1.SelectedIndex = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-20 10:34:29 +00:00
|
|
|
|
void LaunchGameWindow(object example)
|
2007-09-02 00:02:10 +00:00
|
|
|
|
{
|
|
|
|
|
Type ex = example as Type;
|
|
|
|
|
try
|
|
|
|
|
{
|
2007-09-30 12:38:40 +00:00
|
|
|
|
using (GameWindow gw = (GameWindow)(ex.GetConstructor(Type.EmptyTypes).Invoke(null)))
|
|
|
|
|
{
|
|
|
|
|
(gw as IExample).Launch();
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-02 00:02:10 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception expt)
|
|
|
|
|
{
|
2007-10-17 11:33:11 +00:00
|
|
|
|
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);
|
2007-09-02 00:02:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private void RunExample()
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
|
|
|
|
if (listBox1.SelectedItem != null)
|
|
|
|
|
{
|
2007-10-20 10:34:29 +00:00
|
|
|
|
Type example = (listBox1.SelectedItem as ExampleInfo).Example;
|
2007-08-20 12:25:48 +00:00
|
|
|
|
|
2007-08-10 16:55:24 +00:00
|
|
|
|
Debug.Print("Launching example: {0}", example.ToString());
|
2007-09-02 00:02:10 +00:00
|
|
|
|
this.Visible = false;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
|
|
|
|
if (example.BaseType == typeof(GameWindow))
|
|
|
|
|
{
|
2007-10-20 10:34:29 +00:00
|
|
|
|
LaunchGameWindow(example);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
else if (example.BaseType == typeof(Form))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2007-09-30 12:38:40 +00:00
|
|
|
|
using (Form f = (Form)example.GetConstructor(Type.EmptyTypes).Invoke(null))
|
|
|
|
|
{
|
|
|
|
|
f.ShowDialog(this);
|
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception expt)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(
|
|
|
|
|
String.Format(
|
|
|
|
|
"Stacktrace:{0}{1}{0}{0}Inner exception:{0}{2}",
|
|
|
|
|
System.Environment.NewLine,
|
|
|
|
|
expt.StackTrace,
|
|
|
|
|
expt.InnerException
|
|
|
|
|
),
|
2007-08-10 16:55:24 +00:00
|
|
|
|
expt.Message);
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2007-10-17 11:33:11 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2007-10-20 10:34:29 +00:00
|
|
|
|
// Console application.
|
2007-10-17 11:33:11 +00:00
|
|
|
|
IExample ex = (IExample)example.GetConstructor(Type.EmptyTypes).Invoke(null);
|
|
|
|
|
ex.Launch();
|
|
|
|
|
}
|
2007-09-02 00:02:10 +00:00
|
|
|
|
|
|
|
|
|
GC.Collect();
|
|
|
|
|
GC.WaitForPendingFinalizers();
|
|
|
|
|
GC.Collect();
|
|
|
|
|
|
|
|
|
|
this.Visible = true;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private void listBox1_DoubleClick(object sender, EventArgs e)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
RunExample();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private void listBox1_KeyUp(object sender, KeyEventArgs e)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
switch (e.KeyCode)
|
2007-07-23 00:15:18 +00:00
|
|
|
|
{
|
2007-08-20 12:25:48 +00:00
|
|
|
|
case Keys.Enter:
|
|
|
|
|
RunExample();
|
|
|
|
|
break;
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
2007-08-20 12:25:48 +00:00
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
|
2007-08-20 12:25:48 +00:00
|
|
|
|
private void runButton_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
RunExample();
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
2007-09-02 00:02:10 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The main entry point for the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[STAThread]
|
|
|
|
|
static void Main()
|
|
|
|
|
{
|
2007-09-02 13:34:44 +00:00
|
|
|
|
using (Form exampleLauncher = new ExampleLauncher())
|
2007-09-02 00:02:10 +00:00
|
|
|
|
{
|
2007-09-02 13:34:44 +00:00
|
|
|
|
Application.EnableVisualStyles();
|
|
|
|
|
Application.Run(exampleLauncher);
|
2007-09-02 00:02:10 +00:00
|
|
|
|
}
|
2007-09-02 13:34:44 +00:00
|
|
|
|
}
|
2007-09-02 00:02:10 +00:00
|
|
|
|
|
2007-09-02 13:34:44 +00:00
|
|
|
|
private void ExampleLauncher_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Debug.Flush();
|
|
|
|
|
Debug.Close();
|
|
|
|
|
Trace.Flush();
|
|
|
|
|
Trace.Close();
|
2007-09-02 00:02:10 +00:00
|
|
|
|
}
|
2007-07-23 00:15:18 +00:00
|
|
|
|
}
|
2007-08-10 09:27:13 +00:00
|
|
|
|
}
|