本篇是创建游戏内核(6)【接口与实现分离版】的续篇,关于该内核的细节说明请参考创建游戏内核(7),这个版本主要是按照功能划分模块的思想,并严格按照接口与实现相分离的原则来写的,没有用面向对象的思想来写,没有继承没有多态。大家可以对比两个版本,比较优劣。
接口:
void set_material_diffuse(D3DMATERIAL9* material,
unsigned char red, unsigned char green, unsigned char blue);
void get_material_diffuse(const D3DMATERIAL9* material,
unsigned char* red, unsigned char* green, unsigned char* blue);
void set_material_ambient(D3DMATERIAL9* material,
unsigned char red, unsigned char green, unsigned char blue);
void get_material_ambient(const D3DMATERIAL9* material,
unsigned char* red, unsigned char* green, unsigned char* blue);
void set_material_specular(D3DMATERIAL9* material,
unsigned char red, unsigned char green, unsigned char blue);
void get_material_specular(const D3DMATERIAL9* material,
unsigned char* red, unsigned char* green, unsigned char* blue);
void set_material_emissive(D3DMATERIAL9* material,
unsigned char red, unsigned char green, unsigned char blue);
void get_material_emissive(const D3DMATERIAL9* material,
unsigned char *red, unsigned char *green, unsigned char *blue);
void set_material_power(D3DMATERIAL9* material, float power);
float get_material_power(const D3DMATERIAL9* material);
实现:
//-------------------------------------------------------------------------
// set diffuse color.
//-------------------------------------------------------------------------
void set_material_diffuse(D3DMATERIAL9* material,
unsigned char red, unsigned char green, unsigned char blue)
{
material->Diffuse.r = red / 255.0f;
material->Diffuse.g = green / 255.0f;
material->Diffuse.b = blue / 255.0f;
}
//-------------------------------------------------------------------------
// Get diffuse color.
//-------------------------------------------------------------------------
void get_material_diffuse(const D3DMATERIAL9* material,
unsigned char* red, unsigned char* green, unsigned char* blue)
{
if(red != NULL)
*red = (unsigned char)(255.0 * material->Diffuse.r);
if(green != NULL)
*green = (unsigned char)(255.0 * material->Diffuse.g);
if(blue != NULL)
*blue = (unsigned char)(255.0 * material->Diffuse.b);
}
//-------------------------------------------------------------------------
// set ambient color.
//-------------------------------------------------------------------------
void set_material_ambient(D3DMATERIAL9* material,
unsigned char red, unsigned char green, unsigned char blue)
{
material->Ambient.r = red / 255.0f;
material->Ambient.g = green / 255.0f;
material->Ambient.b = blue / 255.0f;
}
//-------------------------------------------------------------------------
// Get ambient color.
//-------------------------------------------------------------------------
void get_material_ambient(const D3DMATERIAL9* material,
unsigned char* red, unsigned char* green, unsigned char* blue)
{
if(red != NULL)
*red = (unsigned char)(255.0f * material->Ambient.r);
if(green != NULL)
*green = (unsigned char)(255.0f * material->Ambient.g);
if(blue != NULL)
*blue = (unsigned char)(255.0f * material->Ambient.b);
}
//-------------------------------------------------------------------------
// set specular color.
//-------------------------------------------------------------------------
void set_material_specular(D3DMATERIAL9* material,
unsigned char red, unsigned char green, unsigned char blue)
{
material->Specular.r = red / 255.0f;
material->Specular.g = green / 255.0f;
material->Specular.b = blue / 255.0f;
}
//-------------------------------------------------------------------------
// Get specular color.
//-------------------------------------------------------------------------
void get_material_specular(const D3DMATERIAL9* material,
unsigned char* red, unsigned char* green, unsigned char* blue)
{
if(red != NULL)
*red = (unsigned char)(255.0 * material->Specular.r);
if(green != NULL)
*green = (unsigned char)(255.0 * material->Specular.g);
if(blue != NULL)
*blue = (unsigned char)(255.0 * material->Specular.b);
}
//-------------------------------------------------------------------------
// set emissive color.
//-------------------------------------------------------------------------
void set_material_emissive(D3DMATERIAL9* material,
unsigned char red, unsigned char green, unsigned char blue)
{
material->Emissive.r = red / 255.0f;
material->Emissive.g = green / 255.0f;
material->Emissive.b = blue / 255.0f;
}
//-------------------------------------------------------------------------
// Get emmisive color.
//-------------------------------------------------------------------------
void get_material_emissive(const D3DMATERIAL9* material,
unsigned char *red, unsigned char *green, unsigned char *blue)
{
if(red != NULL)
*red = (unsigned char)(255.0 * material->Emissive.r);
if(green != NULL)
*green = (unsigned char)(255.0 * material->Emissive.g);
if(blue != NULL)
*blue = (unsigned char)(255.0 * material->Emissive.b);
}
//-------------------------------------------------------------------------
// set power for material, which specifying the sharpness of specular
// highlights.
//-------------------------------------------------------------------------
void set_material_power(D3DMATERIAL9* material, float power)
{
material->Power = power;
}
//-------------------------------------------------------------------------
// Get power for material, which specifying the sharpness of specular
// highlights.
//-------------------------------------------------------------------------
float get_material_power(const D3DMATERIAL9* material)
{
return material->Power;
}