📄 mainapp.cpp
字号:
//--------------------------------------------------
// Desc: Main Application
// Author:
//--------------------------------------------------
#include "MainApp.h"
#include "Common.h"
#include "Terrain.h"
#include "SkyBox.h"
#include "Heightmap.h"
#include "Water.h"
#include "Player.h"
#include "NodeManager.h"
#include "NodeAdjust.h"
#include "ModelManager.h"
#include "ConsoleCommand.h"
#include "Character.h"
#include "GameCommon.h"
#include "LuaInterface.h"
#include "GameOption.h"
#include "PictureTask.h"
#include "InfoPanel.h"
#include "Scene.h"
#include "CSVFile.h"
CNodeManager g_NodeManager;
CNodeAdjust g_NodeAdjust;
// temp
CTerrain terrain;
CSkyBox skybox;
CCommand command;
CWater water;
lua_State *g_pLuastate = NULL;
enum GameState
{
GS_LOGO,
GS_MENU,
GS_MAINOPTION,
GS_TEMPOPTION,
GS_RUN,
GS_PAUSE,
GS_CREDIT,
};
GameState g_GameState;
extern CHeightmap g_Hmap;
CCharacter player;
CScene scene;
RECT g_rcWindow = { 0, 0, 800, 600 };
CGameOption g_Option;
CAnimation g_Logo;
CTaskManager *g_pTaskManager;
CInfoPanel g_Info;
int g_WarningState = 0;
// test
CMainApp::CMainApp()
{
}
CMainApp::~CMainApp()
{
}
BOOL CMainApp::InitApplication(HINSTANCE hInstance, int nCmdShow, char *szCaption /* = "Game Engine" */, char *szIcon /* = IDI_APPLICATION */)
{
SetDLLFileLog(true);
CGameApp::InitApplication(hInstance, nCmdShow, szCaption, szIcon);
// TODO:-------------------------------------------------------
// add your code here
// ------------------------------------------------------------
//---------------------
g_pTextureManager = GetTextureManager();
g_pFrameManager = GetFrameManager();
g_pD3DDevice = GetD3DDevice();
g_pGraphics = GetGraphics();
g_pGUIManager = GetGUIManager();
g_pGameApp = GetMainApp();
g_stInputInfo = GetInputInfo();
g_pMessageManager = GetMessageManager();
// 是否输出日志到文件
//GetLog()->StartLog("debug.html");
//---------------------
g_pModelManager = new CModelManager;
g_pTaskManager = new CTaskManager;
//---------------------
SetFPS(0);
//ShowCursor(FALSE);
// 开启后台运行
// SetNotSuspend(true);
if(!g_pGUIManager->LoadFromFile("Config\\UI.ini"))
{
WriteLog(INFO_ERROR, "Load UI Error!");
}
// Lua
g_pLuastate = lua_open();
if(g_pLuastate==NULL)
{
WriteLog(INFO_ERROR, "can't open lua state");
return FALSE;
}
RegisterAPI(g_pLuastate);
luaL_dofile(g_pLuastate, "Config\\Script.lua");
lua_getglobal(g_pLuastate, "Load");
lua_call(g_pLuastate, 0, 0);
//lua_pop(g_pLuastate, 1);
g_pTextureManager->BatchLoad("config\\Picture.ini");
terrain.LoadTerrain();
skybox.InitSkyBox(10000, 7000, 10000);
command.LoadCommand("Config\\Command.ini");
command.CreateConsole();
scene.LoadScene();
player.Load();
water.InitWater();
// TODO:-------------------------------------------------------
OnLostDevice();
// ------------------------------------------------------------
// g_GameState = GS_RUN;
// GUIShow(MAIN_WINDOW, false);
g_GameState = GS_LOGO;
// g_GameState = GS_MENU;
// GUIShow(MAIN_WINDOW);
CIniFile ItemFile("Config\\Item.ini");
CoolListBox *pBox = (CoolListBox*)GetGUI(EQUIP_LIST1);
if(pBox)
{
pBox->RemoveAllItems();
int num = ItemFile.GetContinueData("Main");
for(int i=0; i<num; i++)
{
char szInfo[256];
ItemFile.ReadString("Main", i, szInfo);
pBox->AddItem(szInfo, NULL);
}
pBox->SelectItem(0);
pBox->SetStyle(0);
}
pBox = (CoolListBox*)GetGUI(EQUIP_LIST2);
if(pBox)
{
pBox->SetStyle(0);
pBox->Show(false);
}
//GUIShow(EQUIP_LIST2, false);
// Option
g_Option.Load();
g_Logo.LoadFromFile("Picture\\logo.amf");
g_Logo.SetLoopType(ANI_ONCE);
g_Logo.Play();
g_Logo.SetPosition(0, 0);
if(g_GameState==GS_LOGO)
{
CAlphaTask *pTask = new CAlphaTask(g_rcWindow, 1002, 30);
pTask->SetTotalCount(1);
pTask->SetAlphaStep(1);
g_pTaskManager->AddTask(pTask);
}
g_Info.Load();
return TRUE;
}
BOOL CMainApp::Destroy()
{
// TODO:-------------------------------------------------------
// add your code here
// ------------------------------------------------------------
//
CGameApp::Destroy();
//---------------------
// 全局
g_pTextureManager = GetTextureManager();
g_pFrameManager = GetFrameManager();
g_pD3DDevice = GetD3DDevice();
g_pGraphics = GetGraphics();
g_pGUIManager = GetGUIManager();
g_pGameApp = GetMainApp();
g_pMessageManager = GetMessageManager();
//---------------------
S_DELETE(g_pModelManager);
S_DELETE(g_pTaskManager);
lua_getglobal(g_pLuastate, "Destroy");
lua_call(g_pLuastate, 0, 0);
if(g_pLuastate)
{
lua_close(g_pLuastate);
}
return TRUE;
}
void CMainApp::MainLoop()
{
g_stInputInfo = GetInputInfo();
::GetPointerOffset(&g_CursorOffsetX, &g_CursorOffsetY, &g_CursorOffsetZ);
g_LastUpdateTime = g_GlobalTime;
g_GlobalTime = timeGetTime();
g_ElapsedTime = g_GlobalTime - g_LastUpdateTime;
// JUST FOR TEST
g_Info.ShowTop();
// Update
command.Update();
if(g_pGUIManager->ProcessEvent())
{
player.SetMouseMove(false);
}
else
{
player.SetMouseMove(true);
}
MessageLoop();
OnSystemKey();
if(g_GameState==GS_CREDIT)
{
if(g_stInputInfo.KeyValue==DIK_ESCAPE)
{
g_GameState = GS_MENU;
GUIShow(MAIN_WINDOW);
}
lua_getglobal(g_pLuastate, "GlobalFlag");
int flag = (int)lua_tonumber(g_pLuastate, 1);
if(flag>9)
{
g_GameState = GS_MENU;
GUIShow(MAIN_WINDOW);
}
lua_pop(g_pLuastate, 1);
if(g_stInputInfo.KeyValue==DIK_RETURN ||
g_stInputInfo.KeyValue==DIK_SPACE ||
g_stInputInfo.KeyValue==DIK_NUMPADENTER)
{
flag++;
lua_pushnumber(g_pLuastate, g_GlobalTime);
lua_setglobal(g_pLuastate, "LastTime");
lua_pushnumber(g_pLuastate, flag);
lua_setglobal(g_pLuastate, "GlobalFlag");
}
}
g_pTaskManager->Update();
if(g_GameState==GS_RUN)
{
//---------------------------------------
// time test start
g_Htimer.Start();
//---------------------------------------
player.Update();
g_NodeAdjust.Update();
//---------------------------------------
// time test end
g_HDelay = g_Htimer.GetAveragePassTime()*1000;
//---------------------------------------
}
//低层支持字体更新,字体更新可能得不偿失
//g_Font.Update();
// Render BEGIN
g_pGraphics->BeginScene();
if(g_GameState==GS_RUN || g_GameState==GS_PAUSE ||g_GameState==GS_TEMPOPTION)
{
g_pGraphics->Render3DObject();
skybox.DrawSkyBox();
terrain.Render();
water.Render();
player.Render();
scene.Update();
scene.Render();
g_NodeAdjust.Render();
if(g_GameState==GS_PAUSE || g_GameState==GS_TEMPOPTION)
{
Render(1001, &g_rcWindow, NULL, 0x7f5f5f5f);
}
}
if(g_GameState==GS_MENU || g_GameState==GS_MAINOPTION)
{
Render(1002, &g_rcWindow);
}
// GUI
g_pGraphics->BeginSprite();
g_pGUIManager->Render();
g_pGraphics->EndSprite();
// Task
g_pTaskManager->Render();
// Logo
if(g_GameState==GS_LOGO)
{
if(g_Logo.GetState()==ANI_STOP)
{
g_GameState = GS_MENU;
GUIShow(MAIN_WINDOW);
}
else
{
g_Logo.Update();
g_Logo.Render();
}
if(g_stInputInfo.MouseValue==LB_DOWN || g_stInputInfo.KeyValue==DIK_RETURN ||
g_stInputInfo.KeyValue==DIK_SPACE || g_stInputInfo.KeyValue==DIK_ESCAPE)
{
g_GameState = GS_MENU;
GUIShow(MAIN_WINDOW);
g_pTaskManager->RemoveAll();
}
}
// Info
DWORD color = 0xff00ff00;
if(m_CurrentFPS<100) color = 0xffffff00;
if(m_CurrentFPS<60) color = 0xffff0000;
DrawSingleLine(0, 0, color, false, "FPS:%d", m_CurrentFPS);
// DrawSingleLine(80, 0, 0xff0000ff, false, "Mouse Position(%d, %d)", g_stInputInfo.point.x, g_stInputInfo.point.y);
// DrawSingleLine(256, 0, 0xffff0000, false, "TIMER:%.4f, Frames:%.2f", g_HDelay, 1000/g_HDelay);
// Lua
if(g_GameState==GS_CREDIT)
{
lua_getglobal(g_pLuastate, "Update");
lua_call(g_pLuastate, 0, 0);
}
// Brightness
Render(1001, &g_rcWindow, NULL, MakeARGB(g_Brightness, 0, 0, 0));
g_pGraphics->EndScene();
// must set this
g_Hmap.m_bViewerChange = false;
// END
}
void CMainApp::OnLostDevice(void)
{
// 灯光
// 全屏后设置丢失
D3DLIGHT9 light;
::ZeroMemory(&light, sizeof(light));
light.Type = D3DLIGHT_DIRECTIONAL;
light.Ambient.r = 0.7f;
light.Ambient.g = 0.7f;
light.Ambient.b = 0.7f;
light.Ambient.a = 0.7f;
light.Diffuse.r = 0.8f;
light.Diffuse.g = 0.8f;
light.Diffuse.b = 0.8f;
light.Diffuse.a = 0.5f;
light.Specular.r = 1.0 * 0.6f;
light.Specular.g = 1.0 * 0.6f;
light.Specular.b = 1.0 * 0.6f;
light.Specular.a = 1.0 * 0.6f;
light.Direction.x = -0.2f;
light.Direction.y = -0.39f;
light.Direction.z = -0.9f;
// light.Position = D3DXVECTOR3(1290, 136, 3921);
// light.Range = 1000.0f;
g_pD3DDevice->SetLight(0, &light);
g_pD3DDevice->LightEnable(0, FALSE);
g_pD3DDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
g_pD3DDevice->SetRenderState(D3DRS_SPECULARENABLE, FALSE);
g_pD3DDevice->SetRenderState(D3DRS_AMBIENT, 0xff7f7f7f);
SetFog(1200);
if(IsSupportFSAA())
{
g_pD3DDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
}
else
{
g_pD3DDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, FALSE);
}
g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
g_pD3DDevice->SetSamplerState(1, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
}
void CMainApp::SetFog(int end)
{
//_FOG_________________________________________________________________
float Start = 200.0f, End = (float)end;
g_pD3DDevice->SetRenderState(D3DRS_FOGENABLE, FALSE);
g_pD3DDevice->SetRenderState(D3DRS_FOGCOLOR, 0x00a4a4a4);
g_pD3DDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);
g_pD3DDevice->SetRenderState(D3DRS_FOGSTART, *(DWORD*)(&Start));
g_pD3DDevice->SetRenderState(D3DRS_FOGEND, *(DWORD*)(&End));
//_FOG_________________________________________________________________
}
void CMainApp::MessageLoop(void)
{
CMessage msg;
int index = 0;
if(PeekMessage(index, msg, true))
{
if(msg.m_dwType==MSG_SYS_LOSTDEVICE)
{
// 设备丢失时需要设置视口矩阵
OnLostDevice();
player.UpdateView();
}
if(msg.m_dwParam2==GUI_STATIC)
{
g_NodeAdjust.SetIndex(msg.m_dwSource-EDIT_POSX);
}
if(msg.m_dwType==MSG_SYS_GUI && msg.m_dwParam1==GUI_MOUSE_LUP)
{
switch(msg.m_dwSource)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -