Managing game states in .NET approach

Irrlicht.Net is no longer developed or supported, Irrlicht.Net Cross Platform is a much more complete wrapper. Please ask your C# related questions on their forums first.
Locked
shivanraptor
Posts: 40
Joined: Tue Aug 15, 2006 6:01 am
Location: Hong Kong
Contact:

Managing game states in .NET approach

Post by shivanraptor »

How can I manage game states in .NET approach ?
i have multiple User Interfaces , and I want to swich between them .
i have written some codes for it .
but i change my mode , it hangs .

My windows form , with a single picture box control only .

Code: Select all

public partial class MainScreen : Form
    {
        TGraphics3D t3d;
        bool to_exit = false;
        int last_display_mode = 0;
        public MainScreen()
        {
            InitializeComponent();
        }
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainScreen());
        }
        private void MainScreen_Load(object sender, EventArgs e)
        {
            this.Show();
            t3d = new TGraphics3D(pbMain, this);
            t3d.startup_path = Application.StartupPath;
            last_display_mode = TGraphics3D.display_mode;
            SwitchDisplay(TGraphics3D.display_mode);
            while(!to_exit)
                Heartbeat();
            GC.Collect();
        }
        private void Heartbeat()
        {
            if (TGraphics3D.display_mode == 7)
                to_exit = true;
            else
            {
                if (last_display_mode != TGraphics3D.display_mode)
                {
                    SwitchDisplay(TGraphics3D.display_mode);
                    last_display_mode = TGraphics3D.display_mode;
                }
            }
        }
        

        // ---------------
        // Display Manager
        // ---------------
        private void SwitchDisplay(int mode)
        {
            TGraphics3D.paused = true;
            t3d.AddStaticText(new Point(500, 500), new Point(600, 520), 0, false, true, null, TGraphics3D.display_mode.ToString());
            switch (mode)
            {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    t3d.AddGUIButton(new Point(10, 10), new Point(500, 50), 101, "still not working ?");
                    t3d.AddGUIButton(new Point(60, 60), new Point(100, 100), 102, "to mode 3");
                    break;
                case 3:
                    t3d.AddGUIButton(new Point(110, 110), new Point(150, 150), 103, "to mode 2");
                    t3d.AddStaticText(new Point(200, 200), new Point(300, 300), 0, false, false, null, "mode 3");
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                default: // should not be here 
                    to_exit = true;
                    break;
            }
            TGraphics3D.paused = false;
            last_display_mode = mode;
            t3d.Heartbeat();
        }
    }
shivanraptor
Posts: 40
Joined: Tue Aug 15, 2006 6:01 am
Location: Hong Kong
Contact:

Post by shivanraptor »

My display class , with some silly debugging messages .

Code: Select all

public class TGraphics3D
    {
        // private variables
        IrrlichtDevice device;
        ISceneManager smgr;
        IVideoDriver driver;
        IGUIEnvironment env;
        Control control;
        IGUISkin skin;
        
        // public variables        
        public static bool paused = false;
        public static int display_mode = 2;
        public string startup_path = "";
        public TGraphics3D(Control c, Form mainCls)
        {
            // start up the engine
            device = new IrrlichtDevice(Irrlicht.Video.DriverType.DIRECT3D9, new Dimension2D(c.Width, c.Height), 32, false, false, false, false, c.Handle);
            smgr = device.SceneManager;
            driver = device.VideoDriver;
            env = device.GUIEnvironment;
            control = c;
            device.EventReceiver = new TInput(c, device);
            skin = env.Skin;
        }
        public void Heartbeat()
        {
            SetFont("arial_10_normal");
            int entering_mode = display_mode;
            while (device.Run() && control.Enabled && !paused)
            {
                device.VideoDriver.BeginScene(true, true, new Irrlicht.Video.Color(50, 200, 200, 200));
                device.SceneManager.DrawAll();
                device.GUIEnvironment.DrawAll();
                device.VideoDriver.EndScene();
                if (display_mode != entering_mode)
                    paused = true;
            }
            GC.Collect();
        }

        public void ClearScreen()
        {
            
        }
        // --------------
        // GUI Interfaces
        // --------------
        public void AddGUIButton(Point lefttop, Point rightbottom, int id, string text)
        {
            env.AddButton(new Rect(lefttop.X, lefttop.Y, rightbottom.X, rightbottom.Y), null, id, text);
        }
        public void AddGUIMessageBox(int id, string caption, string text)
        {
            env.AddMessageBox(caption, text, false, MessageBoxFlag.OK, null, id);
        }
        public void AddGUICheckBox(Point lefttop, Point rightbottom, int id, string text, bool is_checked)
        {
            env.AddCheckBox(is_checked, new Rect(lefttop.X, lefttop.Y, rightbottom.X, rightbottom.Y), null, id, text);
        }
        public void AddGUIScrollbar(Point lefttop, Point rightbottom, int id, bool isHori)
        {
            env.AddScrollBar(isHori, new Rect(lefttop.X, lefttop.Y, rightbottom.X, rightbottom.Y), null, id);
        }
        public void AddStaticText(Point lefttop, Point rightbottom, int id, bool border, bool wordwrap, IGUIElement parent, string text)
        {
            env.AddStaticText(text, new Rect(lefttop.X, lefttop.Y, rightbottom.X, rightbottom.Y), border, wordwrap, parent, id);
        }
        public void AddGUIWindowWithText(Point lefttop, Point rightbottom, int id, string caption, Point txt_lefttop, Point txt_rightbottom, string text)
        {
            IGUIElement window = env.AddWindow(new Rect(lefttop.X, lefttop.Y, rightbottom.X, rightbottom.Y), false, caption, null, id);
            AddStaticText(txt_lefttop, txt_rightbottom, id, false, true, window, text);
        }
        public void AddFileOpenDialog(string text, int id)
        {
            env.AddFileOpenDialog(text, false, null, id);
        }
        public void AddEditBox(Point lefttop, Point rightbottom, int id, string text, bool border)
        {
            env.AddEditBox(text, new Rect(lefttop.X, lefttop.Y, rightbottom.X, rightbottom.Y), border, null, id);
        }
        public void AddImage(Point lefttop, string filename, bool alpha, int id, string text)
        {
            env.AddImage(driver.GetTexture(TSParam.IMG_PATH + filename), new Position2D(lefttop.X, lefttop.Y), alpha, null, id, text);
        }
        public void SetFont(string font_name)
        {
            IGUIFont font = env.GetFont(startup_path + TSParam.FONT_PATH + font_name + ".tfnt");
            if (font != null)
                skin.Font = font;
            else
                AddGUIButton(new Point(400, 400), new Point(500, 500), 102, "no good");
        }
shivanraptor
Posts: 40
Joined: Tue Aug 15, 2006 6:01 am
Location: Hong Kong
Contact:

Post by shivanraptor »

and this is the input class , implementing IEventReceiver

Code: Select all

public class TInput : IEventReceiver
    {
        Control c;
        IrrlichtDevice device;
        public TInput(Control _c, IrrlichtDevice dev)
        {
            c = _c;
            device = dev;
            
        }
        public bool OnEvent(Event e)
        {
            int id = 0;
            if (e.Type == EventType.KeyInput && !e.KeyPressedDown)
            {
                switch (e.Key)
                {
                    case KeyCode.KEY_ESCAPE:
                        c.Visible = false;
                        break;
                    case KeyCode.KEY_KEY_X:
                        c.Visible = true;
                        break;
                    case KeyCode.KEY_KEY_0:
                        TGraphics3D.display_mode = 0;
                        break;
                    case KeyCode.KEY_KEY_1:
                        TGraphics3D.display_mode = 1;
                        break;
                    case KeyCode.KEY_KEY_2:
                        TGraphics3D.display_mode = 2;
                        break;
                    case KeyCode.KEY_KEY_3:
                        TGraphics3D.display_mode = 3;
                        break;
                    case KeyCode.KEY_KEY_4:
                        TGraphics3D.display_mode = 4;
                        break;
                    case KeyCode.KEY_KEY_5:
                        TGraphics3D.display_mode = 5;
                        break;
                    case KeyCode.KEY_KEY_6:
                        TGraphics3D.display_mode = 6;
                        break;
                    case KeyCode.KEY_KEY_7:
                        TGraphics3D.display_mode = 7;
                        break;
                    default:
                        return false;
                }
                return true;
            } else if (e.Type == EventType.GUIEvent)
            {
                if (e.GUIEventCaller != null)
                    id = e.GUIEventCaller.ID;
                else
                    return false;
                switch (e.GUIEventType)
                {
                    case GUIEvent.BUTTON_CLICKED:
                        switch(id) {
                            case 101:
                                TGraphics3D.display_mode = 7;//c.Visible = false;
                                break;
                            case 102:
                                TGraphics3D.display_mode = 3;
                                break;
                            case 103:
                                TGraphics3D.display_mode = 2;
                                break;
                        }
                        break;
                    default:
                        return false;
                }
                return true;
            }
            else
                return false;
        }

    }
Locked