Click here to Skip to main content
15,891,529 members
Articles / Multimedia / DirectX

Game Engine

Rate me:
Please Sign up or sign in to vote.
3.78/5 (6 votes)
20 Nov 2009CPL2 min read 47.5K   1.5K   34  
A game engine first prototype
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using gefx;
using MTV3D65;

namespace UnitEditor
{
    public partial class Form1 : Form
    {
        gefx.gefxs Engine = new gefx.gefxs();
        TV_3DVECTOR v = new TV_3DVECTOR(0, 0, 0);

        bool bMove = false;
        bool bScale = false;
        bool bRotate = false;

        bool Walk = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.Show();
            this.Focus();
            this.picScene.MouseMove += new System.Windows.Forms.MouseEventHandler(this.picScene_MouseMove);
            Engine.SetUpScene(picScene.Handle,800,600,true,CONST_TV_MULTISAMPLE_TYPE.TV_MULTISAMPLE_NONE  ,true,false  );
            Application.DoEvents();
            Engine.Teren.AddHeightMap("heightmap.jpg", ref Engine.Scene);
            Engine.Teren.AddTexture("3.bmp");
            Engine.Kamera2.SetUpCameraPosition(new TV_3DVECTOR(10, 50, 10));
            Engine.Teren.AddTerrainPhysics(ref Engine.Fizika.PhysX);
            Engine.GameLoop();
        }

        public void picScene_MouseMove(object sender, EventArgs e)
        {
            //this.picScene.Focus();


        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Engine.DestroyAllAndExit();
            
            this.Close();

        }

         #region STRING FUNKCIJE
        public static string Left(string param, int length)
        {
            //we start at 0 since we want to get the characters starting from the
            //left and with the specified lenght and assign it to a variable
            string result = param.Substring(0, length);
            //return the result of the operation
            return result;
        }
        public static string Right(string param, int length)
        {
            //start at the index based on the lenght of the sting minus
            //the specified lenght and assign it a variable
            string result = "";
            try
            {
                 result = param.Substring(param.Length - length, length);
            }
            catch (Exception e)
            {
                

            }
            //return the result of the operation
            return result;
        }

        public static string Mid(string param, int startIndex, int length)
        {
            //start at the specified index in the string ang get N number of
            //characters depending on the lenght and assign it to a variable
            string result = param.Substring(startIndex, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param, int startIndex)
        {
            //start at the specified index and return all characters after it
            //and assign it to a variable
            string result = param.Substring(startIndex);
            //return the result of the operation
            return result;
        } 
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            bool CanCreate = false;

            if (txtUnitName.Text.Length <= 1)
            {



            }
            else
            {
                if (Engine.XActor.CheckUnitNameExsist(txtUnitName.Text))
                {
                    txtUnitName.Text = Engine.XActor.SetRandomUnitName();

                }
                else
                {

                    if (Engine.XModel.CheckUnitNameExsist(txtUnitName.Text))
                    {

                        txtUnitName.Text = Engine.XModel.SetRandomUnitName();

                    }
                    else
                    {

                        if (rbUnitTypeActor.Checked)
                        {

                            oLoadUnitMesh.Filter = "Animirani |*.tva";
                            oLoadUnitMesh.ShowDialog();
                            string f = oLoadUnitMesh.FileName;
                            Engine.XActor.AddUnit(txtUnitName.Text.ToString(), f, ref Engine, XTModelActor.ModelActorType.TVA);
                            CanCreate = true;


                        }

                        if (rbUnitTypeMesh.Checked)
                        {
                            oLoadUnitMesh.Filter = "Model|*.tvm|DirectX |*.x";
                            oLoadUnitMesh.ShowDialog();
                            string f = oLoadUnitMesh.FileName;
                            if (Right(f, 3) == "tvm" || Right(f, 3) == "TVM")
                            {

                                Engine.XModel.AddStaticUnit(txtUnitName.Text.ToString(), f, ref Engine, XTModel.ModelType.TVM);
                                CanCreate = true;

                            }
                            if (Right(f, 1) == "x" || Right(f, 1) == "X")
                            {
                                CanCreate = true;

                                Engine.XModel.AddStaticUnit(txtUnitName.Text.ToString(), f, ref Engine, XTModel.ModelType.StaticX);
                            }


                        }
                    }

                }


            }
            
            if (CanCreate)
            {

                TV_3DVECTOR vc = new TV_3DVECTOR(0, 0, 0);

                //Engine.Kamera2.GetMSpacePos((float)Engine.Kamera2.tmpMouseX1, (float)Engine.Kamera2.tmpMouseY1, ref vc);

                //Engine.XModel.SetUnitMatrix(txtUnitName.Text, Engine.Kamera2._camera.GetMatrix());

                TV_3DVECTOR offset = new TV_3DVECTOR(0, 0, 20);
                //offset = Engine.Kamera2.GetCameraDirection();
                
                //v = Engine.Kamera.kamerMouseSpacePos;

              


                float mass = (float)System.Convert.ToSingle(txtUnitMass.Text.ToString());


                if (rbPBox.Checked)
                {

                    Engine.XModel.SetPhysics(ref Engine.Fizika.PhysX, txtUnitName.Text, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_BOX, mass);
                    Engine.XActor.SetPhysics(ref Engine.Fizika.PhysX, txtUnitName.Text, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_BOX, mass);
                }
                if (rbPSphere.Checked)
                {

                    Engine.XModel.SetPhysics(ref Engine.Fizika.PhysX, txtUnitName.Text, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_SPHERE, mass);
                    Engine.XActor.SetPhysics(ref Engine.Fizika.PhysX, txtUnitName.Text, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_SPHERE, mass);
                }
                if (rbPCylinder.Checked)
                {

                    Engine.XModel.SetPhysics(ref Engine.Fizika.PhysX, txtUnitName.Text, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_CYLINDER, mass);
                    Engine.XActor.SetPhysics(ref Engine.Fizika.PhysX, txtUnitName.Text, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_CYLINDER, mass);
                }
                if (rbConvex.Checked)
                {

                    Engine.XModel.SetPhysics(ref Engine.Fizika.PhysX, txtUnitName.Text, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_CONVEXHULL, mass);
                    Engine.XActor.SetPhysics(ref Engine.Fizika.PhysX, txtUnitName.Text, CONST_TV_PHYSICSBODY_BOUNDING.TV_BODY_CONVEXHULL, mass);
                }
                PopulateUnitList();
                //Engine.DrawCoordinateSystem
                //Engine.XModel.DrawCoordinateArroundActor(txtUnitName.Text, 1000f);
                Engine.XActor.MoveUnit(txtUnitName.Text, v, ref Engine.Fizika.PhysX);
                Engine.XModel.MoveUnit(txtUnitName.Text, v, ref Engine.Fizika.PhysX);

            }
            TreeNode node;

            //node = tvUnitsList.Nodes.Add("Fruits");
            //node.Nodes[0].Nodes.Add("ts");
            //node.Nodes.Add("Peach");




        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Application.DoEvents();


            if (Engine.Kamera2.tmpMouseB2)
            {


                v.x = Engine.GetMouseLandPosition.x;
                v.y = Engine.GetMouseLandPosition.y;
                v.z = Engine.GetMouseLandPosition.z;
            }

            if (Engine.Kamera2.tmpMouseB2)
            {
                lblSelectedUnit.Text = Engine.XModel.MouseUnitPick(ref Engine.Scene, Engine.Kamera2.tmpMouseX1, Engine.Kamera2.tmpMouseY1);

                //this.Text = Engine.Kamera2.tmpMouseX.ToString() + "   " + Engine.Kamera2.tmpMouseY.ToString();
                Engine.XModel.ShowUnitBoundingBox("Sve", false);


                Engine.XModel.ShowUnitBoundingBox(lblSelectedUnit.Text, true);

                lblSelectedUnitActor.Text = Engine.XActor.MouseUnitPick(ref Engine.Scene, Engine.Kamera2.tmpMouseX1, Engine.Kamera2.tmpMouseY1);
                Engine.XActor.ShowUnitBoundingBox("Sve", false);

                
                

                Engine.XActor.ShowUnitBoundingBox(lblSelectedUnitActor.Text, true);




                if (bMove)
                {
                    TV_3DVECTOR v = Engine.XModel.GetUnitPosition(lblSelectedUnit.Text);
                    txtMX.Text = v.x.ToString();
                    txtMY.Text = v.y.ToString();
                    txtMZ.Text = v.z.ToString();
                    v = Engine.XActor.GetUnitPosition(lblSelectedUnitActor.Text);
                    txtMX.Text = v.x.ToString();
                    txtMY.Text = v.y.ToString();
                    txtMZ.Text = v.z.ToString();



                }

                if (bRotate)
                {
                    TV_3DVECTOR v = Engine.XModel.GetUnitRotate(lblSelectedUnit.Text);
                    txtMX.Text = v.x.ToString();
                    txtMY.Text = v.y.ToString();
                    txtMZ.Text = v.z.ToString();
                    v = Engine.XActor.GetUnitRotate(lblSelectedUnitActor.Text);
                    txtMX.Text = v.x.ToString();
                    txtMY.Text = v.y.ToString();
                    txtMZ.Text = v.z.ToString();


                }

                if (bScale)
                {

                    TV_3DVECTOR v = Engine.XModel.GetUnitScale(lblSelectedUnit.Text);
                    txtMX.Text = v.x.ToString();
                    txtMY.Text = v.y.ToString();
                    txtMZ.Text = v.z.ToString();
                    v = Engine.XActor.GetUnitScale(lblSelectedUnitActor.Text);
                    txtMX.Text = v.x.ToString();
                    txtMY.Text = v.y.ToString();
                    txtMZ.Text = v.z.ToString();

                }


                


            }

            if (Engine.Kamera2.tmpMouseB1 && Engine.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_SPACE ))
            {
                TV_3DVECTOR v;
                v.x = Engine.GetMouseLandPosition.x;
                v.y = Engine.GetMouseLandPosition.y;
                v.z = Engine.GetMouseLandPosition.z;

                //v = Engine.Kamera.kamerMouseSpacePos;

                Engine.XModel.MoveUnit(lblSelectedUnit.Text, v, ref Engine.Fizika.PhysX);



            }
            if (Engine.Kamera2.tmpMouseB1 && Engine.Kamera2._inputs.IsKeyPressed(CONST_TV_KEY.TV_KEY_SPACE))
            {
                TV_3DVECTOR v;
                v.x = Engine.GetMouseLandPosition.x;
                v.y = Engine.GetMouseLandPosition.y;
                v.z = Engine.GetMouseLandPosition.z;

                //v = Engine.Kamera.kamerMouseSpacePos;
                
                Engine.XActor.MoveUnit( lblSelectedUnitActor.Text , v, ref Engine.Fizika.PhysX);


            }

            Engine.Zvuk.SetListenerPosition(Engine.Kamera2.GetCameraPosition(), Engine.Kamera2.GetCameraDirection());


            if (Walk)
            {

                Application.DoEvents();
                
                //Engine.XActor.Walk(new TV_3DVECTOR(0, 0, 0), lblCurrentActor.Text, ref Engine.Fizika.PhysX, Engine.fTime, 0.1f,Engine.Teren.Land);
               
                
                Engine.XActor.WalkAllSelected(
                    v, 
                    lblCurrentActor.Text, 
                    ref Engine.Fizika.PhysX,
                    Engine.fTime, 
                    0.05f,
                    Engine.Teren.Land , ref Engine ,true,10f );


            }

            try
            {

                if (Engine.XActor.GetUnitStatus(lblSelectedUnitActor.Text) != "null")
                {

                    lstBoneList.Items.Add(Engine.XActor.GetUnitStatus(lblSelectedUnitActor.Text));
                }
            }
            catch (Exception esa)
            {


            }





        }

        private void picScene_Click(object sender, EventArgs e)
        {
            this.picScene.Focus();
        }

        public void PopulateUnitList()
        {

            lstUnitList.Items.Clear();

            foreach (XTModel m in Engine.XModel.UnitS)
            {

                lstUnitList.Items.Add(m.UnitName.ToString());

            }

            foreach (XTModelActor m in Engine.XActor.UnitD)
            {

                lstUnitList.Items.Add(m.UnitActorName.ToString());

            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            Engine.XModel.RemoveStaticUnit(lblSelectedUnit.Text, ref Engine.Scene, ref Engine.Fizika.PhysX);
            Engine.XActor.RemoveStaticUnit(lblSelectedUnitActor.Text, ref Engine.Scene, ref Engine.Fizika.PhysX,ref Engine.XModel );


        }

        private void btnPhysics_Click(object sender, EventArgs e)
        {
            if (Engine.SimuLatePhysics)
            {

                this.btnPhysics.BackColor = Color.White;
                this.Engine.SimuLatePhysics = false;
                this.btnPhysics.Text = "Start physics";

            }
            else
            {
                this.Engine.SimuLatePhysics = true;
                this.btnPhysics.BackColor = Color.Red;
                this.btnPhysics.Text = "Stop physics";




            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            float x = (float)System.Convert.ToSingle(txtMX.Text);
            float y = (float)System.Convert.ToSingle(txtMY.Text);
            float z = (float)System.Convert.ToSingle(txtMZ.Text);
            if (bMove)
            {
                Engine.XModel.MoveUnit(lblSelectedUnit.Text, new TV_3DVECTOR(x, y, z),ref Engine.Fizika.PhysX );
                Engine.XActor.MoveUnit(lblSelectedUnitActor.Text, new TV_3DVECTOR(x, y, z), ref Engine.Fizika.PhysX);


            }

            if (bRotate)
            {

                Engine.XModel.RotateUnit(lblSelectedUnit.Text, new TV_3DVECTOR(x, y, z), ref Engine.Fizika.PhysX);
                Engine.XActor.RotateUnit(lblSelectedUnitActor.Text, new TV_3DVECTOR(x, y, z), ref Engine.Fizika.PhysX);


            }

            if (bScale)
            {

                Engine.XModel.ScaleUnit(lblSelectedUnit.Text, new TV_3DVECTOR(x, y, z),ref Engine.Fizika.PhysX );
                Engine.XActor.ScaleUnit(lblSelectedUnitActor.Text, new TV_3DVECTOR(x, y, z), ref Engine.Fizika.PhysX);


            }
        }

        private void cmdMove_Click(object sender, EventArgs e)
        {
            lblAction.Text = "Move";
            bMove = true;
            bScale = false;
            bRotate = false;

        }

        private void cmdRotate_Click(object sender, EventArgs e)
        {
            lblAction.Text = "Rotate";
            bMove = false;
            bScale = false;
            bRotate = true;
        }

        private void cmdScale_Click(object sender, EventArgs e)
        {
            lblAction.Text = "Scale";
            bMove = false;
            bScale = true;
            bRotate = false;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            Engine.bDrawCoordinateSystem = true;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            //CONST_TV_PHYSICSBODY_BOUNDING.
        }

        private void lblSelectedUnitActor_Click(object sender, EventArgs e)
        {
            lblCurrentActor.Text = lblSelectedUnitActor.Text;


        }

        private void button8_Click(object sender, EventArgs e)
        {
            lstAnimationLists.Items.Clear();

            string[] r = Engine.XActor.GetListAnimation(lblSelectedUnitActor.Text);
            int i;

            for (i = 0; i < r.Length - 1; i++)
            {
                lstAnimationLists.Items.Add(r[i]);


            }



        }

        private void lstAnimationLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            Engine.XActor.PlayAnimation(lstAnimationLists.SelectedItem.ToString(), lblSelectedUnitActor.Text);


        }

        private void button9_Click(object sender, EventArgs e)
        {
            lstBoneList.Items.Clear();
            //lstBoneList.Items.Add (Engine.XActor.GetBoneCount(lblSelectedUnitActor.Text ));


            string[] r = Engine.XActor.GetListBones(lblSelectedUnitActor.Text);
            int i;

            for (i = 0; i < r.Length - 1; i++)
            {
                lstBoneList.Items.Add(r[i]);


            }
        }

        private void lstBoneList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Engine.bLoop = false;
            Engine.DestroyAllAndExit();
            this.Close();
        }

        private void btnWalk_Click(object sender, EventArgs e)
        {
            if (Walk == false)
            {
                Walk = true;
                btnWalk.BackColor = Color.Red;
            }
            else
            {
                Walk = false;
                btnWalk.BackColor = Color.Green ;
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            oLoadSound.Filter = "Wav|*.wav|MP3 |*.mp3|OGG |*.ogg";
            oLoadSound.ShowDialog();
            lblSound.Text = oLoadSound.FileName;
            lstSounds.Items.Add(lblSound.Text);

        }

        private void lstSounds_SelectedIndexChanged(object sender, EventArgs e)
        {
            Engine.Zvuk.Stop2DSound();

            try
            {
                //Engine.Zvuk.Play2DSound(lstSounds.SelectedItem.ToString());
                Engine.Zvuk.Play3DSound(lstSounds.SelectedItem.ToString(), new TV_3DVECTOR(0, 0, 0) );
                Engine.Zvuk.SetDistance(10);
            }
            catch (Exception exa)
            {

            }
        }



        private void button12_Click(object sender, EventArgs e)
        {
            Engine.Zvuk.Stop2DSound();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Engine.XActor.SetActorBoneSkelet("Sve",ref Engine  );
        }

        private void button14_Click(object sender, EventArgs e)
        {
            Engine.XActor.DestroyAttachedBoneMesh(lblSelectedUnitActor.Text,ref Engine );
        }

        private void button15_Click(object sender, EventArgs e)
        {

            string fname;

            sfdUnitActorSave.ShowDialog();
            fname = sfdUnitActorSave.FileName;

            Engine.XActor.SaveUnitToXMLFile(lblSelectedUnitActor.Text, fname);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            Engine.XActor.StopPlayAnimation(lblSelectedUnitActor.Text);

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button18_Click(object sender, EventArgs e)
        {
            if (Engine.XActor.CheckUnitNameExsist(txtVehicaleName.Text))
            {
                txtVehicaleName.Text = Engine.XActor.SetRandomUnitName();

            }
            else
            {

                if (Engine.XModel.CheckUnitNameExsist(txtVehicaleName.Text))
                {

                    txtVehicaleName.Text = Engine.XModel.SetRandomUnitName();

                }
                else
                {

                    TV_3DVECTOR offset = new TV_3DVECTOR(0, 10, 0);
                    Engine.VMannager.AddVehicale(txtVehicaleName.Text, ref Engine, lblVehBody.Text, 2500, XTModel.ModelType.TVM, v + offset);
                    Engine.VMannager.AddWheels(txtVehicaleName.Text, ref Engine, lblVEhWheel.Text, 135, XTModel.ModelType.TVM, 1);


                }
            }


        
        
        }

        private void button16_Click(object sender, EventArgs e)
        {
            oLoadUnitMesh.Filter = "Model|*.tvm|DirectX |*.x";
            oLoadUnitMesh.ShowDialog();
            string f = oLoadUnitMesh.FileName;
            lblVehBody.Text = f;

  
        }

        private void button17_Click(object sender, EventArgs e)
        {
            oLoadUnitMesh.Filter = "Model|*.tvm|DirectX |*.x";
            oLoadUnitMesh.ShowDialog();
            string f = oLoadUnitMesh.FileName;
            lblVEhWheel.Text = f;

        }



       
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Software Developer
Croatia Croatia
Programer

Comments and Discussions