Click here to Skip to main content
15,896,606 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.6K   1.5K   34  
A game engine first prototype
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;


using Microsoft.DirectX.Direct3D;


using gefxem;
using MTV3D65;


namespace gefx
{
   public  class cUnit
    {
       public enum UnitType
       {
           Ally,
           Enemy,
           Object,
           FreeUnit

       };

       public enum MeshType
       {
           TVM,
           TVA,
           DirectX,
           AnimDirectX

       };



      public  gefxem.ExplodeMesh EksplozijaModela = new ExplodeMesh();

       public gefx.Object Objekti = new gefx.Object();
       public Model Modeli = new Model();
       public ModelAnim ModeliAnimirani = new ModelAnim();

       string UnitName;
       string UnitMeshPath;

       public void SetUnitMeshPath(string path)
       {
           UnitMeshPath = path;

       }

       public string GetUnitMeshType()
       {

           return UnitMeshPath;

       }

       public cUnit(string name, ref gefxs GE)
       {
           SetUnitName(name);

       }

       public cUnit()
       {

       }

       public string GetUnitName()
       {
           return UnitName;

       }

       public void SetUnitName(string Name)
       {
           UnitName = Name;

       }

       public void  AddcUnit(string Name, UnitType Type,MeshType MType,string PathMesh, ref gefx.gefxs GE)
       {

           SetUnitMeshPath(PathMesh);
           SetUnitName(Name);

           switch (MType)
           {
               case MeshType.TVM :


                   Modeli.AddModel(ref  GE.Scene,ref  GE.Globals, Model.ModelType.TVM, Name, PathMesh);
                   break;

               case MeshType.TVA :

                   ModeliAnimirani.AddModel(ref GE.Scene, ref GE.Globals, ModelAnim.ModelActorType.TVA, Name, PathMesh);
                   break;

               case MeshType.DirectX :
                   Modeli.AddModel(ref GE.Scene, ref GE.Globals, Model.ModelType.StaticX,Name ,PathMesh);
                   break;

               case MeshType.AnimDirectX :
                   ModeliAnimirani.AddModel(ref GE.Scene, ref GE.Globals, ModelAnim.ModelActorType.AnimatedX , Name, PathMesh);
                   break;

               default :
                   Modeli.AddModel(ref  GE.Scene, ref  GE.Globals, Model.ModelType.TVM, Name, PathMesh);

                   break;


           }

       }
       UnitType UnitTypes;
       public void SetUnitType(UnitType Type)
       {
           UnitTypes = Type;
       }

       public UnitType GetUnitType()
       {

           return UnitTypes;
       }


       public void AddcUnit(string Name,  MeshType MType, string PathMesh, ref gefx.gefxs GE)
       {

           SetUnitMeshPath(PathMesh);
           SetUnitName(Name);

           switch (MType)
           {
               case MeshType.TVM:


                   Modeli.AddModel(ref  GE.Scene, ref  GE.Globals, Model.ModelType.TVM, Name, PathMesh);
                   break;

               case MeshType.TVA:

                   ModeliAnimirani.AddModel(ref GE.Scene, ref GE.Globals, ModelAnim.ModelActorType.TVA, Name, PathMesh);
                   break;

               case MeshType.DirectX:
                   Modeli.AddModel(ref GE.Scene, ref GE.Globals, Model.ModelType.StaticX, Name, PathMesh);
                   break;

               case MeshType.AnimDirectX:
                   ModeliAnimirani.AddModel(ref GE.Scene, ref GE.Globals, ModelAnim.ModelActorType.AnimatedX, Name, PathMesh);
                   break;

               default:
                   Modeli.AddModel(ref  GE.Scene, ref  GE.Globals, Model.ModelType.TVM, Name, PathMesh);

                   break;


           }

       }

       public void DrawUnit(string UnitName)
       {
           Modeli.DrawModels(UnitName);
           ModeliAnimirani.DrawModels(UnitName);

          

       }

       public void MoveUnit(TV_3DVECTOR Point,string Name)
       {
           Modeli.SetMeshPosition(Point, Name);
           ModeliAnimirani.SetMeshPosition(Point, Name);


       }

       public void RotateUnit(float x, float y, float z, string UnitName)
       {
           Modeli.RotateModel(x, y, z, UnitName);
           ModeliAnimirani.RotateModel(x, y, z, UnitName);


       }

       public string PickUnit(int Mx,int My, ref  TVScene Scene)
       {
           string r = "";
           r = Modeli.CheckMousePick(Mx, My, ref  Scene);
           return r;

       }
       public string PickUnitActor(int Mx, int My, ref  TVScene Scene)
       {
           string r = "";
           r = ModeliAnimirani.CheckMousePick(Mx, My, ref  Scene);
           return r;

       }

       public void ShowBB(string Name,bool f)
       {
           Modeli.ShowBoundingBox(Name,f);
           ModeliAnimirani.ShowBoundingBox(Name,f);

       }

       public void RemoveUnits(string Name,ref TVScene sc)
       {
           Modeli.RemoveModel(Name,ref  sc);
           ModeliAnimirani.RemoveModel(Name);

       }

       public void ExplodeUnit(string Name, float Power, int Part,ref TVGlobals GL,ref TVScene Scene)
       {



           InitMesh(Modeli.GetRefMesh(Name));
           ExplodeMesh(Modeli.GetModelID(Name), Part, ref MeshPart, ref GL, ref Scene, Modeli.GetRefMesh(Name));
           InitVelocities(MeshPart,ref  MeshVel,ref  MeshRot);

           MeshTriangles = 0;
           mt = 0;
           //MoveMeshPart(ref  GL, true);
           

       }

       public void MoveMeshPart(ref TVGlobals GL, bool f)
       {
           if(f) 
           {
               MoveParts(ref MeshPart, ref MeshVel, ref MeshRot, ref GL);

           }
       }


        #region ExplodeMesh
       private TVMathLibrary TVMath;
       private MTV3D65.TVMesh[] MeshPart;
       private MTV3D65.TV_3DVECTOR[] MeshVel;
       private MTV3D65.TV_3DVECTOR[] MeshRot;
       //private TVGlobals Tvg;
       private bool Running;
       private int LightID;
       private float mt;
       private Point Scr;
       private float Dist;
       private PointF Ang;
       private int MeshTriangles;
       private int ParticlesID;
       private int SmokeParticleID;
       private float Gravity = 9.8f;
       private const float pi = 3.14159265358979f;

       public bool Explode = false;

       public float ExplodePower = 5.0f;

       public TV_3DVECTOR VectorMid(TV_3DVECTOR Vec1, TV_3DVECTOR Vec2)
       {
           TV_3DVECTOR RetVec = default(TV_3DVECTOR);

           RetVec.x = (Vec1.x + Vec2.x) / 2;
           RetVec.y = (Vec1.y + Vec2.y) / 2;
           RetVec.z = (Vec1.x + Vec2.z) / 2;
           return RetVec;
       }
       private void InitMesh(TVMesh Mesh)
       {
           TV_3DVECTOR Min = default(TV_3DVECTOR);
           TV_3DVECTOR Max = default(TV_3DVECTOR);



           Mesh.ComputeNormals();
           Mesh.ComputeBoundings();
           Mesh.GetBoundingBox(ref Min, ref Max, false);

           MeshTriangles = Mesh.GetTriangleCount();
       }
       private void InitVelocities(TVMesh[] Mesh, ref TV_3DVECTOR[] Vec, ref TV_3DVECTOR[] Rot)
       {
           int i = 0;
           float P = 0;

           if ((Mesh != null))
           {
               if (Mesh.Length >= 0)
               {
                   Vec = new TV_3DVECTOR[Mesh.Length + 1];
                   Rot = new TV_3DVECTOR[Mesh.Length + 1];
                   P = ExplodePower * (1 / 2);
                   for (i = 0; i <= Mesh.Length; i++)
                   {
                       VBMath.Randomize();
                       Vec[i].x = (VBMath.Rnd() * P) - (P / 2);
                       Vec[i].y = (VBMath.Rnd() * (P * 2)) - (P / 2);
                       Vec[i].z = (VBMath.Rnd() * P) - (P / 2);
                       Rot[i].x = (VBMath.Rnd() * (pi / 16)) - (pi / 32);
                       Rot[i].y = (VBMath.Rnd() * (pi / 16)) - (pi / 32);
                       Rot[i].z = (VBMath.Rnd() * (pi / 16)) - (pi / 32);
                   }
               }
           }
       }
       private void MoveParts(ref TVMesh[] Parts, ref TV_3DVECTOR[] MeshVel, ref TV_3DVECTOR[] MeshRot,
           ref TVGlobals Tvg)
       {
           int i = 0;
           bool b = false;
           float t = 0;
           TV_3DVECTOR Vec = new TV_3DVECTOR(0, 0, 0);
           TV_3DVECTOR Mov = new TV_3DVECTOR(1, 1, 1);

           b = false;
           t = mt * 0.1f;
           if ((Parts != null))
           {
               if (Parts.Length >= 0)
               {
                   for (i = 0; i <= Parts.Length; i++)
                   {
                       if (Parts[i].GetTriangleCount() > 0)
                       {
                           Vec = Parts[i].GetPosition();
                           TVMath.TVVec3Scale(ref Mov, MeshVel[i], t);
                           TVMath.TVVec3Add(ref Vec, Vec, Mov);
                           Parts[i].SetPosition(Vec.x, Vec.y, Vec.z);
                           Parts[i].RotateX(MeshRot[i].x * t, true);
                           Parts[i].RotateY(MeshRot[i].y * t, true);
                           Parts[i].RotateZ(MeshRot[i].z * t, true);
                           TVMath.TVVec3Add(ref MeshVel[i], MeshVel[i], Tvg.Vector(0, -Gravity * 0.1f * t, 0));
                           if ((!Parts[i].IsVisible()))
                           {
                               Parts[i].Destroy();
                           }
                           else
                           {
                               b = true;
                           }
                       }
                   }
               }
           }
           //if ((!b) & MeshTriangles == 0) InitMesh();
       }
       private void ExplodeMesh(int MeshIdx, int NumParts, ref TVMesh[] Parts, ref TVGlobals Tvg, ref TVScene Scene, TVMesh Mesh)
       {
           TVInternalObjects TVIO = null;
           TVMesh TmpTVMsh = null;
           Mesh D3DMsh = null;
           System.Array IndBuf = null;
           int[] MatAry = null;
           TV_SVERTEX[] PartVrt = null;
           int[] PartMat = null;
           int[] PartInd = null;
           int MatNum = 0;
           bool[] MatChk = null;
           int[] MatRemap = null;
           int[] TexRemap = null;
           TV_3DVECTOR Min = default(TV_3DVECTOR);
           TV_3DVECTOR Max = default(TV_3DVECTOR);
           TV_3DVECTOR Vec = default(TV_3DVECTOR);
           TV_3DVECTOR v = default(TV_3DVECTOR);
           int C = 0;
           int St = 0;
           int Ed = 0;
           int fv = 0;
           int n = 0;
           int i = 0;

           TmpTVMsh = Tvg.GetMeshFromID(MeshIdx);
           TVIO = new TVInternalObjects();
           D3DMsh = new Mesh(TVIO.GetD3DMesh(MeshIdx));
           int[] Rk = null;
           Rk = new int[2];
           Rk[0] = TmpTVMsh.GetTriangleCount();
           Rk[1] = 3;
           IndBuf = D3DMsh.LockIndexBuffer(typeof(short), LockFlags.ReadOnly, Rk);
           MatAry = D3DMsh.LockAttributeBufferArray(LockFlags.ReadOnly);
           D3DMsh.UnlockIndexBuffer();
           D3DMsh.UnlockAttributeBuffer();
           C = (int)(TmpTVMsh.GetTriangleCount() / NumParts);
           Parts = new TVMesh[NumParts];
           MatRemap = new int[1];
           TexRemap = new int[1];
           for (i = 0; i <= Parts.Length - 1; i++)
           {
               Parts[i] = new TVMesh();
               Parts[i] = Scene.CreateMeshBuilder(TmpTVMsh.GetMeshName() + "_Part" + i.ToString());
               St = i * C;
               Ed = ((i + 1) * C) - 1;
               if (St >= TmpTVMsh.GetTriangleCount() - 1) St = TmpTVMsh.GetTriangleCount() - 1;
               if (Ed >= TmpTVMsh.GetTriangleCount() - 1 | i == Parts.Length) Ed = TmpTVMsh.GetTriangleCount() - 1;
               PartVrt = new TV_SVERTEX[(((Ed - St) + 1) * 3)];
               PartInd = new int[(((Ed - St) + 1) * 3)];
               PartMat = new int[Ed - St + 1];
               MatChk = new bool[TmpTVMsh.GetGroupCount()];
               MatNum = 0;

               try
               {

                   for (n = St; n <= Ed; n++)
                   {
                       for (fv = 0; fv <= 2; fv++)
                       {
                           {
                               float x1 = 0;
                               float x2 = 0;
                               int x3 = 1;

                               TmpTVMsh.GetVertex((int)IndBuf.GetValue(n, fv),
                                   ref PartVrt[((n - St) * 3) + fv].x,
                                   ref PartVrt[((n - St) * 3) + fv].y,
                                   ref PartVrt[((n - St) * 3) + fv].z,
                                   ref PartVrt[((n - St) * 3) + fv].nx,
                                   ref PartVrt[((n - St) * 3) + fv].ny,
                                   ref PartVrt[((n - St) * 3) + fv].nz,
                                   ref PartVrt[((n - St) * 3) + fv].tu,
                                   ref PartVrt[((n - St) * 3) + fv].tv,
                                   ref x1,
                                    ref x2,
                                    ref x3);
                               TVMath.TVVec3TransformCoord(ref v, Tvg.Vector(PartVrt[((n - St) * 3) + fv].x, PartVrt[((n - St) * 3) + fv].y, PartVrt[((n - St) * 3) + fv].z), TmpTVMsh.GetMatrix());
                               PartVrt[((n - St) * 3) + fv].x = v.x;
                               PartVrt[((n - St) * 3) + fv].y = v.y;
                               PartVrt[((n - St) * 3) + fv].z = v.z;
                           }
                           PartInd[((n - St) * 3) + fv] = ((n - St) * 3) + fv;
                       }
                       if (!MatChk[MatAry[n]])
                       {
                           Array.Resize(ref MatRemap, MatNum + 1);
                           Array.Resize(ref TexRemap, MatNum + 1);


                           MatRemap[MatNum] = Mesh.GetMaterial(MatAry[n]);
                           TexRemap[MatNum] = Mesh.GetTexture(MatAry[n]);
                           MatChk[MatAry[n]] = true;
                           MatNum = MatNum + 1;
                       }
                       PartMat[n - St] = MatNum - 1;
                   }
               }

               catch (Exception exa)
               {


               }
               Parts[i].SetGeometry(PartVrt, PartVrt.Length + 1, PartInd, Ed - St + 1, MatNum, PartMat);
               Parts[i].SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED);
               Parts[i].SetBlendingMode(CONST_TV_BLENDINGMODE.TV_BLEND_ALPHA);
               Parts[i].SetCullMode(CONST_TV_CULLING.TV_DOUBLESIDED);
               Parts[i].ComputeNormals();
               Parts[i].ComputeBoundings();
               Parts[i].GetBoundingBox(ref Min, ref Max, false);
               Vec = VectorMid(Min, Max);
               Parts[i].SetPosition(Vec.x, Vec.y, Vec.z);
               Parts[i].SetMeshCenter(Vec.x, Vec.y, Vec.z);
               for (n = 0; n <= MatNum - 1; n++)
               {
                   Parts[i].SetMaterial(MatRemap[n], n);
                   Parts[i].SetTexture(TexRemap[n], n);
               }
           }
           //Tvg.GetMeshFromID(MeshIdx).Destroy();
           TmpTVMsh = null;
       }
        
        #endregion

    }
}

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