Hotcocoa

导航

正交投影的Direct2D绘图基础部分

使用正交投影的Direct2D绘图基础部分

引用:http://hi.baidu.com/shallow_sleep/blog/item/84a1c7f42f12a5d0f3d38513.html

#include <d3d9.h>
#include <d3dx9.h>

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

IDirect3D9* _d3d9;                               //全局变量

   _d3d9=Direct3DCreate9(D3D_SDK_VERSION);

   D3DCAPS9 caps;
   _d3d9->GetDeviceCaps(
    D3DADAPTER_DEFAULT,               //指定主显卡
    D3DDEVTYPE_HAL,                         //指定设备内容 一般使用D3DDEVTYPE_HAL硬件加速.
    &caps);
   int vp=0;
  
   if(caps.DevCaps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
   {
//检测硬件是否支持变换和灯光的,如果支持,设定为用硬件(加速)处理
   vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
   }
   else
   {
    //如果不是,设定为用软件处理
    vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
   }
    //创建设备
    D3DPRESENT_PARAMETERS d3dpp;
d3dpp.BackBufferWidth = 800;                                  //分辨率
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;       //像素格式
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;   //抗锯齿:无
d3dpp.MultiSampleQuality = 0;                                        //抗锯齿等级
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;       //翻转方式:D3DSWAPEFFECT_DISCARD:一般用于全屏、每帧刷新;
                                                                //D3DSWAPEFFECT_FLIP:双缓冲
                                                                //D3DSWAPEFFECT_COPY:从后缓冲拷贝
                                                                //D3DSWAPEFFECT_COPY_VSYNC:从后缓冲拷贝并垂直同步,防止画面撕裂
d3dpp.hDeviceWindow = hWnd;                     //窗口句柄
d3dpp.Windowed = false;                               //是否全屏
d3dpp.EnableAutoDepthStencil = true;            //是否有深度缓存和模板缓存
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;    //深度缓存和模板缓存的格式
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;      //刷新频率
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

    IDirect3DDevice9* device = 0;
HRESULT hr = _d3d9->CreateDevice(
   D3DADAPTER_DEFAULT,                         // 主显卡
   D3DDEVTYPE_HAL,                                 // 设备格式:硬件加速
   hWnd,                                                       // 窗口句柄
   vp,                                                             // 顶点处理方式,刚测试过
   &d3dpp,
   &device);


if( FAILED(hr) )
{
   ::MessageBoxA(0, "CreateDevice() - FAILED", 0, 0);
   return 0;
}

3. 恢复窗口
    会在调整窗口时用到,可能还会在设备丢失时用到(不确定)。

void RestoreWindow(float w, float h)
{

   //创建视口
D3DVIEWPORT9 view;
view.X      = 0;
view.Y      = 0;
view.Width = w;
view.Height = h;
view.MinZ   = 0.0f;
view.MaxZ   = 1.0f;

// 创建视口并添加到设备
if (FAILED(g_pd3dDevice->SetViewport(&view))){
   MessageBoxA(0,"Failed to create a viewport",0,0);
};

D3DXMATRIX Ortho2D;
D3DXMATRIX Identity;

//2D,所以使用正交投影;3D请查D3DXMatrixPerspectiveFovLH
D3DXMatrixPerspectiveFovLH(&Ortho2D,w,h,0.0f,1.0f);                                                  
D3DXMatrixIdentity(&Identity);

g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D);
g_pd3dDevice->SetTransform(D3DTS_WORLD, &Identity);                  
g_pd3dDevice->SetTransform(D3DTS_VIEW, &Identity);

//关闭光照,否则不定义法线则为黑色
g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);              

}

4. 渲染
    放在WM_PAINT消息中。

void Render()
{
     //清除屏幕    
   g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255,0,0,0), 1.0f, 0);  
     //BeginScene和EndScene必须成对出现,不能嵌套        
   g_pd3dDevice->BeginScene();
    // render game scene here                                                             
   g_pd3dDevice->EndScene();
     //在后缓冲区复制最后翻转到前台显示
   g_pd3dDevice->Present(NULL, NULL, NULL, NULL);                                      
}

posted on 2011-03-19 19:23  Hotcocoa  阅读(924)  评论(0编辑  收藏  举报