天行健 君子当自强而不息

创建游戏内核(10)【接口与实现分离版】

 

本篇是创建游戏内核(9)【接口与实现分离版】的续篇,关于该内核的细节说明请参考创建游戏内核(10),这个版本主要是按照功能划分模块的思想,并严格按照接口与实现相分离的原则来写的,没有用面向对象的思想来写,没有继承没有多态。大家可以对比两个版本,比较优劣。


接口:

BOOL create_display(HWND hwnd, long width, long height, char bpp, BOOL is_window, BOOL use_zbuffer);
BOOL present_display();
BOOL set_texture(
short sample, IDirect3DTexture9* texture);
BOOL set_perspective(
float fov, float aspect, float near_z, float far_z);

BOOL clear_display_buffer(
long color);
BOOL clear_display_zbuffer(
float zbuffer);
BOOL clear_display(
long color, float zbuffer);

UINT get_display_width();
UINT get_display_height();
UINT get_refresh_rate();
D3DFORMAT get_d3d_format();
BOOL get_display_format(D3DFORMAT* format);
BOOL get_backbuffer_format(D3DFORMAT* format);
BOOL get_display_mode_info(UINT mode_index, D3DDISPLAYMODE* display_mode, D3DFORMAT format);
char get_display_bpp(D3DFORMAT format);

void extract_rgb(D3DCOLOR color, unsigned char* red, unsigned char* green, unsigned char* blue);

BOOL is_window_mode();
BOOL support_hardware();
BOOL use_zbuffer();
BOOL display_format_support(D3DFORMAT format, BOOL is_window, BOOL support_hal);

BOOL set_ambient_light(unsigned 
char red, unsigned char green, unsigned char blue);
BOOL get_ambient_light(unsigned 
char* red, unsigned char* green, unsigned char* blue);

BOOL enalbe_alpha_blending(BOOL enable, DWORD source, DWORD dest);
BOOL enable_alpha_testing(BOOL enable);
 

实现:

#include "core_common.h"
#include "core_graphics.h"
#include "core_tool.h"

IDirect3D9*         g_d3d;
IDirect3DDevice9*   g_d3d_device;

///////////////////////////////////// functions for D3D display /////////////////////////////////////

//----------------------------------------------------------------------------------------
// Get D3D present paramters.
//----------------------------------------------------------------------------------------
static BOOL _get_present_para(D3DPRESENT_PARAMETERS* present_para)
{
    IDirect3DSwapChain9* _swap_chain;

    
if(FAILED(g_d3d_device->GetSwapChain(1, &_swap_chain)))
        
return FALSE;

    _swap_chain->GetPresentParameters(present_para);

    
return TRUE;
}

//----------------------------------------------------------------------------------------
// Create Direct3D and Direct3DDevice object.
//----------------------------------------------------------------------------------------
BOOL create_display(HWND hwnd, long width, long height, char bpp, BOOL is_window, BOOL use_zbuffer)
{              
    
if(hwnd == NULL)
        
return FALSE;

    
if((g_d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
        
return FALSE;

    
// 1) get current display _format

    D3DDISPLAYMODE _display_mode;  

    
if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &_display_mode)))
        
return FALSE;

    
// 2) Configure width, height, bpp; resizing window.

    RECT _client_rect;
    
int _display_width, _display_height;

    
// configure width
    if(width == 0)
    {
        
if(! is_window)     // default to screen width if fullscreen
            _display_width = _display_mode.Width;
        
else                // otherwise grab from client size
        {
            GetClientRect(hwnd, &_client_rect);
            _display_width = _client_rect.right;
        }
    }
    
else
        _display_width = width;

    
// configure height
    if(height == 0)
    {
        
if(! is_window)     // default to screen height if fullscreen
            _display_height = _display_mode.Height;
        
else                // Otherwise grab from client size
        {
            GetClientRect(hwnd, &_client_rect);
            _display_height = _client_rect.bottom;
        }
    }
    
else
        _display_height = height;

    
// configure bpp
    if(bpp == 0 || is_window)
    {
        
// get display bpp
        if(! (bpp = get_display_bpp(_display_mode.Format)))
            
return FALSE;
    }

    
// resize client window if using windowed mode
    if(is_window)
        resize_window(hwnd, _display_width, _display_height);

    
// 3) setup presentation parameters

    D3DPRESENT_PARAMETERS   _present_para;

    
// clear presentation structure
    ZeroMemory(&_present_para, sizeof(D3DPRESENT_PARAMETERS));

    
// default to no hardware acceleraton detected
    BOOL _support_hal = FALSE;

    
// setup windowed or fullscreen usage
    if(is_window)
    {
        _present_para.Windowed         = TRUE;
        _present_para.SwapEffect       = D3DSWAPEFFECT_DISCARD;
        _present_para.BackBufferFormat = _display_mode.Format;

        
// see if video card supports hardware acceleration
        if(! display_format_support(_display_mode.Format, TRUE, TRUE))
            
return FALSE;

        _support_hal = TRUE;
    }
    
else    // fullscreen mode
    {
        D3DFORMAT _format, _alt_format;

        _present_para.Windowed                   = FALSE;
        _present_para.SwapEffect                 = D3DSWAPEFFECT_FLIP;
        _present_para.BackBufferWidth            = _display_width;
        _present_para.BackBufferHeight           = _display_height;
        _present_para.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
        _present_para.PresentationInterval       = D3DPRESENT_INTERVAL_ONE;

        
// figure display mode to use
        if(bpp == 32)
        {
            _format     = D3DFMT_X8R8G8B8;
            _alt_format = D3DFMT_X8R8G8B8;
        }
        
else if(bpp == 24)
        {
            _format     = D3DFMT_R8G8B8;
            _alt_format = D3DFMT_R8G8B8;
        }
        
else if(bpp == 16)
        {
            _format     = D3DFMT_R5G6B5;
            _alt_format = D3DFMT_X1R5G5B5;
        }
        
else if(bpp = 8)
        {
            _format     = D3DFMT_P8;
            _alt_format = D3DFMT_P8;
        }

        
// check for hal device
        if(display_format_support(_format, FALSE, TRUE))
            _support_hal = TRUE;
        
else
        {
            
// check for hal device in alternate _format
            if(display_format_support(_alt_format, FALSE, TRUE))
            {
                _support_hal = TRUE;
                _format = _alt_format;
            }
            
else
            {
                
// check for emulation device in alternate _format
                if(! display_format_support(_alt_format, FALSE, FALSE))
                    
return FALSE;
                
else
                    _format = _alt_format;
            }
        }

        _present_para.BackBufferFormat = _format;
    }

    
// setup zbuffer _format - 16bit
    if(use_zbuffer)
    {
        _present_para.EnableAutoDepthStencil = TRUE;
        _present_para.AutoDepthStencilFormat = D3DFMT_D16;
    }
    
else
        _present_para.EnableAutoDepthStencil = FALSE;

    
// create the Direct3D device object
    if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, _support_hal ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF,
        hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &_present_para, &g_d3d_device)))
    {
        
if(! use_zbuffer)
            
return FALSE;

        
// now, create Direct3D device no use zbuffer.

        use_zbuffer = FALSE;

        _present_para.EnableAutoDepthStencil = FALSE;

        
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, _support_hal ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF,
            hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &_present_para, &g_d3d_device)))
        {
            
return FALSE;
        }
    }

    
// 4) set rendering state

    g_d3d_device->SetRenderState(D3DRS_LIGHTING, FALSE);
    g_d3d_device->SetRenderState(D3DRS_ZENABLE, use_zbuffer ? D3DZB_TRUE : D3DZB_FALSE);
   
    enalbe_alpha_blending(FALSE, 0, 0);
    enable_alpha_testing(FALSE);

    
// enable texture rendering stages and filter types
    g_d3d_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    g_d3d_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
    g_d3d_device->SetTextureStageState(0, D3DTSS_COLOROP,   D3DTOP_MODULATE);

    
// set the sampler state value
    g_d3d_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
    g_d3d_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);

    
// 5) set color and transform matrix

    // set default ambient color to white
    set_ambient_light(255, 255, 255);

    
// calculate the _aspect ratio based on window size
    float _aspect = (float) _display_height / _display_width;
    
    set_perspective(D3DX_PI/4, _aspect, 1.0f, 10000.0f);

    
return TRUE;
}

//-------------------------------------------------------------------
// Presents the contents of the next buffer in the sequence of back 
// buffers owned by the device.
//-------------------------------------------------------------------
BOOL present_display()
{
    
if(g_d3d_device == NULL)
        
return FALSE;

    
if(FAILED(g_d3d_device->Present(NULL, NULL, NULL, NULL)))
        
return FALSE;

    
return TRUE;
}

//-------------------------------------------------------------------
// set texture for Direct3D device.
//-------------------------------------------------------------------
BOOL set_texture(short sample, IDirect3DTexture9* texture)
{
    
// error checking
    if(g_d3d_device == NULL || sample < 0 || sample > 7)
        
return FALSE;

    
if(FAILED(g_d3d_device->SetTexture(sample, texture)))
        
return FALSE;

    
return TRUE;
}

//-------------------------------------------------------------------
// set perspective projection transform matrix.
//-------------------------------------------------------------------
BOOL set_perspective(float fov, float aspect, float near_z, float far_z)
{
    D3DXMATRIX _mat_proj;

    
if(g_d3d_device == NULL)
        
return FALSE;

    
// builds a left-handed perspective projection matrix based on a field of view
    D3DXMatrixPerspectiveFovLH(&_mat_proj, fov, aspect, near_z, far_z);

    
// set projection matrix
    if(FAILED(g_d3d_device->SetTransform(D3DTS_PROJECTION, &_mat_proj)))
        
return FALSE;

    
return TRUE;
}

//-------------------------------------------------------------------
// clear display buffer with specified color.
//-------------------------------------------------------------------
BOOL clear_display_buffer(long color)
{
    
if(g_d3d_device == NULL)
        
return FALSE;

    
if(FAILED(g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET, color, 1.0f, 0)))
        
return FALSE;

    
return TRUE;
}

//-------------------------------------------------------------------
// clear z-buffer.
//-------------------------------------------------------------------
BOOL clear_display_zbuffer(float zbuffer)
{        
    
if(!g_d3d_device || !use_zbuffer())
        
return FALSE;

    
if(FAILED(g_d3d_device->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0, zbuffer, 0)))
        
return FALSE;

    
return TRUE;
}

//-------------------------------------------------------------------
// clear screen, if using z-buffer, then clear z-buffer too.
//-------------------------------------------------------------------
BOOL clear_display(long color, float zbuffer)
{
    
if(g_d3d_device == NULL)
        
return FALSE;    

    
// only clear screen if no zbuffer
    if(! use_zbuffer())
        
return clear_display_buffer(color);

    
// clear display buffer and zbuffer
    if(FAILED(g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, color, zbuffer, 0)))
        
return FALSE;

    
return TRUE;
}

//----------------------------------------------------------------------------------------
// Return width of adapter display.
//----------------------------------------------------------------------------------------
UINT get_display_width()
{
    D3DDISPLAYMODE _display_mode;

    g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &_display_mode);

    
return _display_mode.Width;
}

//----------------------------------------------------------------------------------------
// Return height of adapter display.
//----------------------------------------------------------------------------------------
UINT get_display_height()
{
    D3DDISPLAYMODE _display_mode;

    g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &_display_mode);

    
return _display_mode.Height;
}

//----------------------------------------------------------------------------------------
// Return refresh rate of adapter display.
//----------------------------------------------------------------------------------------
UINT get_refresh_rate()
{
    D3DDISPLAYMODE _display_mode;

    g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &_display_mode);

    
return _display_mode.RefreshRate;
}

//----------------------------------------------------------------------------------------
// Return Direct3D format of adapter display.
//----------------------------------------------------------------------------------------
D3DFORMAT get_d3d_format()
{
    D3DDISPLAYMODE _display_mode;

    g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &_display_mode);

    
return _display_mode.Format;
}

//----------------------------------------------------------------------------------------
// Get primary surface buffer format.
//----------------------------------------------------------------------------------------
BOOL get_display_format(D3DFORMAT* format)
{
    
// get primary buffer display mode
    D3DDISPLAYMODE _display_mode;
    
    
if(FAILED(g_d3d_device->GetDisplayMode(1, &_display_mode)))
        
return FALSE;
    
    *format = _display_mode.Format;

    
return TRUE;
}

//----------------------------------------------------------------------------------------
// Get back surface buffer 
//----------------------------------------------------------------------------------------
BOOL get_backbuffer_format(D3DFORMAT* format)
{
    D3DPRESENT_PARAMETERS _present_para;

    
if(! _get_present_para(&_present_para))
        
return FALSE;

    *format = _present_para.BackBufferFormat;

    
return TRUE;
}

//-------------------------------------------------------------------
// Get display mode information.
//-------------------------------------------------------------------
BOOL get_display_mode_info(UINT mode_index, D3DDISPLAYMODE* display_mode, D3DFORMAT format)
{
    
if(g_d3d == NULL)
        
return FALSE;

    
// get the number of display modes avaible on this adapter
    UINT _max_mode = g_d3d->GetAdapterModeCount(D3DADAPTER_DEFAULT, format);

    
if(mode_index >= _max_mode)
        
return FALSE;

    
// Queries the device to determine whether the specified adapter supports the requested format and display mode. 
    // This method could be used in a loop to enumerate all the available adapter modes. 
    if(FAILED(g_d3d->EnumAdapterModes(D3DADAPTER_DEFAULT, format, mode_index, display_mode)))
        
return FALSE;

    
return TRUE;
}

//----------------------------------------------------------------------------------------
// Get display mode BPP.
//----------------------------------------------------------------------------------------
char get_display_bpp(D3DFORMAT format)
{
    
switch(format)
    {
    
// 32 bit modes
    case D3DFMT_A8R8G8B8:
    
case D3DFMT_X8R8G8B8:
        
return 32;

    
// 24 bit modes
    case D3DFMT_R8G8B8:
        
return 24;

    
// 16 bit modes
    case D3DFMT_R5G6B5:
    
case D3DFMT_X1R5G5B5:
    
case D3DFMT_A1R5G5B5:
    
case D3DFMT_A4R4G4B4:
        
return 16;

    
// 8 bit modes
    case D3DFMT_A8P8:
    
case D3DFMT_P8:
        
return 8;
    }

    
return 0;
}

//----------------------------------------------------------------------------------------
// Extract red, green, blue from D3D color value.
//----------------------------------------------------------------------------------------
void extract_rgb(D3DCOLOR color, unsigned char* red, unsigned char* green, unsigned char* blue)
{
    
if(red   != NULL) *red   = unsigned char((color >> 16) & 0xff);
    
if(green != NULL) *green = unsigned char((color >> 8) & 0xff);
    
if(blue != NULL)  *blue  = unsigned char(color & 0xff);
}

//----------------------------------------------------------------------------------------
// Check whether D3D display mode is windowed or full-screen.
//----------------------------------------------------------------------------------------
BOOL is_window_mode()
{
    D3DPRESENT_PARAMETERS _present_para;

    
if(! _get_present_para(&_present_para))
        
return FALSE;

    
return _present_para.Windowed;
}

//----------------------------------------------------------------------------------------
// Check whether D3D support hardware accelerator in current setting.
//----------------------------------------------------------------------------------------
BOOL support_hardware()
{
    D3DFORMAT _display_format, _backbuffer_format;

    get_display_format(&_display_format);
    get_backbuffer_format(&_backbuffer_format);

    
if(FAILED(g_d3d->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 
        _display_format, _backbuffer_format, is_window_mode())))
    {
        
return FALSE;
    }

    
return TRUE;
}

//----------------------------------------------------------------------------------------
// Check whether D3D device use zbuffer.
//----------------------------------------------------------------------------------------
BOOL use_zbuffer()
{
    D3DPRESENT_PARAMETERS _present_para;

    
if(! _get_present_para(&_present_para))
        
return FALSE;

    
return _present_para.EnableAutoDepthStencil;
}

//-------------------------------------------------------------------
// Check whether specified display mode is supported on this adapter. 
//-------------------------------------------------------------------
BOOL display_format_support(D3DFORMAT format, BOOL is_window, BOOL support_hal)
{
    
// verifies whether a hardware accelerated device type can be used on this adapter
    if(FAILED(g_d3d->CheckDeviceType(D3DADAPTER_DEFAULT, support_hal ? D3DDEVTYPE_HAL : D3DDEVTYPE_REF,
                                     format, format, is_window)))
    {
        
return FALSE;
    }

    
return TRUE;
}

//-------------------------------------------------------------------
// set ambient light color.
//-------------------------------------------------------------------
BOOL set_ambient_light(unsigned char red, unsigned char green, unsigned char blue)
{
    
if(g_d3d_device == NULL)
        
return FALSE;

    D3DCOLOR _color = D3DCOLOR_XRGB(red, green, blue);

    
if(FAILED(g_d3d_device->SetRenderState(D3DRS_AMBIENT, _color)))
        
return FALSE;

    
return TRUE;
}

//-------------------------------------------------------------------
// Get ambient light color.
//-------------------------------------------------------------------
BOOL get_ambient_light(unsigned char* red, unsigned char* green, unsigned char* blue)
{
    D3DCOLOR _color;

    
if(FAILED(g_d3d_device->GetRenderState(D3DRS_AMBIENT, &_color)))
        
return FALSE;    

    extract_rgb(_color, red, green, blue);    

    
return TRUE;
}

//-------------------------------------------------------------------
// Enable or disable alpha blending.
//-------------------------------------------------------------------
BOOL enalbe_alpha_blending(BOOL enable, DWORD source, DWORD dest)
{
    
if(g_d3d_device == NULL)
        
return FALSE;

    
// enable or disable alpha blending
    if(FAILED(g_d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE, enable)))
        
return FALSE;

    
// set blend type
    if(enable)
    {
        g_d3d_device->SetRenderState(D3DRS_SRCBLEND,  source);
        g_d3d_device->SetRenderState(D3DRS_DESTBLEND, dest);
    }

    
return TRUE;
}

//-------------------------------------------------------------------
// Enable or disable alpha testing.
//-------------------------------------------------------------------
BOOL enable_alpha_testing(BOOL enable)
{
    
if(g_d3d_device == NULL)
        
return FALSE;

    
if(FAILED(g_d3d_device->SetRenderState(D3DRS_ALPHATESTENABLE, enable)))
        
return FALSE;

    
// set alpha testing type
    if(enable)
    {
        g_d3d_device->SetRenderState(D3DRS_ALPHAREF, 0x08);
        g_d3d_device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
    }

    
return TRUE;
}

测试代码:
/***********************************************************************************
PURPOSE:
    Test D3D base display function.
***********************************************************************************/


#include <windows.h>
#include "core_framework.h"
#include "core_graphics.h"
#include "core_tool.h"

// The 2D vertex format and descriptor
typedef struct
{
    
float x, y, z;  // 2D coordinates
    float rhw;      // rhw
    float u, v;     // texture coordinates
} VERTEX;

#define VERTEX_FVF   (D3DFVF_XYZRHW | D3DFVF_TEX1)

IDirect3DVertexBuffer9* g_vertex_buffer;
IDirect3DTexture9*      g_texture;

//--------------------------------------------------------------------------------
// Initialize data for game.
//--------------------------------------------------------------------------------
BOOL game_init()
{
    
// initialize vertex data
    VERTEX _verts[] = {
      { 20.0f,  40.0f, 1.0f, 1.0f, 0.0f, 0.0f },
      { 620.0f, 40.0f, 1.0f, 1.0f, 1.0f, 0.0f },
      { 20.0f,  440.0f, 1.0f, 1.0f, 0.0f, 1.0f },
      { 620.0f, 440.0f, 1.0f, 1.0f, 1.0f, 1.0f }
    }; 

    
// Create Direct3D and Direct3DDevice object
    if(! create_display(g_hwnd, get_window_width(g_hwnd), get_window_height(g_hwnd), 16, TRUE, FALSE))
        
return FALSE;    

    BYTE* _vertex_ptr;

    
// create the vertex buffer and set data
    g_d3d_device->CreateVertexBuffer(sizeof(VERTEX) * 4, 0, VERTEX_FVF, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);  

    
// locks a range of vertex data and obtains a pointer to the vertex buffer memory
    g_vertex_buffer->Lock(0, 0, (void**)&_vertex_ptr, 0);

    memcpy(_vertex_ptr, _verts, 
sizeof(_verts));

    
// unlocks vertex data
    g_vertex_buffer->Unlock();

    
// load the texture map
    D3DXCreateTextureFromFile(g_d3d_device, "test.jpg", &g_texture);

    
// assigns a texture to a stage for a device
    set_texture(0, g_texture);

    
return TRUE;
}

//--------------------------------------------------------------------------------
// Render every game frame.
//--------------------------------------------------------------------------------
BOOL game_frame()
{
    clear_display_buffer(D3DCOLOR_RGBA(0, 0, 0, 255));

    
if(SUCCEEDED(g_d3d_device->BeginScene()))
    {
        
// set the vertex stream, shader, and texture.

        // binds a vertex buffer to a device data stream
        g_d3d_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(VERTEX));

        
// set the current vertex stream declation
        g_d3d_device->SetFVF(VERTEX_FVF);

        
// assigns a texture to a stage for a device
        set_texture(0, g_texture);

        
// renders a sequence of noindexed, geometric primitives of the specified type from the current set
        // of data input stream.
        g_d3d_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

        
// release texture
        set_texture(0, NULL);

        g_d3d_device->EndScene();
    }

    present_display();

    
return TRUE;
}

//--------------------------------------------------------------------------------
// Release all game resources.
//--------------------------------------------------------------------------------
BOOL game_shutdown()
{
    release_com(g_vertex_buffer);
    release_com(g_texture);
    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;
}
 

点击下载源码和工程

程序截图:


posted on 2007-10-04 00:03 lovedday 阅读(1351) 评论(1)  编辑 收藏 引用

评论

# re: 创建游戏内核(10)【接口与实现分离版】 2007-12-10 00:38 王春

好  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论