Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » DirectX » Games » Downloads
 
Add your own
alternative version

Game Engine

, 20 Nov 2009 CPL
A game engine first prototype
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.IO;

using MTV3D65;
using IrrKlang; 


namespace gefx
{
    public class XTModel
    {
        public enum ModelType
        {
            StaticX,

            TVM


        };
        public TVMesh Unit;
        TVTextureFactory TextureFactory;
        public string UnitName;

        string material_file="no file include";
        string shder_file="no file include";
        string GPathToModel = "no path";
        public TVScreen2DImmediate Screen2DImmediate;

        


        public int RigidBodyIndex;
        public CONST_TV_PHYSICSBODY_BOUNDING PhysicObjectType;
        public float ModelMass;

        

        public enum XTModelType
        {
            Free,
            Building,
            Bullet,
            Scene,
            Vegetation

        };
        public XTModelType WModelType = XTModelType.Free;
        public void SetModelType(XTModelType MType)
        {
            WModelType = MType;

        }

        public XTModelType GetModelType()
        {

            return WModelType;
        }

        public XTModel()
        {
            Unit = new TVMesh();
            TextureFactory = new TVTextureFactory();
        }
        public XTModel(ref TVScene Scene,ref TVGlobals Globals, string Name, string PathToModel, ModelType Type)
        {
            Unit = new TVMesh();
            TextureFactory = new TVTextureFactory();
            if (Type == ModelType.TVM)
            {

                Unit = Scene.CreateMeshBuilder(Name);
                Unit.LoadTVM(PathToModel, true, true);
                Unit.SetMaterial(Globals.GetMat("Matte"));
                Unit.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE);
                Unit.SetShadowCast(true, true);
                Unit.SetMeshName(Name);
            }
            if (Type == ModelType.StaticX)
            {
                Unit = Scene.CreateMeshBuilder(Name);
                Unit.LoadXFile(PathToModel, true, true);
                Unit.SetMaterial(Globals.GetMat("Matte"));
                Unit.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE);
                Unit.SetShadowCast(true, true);
                Unit.SetMeshName(Name);
            }
            GPathToModel = PathToModel;
            UnitName = Name;
        }

        public XTModel(ref TVScene Scene, ref TVGlobals Globals, string Name,  ModelType Type)
        {
            Unit = new TVMesh();
            TextureFactory = new TVTextureFactory();
            if (Type == ModelType.TVM)
            {

                Unit = Scene.CreateMeshBuilder(Name);
                Unit.CreateBox(1, 1, 1);

                Unit.SetMaterial(Globals.GetMat("Matte"));
                Unit.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE);
                Unit.SetShadowCast(true, true);
                Unit.SetMeshName(Name);
            }
           
            UnitName = Name;
        }

        public void CrtajJedinicu()
        {
            if (Unit != null)
            {
                Unit.Render();

            }
            else
            {


                throw new System.ArgumentException("Move fale unit is not declared !");
            }

        }
        public void MakniJedinicu(ref TVScene Scene)
        {
            if (Unit != null)
            {
                Scene.DestroyMesh(Unit);

            }

        }

        public void MakniJedinicu(ref TVScene Scene, ref TVPhysics PHys)
        {
            if (Unit != null)
            {
                PHys.DestroyBody(RigidBodyIndex);
                Scene.DestroyMesh(Unit);

            }

        }

        #region Pozicija jedinice
        public void SetUnitPosition(TV_3DVECTOR V)
        {
            Unit.SetPosition(V.x, V.y, V.z);

        }
        public void SetUnitPosition(float X, float Y, float Z)
        {
            Unit.SetPosition(X, Y, Z);

        }

        public TV_3DVECTOR GetUnitPosition()
        {
            TV_3DVECTOR r = new TV_3DVECTOR(0, 0, 0);

            if (Unit != null)
            {
                r = Unit.GetPosition();
            }

            return r;

        }
        public void SetUnitRotation(TV_3DVECTOR r, float time)
        {
            if (Unit != null)
            {
                Unit.SetRotation(r.x * time , r.y * time , r.z * time );

            }

        }

        public void SetUnitRotation(float x, float y, float z, float time)
        {
            if (Unit != null)
            {
                Unit.SetRotation(x * time , y * time , z *time );

            }

        }


        public void SetUnitRotation(TV_3DVECTOR r)
        {
            if (Unit != null)
            {
                Unit.SetRotation(r.x,r.y,r.z );

            }

        }

        public void SetUnitRotation(float x, float y, float z)
        {
            if (Unit != null)
            {
                Unit.SetRotation(x, y, z);

            }

        }

        #endregion
        public void SetUnitScale(float x, float y, float z)
        {
            if (Unit != null)
            {

                Unit.SetScale(x, y, z);

            }
            else
            {


                throw new System.ArgumentException("Scale fale unit is not declared !");
            }

        }
        public void SetUnitScale(TV_3DVECTOR s)
        {
            if (Unit != null)
            {

                Unit.SetScale(s.x, s.y, s.z);

            }
            else
            {


                throw new System.ArgumentException("Scale fale unit is not declared !");
            }

        }
        public TV_3DVECTOR GetUnitScale()
        {

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

            if (Unit != null)
            {
                r = Unit.GetScale();

            }
            else
            {


                throw new System.ArgumentException("Get Scale fale unit is not declared !");
            }

            return r;

        }
        public int GetUnitID()
        {
            int r = 0;

            if (Unit != null)
            {

                r = Unit.GetIndex();
            }
            else
            {


                throw new System.ArgumentException("Get ID fale unit is not declared !");
            }
            return r;

        }
        public void SetUnitTexture(string TexturePack, ref TVGlobals Globals)
        {
            if (Unit != null)
            {

                TextureFactory.LoadTexture(TexturePack, "Texture" + Unit.GetMeshName().ToString() , -1, -1, CONST_TV_COLORKEY.TV_COLORKEY_NO, true);
                Unit.SetTexture(Globals.GetTex("Texture" + Unit.GetMeshName().ToString()), -1);


            }
            else
            {


                throw new System.ArgumentException("Set unit texture fale unit is not declared !");
            }

        }
        public void AddMaterialUnitFromFile(string FName, ref TVMaterialFactory mat, ref TVGlobals glob)
        {
            string[] para = new string[19];
            string line;
            int counter = 0;
            TextReader tr = new StreamReader(FName);
            material_file = FName;

            while ((line = tr.ReadLine()) != null)
            {
                para[counter] = line;
                counter++;
            }
            string[] D = para[0].Split(',');
            string[] A = para[1].Split(',');
            string[] S = para[2].Split(',');
            string[] P = para[3].Split(',');
            string[] E = para[4].Split(',');
            tr.Close();
            int i = 0;
                    string Name = System.IO.Path.GetFileName(FName);
                    mat.CreateMaterial("Mat" + Name);
                    mat.SetAmbient(glob.GetMat("Mat" + Name + Unit.GetMeshName()),
                        (float)System.Convert.ToSingle(A[1]),
                        (float)System.Convert.ToSingle(A[2]),
                        (float)System.Convert.ToSingle(A[3]),
                        1);
                    mat.SetDiffuse(glob.GetMat("Mat" + Name + Unit.GetMeshName()),
                        (float)System.Convert.ToSingle(D[1]),
                        (float)System.Convert.ToSingle(D[2]),
                        (float)System.Convert.ToSingle(D[3]),
                        1);
                    mat.SetSpecular(glob.GetMat("Mat" + Name + Unit.GetMeshName()),
                        (float)System.Convert.ToSingle(S[1]),
                        (float)System.Convert.ToSingle(S[2]),
                        (float)System.Convert.ToSingle(S[3]),
                        1);
                    mat.SetEmissive(glob.GetMat("Mat" + Name + Unit.GetMeshName()),
                        (float)System.Convert.ToSingle(E[1]),
                        (float)System.Convert.ToSingle(E[2]),
                        (float)System.Convert.ToSingle(E[3]),
                        1);
                    mat.SetPower(glob.GetMat("Mat" + Name + Unit.GetMeshName()), (float)System.Convert.ToSingle(P[1]));

                    Unit.SetMaterial(glob.GetMat("Mat" + Name + Unit.GetMeshName()));
        }
        public void ShowUnitBoundingBox(bool isShow)
        {

            if (Unit != null)
            {

                if (isShow)
                {

                    Unit.ShowBoundingBox(true );

                }
                else
                {
                    Unit.ShowBoundingBox(false );

                }

            }
            else
            {


                throw new System.ArgumentException("BB fale unit is not declared !");
            }

        }
        public bool  CheckMousePick(int Mx, int My, ref TVScene Scene)
        {
            TVCollisionResult CollisionResult;
            bool Ret = false;

            CollisionResult = Scene.MousePick(Mx, My,
                (int)CONST_TV_OBJECT_TYPE.TV_OBJECT_MESH,
                CONST_TV_TESTTYPE.TV_TESTTYPE_BOUNDINGBOX);
            // Check if we did have a collision with a tank
            if (CollisionResult.IsCollision())
            {


               
                    TVMesh colMesh;
                    TV_3DVECTOR Tankpos;
                    TV_3DVECTOR ColMeshpos;

                    colMesh = CollisionResult.GetCollisionMesh();
                    Tankpos = Unit.GetPosition();
                    ColMeshpos = colMesh.GetPosition();

                    if ((Tankpos.x == ColMeshpos.x) && (Tankpos.z == ColMeshpos.z))
                    {
                        Ret = true;
                        
                    }


                




            }


            return Ret;

        }

        

        public  void SaveUnitsToFile(string FName)
        {
            try
            {
                //pick whatever filename with .xml extension
                string filename = FName + ".xml";
                  string RelativeToP;

                  RelativeToP = Directory.GetCurrentDirectory();

                XmlDocument xmlDoc = new XmlDocument();

                try
                {
                    xmlDoc.Load(filename);
                    //xmlDoc.CreateComment(filename);
                }
                catch (System.IO.FileNotFoundException)
                {
                    //if file is not found, create a new xml file
                    XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
                    xmlWriter.Formatting = Formatting.Indented;
                    xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
                    xmlWriter.WriteStartElement("Root");
                    xmlWriter.Close();
                    xmlDoc.Load(filename);
                }
                XmlNode root = xmlDoc.DocumentElement;
                XmlElement childNode = xmlDoc.CreateElement("Unit");
                XmlElement childNode2 = xmlDoc.CreateElement("Paths");
                XmlElement childNode3 = xmlDoc.CreateElement("Position");
                XmlElement childNode4 = xmlDoc.CreateElement("Scale");
                XmlElement childNode5 = xmlDoc.CreateElement("Rotation");
                XmlElement childNode6 = xmlDoc.CreateElement("Materijal");
                XmlElement childNode7 = xmlDoc.CreateElement("Shader");

                //XmlText textNode = xmlDoc.CreateTextNode("hello");
                //textNode.Value = "hello, world";

                root.AppendChild(childNode);
                childNode.AppendChild(childNode2);
                childNode2.SetAttribute("NAME", UnitName );
                //ToRelative(RelativeToP, GPathToModel)
                childNode2.SetAttribute("MODEL", ToRelative( RelativeToP,GPathToModel  ));

                childNode2.AppendChild(childNode3);

                childNode3.SetAttribute("X", Unit.GetPosition().x.ToString());
                childNode3.SetAttribute("Y", Unit.GetPosition().y.ToString());
                childNode3.SetAttribute("Z", Unit.GetPosition().z.ToString());

                childNode2.AppendChild(childNode4);

                childNode4.SetAttribute("X", Unit.GetScale().x.ToString());
                childNode4.SetAttribute("Y", Unit.GetScale().x.ToString());
                childNode4.SetAttribute("Z", Unit.GetScale().x.ToString());
                

                childNode2.AppendChild(childNode5);

                childNode5.SetAttribute("X", Unit.GetRotation().x.ToString());
                childNode5.SetAttribute("Y", Unit.GetRotation().x.ToString());
                childNode5.SetAttribute("Z", Unit.GetRotation().x.ToString());
                

                //textNode.Value = "replacing hello world";
                xmlDoc.Save(filename);
            }
            catch (Exception ex)
            {
                throw new System.ArgumentException("GRESKA tip : global u spremanju jedinice u file : " + ex.Message.ToString());
            } 
        }

        public void SetUnitMatrix(TV_3DMATRIX Matrix)
        {

            Unit.SetMatrix(Matrix);

        }

       

        private string ToRelative(string absolutePath, string relativeTo)
        {
            StringBuilder relativePath = new StringBuilder();


            try
            {

                string[] absoluteDirectories = absolutePath.Split('\\');
                string[] relativeDirectories = relativeTo.Split('\\');

                //Get the shortest of the two paths
                int length = absoluteDirectories.Length < relativeDirectories.Length ? absoluteDirectories.Length : relativeDirectories.Length;

                //Use to determine where in the loop we exited
                int lastCommonRoot = -1;
                int index;

                //Find common root
                for (index = 0; index < length; index++)
                    if (absoluteDirectories[index] == relativeDirectories[index])
                        lastCommonRoot = index;
                    else
                        break;

                //If we didn't find a common prefix then throw
                if (lastCommonRoot == -1)
                    throw new ArgumentException("Paths do not have a common base");

                //Build up the relative path


                //Add on the ..
                for (index = lastCommonRoot + 1; index < absoluteDirectories.Length; index++)
                    if (absoluteDirectories[index].Length > 0)
                        relativePath.Append("..\\");

                //Add on the folders
                for (index = lastCommonRoot + 1; index < relativeDirectories.Length - 1; index++)
                    relativePath.Append(relativeDirectories[index] + "\\");
                relativePath.Append(relativeDirectories[relativeDirectories.Length - 1]);
            }
            catch (Exception e)
            {

                return relativeTo;


            }

            return relativePath.ToString();
        }

        

        public TV_3DVECTOR GetUnitRotate()
        {

            return Unit.GetRotation();
        }
        public TV_3DMATRIX GetUnitRotateMatrix()
        {

            return Unit.GetRotationMatrix();

        }

        public TV_3DMATRIX GetMatrix()
        {

            return Unit.GetMatrix();
        }

        public void DrawCoordinateSystem( float Length)
        {
            Screen2DImmediate = new TVScreen2DImmediate();
            TV_3DMATRIX mtxMesh;
            mtxMesh = Unit.GetMatrix();
            TV_3DVECTOR vecLookAt = new TV_3DVECTOR(mtxMesh.m31, mtxMesh.m32, mtxMesh.m33);

            TVMathLibrary Math = new TVMathLibrary();


            vecLookAt.x = vecLookAt.x + Math.ACos(Unit.GetRotation().y) * Length;
            vecLookAt.y = vecLookAt.y + Math.ASin(Unit.GetRotation().x) * Length;
            vecLookAt.z = vecLookAt.z + Math.ASin(Unit.GetRotation().y) * Length;

            Screen2DImmediate.Draw_Line3D(Unit.GetPosition().x, Unit.GetPosition().y,
                Unit.GetPosition().z, vecLookAt.x, vecLookAt.y, vecLookAt.z, 0, 1);





        }

         public void SetUnitPosition(TV_3DVECTOR V, ref TVPhysics TVP)
        {
            Unit.SetPosition(V.x, V.y, V.z);
            TVP.SetBodyPosition(RigidBodyIndex, V.x, V.y, V.z);

        }
         public void SetUnitPosition(float X, float Y, float Z, ref TVPhysics TVP)
        {
            Unit.SetPosition(X, Y, Z);
            TVP.SetBodyPosition(RigidBodyIndex, X, Y, Z);

        }

    
        public void SetUnitRotation(TV_3DVECTOR r, float time, ref TVPhysics TVP)
        {
            if (Unit != null)
            {
                Unit.SetRotation(r.x * time , r.y * time , r.z * time );
                TVP.SetBodyRotation(RigidBodyIndex, r.x * time, r.y * time, r.z * time);

            }

        }

        public void SetUnitRotation(float x, float y, float z, float time,ref TVPhysics TVP)
        {
            if (Unit != null)
            {
                Unit.SetRotation(x * time , y * time , z *time );
                TVP.SetBodyRotation(RigidBodyIndex, x * time, y * time, z * time);


            }

        }


        public void SetUnitRotation(TV_3DVECTOR r, ref TVPhysics TVP)
        {
            if (Unit != null)
            {
                Unit.SetRotation(r.x,r.y,r.z );
                TVP.SetBodyRotation(RigidBodyIndex, r.x, r.y, r.z);


            }

        }

        public void SetUnitRotation(float x, float y, float z, ref TVPhysics TVP)
        {
            if (Unit != null)
            {
                Unit.SetRotation(x, y, z);
                TVP.SetBodyRotation(RigidBodyIndex, x, y, z);


            }

        }


        public void SetUnitScale(float x, float y, float z, ref TVPhysics TVP)
        {
            if (Unit != null)
            {

                Unit.SetScale(x, y, z);
                

            }
            else
            {


                throw new System.ArgumentException("Scale fale unit is not declared !");
            }

        }


        public void Update()
        {
            Unit.Update();


        }

        public void SetVisiable(bool IsV)
        {

            


        }

        public void AddPhysicxJoint(ref TVPhysics Phy,int attcahBody,TV_3DVECTOR ws,TV_3DVECTOR hs)
        {

           int joint = Phy.CreateHingeJoint(attcahBody, RigidBodyIndex, ws, hs);
            

        }


        
    }
    
}

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)

Share

About the Author

Member 3873633
Software Developer
Croatia Croatia
Programer

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Member 3873633
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid