• <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)【接口與實現分離版】

             
            本篇是創建游戲內核(8)【接口與實現分離版】的續篇,關于該內核的細節說明請參考創建游戲內核(9),這個版本主要是按照功能劃分模塊的思想,并嚴格按照接口與實現相分離的原則來寫的,沒有用面向對象的思想來寫,沒有繼承沒有多態。大家可以對比兩個版本,比較優劣。


            接口:

            BOOL load_texture(LPDIRECT3DTEXTURE9* texture, char* filename, DWORD transparent, D3DFORMAT format);
            BOOL create_texture(LPDIRECT3DTEXTURE9* texture, DWORD width, DWORD height, D3DFORMAT format);
            BOOL clone_texture(LPDIRECT3DTEXTURE9* dest_texture, 
            const LPDIRECT3DTEXTURE9 src_texture);

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

            BOOL draw_texture(
            const LPDIRECT3DTEXTURE9 texture, 
                              
            long dest_x,      long dest_y,
                              
            long src_x,       long src_y,
                              
            long width,       long height,
                              
            float x_scale,    float y_scale,
                              D3DCOLOR color);

            實現:

            //-------------------------------------------------------------------
            // create texture object from specified file, you can specify transparent
            // value and pixel format of the texture.
            //-------------------------------------------------------------------
            BOOL load_texture(LPDIRECT3DTEXTURE9* texture, char* filename, DWORD transparent, D3DFORMAT format)
            {
                
            // check condition first
                if(g_d3d_device == NULL || filename == NULL)
                    
            return FALSE;

                
            // create a texture from 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, texture)))
                {
                    
            return FALSE;
                }

                
            return TRUE;
            }

            //-------------------------------------------------------------------
            // Creates a texture resource.
            //-------------------------------------------------------------------
            BOOL create_texture(LPDIRECT3DTEXTURE9* texture, DWORD width, DWORD height, D3DFORMAT format)
            {
                
            if(FAILED(g_d3d_device->CreateTexture(width, height, 0, 0, format, D3DPOOL_MANAGED, texture, NULL)))
                    
            return FALSE;

                
            return TRUE;
            }

            //-------------------------------------------------------------------
            // Configure a TEXTURE class from an existing IDirect3DTexture9 object 
            // instance.
            //-------------------------------------------------------------------
            BOOL clone_texture(LPDIRECT3DTEXTURE9* dest_texture, const LPDIRECT3DTEXTURE9 src_texture)
            {
                D3DLOCKED_RECT _src_rect, _dest_rect;
                D3DSURFACE_DESC _surface_desc;    

                
            if(src_texture == NULL)
                    
            return FALSE;

                
            // copy texture voer, from source to dest.

                
            if(FAILED(src_texture->GetLevelDesc(0, &_surface_desc)))
                    
            return FALSE;
                
                g_d3d_device->CreateTexture(_surface_desc.Width, _surface_desc.Height, 0, 0, 
                                            _surface_desc.Format, D3DPOOL_MANAGED, dest_texture, NULL);

                
            // locks a rectangle on a texture resource
                src_texture->LockRect(0, &_src_rect, NULL, D3DLOCK_READONLY);
                (*dest_texture)->LockRect(0, &_dest_rect, NULL, 0);

                memcpy(_dest_rect.pBits, _src_rect.pBits, _src_rect.Pitch * _surface_desc.Height);

                
            // unlocks a rectangle on a texture resource
                src_texture->UnlockRect(0);
                (*dest_texture)->UnlockRect(0);

                
            return TRUE;
            }

            //-------------------------------------------------------------------
            // 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.
            //-------------------------------------------------------------------
            long 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;
            }

            //-------------------------------------------------------------------
            // Draw a 2D portion of texture to device.
            //-------------------------------------------------------------------
            BOOL draw_texture(const LPDIRECT3DTEXTURE9 texture, 
                              
            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(texture == NULL)
                    
            return FALSE;

                LPD3DXSPRITE _sprite;

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

                
            // If no specify width and height, set with texture width and height.
                
                
            if(width == 0)
                    width = get_texture_width(texture);

                
            if(height == 0)
                    height = get_texture_height(texture);

                
            // set the portion of the source texture

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

                
            // sets the sprite transforma
                _sprite->SetTransform(&_transform_matrix);

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

                
            return TRUE;
            }
             

            測試代碼:

            /***********************************************************************************
            PURPOSE:
                Test D3D texture function.
            ***********************************************************************************/


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

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

            IDirect3DVertexBuffer9* g_vertex_buffer;
            IDirect3DTexture9*      g_texture;

            //--------------------------------------------------------------------------------
            // Initialize data for game.
            //--------------------------------------------------------------------------------
            BOOL game_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_window_width(g_hwnd), get_window_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, &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
                if(! load_texture(&g_texture, "texture.jpg", 0, D3DFMT_UNKNOWN))
                    
            return FALSE;

                
            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 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, 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-03 23:21 lovedday 閱讀(494) 評論(2)  編輯 收藏 引用

            評論

            # re: 創建游戲內核(9)【接口與實現分離版】 2007-10-07 12:02

            這個博客的內容也太不基礎了嘛,還是新手區.  回復  更多評論   

            # re: 創建游戲內核(9)【接口與實現分離版】 2007-10-07 13:15 lovedday

            嗯,這種代碼只能算新手。  回復  更多評論   

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            一本色道久久88综合日韩精品 | 亚洲精品乱码久久久久66| 欧美国产成人久久精品| 97久久国产露脸精品国产| 成人国内精品久久久久一区| 99久久成人18免费网站| 国产精品成人久久久| 国产精品青草久久久久婷婷 | 亚洲精品无码久久久| 伊人久久精品无码二区麻豆| 久久亚洲国产精品一区二区| 久久丫忘忧草产品| 国内精品免费久久影院| 丁香狠狠色婷婷久久综合| 亚洲欧洲久久av| 国产亚洲精午夜久久久久久| 久久国产精品成人影院| 97久久婷婷五月综合色d啪蜜芽| 国产精品亚洲美女久久久| 久久精品中文字幕无码绿巨人| 午夜精品久久影院蜜桃| 99久久精品免费| 久久精品人人槡人妻人人玩AV| 麻豆精品久久久久久久99蜜桃| 久久久国产精品网站| 久久精品国产亚洲av高清漫画| 欧美精品国产综合久久| 女人高潮久久久叫人喷水| 欧洲性大片xxxxx久久久| 国产成人香蕉久久久久| 一级做a爰片久久毛片人呢| 97久久久精品综合88久久| 久久66热人妻偷产精品9| 久久精品国产清自在天天线| 99久久国产亚洲综合精品| 2021国内久久精品| 久久久国产打桩机| 久久香综合精品久久伊人| 久久久久亚洲av无码专区导航| 久久人爽人人爽人人片AV | 亚洲欧美久久久久9999|