• <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>

            天行健 君子當自強而不息

            一個用D3D繪制2D圖形的例子

            思路如下:

            (1)函數Do_Init初始化D3D,D3D設備,頂點緩沖,紋理;主要調用這幾個函數:
                    Direct3DCreate9,GetAdapterDisplayMode,CreateDevice,CreateVertexBuffer, Lock,Unlock,D3DXCreateTextureFromFile。

            (2)函數Do_Frame繪制一幀,主要調用這幾個函數:
                   Clear,BeginScene,EndScene,SetStreamSource,SetFVF,SetTexture, DrawPrimitive。

            (3)函數Do_Shutdown釋放D3D資源。

            源碼如下:

            /***************************************************************************************
            PURPOSE:
                2D Drawing Demo

            Required libraries:
              D3D9.LIB and D3DX9.LIB
             **************************************************************************************
            */

            #include 
            <windows.h>
            #include 
            <stdio.h>
            #include 
            "d3d9.h"
            #include 
            "d3dx9.h"

            #pragma comment(lib, 
            "d3d9.lib")
            #pragma comment(lib, 
            "d3dx9.lib")

            #define WINDOW_WIDTH    400
            #define WINDOW_HEIGHT   400

            #define Safe_Release(p) if((p)) (p)->Release();

            // window handles, class and caption text.
            HWND g_hwnd;
            HINSTANCE g_inst;
            static char g_class_name[] = "Draw2DClass";
            static char g_caption[]    = "Draw2D Demo";

            // the Direct3D and device object
            IDirect3D9* g_d3d = NULL;
            IDirect3DDevice9
            * g_d3d_device = NULL;

            // 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 = NULL;
            IDirect3DTexture9
            *      g_texture = NULL;

            //--------------------------------------------------------------------------------
            // Window procedure.
            //--------------------------------------------------------------------------------
            long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
            {
                
            switch(msg)
                {
                
            case WM_DESTROY:
                    PostQuitMessage(
            0);
                    
            return 0;
                }

                
            return (long) DefWindowProc(hwnd, msg, wParam, lParam);
            }

            //--------------------------------------------------------------------------------
            // Initialize d3d, d3d device, vertex buffer, texutre. 
            //--------------------------------------------------------------------------------
            BOOL Do_Init()
            {
                D3DPRESENT_PARAMETERS present_param;
                D3DDISPLAYMODE  display_mode;
                BYTE
            * vertex_ptr;

                
            // initialize vertex data
                VERTEX verts[] = {
                  {  
            50.0f,  50.0f1.0f1.0f0.0f0.0f },
                  { 
            350.0f,  50.0f1.0f1.0f1.0f0.0f },
                  {  
            50.0f350.0f1.0f1.0f0.0f1.0f },
                  { 
            350.0f350.0f1.0f1.0f1.0f1.0f }
                }; 

                
            // do a windowed mode initialization of Direct3D
                if((g_d3d = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
                    
            return FALSE;

                
            // retrieves the current display mode of the adapter
                if(FAILED(g_d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &display_mode)))
                    
            return FALSE;

                ZeroMemory(
            &present_param, sizeof(present_param));

                
            // initialize d3d presentation parameter
                present_param.Windowed          = TRUE;
                present_param.SwapEffect        
            = D3DSWAPEFFECT_DISCARD;
                present_param.BackBufferFormat  
            = display_mode.Format;

                
            // creates a device to represent the display adapter
                if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd,
                                              D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
            &present_param, &g_d3d_device)))
                    
            return FALSE;     

                
            // create the vertex buffer and set data
                g_d3d_device->CreateVertexBuffer(sizeof(VERTEX) * 40, 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(00, (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, "Texture.bmp"&g_texture);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Release all d3d resource.
            //--------------------------------------------------------------------------------
            BOOL Do_Shutdown()
            {
                Safe_Release(g_vertex_buffer);
                Safe_Release(g_texture);
                Safe_Release(g_d3d_device);
                Safe_Release(g_d3d);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Render a frame.
            //--------------------------------------------------------------------------------
            BOOL Do_Frame()
            {
                
            // clear device back buffer
                g_d3d_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(064128255), 1.0f0);

                
            // Begin scene
                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, 0sizeof(VERTEX));

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

                    
            // assigns a texture to a stage for a device
                    g_d3d_device->SetTexture(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, 02);

                    
            // release texture
                    g_d3d_device->SetTexture(0, NULL);

                    
            // end the scene
                    g_d3d_device->EndScene();
                }

                
            // present the contents of the next buffer in the sequence of back buffers owned by the device
                g_d3d_device->Present(NULL, NULL, NULL, NULL);

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Main function, routine entry.
            //--------------------------------------------------------------------------------
            int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
            {
                WNDCLASSEX  win_class;
                MSG         msg;

                g_inst 
            = inst;

                
            // create window class and register it
                win_class.cbSize        = sizeof(win_class);
                win_class.style         
            = CS_CLASSDC;
                win_class.lpfnWndProc   
            = Window_Proc;
                win_class.cbClsExtra    
            = 0;
                win_class.cbWndExtra    
            = 0;
                win_class.hInstance     
            = inst;
                win_class.hIcon         
            = LoadIcon(NULL, IDI_APPLICATION);
                win_class.hCursor       
            = LoadCursor(NULL, IDC_ARROW);
                win_class.hbrBackground 
            = NULL;
                win_class.lpszMenuName  
            = NULL;
                win_class.lpszClassName 
            = g_class_name;
                win_class.hIconSm       
            = LoadIcon(NULL, IDI_APPLICATION);

                
            if(! RegisterClassEx(&win_class))
                    
            return FALSE;

                
            // create the main window
                g_hwnd = CreateWindow(g_class_name, g_caption, WS_CAPTION | WS_SYSMENU, 00,
                                      WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, inst, NULL);

                
            if(g_hwnd == NULL)
                    
            return FALSE;

                ShowWindow(g_hwnd, SW_NORMAL);
                UpdateWindow(g_hwnd);

                
            // initialize game
                if(Do_Init() == FALSE)
                    
            return FALSE;

                
            // start message pump, waiting for signal to quit.
                ZeroMemory(&msg, sizeof(MSG));

                
            while(msg.message != WM_QUIT)
                {
                    
            if(PeekMessage(&msg, NULL, 00, PM_REMOVE))
                    {
                        TranslateMessage(
            &msg);
                        DispatchMessage(
            &msg);
                    }
                    
                    
            // draw a frame
                    if(Do_Frame() == FALSE)
                        
            break;
                }

                
            // run shutdown function
                Do_Shutdown();

                UnregisterClass(g_class_name, inst);
                
                
            return (int) msg.wParam;
            }

            效果:


            posted on 2007-07-01 23:06 lovedday 閱讀(4530) 評論(0)  編輯 收藏 引用 所屬分類: ■ DirectX 9 Program

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            国产精品免费看久久久香蕉| 久久精品极品盛宴观看| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 久久精品亚洲中文字幕无码麻豆| 欧美喷潮久久久XXXXx| 72种姿势欧美久久久久大黄蕉| 久久er国产精品免费观看8| 伊人久久大香线蕉综合5g| 精品久久8x国产免费观看| 国产精品亚洲综合专区片高清久久久 | 无遮挡粉嫩小泬久久久久久久 | 国产精品欧美久久久天天影视| 久久伊人五月天论坛| 久久精品欧美日韩精品| 日韩久久无码免费毛片软件| 久久精品无码专区免费东京热| 久久99国产一区二区三区| 色婷婷综合久久久久中文 | 久久综合九色综合97_久久久| 亚洲国产精品综合久久网络| 久久精品国产亚洲欧美| 中文成人无码精品久久久不卡| 999久久久国产精品| 精品久久久久久久无码| 久久人妻无码中文字幕| 色综合久久天天综合| 久久久久高潮毛片免费全部播放| 爱做久久久久久| 久久精品视频网| 久久国产精品久久| 国产精品99久久99久久久| 97久久婷婷五月综合色d啪蜜芽| 久久婷婷人人澡人人| 精品久久人人妻人人做精品| 久久精品国产亚洲AV大全| 囯产精品久久久久久久久蜜桃| 亚洲国产精品综合久久网络 | 精品人妻久久久久久888| 久久人爽人人爽人人片AV| 人妻少妇久久中文字幕| 久久久久亚洲AV无码永不|