本篇是創(chuàng)建游戲內(nèi)核(11)【接口與實(shí)現(xiàn)分離版】的續(xù)篇,關(guān)于該內(nèi)核的細(xì)節(jié)說(shuō)明請(qǐng)參考創(chuàng)建游戲內(nèi)核(12),這個(gè)版本主要是按照功能劃分模塊的思想,并嚴(yán)格按照接口與實(shí)現(xiàn)相分離的原則來(lái)寫(xiě)的,沒(méi)有用面向?qū)ο蟮乃枷雭?lái)寫(xiě),沒(méi)有繼承沒(méi)有多態(tài)。大家可以對(duì)比兩個(gè)版本,比較優(yōu)劣。
接口:
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);
實(shí)現(xiàn):
//-------------------------------------------------------------------
// 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;
}
測(cè)試代碼:
/***********************************************************************************
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;
}
點(diǎn)擊下載源碼和工程
程序截圖:
