📄 gameengine.cs
字号:
//-----------------------------------------------------------------------------
// File: GameEngine.cs
//
// Desc: GameEngine code for Chapter 1 of Introduction to 3D Game Engine Design.
//
// This File contains the GameEngine class definition for Chapter 1. You
// will notice that at this point it consists of only a few stub methods
// That provide no functionality yet.
//
// Copyright (c) 2002 Lynn T. Harrison All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Threading;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX.Direct3D;
namespace GameEngine
{
/// <summary>
/// Summary description for GameEngine.
/// This is the definition of the 3D game engine developed as part of the bok
/// Introduction to 3D Game Engine Design.
/// </summary>
#region delegates
public delegate void BackgroundTask();
#endregion
public class CGameEngine : IDisposable
{
#region Attributes
// A local reference to the DirectX device
private static Microsoft.DirectX.Direct3D.Device m_pd3dDevice;
private System.Windows.Forms.Form m_WinForm;
private SplashScreen m_SplashScreen = null;
private OptionScreen m_OptionScreen = null;
private SkyBox m_Skybox = null;
private Camera m_Camera = null;
public static GameInput m_GameInput = null;
private static Terrain m_Terrain = null;
private static Quad m_QuadTree = null;
private ArrayList m_Objects = null;
private ArrayList m_Cameras = null;
public float fTimeLeft = 0.0f;
Thread m_threadTask = null;
#endregion
#region Properties
public Camera Cam { get { return m_Camera; } }
public ArrayList Objects { get { return m_Objects; } }
public static Terrain Ground { get { return m_Terrain; } }
public static Quad QuadTree { get { return m_QuadTree; } }
public static Microsoft.DirectX.Direct3D.Device Device3D { get { return m_pd3dDevice; } }
public static GameInput Inputs { get { return m_GameInput; } }
public static Color FogColor { set { m_pd3dDevice.RenderState.FogColor = value; } }
public static FogMode FogTableMode { set { m_pd3dDevice.RenderState.FogTableMode = value; } }
public static FogMode FogVertexMode { set { m_pd3dDevice.RenderState.FogVertexMode = value; } }
public static float FogDensity { set { m_pd3dDevice.RenderState.FogDensity = value; } }
public static float FogStart { set { m_pd3dDevice.RenderState.FogStart = value; } }
public static float FogEnd { set { m_pd3dDevice.RenderState.FogEnd = value; } }
public static bool FogEnable { set { m_pd3dDevice.RenderState.FogEnable = value; } }
#endregion
int frame = 0;
public void Dispose()
{
Debug.WriteLine("disposing of game engine objects");
m_GameInput.Dispose();
Debug.WriteLine("disposing of terrain");
if ( m_Terrain != null ) m_Terrain.Dispose();
Debug.WriteLine("disposing of skybox");
m_Skybox.Dispose();
Debug.WriteLine("disposing of quadtree");
m_QuadTree.Dispose();
Debug.WriteLine("disposing of splashscreen");
if ( m_SplashScreen != null )
{
m_SplashScreen.Dispose();
}
Debug.WriteLine("disposing of optionscreen");
if ( m_OptionScreen != null )
{
m_OptionScreen.Dispose();
}
Debug.WriteLine("number of objects="+m_Objects.Count);
for ( int i=0; i < m_Objects.Count; i++ )
{
try
{
Object3D obj = (Object3D)m_Objects[i];
Debug.WriteLine("calling dispose for " + obj.Name);
obj.Dispose( );
}
catch
{
}
}
for ( int i=0; i < BillBoard.Objects.Count; i++ )
{
Object3D obj = (Object3D)BillBoard.Objects[i];
obj.Dispose();
}
}
public void RestoreSurfaces()
{
if ( m_SplashScreen != null )
{
m_SplashScreen.Restore();
}
if ( m_OptionScreen != null )
{
m_OptionScreen.Restore();
}
}
public void SetOptionScreen( OptionScreen Screen )
{
m_OptionScreen = Screen;
}
/// <summary>
/// Initial setup of the Game Engine
/// </summary>
/// <param name="pd3dDevice"></param>
public void Initialize ( System.Windows.Forms.Form form, Microsoft.DirectX.Direct3D.Device pd3dDevice )
{
// capture a reference to the window handle
m_WinForm = form;
// For now just capture a reference to the DirectX device for future use
m_pd3dDevice = pd3dDevice;
m_GameInput = new GameInput( m_WinForm );
m_Skybox = new SkyBox(@"..\..\Resources\Dunes_Front.tga",
@"..\..\Resources\Dunes_Right.tga",
@"..\..\Resources\Dunes_Back.tga",
@"..\..\Resources\Dunes_Left.tga",
@"..\..\Resources\Dunes_Top.tga",
@"..\..\Resources\Dunes_Bottom.tga" );
m_Camera = new Camera();
m_Cameras = new ArrayList();
m_Cameras.Add(m_Camera);
m_Objects = new ArrayList();
m_pd3dDevice.RenderState.Ambient = System.Drawing.Color.Gray;
// Set light #0 to be a simple, faint grey directional light so
// the walls and floor are slightly different shades of grey
m_pd3dDevice.RenderState.Lighting = true; // was true
GameLights.InitializeLights();
// SetDefaultStates( );
}
/// <summary>
/// Display a Splash Screen based on a suppplied bitmap filename
/// </summary>
/// <param name="sFileName"></param>
public bool ShowSplash ( string sFileName, int nSeconds, BackgroundTask task )
{
bool bDone = false;
if ( m_SplashScreen == null )
{
m_SplashScreen = new SplashScreen( sFileName, nSeconds);
if ( task != null )
{
m_threadTask = new Thread(new ThreadStart(task) );
m_threadTask.Name = "Game_backgroundTask";
m_threadTask.Start();
}
}
bDone = m_SplashScreen.Render();
fTimeLeft = m_SplashScreen.fTimeLeft;
if ( bDone )
{
m_SplashScreen.Dispose();
m_SplashScreen = null;
}
return bDone;
}
/// <summary>
/// Display the Options screen
/// </summary>
public void DoOptions ( )
{
if ( m_OptionScreen != null )
{
m_OptionScreen.SetMousePosition(m_GameInput.GetMousePoint().X, m_GameInput.GetMousePoint().Y, m_GameInput.IsMouseButtonDown(0) );
m_OptionScreen.Render();
}
}
/// <summary>
/// Display the latest game frame
/// </summary>
public void Render ( )
{
m_Camera.Render();
m_QuadTree.Cull( m_Camera );
GameLights.CheckCulling( m_Camera );
// test code
Model ownship = (Model)GetObject("car1");
if ( ownship != null && ownship.IsCulled )
{
Console.AddLine("ownship culled at " + ownship.North + " " + ownship.East + " H " + ownship.Heading );
}
GameLights.DeactivateLights();
if ( m_Skybox != null )
{
m_Skybox.Render( m_Camera );
}
GameLights.SetupLights();
if ( m_Terrain != null )
{
m_Terrain.Render( m_Camera );
}
BillBoard.RenderAll( m_Camera );
foreach ( Object3D obj in m_Objects )
{
if ( !obj.IsCulled )
{
obj.Render( m_Camera );
}
}
}
void SetDefaultStates( )
{
//
// Set the recomended defaults by nVidia
//
m_pd3dDevice.RenderState.MultiSampleAntiAlias = true;
m_pd3dDevice.RenderState.MultiSampleMask = -1;
m_pd3dDevice.RenderState.ColorWriteEnable = ColorWriteEnable.Alpha | ColorWriteEnable.Blue |
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -