天行健 君子当自强而不息

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

 

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


接口:

class FONT
{
public:
    FONT();
    ~FONT();

    ID3DXFont* get_font();
    
void free();

    BOOL create(
const char* face_name, long size, BOOL is_bold, BOOL is_italic);    

    BOOL draw(
const char* text, 
              
long x_pos, long y_pos, long width, long height,
              D3DCOLOR color, DWORD format);    

private:
    ID3DXFont*  m_font;
};

typedef FONT* FONT_PTR;
 

实现:

//-------------------------------------------------------------------
// Constructor, initialize data member.
//-------------------------------------------------------------------
FONT::FONT()
{
    m_font = NULL;
}

//-------------------------------------------------------------------
// Destructor, release font resource.
//-------------------------------------------------------------------
FONT::~FONT()
{
    free();
}

//-------------------------------------------------------------------
// Get pointer to font object.
//-------------------------------------------------------------------
ID3DXFont* FONT::get_font()
{
    
return m_font;
}

//-------------------------------------------------------------------
// Release font resource.
//-------------------------------------------------------------------
void FONT::free()
{
    release_com(m_font);
}

//-------------------------------------------------------------------
// create font object.
//-------------------------------------------------------------------
BOOL FONT::create(const char* face_name, long size, BOOL is_bold, BOOL is_italic)
{
    D3DXFONT_DESC _font_desc;

    
// check condition first
    if(g_d3d_device == NULL || face_name == NULL)
        
return FALSE;
    
    
// 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, &m_font)))
        
return FALSE;

    
return TRUE;
}

//-------------------------------------------------------------------
// Draw text.
//-------------------------------------------------------------------
BOOL FONT::draw(const char* text, 
                
long x_pos, long y_pos, long width, long height, 
                D3DCOLOR color, DWORD format)
{
    
if(m_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(m_font->DrawText(NULL, text, -1, &_rect, format, color)))
        
return FALSE;

    
return TRUE;
}


测试代码:

/***********************************************************************************
PURPOSE:
    Test font class.
***********************************************************************************/


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

class APP : public FRAMEWORK
{
public:
    
//--------------------------------------------------------------------------------
    // Initialize data for game.
    //--------------------------------------------------------------------------------
    BOOL 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;    

        m_font.create("Segoe Script", 32, FALSE, FALSE);
        
        
return TRUE;
    }

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

        
if(SUCCEEDED(g_d3d_device->BeginScene()))
        {
            m_font.draw("nothing to say", 
                      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 shutdown()
    {
        release_com(g_d3d_device);
        release_com(g_d3d);

        
return TRUE;
    }

private:
    FONT m_font;
};

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

    APP app;

    app.run();
    
    
return 0;
}

posted on 2007-10-07 01:00 lovedday 阅读(230) 评论(0)  编辑 收藏 引用


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


公告

导航

统计

常用链接

随笔分类(178)

3D游戏编程相关链接

搜索

最新评论