创建游戏内核(12)【接口与实现分离版】
本篇是创建游戏内核(11)【接口与实现分离版】的续篇,关于该内核的细节说明请参考创建游戏内核(12),这个版本主要是按照功能划分模块的思想,并严格按照接口与实现相分离的原则来写的,没有用面向对象的思想来写,没有继承没有多态。大家可以对比两个版本,比较优劣。
接口:
BOOL create_font(LPD3DXFONT* font,
const char * face_name, long size,
BOOL is_bold, BOOL is_italic);
BOOL draw_font(LPD3DXFONT font, const char * text,
long x_pos, long y_pos, long width, long height,
D3DCOLOR color, DWORD format);
const char * face_name, long size,
BOOL is_bold, BOOL is_italic);
BOOL draw_font(LPD3DXFONT font, const char * text,
long x_pos, long y_pos, long width, long height,
D3DCOLOR color, DWORD format);
实现:
//-------------------------------------------------------------------
// create font object.
//-------------------------------------------------------------------
BOOL create_font(LPD3DXFONT* font,
const char * face_name, long size,
BOOL is_bold, BOOL is_italic)
{
// check condition
if (g_d3d_device == NULL || face_name == NULL)
return FALSE;
D3DXFONT_DESC _font_desc;
// clear out the font structure
ZeroMemory(&_font_desc, sizeof (D3DXFONT_DESC));
// set the font property
strcpy(_font_desc.FaceName, face_name);
_font_desc.Height = -size;
_font_desc.Weight = is_bold ? 700 : 0;
_font_desc.Italic = is_italic;
// create the font object
if (FAILED(D3DXCreateFontIndirect(g_d3d_device, &_font_desc, font)))
return FALSE;
return TRUE;
}
//-------------------------------------------------------------------
// Draw text.
//-------------------------------------------------------------------
BOOL draw_font(LPD3DXFONT font, const char * text,
long x_pos, long y_pos, long width, long height,
D3DCOLOR color, DWORD format)
{
if (font == NULL)
return FALSE;
// set draw region's width and height
if (width == 0) width = 65536;
if (height == 0) height = 65536;
RECT _rect;
// set draw region
_rect.left = x_pos;
_rect.top = y_pos;
_rect.right = x_pos + width;
_rect.bottom = y_pos + height;
// draw text now
if (FAILED(font->DrawText(NULL, text, -1, &_rect, format, color)))
return FALSE;
return TRUE;
}
// create font object.
//-------------------------------------------------------------------
BOOL create_font(LPD3DXFONT* font,
const char * face_name, long size,
BOOL is_bold, BOOL is_italic)
{
// check condition
if (g_d3d_device == NULL || face_name == NULL)
return FALSE;
D3DXFONT_DESC _font_desc;
// clear out the font structure
ZeroMemory(&_font_desc, sizeof (D3DXFONT_DESC));
// set the font property
strcpy(_font_desc.FaceName, face_name);
_font_desc.Height = -size;
_font_desc.Weight = is_bold ? 700 : 0;
_font_desc.Italic = is_italic;
// create the font object
if (FAILED(D3DXCreateFontIndirect(g_d3d_device, &_font_desc, font)))
return FALSE;
return TRUE;
}
//-------------------------------------------------------------------
// Draw text.
//-------------------------------------------------------------------
BOOL draw_font(LPD3DXFONT font, const char * text,
long x_pos, long y_pos, long width, long height,
D3DCOLOR color, DWORD format)
{
if (font == NULL)
return FALSE;
// set draw region's width and height
if (width == 0) width = 65536;
if (height == 0) height = 65536;
RECT _rect;
// set draw region
_rect.left = x_pos;
_rect.top = y_pos;
_rect.right = x_pos + width;
_rect.bottom = y_pos + height;
// draw text now
if (FAILED(font->DrawText(NULL, text, -1, &_rect, format, color)))
return FALSE;
return TRUE;
}
测试代码:
/***********************************************************************************
PURPOSE:
Test font function.
***********************************************************************************/
#include <windows.h>
#include "core_framework.h"
#include "core_graphics.h"
#include "core_tool.h"
LPD3DXFONT g_font;
//--------------------------------------------------------------------------------
// Initialize data for game.
//--------------------------------------------------------------------------------
BOOL game_init()
{
// Create Direct3D and Direct3DDevice object
if (! create_display(g_hwnd, get_client_width(g_hwnd), get_client_height(g_hwnd), 16, TRUE, FALSE))
return FALSE;
// create font object
if (! create_font(&g_font, "Segoe Script", 32, FALSE, FALSE))
return FALSE;
return TRUE;
}
//--------------------------------------------------------------------------------
// Render every game frame.
//--------------------------------------------------------------------------------
BOOL game_frame()
{
clear_display_buffer(D3DCOLOR_RGBA(0, 0, 0, 255));
if (SUCCEEDED(g_d3d_device->BeginScene()))
{
draw_font(g_font, "reject object-oriented programming",
0, 0, get_client_width(g_hwnd), get_client_height(g_hwnd),
0xFFFFFFFF, DT_CENTER | DT_VCENTER);
g_d3d_device->EndScene();
}
present_display();
return TRUE;
}
//--------------------------------------------------------------------------------
// Release all game resources.
//--------------------------------------------------------------------------------
BOOL game_shutdown()
{
release_com(g_d3d_device);
release_com(g_d3d);
return TRUE;
}
//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE pre_inst, LPSTR cmd_line, int cmd_show)
{
if (! build_window(inst, "MainClass", "MainWindow", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480))
return FALSE;
run_game(game_init, game_frame, game_shutdown);
return 0;
}
PURPOSE:
Test font function.
***********************************************************************************/
#include <windows.h>
#include "core_framework.h"
#include "core_graphics.h"
#include "core_tool.h"
LPD3DXFONT g_font;
//--------------------------------------------------------------------------------
// Initialize data for game.
//--------------------------------------------------------------------------------
BOOL game_init()
{
// Create Direct3D and Direct3DDevice object
if (! create_display(g_hwnd, get_client_width(g_hwnd), get_client_height(g_hwnd), 16, TRUE, FALSE))
return FALSE;
// create font object
if (! create_font(&g_font, "Segoe Script", 32, FALSE, FALSE))
return FALSE;
return TRUE;
}
//--------------------------------------------------------------------------------
// Render every game frame.
//--------------------------------------------------------------------------------
BOOL game_frame()
{
clear_display_buffer(D3DCOLOR_RGBA(0, 0, 0, 255));
if (SUCCEEDED(g_d3d_device->BeginScene()))
{
draw_font(g_font, "reject object-oriented programming",
0, 0, get_client_width(g_hwnd), get_client_height(g_hwnd),
0xFFFFFFFF, DT_CENTER | DT_VCENTER);
g_d3d_device->EndScene();
}
present_display();
return TRUE;
}
//--------------------------------------------------------------------------------
// Release all game resources.
//--------------------------------------------------------------------------------
BOOL game_shutdown()
{
release_com(g_d3d_device);
release_com(g_d3d);
return TRUE;
}
//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE pre_inst, LPSTR cmd_line, int cmd_show)
{
if (! build_window(inst, "MainClass", "MainWindow", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480))
return FALSE;
run_game(game_init, game_frame, game_shutdown);
return 0;
}
点击下载源码和工程
程序截图: