Click here to Skip to main content
11,494,407 members (63,986 online)
Click here to Skip to main content
Articles » Multimedia » DirectX » Games » Downloads
Add your own
alternative version

Game Engine

, 20 Nov 2009 CPL 24.7K 1.3K 33
A game engine first prototype
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using MTV3D65;
using IrrKlang; 


namespace gefx
{
    public class gefxs
    {
        public  TVEngine TV;        //This is the main engine class.
        public  TVScene Scene;      //This is used for instantiating resource types - Geometry, Render Targets, Shaders and so on.
        public  TVGlobals Globals;

        public TVTextureFactory TextureFactory;
        public TVMaterialFactory Materials;
        public TVGraphicEffect GraphicsFX;
        public TVLightEngine LightEngine;

        public Sounds Zvuk;
        public EXDecals Dekoli;
        public  float fTime;        //This is our tick value
        public  bool bLoop=true ;
        public EXVehicaleManager VMannager;
        public Terrain Teren;
        public Flor PodlogaTest;
        public XGEPhysX Fizika;
        public Fog Magla;
        public Object Objekti = new Object();
        //public Model Modeli= new Model();
        //public ModelAnim ModeliAnimirani = new ModelAnim();
        public cUnit Units = new cUnit();
        public cUnitManager Man = new cUnitManager();
        public TVScreen2DImmediate Screen;
        public TVTextureFactory Tex;
        public Clouds Oblaci;
        public bool SimuLatePhysics = false;
        public bool bDrawCoordinateSystem = false;
        public TVScreen2DText Text2D = new TVScreen2DText();
        public Camera Kamera = new Camera();
        public Light Svijetlo;
        public UserInput KorisnikUlaz;
        public Effects Efekti = new Effects();
        public Particles Partikali = new Particles();
        ISoundEngine SoundEngine = new ISoundEngine();
        private TVRenderSurface Reflect;    //Render target for reflection.
        private TVRenderSurface Refract;    //Render target for refraction.
        public TVMaterialFactory Materijali;
        public Watter Voda;
        public SkyBox SkyBoxs = new SkyBox();
        public TV_3DVECTOR GetMouseLandPosition;
        public TV_3DVECTOR GetMouseSpacePosiion;
        public  XTMannagerActor XActor = new XTMannagerActor();
        public XTMannagerStatic XModel = new XTMannagerStatic();
        public PickUpRect RectSelect = new PickUpRect();
        public XCamera Kamera2;
        public void AddBasicObject(Object.ObjectType Type, string Name)
        {
            Objekti.AddObject(ref Scene, ref Globals, Type, Name);

            


        }
        public void AddBasicObjectBox(Object.ObjectType Type, string Name,Object.Dimensions Dim)
        {
            Objekti.AddObject(ref Scene, ref Globals, Type, Name,Dim );

        }
        public void DrawCoordinateSystem()
        {


            Screen.Draw_Line3D(0, 0, 0, 1000, 0, 0, Globals.RGBA(1, 0, 0, 1));
            Screen.Draw_Line3D(0, 0, 0, 0, 1000, 0, Globals.RGBA(0, 1, 0, 1));
            Screen.Draw_Line3D(0, 0, 0, 0, 0, 1000, Globals.RGBA(0, 0, 1, 1));

            
            

        }
        public void AddWater(string Tex,TVLandscape Land)
        {
            Voda = new Watter();
            Voda.AddWatter(ref Scene, ref Reflect, ref Refract, Tex, Land,ref Globals );
            //Voda.AddRenderSurface(ref Scene, ref Reflect, ref Refract);

        }
        public void AddSkyBox(string SkyTop, string SkyBottom, string SkyLeft, string SkyRight,
            string SkyFront, string SkyBack)
        {

            SkyBoxs.AddSkyBox(SkyTop, SkyBottom, SkyLeft, SkyRight, SkyFront, SkyBack, ref Globals);


        }
        public void AddParticles(string Name, string Path)
        {
            Partikali.AddParticles(ref Scene  ,Path, Name);

        }
        public void SetUpScene(IntPtr Handle,
             int wRes,
             int hRes,
             bool windowdornot,
             CONST_TV_MULTISAMPLE_TYPE AntialiasingType,
             bool ShowWinCurssor,
             bool EnableVSync)
        {
            //
            
            //TV.SetDebugFile(Application.StartupPath + "\\debug.txt"); 
            //TV.Init3DWindowed(hWnd, true);
            //TV.SetAngleSystem(CONST_TV_ANGLE.TV_ANGLE_DEGREE);
           
            //Scene.SetAutoTransColor((int )CONST_TV_COLORKEY.TV_COLORKEY_USE_ALPHA_CHANNEL);
            //Scene.SetDepthBuffer(CONST_TV_DEPTHBUFFER.TV_WBUFFER);
            //Scene.SetRenderMode(CONST_TV_RENDERMODE.TV_SOLID);
            //Scene.SetBackgroundColor(0, 0, 0);
            //Scene.SetTextureFilter(CONST_TV_TEXTUREFILTER.TV_FILTER_ANISOTROPIC);
            //Screen.Settings_SetTextureFilter(CONST_TV_TEXTUREFILTER.TV_FILTER_ANISOTROPIC);
           
         {
            
             TV = new TVEngine();
             Scene = new TVScene();
             Globals = new TVGlobals();
             Materijali = new TVMaterialFactory();
             GraphicsFX = new TVGraphicEffect();
             TextureFactory = new TVTextureFactory();
             Kamera2 = new XCamera();
             Tex = new TVTextureFactory();
             LightEngine = new TVLightEngine();
             TV.SetAngleSystem(CONST_TV_ANGLE.TV_ANGLE_DEGREE);
             TV.SetDebugMode(true);
                 TV.SetDebugFile(Application.StartupPath + "\\DebugLog.txt");
                 TV.DisplayFPS(true );
                 if (windowdornot)
                 {
                     TV.Init3DWindowed(Handle);
                 }
                 else
                 {
                     TV.Init3DFullscreen(wRes, hRes, 32, true , false );
                 }

                 if (AntialiasingType == CONST_TV_MULTISAMPLE_TYPE.TV_MULTISAMPLE_NONE)
                 {
                     TV.SetAntialiasing(false , CONST_TV_MULTISAMPLE_TYPE.TV_MULTISAMPLE_NONE);
                 }
                 else
                 {

                     TV.SetAntialiasing(true, AntialiasingType);
                 }

                 if (EnableVSync)
                 {

                     TV.SetVSync(true );
                 }
                 else
                 {

                     TV.SetVSync(false );


                 }
                 TV.ShowWinCursor(ShowWinCurssor);


              
                Scene.SetAutoTransColor((int )CONST_TV_COLORKEY.TV_COLORKEY_USE_ALPHA_CHANNEL);
                Scene.SetDepthBuffer(CONST_TV_DEPTHBUFFER.TV_WBUFFER);
                Scene.SetRenderMode(CONST_TV_RENDERMODE.TV_SOLID);
                Scene.SetBackgroundColor(0, 0, 0);
                Scene.SetTextureFilter(CONST_TV_TEXTUREFILTER.TV_FILTER_ANISOTROPIC);

                GraphicsFX.ChangeGamma(1.0f);
                TV.SetCamera(Kamera2._camera );
                Scene.SetMaxAnisotropy(16);
                TextureFactory.SetTextureMode(CONST_TV_TEXTUREMODE.TV_TEXTUREMODE_COMPRESSED);
                LightEngine.SetGlobalAmbient(0f, 0f, 0f);


         }

         Reflect = Scene.CreateRenderSurface(256, 256, true);
         Refract = Scene.CreateRenderSurface(256, 256, true);
         Efekti.InitMaterials(ref Materijali);
         Screen = new TVScreen2DImmediate();

            Svijetlo = new Light(ref  Scene, Light.LightType.pointlight, new TV_3DVECTOR(100, 100, -200));



            Kamera2._inputs.Initialize(true, true);

           
            Teren  = new Terrain();
            Fizika = new XGEPhysX();
            Tex.SetTextureMode(CONST_TV_TEXTUREMODE.TV_TEXTUREMODE_BETTER);
            Text2D.TextureFont_Create("font_tex1", "Arial", 6, false  , false , false , false );
            Text2D.TextureFont_Create("font_tex2", "Arial", 4, false , false, false, false, true );
            Oblaci = new Clouds();
            Magla = new Fog();
            Dekoli = new EXDecals();
            Zvuk = new Sounds();
            VMannager = new EXVehicaleManager();
        }

        public void DestroyAllAndExit()
        {
            Kamera = null;
            Kamera2 = null;
            XActor = null;
            XModel = null;
            Voda = null;
            Svijetlo = null;
            KorisnikUlaz = null;
            Partikali = null;
            Fizika = null;
            Teren = null;
            Globals = null;
            Scene.DestroyAllActors();
            Scene.DestroyAllLandscapes();
            Scene.DestroyAllMeshes();
            Scene.DestroyAllMinimeshes();
            Scene.DestroyAllParticleSystems();
            Oblaci  = null;
            Scene = null;
            Dekoli = null;
            TV.ReleaseAll();
            VMannager = null;
            TV = null;
        }
        float  ValueScrool;
        public void GameLoop()
        {
            while (bLoop)
            {
                fTime = TV.AccurateTimeElapsed();

                if (SimuLatePhysics)
                {
                    Fizika.PhysX.Simulate(TV.TimeElapsed() * 0.0025f);
                }
                if (Teren.Land == null)
                {
                    Kamera2.Update(TV.TimeElapsed());
                }
                else
                {
                    Kamera2.UpdateTerrainCamera(ref TV,  Teren.Land  );
                }
                GetMouseLandPosition = Teren.GetTerreinPosition(ref Scene, Kamera2.tmpMouseX1 ,Kamera2.tmpMouseY1 );
                UpdateScene();
                TV.Clear(false);
                Teren.DrawTerrain();
                if (SkyBoxs.Atmos != null)
                {
                    SkyBoxs.DrawSkyBox();
                }
                Svijetlo.RenderLight();
                XActor.CrtajJedinice("Sve",ref Text2D,ref Globals );
                XModel.CrtajJedinice("Sve");
                Scene.FinalizeShadows();
                Partikali.DrawParticles("All");
                if (PodlogaTest != null)
                {

                    PodlogaTest.DrawFlor();

                }

                if (Oblaci.Atmos != null )
                {

                    Oblaci.DrawClouds();

                }

                if (Magla.Enabled)
                {

                    Magla.DrawFog();

                }

                if (Voda != null)
                {
                    Voda.DrawWatter();
                }
               

                if (bDrawCoordinateSystem)
                {
                    DrawCoordinateSystem();
                }
                VMannager.DrawVehicale();
                VMannager.CheckInputs(Kamera2._inputs, ref Fizika.PhysX);

                Dekoli.RemoveDecals(TV);
                Dekoli.DrawDecals();
                Scene.RenderAllMeshes(true);
                Scene.RenderAllActors(true);
                if (Kamera2.tmpMouseB1)
                {
                    XActor.DeselectUnits();
                }

                if (Kamera2.tmpMouseB1)
                {
                    Screen.Action_Begin2D();
                    Screen.Draw_Box(XActor.Mouse1Coor.x,
                    XActor.Mouse1Coor.y, XActor.Mouse2Coor.x,
                    XActor.Mouse2Coor.y, Globals.RGBA(0, 1, 0, 1));

                    Screen.Action_End2D();
                }
                XActor.UpdateActors(Kamera2.tmpMouseX1, Kamera2.tmpMouseY1, Kamera2.tmpMouseB1, Scene, Kamera2._inputs);
                TV.RenderToScreen();
                Application.DoEvents();
            }
        }

        public void UpdateScene()
        {
            if (Teren.Land != null)
            {
                float y = Teren.Land.GetHeight(0, 0) + 200;
                Svijetlo.SetLightPosition(new TV_3DVECTOR(0, y, 0));
            }

            if (Voda  != null)
            {
                Reflect.StartRender(false);
                if (SkyBoxs.Atmos != null)
                {
                    SkyBoxs.DrawSkyBox();
                }
                if (Oblaci.Atmos != null )
                {
                    Oblaci.DrawClouds();
                }
                if (Magla.Enabled)
                {
                    Magla.DrawFog();
                }
                Teren.DrawTerrain();
                VMannager.DrawVehicale();
                XActor.CrtajJedinice("Sve", ref Text2D, ref Globals);
                XModel.CrtajJedinice("Sve");
                Partikali.DrawParticles("All");
                Reflect.EndRender();
                Refract.StartRender(false);
                if (SkyBoxs.Atmos != null)
                {
                    SkyBoxs.DrawSkyBox();
                }
                if (Oblaci.Atmos != null )
                {
                    Oblaci.DrawClouds();
                }
                if (Magla.Enabled)
                {
                    Magla.DrawFog();
                }
                Teren.DrawTerrain();
                VMannager.DrawVehicale();
                XActor.CrtajJedinice("Sve", ref Text2D, ref Globals);
                XModel.CrtajJedinice("Sve");
                Partikali.DrawParticles("All");
                Refract.EndRender();
            }
            Partikali.UpdateParticles("All");
            XModel.UpdateUnits();
            XActor.UpdateActors(ref XModel, true);
            XActor.TestSpoted(Scene);
        }
    }
}

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
Web01 | 2.8.150520.1 | Last Updated 20 Nov 2009
Article Copyright 2009 by Member 3873633
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid