DX9教程一

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baickl/article/details/567761
#include <windows.h>
#include <d3dx9.h>
#pragma comment(lib,"d3d9.lib")
//----------------------------------------------------------------------------
//定义为全屏模式
//----------------------------------------------------------------------------
//#define FULLSCREEN
//-----------------------------------------------------------------------------
//全局变量声明
//-----------------------------------------------------------------------------
static const int WIDTH   = 800;
static const int HEIGHT  = 600;
LPDIRECT3D9        g_pD3D       = NULL;
LPDIRECT3DDEVICE9  g_pd3dDevice = NULL;
//-----------------------------------------------------------------------------
//函数声明
//-----------------------------------------------------------------------------
HWND    InitHWND(HINSTANCE,int,int,int,int,char*);
HRESULT InitD3D(HWND);
void    Cleanup();
void    Render();
LRESULT WINAPI MsgProc(HWND,UINT,WPARAM,LPARAM);

//----------------------------------------------------------------------------
//初始化窗口
//----------------------------------------------------------------------------
HWND InitHWND(HINSTANCE hInst,int l,int t,int w,int h,char*title)
{
 //注册窗口类型
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      hInst, NULL, NULL, NULL, NULL,"Lesson1", NULL };
    RegisterClassEx( &wc );
    //创建程序窗口
    HWND hWnd = CreateWindow( "Lesson1", title, WS_OVERLAPPEDWINDOW, l, t, w, h,
                              NULL, NULL, wc.hInstance, NULL );
 return hWnd;
}
//-----------------------------------------------------------------------------
//初始化D3d
//-----------------------------------------------------------------------------
HRESULT InitD3D(HWND hwnd)
{
 //创建D3D
 g_pD3D=Direct3DCreate9(D3D_SDK_VERSION);
 if(g_pD3D==NULL)return E_FAIL;
 //设置D3D参数,以创建D3D设备
 D3DPRESENT_PARAMETERS d3dpp;
 
 ZeroMemory(&d3dpp,sizeof(d3dpp));
 d3dpp.hDeviceWindow              = hwnd;
 d3dpp.BackBufferCount            = 1;
 d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;
#ifdef  FULLSCREEN
 d3dpp.Windowed                   = false;
 d3dpp.BackBufferHeight           = HEIGHT;
 d3dpp.BackBufferWidth            = WIDTH;
 d3dpp.BackBufferFormat           = D3DFMT_R5G6B5;
#else
 d3dpp.Windowed                   = true;
 d3dpp.BackBufferFormat           = D3DFMT_UNKNOWN;
#endif//FULLSCREEN
 //创建D3D设备
 if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,
         &d3dpp,&g_pd3dDevice)))
 {
  return E_FAIL;
 }
 return S_OK;
}
//-----------------------------------------------------------------------------
//清除操作,释放的时候,按顺序释放
//-----------------------------------------------------------------------------
void Cleanup()
{
 if(g_pd3dDevice!=NULL)
 {
  g_pd3dDevice->Release();
  g_pd3dDevice=NULL;
 }
 if(g_pD3D!=NULL)
 {
  g_pD3D->Release();
  g_pD3D=NULL;
 }
}
//-----------------------------------------------------------------------------
//渲染过程
//-----------------------------------------------------------------------------
void Render()
{
 if(g_pd3dDevice==NULL)return;
 //用蓝色清除离屏缓冲区
 g_pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
 //开始渲染
 if(SUCCEEDED(g_pd3dDevice->BeginScene()))
 {
  g_pd3dDevice->EndScene();
 }
 //翻转离屏缓冲区
 g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
}

//-----------------------------------------------------------------------------
//窗口过程处理函数
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
        case WM_PAINT:
            Render();
            ValidateRect( hWnd, NULL );
            return 0;
    }
    return DefWindowProc( hWnd, msg, wParam, lParam );
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int)
{
 HWND hWnd=InitHWND(hInst,0,0,WIDTH,HEIGHT,"第一课");
    //初始化D3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        //显示窗口
        ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );
        //进入消息循环
        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }
    return 0;
}
阅读更多

没有更多推荐了,返回首页