Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using IrrlichtNETCP;
using IrrlichtNETCP.Inheritable;
namespace WinFormsIrrlicht
{
public partial class IrrlichtPanel : UserControl
{
IrrlichtDevice device;
public IrrlichtDevice Device
{
get
{
return this.device;
}
}
VideoDriver driver;
public VideoDriver VideoDriver
{
get
{
return this.driver;
}
}
SceneManager smgr;
public SceneManager SceneManager
{
get
{
return this.smgr;
}
}
IrrlichtNETCP.Timer timer;
public IrrlichtPanel()
{
InitializeComponent();
/*
Since we need to ensure that this code is run when
* the application starts, we place it into the constructor
* of the form. This means that we are guaranteed
* that this code is run before we call any methods on that class */
this.SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.Opaque, true);
#region IrrlichtEngine Initialization
device = new IrrlichtDevice(DriverType.OpenGL, new Dimension2D(this.ClientRectangle.Width, this.ClientRectangle.Height), 32, false, true, true, true,this.Handle);
timer = device.Timer;
driver = device.VideoDriver;
smgr = device.SceneManager;
#endregion
}
/* Loop:
We can programmatically trigger the Paint event by calling the Invalidate method of a form. This causes the Paint event to be triggered and Windows to enter back into our OnPaint event handler. Then we execute any code we want to run every frame and start all over again by calling the Invalidate method.
You might ask, “Why can’t we just add a while(true) loop directly within OnPaint() and never leave it?” The answer is that even though we’re game programmers, we’re expected to play well with others. Creating a loop within OnPaint() would starve the rest of the programs running on the system. While our game might gain a few frames per second, the rest of the system would become, at best, ugly, and at worst, unstable. So, instead of directly looping, we essentially “ask” to be called again as soon as possible.
*/
protected override void OnPaint(PaintEventArgs e)
{
timer.Tick();
#region Painting
base.OnPaint(e);
driver.BeginScene(true, true,IrrlichtNETCP.Color.Gray);
smgr.DrawAll();
driver.EndScene();
#endregion
this.Invalidate();
}
}
}