天行健 君子当自强而不息

创建游戏内核(9)【OO改良版】

 

本篇是创建游戏内核(8)【OO改良版】的续篇,关于该内核的细节说明请参阅创建游戏内核(9)


接口:

long get_texture_width(const LPDIRECT3DTEXTURE9 texture);
long get_texture_height(const LPDIRECT3DTEXTURE9 texture);
D3DFORMAT get_texture_format(
const LPDIRECT3DTEXTURE9 texture);

//================================================================================
// Defines for class TEXTURE.
//================================================================================
class TEXTURE
{
public:
    TEXTURE();
    ~TEXTURE();

    IDirect3DTexture9* get_texture();

    BOOL load(
const char* filename, DWORD transparent, D3DFORMAT format);

    BOOL create(DWORD width, DWORD height, D3DFORMAT format);
    BOOL clone(IDirect3DTexture9* texture);

    
void free();
    BOOL is_loaded();

    
long get_width();
    
long get_height();
    D3DFORMAT get_format();
    
    BOOL draw(
long dest_x, long dest_y, 
              
long src_x, long src_y,
              
long width, long height,
              
float x_scale, float y_scale,
              D3DCOLOR color);

private:    
    IDirect3DTexture9*  m_texture;       
// pointer to texture object    
};

typedef TEXTURE* TEXTURE_PTR;
 

实现:

//-------------------------------------------------------------------
// Get width of the texture.
//-------------------------------------------------------------------
long get_texture_width(const LPDIRECT3DTEXTURE9 texture)
{
    D3DSURFACE_DESC _surface_desc;

    
if(texture == NULL)
        
return 0;

    
if(FAILED(texture->GetLevelDesc(0, &_surface_desc)))
        
return 0;

    
return _surface_desc.Width;
}

//-------------------------------------------------------------------
// Get height of the texture.
//-------------------------------------------------------------------
long get_texture_height(const LPDIRECT3DTEXTURE9 texture)
{
    D3DSURFACE_DESC _surface_desc;

    
if(texture == NULL)
        
return 0;

    
if(FAILED(texture->GetLevelDesc(0, &_surface_desc)))
        
return 0;

    
return _surface_desc.Height;
}

//-------------------------------------------------------------------
// Get texture storage format.
//-------------------------------------------------------------------
D3DFORMAT get_texture_format(const LPDIRECT3DTEXTURE9 texture)
{
    D3DSURFACE_DESC _surface_desc;

    
if(texture == NULL)
        
return D3DFMT_UNKNOWN;

    
if(FAILED(texture->GetLevelDesc(0, &_surface_desc)))
        
return D3DFMT_UNKNOWN;

    
return _surface_desc.Format;
}

//---------------------------------------------------------------------------
// Constructor, zero member data.
//---------------------------------------------------------------------------
TEXTURE::TEXTURE()
{
    m_texture = NULL;
}

//-------------------------------------------------------------------
// Destructor, free Direct3D COM object.
//-------------------------------------------------------------------
TEXTURE::~TEXTURE()
{
    free();
}

//-------------------------------------------------------------------
// create texture object from specified file, you can specify transparent
// value and pixel format of the texture.
//-------------------------------------------------------------------
BOOL TEXTURE::load(const char* filename, DWORD transparent, D3DFORMAT format)
{
    
// free Direct3D COM object
    free();

    
// Check if condition is sufficient.
    
    
if(g_d3d_device == NULL || filename == NULL)
        
return FALSE;

    
// Creates a texture from a file
    if(FAILED(D3DXCreateTextureFromFileEx(g_d3d_device, filename, D3DX_DEFAULT, D3DX_DEFAULT,
        D3DX_DEFAULT, 0, format, D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE, D3DX_FILTER_TRIANGLE,
        transparent, NULL, NULL, &m_texture)))
    {
        
return FALSE;
    }

    
return TRUE;
}

//-------------------------------------------------------------------
// Creates a texture resource.
//-------------------------------------------------------------------
BOOL TEXTURE::create(DWORD width, DWORD height, D3DFORMAT format)
{
    free();

    
if(g_d3d_device == NULL)
        
return FALSE;

    
if(FAILED(g_d3d_device->CreateTexture(width, height, 0, 0, format, D3DPOOL_MANAGED, &m_texture, NULL)))
        
return FALSE;

    
return TRUE;
}

//-------------------------------------------------------------------
// Configure a TEXTURE class from an existing IDirect3DTexture9 object 
// instance.
//-------------------------------------------------------------------
BOOL TEXTURE::clone(IDirect3DTexture9* texture)
{
    D3DLOCKED_RECT _src_rect, _dest_rect;
    D3DSURFACE_DESC _surface_desc;

    free();

    
// check condition
    if(g_d3d_device == NULL || texture == NULL)
        
return FALSE;

    
// copy texture over, from source to dest.

    texture->GetLevelDesc(0, &_surface_desc);

    
long _width  = _surface_desc.Width;
    
long _height = _surface_desc.Height;

    g_d3d_device->CreateTexture(_width, _height, 0, 0, _surface_desc.Format, D3DPOOL_MANAGED, &m_texture, NULL);

    
// Locks a rectangle on a texture resource.
    texture->LockRect(0, &_src_rect, NULL, D3DLOCK_READONLY);
    m_texture->LockRect(0, &_dest_rect, NULL, 0);

    memcpy(_dest_rect.pBits, _src_rect.pBits, _src_rect.Pitch * _height);

    
// unlocks a rectangle on a texture resource
    m_texture->UnlockRect(0);
    texture->UnlockRect(0);

    
return TRUE;
}

//-------------------------------------------------------------------
// free Direct3D COM object.
//-------------------------------------------------------------------
void TEXTURE::free()
{
    release_com(m_texture);    
}

//-------------------------------------------------------------------
// Check if loaded texture successfully.
//-------------------------------------------------------------------
BOOL TEXTURE::is_loaded()
{
    
return (m_texture != NULL);
}

//-------------------------------------------------------------------
// Get texture component.
//-------------------------------------------------------------------
IDirect3DTexture9* TEXTURE::get_texture()
{
    
return m_texture;
}

//-------------------------------------------------------------------
// Get width of the texture.
//-------------------------------------------------------------------
long TEXTURE::get_width()
{
    
return get_texture_width(get_texture());
}

//-------------------------------------------------------------------
// Get height of the texture.
//-------------------------------------------------------------------
long TEXTURE::get_height()
{
    
return get_texture_height(get_texture());
}

//-------------------------------------------------------------------
// Get texture storage format.
//-------------------------------------------------------------------
D3DFORMAT TEXTURE::get_format()
{
    
return get_texture_format(get_texture());
}

//-------------------------------------------------------------------
// Draw a 2D portion of texture to device.
//-------------------------------------------------------------------
BOOL TEXTURE::draw(long dest_x, long dest_y, 
                   
long src_x,  long src_y,
                   
long width,  long height,
                   
float x_scale, float y_scale,
                   D3DCOLOR color)
{
    
// check condition
    if(m_texture == NULL)
        
return FALSE;  

    
// If no specify width and height, set with member width and height.

    
if(width == 0)
        width = get_width();

    
if(height == 0)
        height = get_height();

    
// set the portion of the source texture to use for the sprite will to be drawed

    RECT _rect;

    _rect.left   = src_x;
    _rect.top    = src_y;
    _rect.right  = src_x + width;
    _rect.bottom = src_y + height;

    D3DXMATRIX transform_matrix(x_scale,        0,              0,  0,
                                0,              y_scale,        0,  0,
                                0,              0,              0,  0,
                                (
float)dest_x,  (float)dest_y,  0,  0);

    LPD3DXSPRITE _sprite;

    
if(FAILED(D3DXCreateSprite(g_d3d_device, &_sprite)))
        
return FALSE;

    
// Sets the sprite transform
    _sprite->SetTransform(&transform_matrix);

    _sprite->Begin(0);

    
// Adds a sprite to the list of batched sprites.
    if(FAILED(_sprite->Draw(m_texture, &_rect, NULL, NULL, color)))
        
return FALSE;

    _sprite->End();

    
return TRUE;
}
 


测试代码:
/*****************************************************************************
PURPOSE:
    Test for class TEXTURE.
*****************************************************************************/


#include "core_framework.h"
#include "core_graphics.h"

#pragma warning(disable : 4996)

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

#define VERTEX_FVF   (D3DFVF_XYZ | D3DFVF_TEX1)

//===========================================================================
// Defines class APP which public inherits from class FRAMEWORK.
//===========================================================================
class APP : public FRAMEWORK
{
public:    
    BOOL init()
    {
        
// initialize vertex data
        VERTEX _verts[] = {
          { -150.0f,  150.0f, 0.0f, 0.0f, 0.0f },
          {  150.0f,  150.0f, 0.0f, 1.0f, 0.0f },
          { -150.0f, -150.0f, 0.0f, 0.0f, 1.0f },
          {  150.0f, -150.0f, 0.0f, 1.0f, 1.0f }
        }; 

        
// 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;   

        
// set perspective projection transform matrix
        set_perspective(D3DX_PI/4.0f, 1.33333f, 1.0f, 1000.0f);

        D3DXMATRIX _mat_view;

        
// create and set the view matrix
        D3DXMatrixLookAtLH(&_mat_view, 
                           &D3DXVECTOR3(0.0, 0.0, -500.0),
                           &D3DXVECTOR3(0.0f, 0.0f, 0.0f), 
                           &D3DXVECTOR3(0.0f, 1.0f, 0.0f));

        g_d3d_device->SetTransform(D3DTS_VIEW, &_mat_view);

        BYTE* _vertex_ptr;

        
// create the vertex buffer and set data
        g_d3d_device->CreateVertexBuffer(sizeof(_verts), 0, VERTEX_FVF, D3DPOOL_DEFAULT, &m_vertex_buffer, NULL);  

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

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

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

        
// load the texture map
        if(! m_texture.load("texture.jpg", 0, D3DFMT_UNKNOWN))
            
return FALSE;
        
        
return TRUE;
    }

    BOOL frame()
    {
        clear_display_buffer(D3DCOLOR_RGBA(0, 0, 0, 255));

        
if(SUCCEEDED(g_d3d_device->BeginScene()))
        {
            
// set world tranformation

            D3DXMATRIX _mat_world;

            D3DXMatrixRotationZ(&_mat_world, (
float) (timeGetTime() / 1000.0));

            g_d3d_device->SetTransform(D3DTS_WORLD, &_mat_world);

            
// set the vertex stream, shader, and texture.

            // binds a vertex buffer to a device data stream
            g_d3d_device->SetStreamSource(0, m_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, m_texture.get_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;
    }

    BOOL shutdown()
    {
        release_com(m_vertex_buffer);
        release_com(g_d3d_device);
        release_com(g_d3d);

        
return TRUE;
    }
   
private:
    TEXTURE                     m_texture; 
    LPDIRECT3DVERTEXBUFFER9     m_vertex_buffer;
};


int PASCAL WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
    APP app;

    
if(! build_window(inst, "MainClass", "MainWindow", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480))
        
return -1;
    
    app.run();

    
return 0;
}

posted on 2007-10-06 20:07 lovedday 阅读(417) 评论(0)  编辑 收藏 引用


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


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论