C# Control with Irrlich rendering, supporting WinForm Events

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Cyberdrill
Posts: 18
Joined: Wed Feb 27, 2008 2:32 pm
Location: Russia, Moscow

C# Control with Irrlich rendering, supporting WinForm Events

Post by Cyberdrill »

This control can be added to WinForm and it supports WinForm Events! By using alternative render loop. Access to Irrlicht device provided with property Device.

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();
           
       
        }


       
    }
} 
TheBeef
Posts: 25
Joined: Wed Feb 20, 2008 4:38 am

Post by TheBeef »

The .NET wrapper forum is that way--> link :)

Looks pretty good, though.
Cyberdrill
Posts: 18
Joined: Wed Feb 27, 2008 2:32 pm
Location: Russia, Moscow

Post by Cyberdrill »

Yes, I've been there :) And I think most people look this forum first, so maybe it may help someone
Post Reply