[VC++游戏开发]用C++来架构一个适合windows游戏编程的框架――取名为BCF(二)

2014-11-24 08:06:28 · 作者: · 浏览: 1
------------------------- 设计窗口类 --------------------------------------*/ bool CCWindow::InitWndClass(WNDCLASS wndclass) { m_wndclass = wndclass; return true; } /*-------------------------------------- 注册窗口类 --------------------------------------*/ ATOM CCWindow::RegisterWndClass() { return RegisterClass(&m_wndclass); } /*-------------------------------------- 创建窗口 --------------------------------------*/ void CCWindow::Create( LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int nWidth, int nHeight) { //获取屏幕宽度和高度 int screenW = GetSystemMetrics(SM_CXSCREEN); int screenH = GetSystemMetrics(SM_CYSCREEN); //创建并居中显示窗口 m_hwnd = CreateWindow(lpClassName, lpWindowName, dwStyle, (screenW-nWidth)/2, (screenH-nHeight)/2, nWidth, nHeight, NULL, NULL, m_wndclass.hInstance, NULL); } /*-------------------------------------- 显示窗口 --------------------------------------*/ void CCWindow::Show(int nCmdShow) { ShowWindow(m_hwnd, nCmdShow); } //UpdateWindow(...)更新窗口(可以省略) /*-------------------------------------- 一般的消息循环 GetMessage() --------------------------------------*/ int CCWindow::RunMsgLoop() { MSG msg; ZeroMemory(&msg, sizeof(MSG)); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } /*-------------------------------------- 消息循环(更好的消息循环) PeekMessage() --------------------------------------*/ int CCWindow::RunMsgLoop(void (*Display)(), int interval) { MSG msg; ZeroMemory(&msg, sizeof(MSG)); //获取运行到此处时的时间 int last = GetTickCount(); //如果不是退出消息 while(msg.message != WM_QUIT) { //如果有消息 if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } //否则, 空闲的时候执行响应函数(大多数是绘制函数) else { //如果窗口客户区大小不为0就是显示(有可能窗口是在最小化) if(m_rect.Width() && m_rect.Height()) { Display(); Sleep(interval); } } } return msg.wParam; } //---------------------------------------------------------- // 消息响应函数 //---------------------------------------------------------- //注:在这里添加需要响应的消息处理函数的实现

Main.h头文件(主程序头文件)

/***
*
*	Main.h
*	主程序所需包含的头文件、宏定义、声明等
*
***/

#pragma once
#include CWindow.h

#define WNDNAME	【VC++游戏开发】窗口名称//窗口名称
#define WNDWIDTH 800//窗口宽度
#define WNDHEIGHT 600//窗口高度

//窗口关联对象:全局对象
CCWindow wnd;

//窗口过程
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

Main.cpp(主程序代码:负责WinMain、WndProc)

#include Main.h

/*
	显示函数:绘制随机位置、大小、颜色的矩形
	注:由于调用很频繁,故设为内联函数
*/
inline void Display()
{
	//做响应的操作
}

//
//==============WinMain=======================
//
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{
	//设计窗口类
	CString iconPath = ;//图标的路径(这里没有, 需自己设定)
	wnd.InitWndClass(hInstance, WndProc, WNDNAME, iconPath);

	//注册窗口类
	if(!wnd.RegisterWndClass())
	{
		::AfxMessageBox(RegisterWndClass() Failed);

		return 0;
	}

	DWORD style = WS_OVERLAPPEDWINDOW &
				~(WS_THICKFRAME | WS_MAXIMIZEBOX);

	//创建窗口并居中窗口
	wnd.Create(WNDNAME, WNDNAME, style,
			   WNDWIDTH, WNDHEIGHT);

	//显示窗口
	wnd.Show(nCmdShow);

	/*进入消息循环
		1. 使用更好的消息循环 如:wnd.RunMsgLoop(Display, 100)
		2. 使用一般的消息循环 如:wnd.RunMsgLoop()
	*/
	return wnd.RunMsgLoop();
}

//
//================窗口过程