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
A game engine first prototype
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MTV3D65;

namespace gefx
{
    public  class SaveUnit
    {

        public string RelativeToP;

        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 void WriteUnitFile(string Path, cUnit Unit, string Name)
        {

            TextWriter tw = new StreamWriter(Path);

            //RelativeToP = Directory.GetDirectoryRoot(Path);

            RelativeToP = Directory.GetCurrentDirectory();
            tw.WriteLine("Sino unit file " + DateTime.Today.ToString());
            tw.WriteLine("Name=" + Name);

            foreach (ModelAnim.MeshSceneInfo i in Unit.Modeli.MeshInfo)
            {

                if (i.Name == Name)
                {
                    try
                    {

                        if (i.Path.Length > 5)
                        {

                            tw.WriteLine("Mesh=" + ToRelative(RelativeToP, i.Path));
                        }
                    }

                    catch (Exception e)
                    {

                    }
                    break;

                }
            }

            foreach (ModelAnim.MeshSceneInfo i in Unit.ModeliAnimirani.MeshInfo )
            {

                if (i.Name == Name)
                {
                    try
                    {
                        if (i.Path.Length > 5)
                        {
                            tw.WriteLine("Mesh=" + ToRelative(RelativeToP, i.Path));
                        }
                    }
                    catch (Exception x)
                    {


                    }


                    break;

                }
            }

            tw.WriteLine("Position=" + Unit.Modeli.GetMeshPosition(Name).x.ToString() + ";" + 
                Unit.Modeli.GetMeshPosition(Name).y.ToString()
                + ";" + Unit.Modeli.GetMeshPosition(Name).z.ToString());

            

            tw.WriteLine("Rotacija=" + Unit.Modeli.GetMeshRotation(Name).x.ToString() + ";" +
                Unit.Modeli.GetMeshRotation(Name).y.ToString() 
                + ";" + Unit.Modeli.GetMeshRotation(Name).z.ToString());

            tw.WriteLine("Scale=" + Unit.Modeli.GetModelScale(Name ).x.ToString () + ";" +

                Unit.Modeli.GetModelScale(Name ).y.ToString () 
                + ";" + Unit.Modeli.GetModelScale(Name ).z.ToString ());


            tw.WriteLine("Position=" + Unit.ModeliAnimirani.GetMeshPosition(Name).x.ToString() + ";" +
                            Unit.ModeliAnimirani.GetMeshPosition(Name).y.ToString()
                            + ";" + Unit.ModeliAnimirani.GetMeshPosition(Name).z.ToString());



            tw.WriteLine("Rotacija=" + Unit.ModeliAnimirani.GetMeshRotation(Name).x.ToString() + ";" +
                Unit.ModeliAnimirani.GetMeshRotation(Name).y.ToString()
                + ";" + Unit.ModeliAnimirani.GetMeshRotation(Name).z.ToString());

            tw.WriteLine("Scale=" + Unit.ModeliAnimirani.GetModelScale(Name).x.ToString() + ";" +

                Unit.ModeliAnimirani.GetModelScale(Name).y.ToString()
                + ";" + Unit.ModeliAnimirani.GetModelScale(Name).z.ToString());


            foreach (ModelAnim.MeshSceneInfo i in Unit.Modeli.MeshInfo)
            {

                if (i.Name == Name)
                {
                    tw.WriteLine("Mat=" + ToRelative(RelativeToP, i.MaterijalFile));
                    break;

                }
            }

            foreach (ModelAnim.MeshSceneInfo i in Unit.ModeliAnimirani.MeshInfo)
            {

                if (i.Name == Name)
                {
                    tw.WriteLine("Mat=" + ToRelative(RelativeToP, i.MaterijalFile));
                    break;

                }
            }


            tw.Close();
        }

        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 = param.Substring(param.Length - length, length);
            //return the result of the operation
            return result;
        }

        public void LoadUnitFile(string File, ref cUnit Units, ref gefxs GE)
        {
            string[] para = new string[19];
            string line;
            int counter = 0;
            TextReader tr = new StreamReader(File);
            while ((line = tr.ReadLine()) != null)
            {
                para[counter] = line;
                counter++;
            }
            

            //T.AddHeightMap(para[2].Split(
            string[] ime = para[1].Split('=');
            string[] path = para[2].Split('=');
            string[] pozicija = para[3].Split('=');
            string[] rotacija =para[4].Split('=');
            string[] scale = para[5].Split('=');

            string Types = Right(path[1].Trim(),3);






            string[] p = pozicija[1].Split(';');

            float x = System.Convert.ToSingle(p[0]);
            float y = System.Convert.ToSingle(p[1]);
            float z = System.Convert.ToSingle(p[2]);


            TV_3DVECTOR v = new TV_3DVECTOR(x, y, z);

            string[] p1 = rotacija[1].Split(';');

            float x1 = System.Convert.ToSingle(p1[0]);
            float y1 = System.Convert.ToSingle(p1[1]);
            float z1 = System.Convert.ToSingle(p1[2]);


            TV_3DVECTOR v1 = new TV_3DVECTOR(x1, y1, z1);

            string[] p2 = scale[1].Split(';');

            float x2 = System.Convert.ToSingle(p2[0]);
            float y2 = System.Convert.ToSingle(p2[1]);
            float z2 = System.Convert.ToSingle(p2[2]);


           

                string[] pozicijas = para[6].Split('=');
                string[] rotacijas = para[7].Split('=');
                string[] scales = para[8].Split('=');


                string[] ps = pozicijas[1].Split(';');

                float xs = System.Convert.ToSingle(ps[0]);
                float ys = System.Convert.ToSingle(ps[1]);
                float zs = System.Convert.ToSingle(ps[2]);


                TV_3DVECTOR vs = new TV_3DVECTOR(xs, ys, zs);

                string[] p1s = rotacijas[1].Split(';');

                float x1s = System.Convert.ToSingle(p1s[0]);
                float y1s = System.Convert.ToSingle(p1s[1]);
                float z1s = System.Convert.ToSingle(p1s[2]);


                TV_3DVECTOR v1s = new TV_3DVECTOR(x1s, y1s, z1s);

                string[] p2s = scales[1].Split(';');

                float x2s = System.Convert.ToSingle(p2[0]);
                float y2s = System.Convert.ToSingle(p2[1]);
                float z2s = System.Convert.ToSingle(p2[2]);
                string[] MatAnim= new string [2];
                string[] MatStatic = new string [2];

                bool isMat = false;
                bool isMatD = false;
                try
                {

                    MatStatic = para[10].Split('=');
                    
                     isMat = true;
                }
                catch (Exception e)
                {

                    isMat = false;
                }


                try
                {

                    
                    MatAnim = para[9].Split('=');
                    isMatD = true;

                    if (MatAnim[1].Length <= 5)
                    {

                        isMatD = false;

                    }

                }
                catch (Exception e)
                {

                    isMatD = false;
                }

            



            switch (Types)
            {

                case "tva":

                    

                    Units.AddcUnit(ime[1], cUnit.MeshType.TVA, path[1], ref GE);
                    Units.ModeliAnimirani.SetMeshPosition(vs, ime[1]);
                    Units.ModeliAnimirani.SetMeshRotation(v1s, ime[1]);
                    Units.ModeliAnimirani.SetModelScale(x2s, y2s, z2s, ime[1]);

                    if (isMatD)
                    {

                        Units.ModeliAnimirani.AddMaterialToModel(MatAnim[1], ime[1], ref GE.Materijali, ref GE.Globals);
                    }

                    break;

                case "TVA":
                    Units.AddcUnit(ime[1], cUnit.MeshType.TVA, path[1], ref GE);
                    Units.ModeliAnimirani.SetMeshPosition(vs, ime[1]);
                    Units.ModeliAnimirani.SetMeshRotation(v1s, ime[1]);
                    Units.ModeliAnimirani.SetModelScale(x2s, y2s, z2s, ime[1]);
                    if (isMatD)
                    {
                        Units.ModeliAnimirani.AddMaterialToModel(MatAnim[1], ime[1], ref GE.Materijali, ref GE.Globals);

                    }
                    break;

                case "tvm":
                    Units.AddcUnit(ime[1], cUnit.MeshType.TVM, path[1], ref GE);
                    Units.Modeli.SetMeshPosition(v, ime[1]);
                    Units.Modeli.SetMeshRotation(v1, ime[1]);
                    Units.Modeli.SetModelScale(x2, y2, z2, ime[1]);
                    if (isMat)
                    {

                        Units.Modeli.AddMaterialToModel(MatStatic[1], ime[1], ref GE.Materijali, ref GE.Globals);
                    }

                    break;

                case "TVM":
                    Units.AddcUnit(ime[1], cUnit.MeshType.TVM, path[1], ref GE);
                    Units.Modeli.SetMeshPosition(v, ime[1]);
                    Units.Modeli.SetMeshRotation(v1, ime[1]);
                    Units.Modeli.SetModelScale(x2, y2, z2, ime[1]);
                    if (isMat)
                    {
                        Units.Modeli.AddMaterialToModel(MatStatic[1], ime[1], ref GE.Materijali, ref GE.Globals);
                    }

                    break;

                case "x":
                    Units.AddcUnit(ime[1], cUnit.MeshType.DirectX, path[1], ref GE);
                    Units.Modeli.SetMeshPosition(v, ime[1]);
                    Units.Modeli.SetMeshRotation(v1, ime[1]);
                    Units.Modeli.SetModelScale(x2, y2, z2, ime[1]);
                    if (isMat)
                    {
                        Units.Modeli.AddMaterialToModel(MatStatic[1], ime[1], ref GE.Materijali, ref GE.Globals);
                    }

                    break;

                case "X":
                    Units.AddcUnit(ime[1], cUnit.MeshType.DirectX, path[1], ref GE);
                    Units.Modeli.SetMeshPosition(v, ime[1]);
                    Units.Modeli.SetMeshRotation(v1, ime[1]);
                    Units.Modeli.SetModelScale(x2, y2, z2, ime[1]);
                    if (isMat)
                    {
                        Units.Modeli.AddMaterialToModel(MatStatic[1], ime[1], ref GE.Materijali, ref GE.Globals);
                    }

                    break;


            }





            tr.Close();
        }

    }
}

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)

About the Author

Member 3873633
Software Developer
Croatia Croatia
Programer

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