• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            創建游戲內核(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 閱讀(429) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            免费一级做a爰片久久毛片潮| 国产一级做a爰片久久毛片| 久久久久久一区国产精品| 久久精品一区二区影院| 亚洲精品NV久久久久久久久久 | 久久天天躁夜夜躁狠狠| 无码人妻久久一区二区三区免费丨 | 2022年国产精品久久久久| 精品久久久久久无码中文字幕| 久久亚洲国产精品123区| 婷婷久久香蕉五月综合加勒比| 日本精品久久久久中文字幕8| 亚洲午夜久久久| 国产精品九九久久免费视频| 国产免费久久精品99re丫y| 久久精品国产亚洲综合色| 久久人与动人物a级毛片| 国产亚洲精久久久久久无码AV| 亚洲国产精品无码久久98| 久久精品无码一区二区app| 激情伊人五月天久久综合| 久久只有这里有精品4| 国产激情久久久久影院| www久久久天天com| 久久婷婷五月综合国产尤物app| 欧美一级久久久久久久大片| 69久久夜色精品国产69| 久久无码人妻一区二区三区| 一本久久精品一区二区| 久久精品无码av| 久久精品国产一区二区三区| 香蕉久久一区二区不卡无毒影院| 国产精品久久久久久久人人看| 久久久久这里只有精品| 久久青青草原国产精品免费| 久久天天躁狠狠躁夜夜网站| 7777精品伊人久久久大香线蕉| 久久久久久毛片免费看| 人妻系列无码专区久久五月天| 国产免费久久精品99久久| 久久久久99精品成人片|