本篇是创建游戏内核(4)【接口与实现分离版】的续篇,关于该内核的细节说明请参考创建游戏内核(5),这个版本主要是按照功能划分模块的思想,并严格按照接口与实现相分离的原则来写的,没有用面向对象的思想来写,没有继承没有多态。大家可以对比两个版本,比较优劣。
接口:
void update_world_matrix(BOOL use_billboard, D3DXMATRIX* mat_world,
const D3DXMATRIX* mat_scale, const D3DXMATRIX* mat_rotation, const D3DXMATRIX* mat_translation,
const D3DXMATRIX* mat_combine1, const D3DXMATRIX* mat_combine2);
实现:
//-------------------------------------------------------------------------
// Update world transformation matrix.
//-------------------------------------------------------------------------
void update_world_matrix(BOOL use_billboard, D3DXMATRIX* mat_world,
const D3DXMATRIX* mat_scale, const D3DXMATRIX* mat_rotation, const D3DXMATRIX* mat_translation,
const D3DXMATRIX* mat_combine1, const D3DXMATRIX* mat_combine2)
{
D3DXMATRIX _mat_view, _mat_transposed;
// setup billboarding matrix
if(use_billboard)
{
if(g_d3d_device)
{
g_d3d_device->GetTransform(D3DTS_VIEW, &_mat_view);
D3DXMatrixTranspose(&_mat_transposed, &_mat_view);
_mat_transposed._41 = _mat_transposed._42 = _mat_transposed._43 = 0.0;
_mat_transposed._14 = _mat_transposed._24 = _mat_transposed._34 = 0.0;
}
else
D3DXMatrixIdentity(&_mat_transposed);
}
// combine scaling and rotation matrices first
D3DXMatrixMultiply(mat_world, mat_scale, mat_rotation);
// apply billboard matrix
if(use_billboard)
D3DXMatrixMultiply(mat_world, mat_world, &_mat_transposed);
// combine with translation matrix
D3DXMatrixMultiply(mat_world, mat_world, mat_translation);
// combine with combined matrices (if any)
if(mat_combine1)
D3DXMatrixMultiply(mat_world, mat_world, mat_combine1);
if(mat_combine2)
D3DXMatrixMultiply(mat_world, mat_world, mat_combine2);
}