这是面向对象游戏内核的改良版,主要对原来游戏内核对象组织一些不合理之处做出了改良,并且遵循以下原则:
1、浅分层、薄胶合的原则,对于简单的功能只用函数封装,如果需要实现的功能模块较复杂,并且需要用到较多的辅助数据结构则用类封装,以减少对象的使用。
2、本版本不使用模板,尽量减少封装层数,最大限度地增加透明度。
3、尽量不在构造函数内用表达式初始化任何数据,以使用memet简化类数据成员的初始化,只使用默认构造函数,不使用其他任何形式的构造函数。
4、尽量不在类内部保存冗余的数据,如果这个数据真正存储在另一个地方,则不在其他类中做多余的保存,保证数据存储的唯一性。
5、不重载函数,不使用参数默认值。
关于该内核的细节说明请参阅创建游戏内核(1)。
公共头文件:
/**************************************************
PURPOSE:
Include common game core header file.
**************************************************/
#ifndef _CORE_COMMON_H_
#define _CORE_COMMON_H_
#define DIRECTINPUT_VERSION 0x0800
// Windows includes
#include <windows.h>
// Standard ANSI-C includes
#include <stdio.h>
#include <string.h>
// DirectX includes
#include <d3d9.h>
#include <d3dx9.h>
#include <dmusici.h>
#include <dsound.h>
#include <dplay8.h>
#include <dpaddr.h>
#include <dinput.h>
#include <dshow.h>
#include <dxfile.h>
#pragma warning(disable : 4996)
#define release_com(x) { if(x) { x->Release(); x = NULL; } }
#define free_memory(x) { free(x); (x) = NULL; }
#define STREQ(a, b) (*(a) == (*b) && strcmp((a), (b)) == 0)
typedef unsigned char uchar;
typedef unsigned long ulong;
#endif
游戏框架头文件:
/*************************************************************************
PURPOSE:
Interface for main window framework.
*************************************************************************/
#ifndef _CORE_FRAMEWORK_H_
#define _CORE_FRAMEWORK_H_
extern HWND g_hwnd;
HINSTANCE get_window_inst();
void get_class_name(char* class_name, int length);
void show_error_msg(BOOL is_fatal, char* text, );
void move_window(HWND hwnd, long x_pos, long y_pos);
void resize_window(HWND hwnd, long width, long height);
long get_client_width(HWND hwnd);
long get_client_height(HWND hwnd);
long get_window_width(HWND hwnd);
long get_window_height(HWND hwnd);
BOOL build_window(HINSTANCE inst, const char* class_name, const char* caption,
DWORD style, DWORD x_pos, DWORD y_pos, DWORD width, DWORD height);
class FRAMEWORK
{
public:
virtual BOOL init() { return TRUE; }
virtual BOOL frame() { return TRUE; }
virtual BOOL shutdown() { return TRUE; }
void run();
};
static FRAMEWORK* g_framework = NULL;
#endif
框架实现代码:
/*************************************************************************
PURPOSE:
Implement for main window framework.
*************************************************************************/
#include "core_common.h"
#include "core_framework.h"
HWND g_hwnd;
//-----------------------------------------------------------------------------
// Return window instnace.
//-----------------------------------------------------------------------------
HINSTANCE get_window_inst()
{
return GetModuleHandle(NULL);
}
//-----------------------------------------------------------------------------
// Get window class name.
//-----------------------------------------------------------------------------
void get_class_name(char* class_name, int length)
{
GetClassName(g_hwnd, class_name, length);
}
//-----------------------------------------------------------------------------
// Show error message box.
//-----------------------------------------------------------------------------
void show_error_msg(BOOL is_fatal, char* text, )
{
char _caption_text[12];
char _error_text[2048];
va_list _valist;
// Build the message box caption based on fatal flag
strcpy(_caption_text, is_fatal ? "Fatal error" : "error");
// Build variable text buffer
va_start(_valist, text);
vsprintf(_error_text, text, _valist);
va_end(_valist);
// display the message box
MessageBox(NULL, _error_text, _caption_text, MB_OK | MB_ICONEXCLAMATION);
// Post a quit message if error was fatal.
if(is_fatal)
PostQuitMessage(0);
}
//-----------------------------------------------------------------------------
// move window to new position.
//-----------------------------------------------------------------------------
void move_window(HWND hwnd, long x_pos, long y_pos)
{
RECT _client_rect;
GetClientRect(hwnd, &_client_rect);
MoveWindow(hwnd, x_pos, y_pos, _client_rect.right, _client_rect.bottom, TRUE);
}
//-----------------------------------------------------------------------------
// Resize window to new width and height.
//-----------------------------------------------------------------------------
void resize_window(HWND hwnd, long width, long height)
{
RECT _window_rect, _client_rect;
long _new_window_width, _new_window_height;
// Retrieves the dimensions of the bounding rectangle of the specified window.
// The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.
GetWindowRect(hwnd, &_window_rect);
// Retrieves the coordinates of a window's client area.
//
// The client coordinates specify the upper-left and lower-right corners of the client area.
// Because client coordinates are relative to the upper-left corner of a window's client area,
// the coordinates of the upper-left corner are (0,0).
GetClientRect(hwnd, &_client_rect);
_new_window_width = (_window_rect.right - _window_rect.left) - _client_rect.right + width;
_new_window_height = (_window_rect.bottom - _window_rect.top) - _client_rect.bottom + height;
// Changes the position and dimensions of the specified window.
//
// For a top-level window, the position and dimensions are relative to the upper-left corner of the screen.
// For a child window, they are relative to the upper-left corner of the parent window's client area.
MoveWindow(hwnd, _window_rect.left, _window_rect.top, _new_window_width, _new_window_height, TRUE);
}
//-----------------------------------------------------------------------------
// Get window client width.
//-----------------------------------------------------------------------------
long get_client_width(HWND hwnd)
{
RECT _client_rect;
GetClientRect(hwnd, &_client_rect);
return (_client_rect.right - _client_rect.left);
}
//-----------------------------------------------------------------------------
// Get window client height.
//-----------------------------------------------------------------------------
long get_client_height(HWND hwnd)
{
RECT _client_rect;
GetClientRect(hwnd, &_client_rect);
return (_client_rect.bottom - _client_rect.top);
}
//-----------------------------------------------------------------------------
// Get window width.
//-----------------------------------------------------------------------------
long get_window_width(HWND hwnd)
{
RECT _window_rect;
GetWindowRect(hwnd, &_window_rect);
return (_window_rect.right - _window_rect.left);
}
//-----------------------------------------------------------------------------
// Get window height.
//-----------------------------------------------------------------------------
long get_window_height(HWND hwnd)
{
RECT _window_rect;
GetWindowRect(hwnd, &_window_rect);
return (_window_rect.bottom - _window_rect.top);
}
//-----------------------------------------------------------------------------
// The message procedure.
//-----------------------------------------------------------------------------
LRESULT CALLBACK window_proc(HWND hwnd, UINT msg_id, WPARAM w_param, LPARAM l_param)
{
switch(msg_id)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg_id, w_param, l_param);
}
//-----------------------------------------------------------------------------
// Register window aclas, create window and show it.
//-----------------------------------------------------------------------------
BOOL build_window(HINSTANCE inst, const char* class_name, const char* caption,
DWORD style, DWORD x_pos, DWORD y_pos, DWORD width, DWORD height)
{
WNDCLASSEX _win_class;
// create window class and register it
_win_class.cbSize = sizeof(_win_class);
_win_class.style = CS_CLASSDC;
_win_class.lpfnWndProc = window_proc;
_win_class.cbClsExtra = 0;
_win_class.cbWndExtra = 0;
_win_class.hInstance = inst;
_win_class.hIcon = LoadIcon(NULL, IDI_APPLICATION);
_win_class.hCursor = LoadCursor(NULL, IDC_ARROW);
_win_class.hbrBackground = NULL;
_win_class.lpszMenuName = NULL;
_win_class.lpszClassName = class_name;
_win_class.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(! RegisterClassEx(&_win_class))
return FALSE;
// create the main window
g_hwnd = CreateWindow(class_name, caption, style, x_pos, y_pos, width, height, NULL, NULL, inst, NULL);
if(g_hwnd == NULL)
return FALSE;
ShowWindow(g_hwnd, SW_NORMAL);
UpdateWindow(g_hwnd);
return TRUE;
}
//-----------------------------------------------------------------------------
// Run game framework.
//-----------------------------------------------------------------------------
void FRAMEWORK::run()
{
MSG _msg;
// intialize game
if(! init())
return;
// start message pump, waiting for signal to quit.
ZeroMemory(&_msg, sizeof(MSG));
while(_msg.message != WM_QUIT)
{
if(PeekMessage(&_msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&_msg);
DispatchMessage(&_msg);
}
// draw a frame
if(! frame())
break;
}
// run shutdown function
shutdown();
// get window class name
char _class_name[MAX_PATH];
get_class_name(_class_name, sizeof(_class_name));
// unregister window class
UnregisterClass(_class_name, get_window_inst());
}
测试代码:
/*****************************************************************************
PURPOSE:
Test for class FRAMEWORK.
*****************************************************************************/
#include "core_common.h"
#include "core_framework.h"
class APP : public FRAMEWORK
{
public:
BOOL init();
BOOL frame();
BOOL shutdown();
private:
char* m_name;
};
//-----------------------------------------------------------------------------
// Initialize application.
//-----------------------------------------------------------------------------
BOOL APP::init()
{
if((m_name = new char[9]))
{
strcpy(m_name, "no_name");
return TRUE;
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Do a frame.
//-----------------------------------------------------------------------------
BOOL APP::frame()
{
// If user clicks button CANCEL, then exit application.
if(MessageBox(g_hwnd, m_name, "My name is", MB_OKCANCEL) == IDCANCEL)
return FALSE;
return TRUE;
}
//-----------------------------------------------------------------------------
// shutdown application.
//-----------------------------------------------------------------------------
BOOL APP::shutdown()
{
delete[] m_name;
m_name = NULL;
return TRUE;
}
//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE pre_inst, LPSTR cmd_line, int cmd_show)
{
APP app;
if(! build_window(inst, "MainClass", "MainWindow", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480))
return -1;
app.run();
return 0;
}