MainWindow.cs
上传用户:gb3593
上传日期:2022-01-07
资源大小:3028k
文件大小:4k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using Tao.OpenGl;
  9. using Box2D.Net;
  10. using System.Reflection;
  11. namespace TestBed.Net
  12. {
  13.     public partial class MainWindow : Form
  14.     {
  15.         private Settings Settings = new Settings();
  16.         private Test mCurrentTest;
  17.         public Test CurrentTest
  18.         {
  19.             get
  20.             {
  21.                 return mCurrentTest;
  22.             }
  23.             set
  24.             {
  25.                 //Call code to reset current test
  26.                 mCurrentTest = value;
  27.             }
  28.         }
  29.         public MainWindow()
  30.         {
  31.             InitializeComponent();
  32.             
  33.             //Find all Tests in this module
  34.             foreach (Type type in Assembly.GetExecutingAssembly().GetExportedTypes())
  35.             {
  36.                 Test test = new Test();
  37.                 if (type.IsSubclassOf(test.GetType()))
  38.                 {
  39.                     TestsComboBox.Items.Add(System.Activator.CreateInstance(type));
  40.                 }
  41.             }
  42.             TestsComboBox.SelectedIndex = 0;
  43.             CurrentTest = (Test)System.Activator.CreateInstance(TestsComboBox.SelectedItem.GetType());
  44.             OpenGLControl.Dock = DockStyle.Fill;
  45.         }
  46.         private void MainWindow_Load(object sender, EventArgs e)
  47.         {
  48.             OpenGLControl.InitializeContexts();
  49.             panel1.Dock = DockStyle.Fill;
  50.             panel2.Dock = DockStyle.Right;
  51.             MainWindow_Resize(null, null);
  52.             RedrawTimer.Interval = (int)(1000.0f / (float)Settings.Hz);
  53.             RedrawTimer.Start();
  54.         }   
  55.         private void MainWindow_Resize(object sender, EventArgs e)
  56.         {
  57.             Size size = new Size(OpenGLControl.Size.Width - panel2.Width, OpenGLControl.Size.Height);
  58.             Renderer.InitOpenGL(size, CurrentTest.Zoom, CurrentTest.ViewOffset);
  59.             Renderer.OpenGLDraw(CurrentTest);
  60.         }
  61.         private void OpenGLControl_Paint(object sender, PaintEventArgs e)
  62.         {
  63.             Renderer.OpenGLDraw(CurrentTest);
  64.         }
  65.         private Vector RelativeCoordinates(Point MousePoint)
  66.         {
  67.             float Height = (float)OpenGLControl.Size.Height;
  68.             float Width = (float)(OpenGLControl.Size.Width - panel2.Width);
  69.             if(Height <= 0)
  70.                 Height = 1;
  71.             float AspectRatio = Width / Height;
  72.             Vector relative = new Vector(
  73.                 (float)MousePoint.X / Width,
  74.                 (float)MousePoint.Y / Height);
  75.             relative -= new Vector(.5f, .5f);
  76.             relative *= 2;
  77.             relative.Y *= -1;
  78.             relative.X *= AspectRatio;
  79.             return relative;
  80.         }
  81.         private void OpenGLControl_MouseMove(object sender, MouseEventArgs e)
  82.         {
  83.             CurrentTest.MouseMove(RelativeCoordinates(e.Location));
  84.         }
  85.         private void OpenGLControl_MouseUp(object sender, MouseEventArgs e)
  86.         {
  87.             CurrentTest.MouseUp(RelativeCoordinates(e.Location));
  88.         }
  89.         private void OpenGLControl_MouseDown(object sender, MouseEventArgs e)
  90.         {
  91.             CurrentTest.MouseDown(RelativeCoordinates(e.Location));
  92.         }
  93.         private void RedrawTimer_Tick(object sender, EventArgs e)
  94.         {
  95.             CurrentTest.Step(Settings);
  96.             Renderer.OpenGLDraw(CurrentTest);
  97.             OpenGLControl.Draw();
  98.             int errorCode = 0;
  99.             if ((errorCode = Gl.glGetError()) > 0)
  100.             {
  101.                 RedrawTimer.Stop();
  102.                 //Handled by the OpenGLControl
  103.                 //MessageBox.Show(Glu.gluErrorString(errorCode));
  104.             }
  105.         }
  106.         private void OpenGLControl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
  107.         {
  108.             if(e.KeyCode == Keys.R)
  109.                 CurrentTest = (Test)System.Activator.CreateInstance(CurrentTest.GetType());
  110.             else
  111.                 CurrentTest.KeyPress(e.KeyCode);
  112.         }
  113.     }
  114. }