⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 application.cpp

📁 一个自己写的游戏引擎,用DirectX 写成
💻 CPP
字号:
//--------------------------------------------------
//  Desc: application
//  Author: artsylee/2006.12.18
//--------------------------------------------------
#include "../stdafx.h"
#include "Application.h"
#include "Log.h"
#include "Common.h"
#include "Font.h"
#include "Graphics.h"
#include "Message.h"
#include "../gui/GUIManager.h"
#include "Input.h"
#include "Interface.h"

#define		WndClass  "artsylee's game engine"

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

CGameApp::CGameApp()
{
	m_bActive = true;
	m_bNotSuspend = false;
	m_bDLLFileLog = false;
	m_hWnd = NULL;
	m_hInst = NULL;
	m_dwLastTime = 0;
	m_dwOldTime = 0;
	m_nFixedDelta = 0;
	m_CurrentFPS = 0;
	m_Frame = 0;

	g_pGameApp = this;

	m_ScreenWidth = 800;
	m_ScreenHeight = 600;
	m_ScreenDepth = 32;
	m_bWindowed = true;
}

CGameApp::~CGameApp()
{
	g_pGameApp = NULL;
}

void CGameApp::SetFPS(int fps)
{
	if(fps)
	{
		m_nFixedDelta = (int)(1000.0f/fps);
	}
	else
	{
		m_nFixedDelta = 0;
	}
}

BOOL CGameApp::InitApplication(HINSTANCE hInstance, int nCmdShow, const char *szCaption /* = "Game Engine" */, 
							   const char *szIcon /* = IDI_APPLICATION */)
{
	m_hWnd = InitWindow(hInstance, nCmdShow, szCaption, szIcon);
	if(m_hWnd == NULL)
		return FALSE;

	//精确系统定时
	::timeBeginPeriod(1);
	// 全局变量
	g_pLog = new CLog;
	if(g_pLog == NULL)
	{
		return FALSE;
	}
	if(m_bDLLFileLog)
	{
		g_pLog->StartLog("ASELog.html", false);
	}
	if(g_Input.CreateInput(m_hWnd))
	{
		WriteLog(INFO_NORMAL, "Initiate Keyboard input device...OK");
		WriteLog(INFO_NORMAL, "Initiate Mouse input device...OK");
	}
	m_pGraphics = new CGraphics;
	if(m_pGraphics == NULL)
	{
		return FALSE;
	}
	// 设置ZBuffer
	m_pGraphics->SetZBuffer(true);
	WriteLog(INFO_NORMAL, "Setup Z Buffer : true");
	if(!m_pGraphics->InitGraphics(m_hWnd, m_ScreenWidth, m_ScreenHeight, m_bWindowed, m_ScreenDepth))
	{
		return FALSE;
	}
	g_pMessageManager = new CMessageManager;
	if(g_pMessageManager == NULL)
	{
		return FALSE;
	}
	WriteLog(INFO_NORMAL, "Create Main Message System...");
	g_pGUIManager = new GUIManager;
	if(g_pGUIManager == NULL)
	{
		return FALSE;
	}
	WriteLog(INFO_NORMAL, "Create GUI System...");
	if(!g_Font.CreateFont("宋体", 12))
	{
		return FALSE;
	}
	g_Font.SetLineCount(50);
	WriteLog(INFO_NORMAL, "Create System Font...");
	WriteLog(INFO_NORMAL, "Initiate ASEngine Core Application is OK.");
	WriteLog(INFO_NORMAL, "Processing User's code...");

	return true;
}

void CGameApp::Run(void)
{
	MSG msg;
	while(1)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
		{
			if(!GetMessage(&msg, NULL, 0, 0)) 
				return;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else if(m_bActive || m_bNotSuspend)
		{
			DWORD dt;
			do 
			{ 
				dt = timeGetTime() - m_dwLastTime; 
			}while(dt < 1);
			if(dt >= m_nFixedDelta)
			{
				m_dwLastTime = timeGetTime();
				// FPS
				if(m_dwLastTime - m_dwOldTime <= 1000)
				{
					m_Frame++;
				}
				else
				{
					m_CurrentFPS = m_Frame;
					m_Frame = 0;
					m_dwOldTime = m_dwLastTime;
				}
				// INPUT
				g_Input.UpdateBufferInput(&g_stInputInfo);
				MainLoop();
				if(!m_bWindowed && 1)
				{
					Sleep(1);
				}
			}
			else 
			{ 
				if(m_nFixedDelta && dt+3 < m_nFixedDelta) 
					Sleep(1); 
			}

		}
		else WaitMessage();
	}
}

BOOL CGameApp::Destroy(void)
{
	S_DELETE(g_pLog);
	S_DELETE(g_pGUIManager);
	S_DELETE(g_pMessageManager);
	S_DELETE(m_pGraphics);
	g_pGraphics = NULL;
	g_Input.ReleaseInput();
	::timeEndPeriod(1); 
	UnregisterClass(WndClass, m_hInst);
	return TRUE;
}

//-------------------------------------------------
// Create Window
//-------------------------------------------------
HWND CGameApp::InitWindow(HINSTANCE hInstance, int nCmdShow, const char *szCaption /* = "Game Engine" */, 
						  const char *szIcon /* = IDI_APPLICATION */)
{
	HWND				hwnd;	
	WNDCLASS			wc;		

	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WinProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(hInstance, szIcon);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = WndClass;

	if(!RegisterClass(&wc)) 
	{
		WriteLog(INFO_ERROR, "Register class fail!");
		return FALSE;
	}

	if(m_bWindowed == false)
	{
		// Full Screen
		hwnd = CreateWindowEx(
			WS_EX_TOPMOST,
			WndClass,
			szCaption,
			WS_POPUP,	
			0,
			0,
			GetSystemMetrics(SM_CXSCREEN),
			GetSystemMetrics(SM_CYSCREEN),
			NULL,
			NULL,
			hInstance,
			NULL);
	}
	else // Windows
	{
		int Width = m_ScreenWidth + GetSystemMetrics(SM_CXFIXEDFRAME)*2;
		int Height = m_ScreenHeight + GetSystemMetrics(SM_CYFIXEDFRAME)*2 + GetSystemMetrics(SM_CYCAPTION);

		hwnd = CreateWindowEx(
			0,
			WndClass,
			szCaption,
			WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME,
			(GetSystemMetrics(SM_CXSCREEN)-Width)/2,
			(GetSystemMetrics(SM_CYSCREEN)-Height)/2,
			Width,
			Height,
			NULL,
			NULL,
			hInstance,
			NULL);
	}

	if(!hwnd)
	{
		WriteLog(INFO_ERROR, "CreateWindowEx fail!");
		return FALSE;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);
	return hwnd;
}

//-------------------------------------------------
// WinProc
//-------------------------------------------------
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_CLOSE:
		::PostQuitMessage(0);
		return 0L;
		break;

	case WM_ACTIVATE:

		if(LOWORD(wParam)==WA_INACTIVE || HIWORD(wParam))
		{
			g_pGameApp->SetActive(false);
		}
		else
		{
			g_pGameApp->SetActive(true);
		}

	case WM_SYSKEYDOWN:
		switch(wParam)
		{
		case VK_RETURN:
			{
				g_pGraphics->ToggleScreen();
			}
			break;
		case 'Q':
		case 'q':
		case VK_F4:
			::PostQuitMessage(0);
			return 0L;
		}
		break;
		/*
	case WM_KEYDOWN:
		if(wParam==VK_ESCAPE)
		{
			::PostQuitMessage(0);
			return 0L;
		}
		break;
		*/
	}
	
	return DefWindowProc(hWnd, message, wParam, lParam);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -