-Due to spam, wiki editing has been restricted to the dev team only. Sorry for any inconvenience-
Early Tutorial
From IrrlichtSharp
Welcome to the first Irrlicht# tutorial. Its purpose is to show how you can use the engine and how it is easy to draw something with.
Note: If you get a LoaderLock error, simply disable the exception in Debug/Exceptions.../Managed Debugging Assistants -> LoaderLock. It's a pretty well documented Managed DirectX issue which causes this, not Irrlicht#.
Contents |
First step
Retrieve the last version of the engine (at least revision 132, see Subversion (SVN) Setup). Compile it.
- A full download of this tutorial is available including media
- Media was originally sourced from the original Irrlicht distribution.
Create a project
For this example, create an empty project and add one empty code file (called MainFile.cs). Now, we have a project and a c# file where we can put some code.
- If you create the project as a console application, you will of course need to add System.Windows.Forms and System.Drawing as dependancies.
Code Below
This code has been updated to SVN162
using System;
using System.Drawing; // For the Size struct
using System.Windows.Forms; //For the Keys enum
using IrrlichtSharp;
using IrrlichtSharp.Video;
using IrrlichtSharp.Scene;
using IrrlichtSharp.Core;
namespace EarlyTutorial
{
/// <summary>
/// Main class used in the tutorial.
/// </summary>
/// <remarks>
/// We inherit from IEventReceiver, so we have access to Irrlicht# events.
/// We also could use the .net events provided by ILS.
/// </remarks>
public class IrrMain : IEventReceiver
{
/// <summary>
/// The Irrlicht# device used to render stuff to the screen.
/// </summary>
IlsDevice device = null;
/// <summary>
/// Constructor.
/// </summary>
public IrrMain()
{
// Create the device using IrrlichtCreationParameters to specify:
// DirectX9 driver, 32bpp color format, and event receiver.
CreationParameters myParameters = new CreationParameters();
myParameters.DriverType = DriverType.DirectX9;
myParameters.Bits = 32;
myParameters.EventReceiver = this;
device = new IlsDevice(myParameters);
}
#region IEventReceiver Members
/// <summary>
/// IEventReceiver's OnEvent implementation.
/// </summary>
/// <param name="e">The OnEvent arguments.</param>
/// <returns>True if the event is handled here. False if the event is allowed to be handled by another handler.</returns>
public bool OnEvent(IrrEvent e)
{
if (e.EventType == EventTypes.KeyInput)
{
if (e.Key.Key == Keys.Escape && e.Key.PressedDown)
{
device.Quit = true;
return true;//we return true since we have used the input
}
}
return false;//we return false: we haven't used the input
}
#endregion
#region Main
/// <summary>
/// Main method.
/// </summary>
/// <remarks>
/// When running from the debugger, the console window appears above the rendering window.
/// The team is working on a way to override this behavior. (Remember we're still in alpha here! ;) )
/// </remarks>
[STAThread]
static void Main()
{
// Specifying the LoggingLevel in this way, lets the engine know at what level to start logging messages.
// For instance, if you only wanted to see messages with a severity of warning and above, you could
// specify the logging level like so: Logger.LoggingLevel = LoggingLevel.Warning;
// The below line specifies that we will monitor all log messages.
Logger.LoggingLevel = Logger.Level.Information;
// Instantiate the tutorial's main class.
IrrMain main = new IrrMain();
// Grab a reference to the tutorial class' device.
IlsDevice device = main.device;
// Grab a reference to the device's video driver (this is what manages all the rendering).
IVideoDriver driver = device.VideoDriver;
// Grab a reference to the scene manager (this is what manages the 3d objects being rendered).
SceneManager smgr = device.SceneManager;
// Add a camera. The FPS camera is a cam wich allows a kind of default control for the user.
ICameraSceneNode camera = smgr.AddCameraSceneNodeFPS(null, -1, 250f, 100f);
// Hide the cursor.
device.CursorControl.Visible = false;
// Add the test SceneNode (which is a cube).
TestSceneNode test = smgr.AddTestSceneNode(10f, null, -1);
// Set the position of our new scene node.
test.Position = new Vector3Df(0f, 0f, 15f);
// To add a texture to the cube, we first load the desired texture.
// This texture is from the media folder of the original Irrlicht project,
// but you can use any texture you want.
ITexture cubeTexture = driver.GetTexture("rockwall.bmp");
// Set the texture to layer 0.
// (Currently, there are two layers possible - 0 and 1)
test.SetMaterialTexture(0, cubeTexture);
// Now, we want to animate the SceneNode. (Because tutorials are a lot mor fun when stuff moves. :) )
// Create an animator.
ISceneNodeAnimator rotationAnim = smgr.CreateRotationAnimator(new Vector3Df(0f, 0.5f, 0f));
// Now, add the animator to the our test scene node (the cube).
test.AddAnimator(rotationAnim);
// Use the run method to test if the loop should continue.
// For now, this is the way the render loop should be done,
// but we may change this to something more efficient like
// the message pump recommended by MS for DX apps.
while (device.Run())
{
// Begin the scene and set the "clear" color
driver.BeginScene(Color.Blue);
// Draw everything in the scene manager.
smgr.DrawAll();
// End the scene. (This also renders everything to the screen.)
driver.EndScene();
}//end of the loop
}//end of the main method
#endregion
}//end of the class
}//end of the namespace
Conclusion
As you can see, it's really easy to use. But this tutorial uses the only SceneNode already done, so there is still a lot of work to do before the engine will be really usable. You can help us by converting the scene nodes from c++ to c#, which is quite easy to do (10 min for the test scene node) and very helpful.
