DirectX 9.0 3d 初始程序框架

本文介绍了一个基于DirectX9.0的3D游戏开发框架初始化过程,包括创建Direct3D设备、窗口消息循环及屏幕清除等关键步骤。

这个是《DirectX 9.0 3D游戏开发编程基础》上第二章的的框架,很有参考意义,框架分3个文件,主逻辑在

d3dInit.cpp中。

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////

//

// File: d3dUtility.h

//

// Author: Frank Luna (C) All Rights Reserved

//

// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0

//

// Desc: Provides utility functions for simplifying common tasks.

//         

//////////////////////////////////////////////////////////////////////////////////////////////////

 

#ifndef __d3dUtilityH__

#define __d3dUtilityH__

 

#include <d3dx9.h>

#include <string>

 

#include <tchar.h>

using namespace std;

 

namespace d3d

{

    bool InitD3D(

       HINSTANCE hInstance,       // [in] Application instance.

       int width, int height,     // [in] Backbuffer dimensions.

       bool windowed,             // [in] Windowed (true)or full screen (false).

       D3DDEVTYPE deviceType,     // [in] HAL or REF

       IDirect3DDevice9** device);// [out]The created device.

 

    int EnterMsgLoop(

       bool (*ptr_display)(float timeDelta));

 

    LRESULT CALLBACK WndProc(

       HWND hwnd,

       UINT msg,

       WPARAM wParam,

       LPARAM lParam);

 

    template<class T> void Release(T t)

    {

       if( t )

       {

           t->Release();

           t = 0;

       }

    }

      

    template<class T> void Delete(T t)

    {

       if( t )

       {

           delete t;

           t = 0;

       }

    }

}

 

#endif // __d3dUtilityH__

 

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////

//

// File: d3dUtility.cpp

//

// Author: Frank Luna (C) All Rights Reserved

//

// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0

//

// Desc: Provides utility functions for simplifying common tasks.

//         

//////////////////////////////////////////////////////////////////////////////////////////////////

 

#include "d3dUtility.h"

 

bool d3d::InitD3D(

    HINSTANCE hInstance,

    int width, int height,

    bool windowed,

    D3DDEVTYPE deviceType,

    IDirect3DDevice9** device)

{

    //

    // Create the main application window.

    //

 

    WNDCLASS wc;

 

    wc.style         = CS_HREDRAW | CS_VREDRAW;

    wc.lpfnWndProc   = (WNDPROC)d3d::WndProc;

    wc.cbClsExtra    = 0;

    wc.cbWndExtra    = 0;

    wc.hInstance     = hInstance;

    wc.hIcon         = LoadIcon(0, IDI_APPLICATION);

    wc.hCursor       = LoadCursor(0, IDC_ARROW);

    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

    wc.lpszMenuName  = 0;

    wc.lpszClassName = _T("Direct3D9App");

 

    if( !RegisterClass(&wc) )

    {

       ::MessageBox(0, _T("RegisterClass() - FAILED"), 0, 0);

       return false;

    }

      

    HWND hwnd = 0;

    hwnd = ::CreateWindow(_T("Direct3D9App"), _T("Direct3D9App"),

       WS_EX_TOPMOST,

       0, 0, width, height,

       0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/);

 

    if( !hwnd )

    {

       ::MessageBox(0, _T("CreateWindow() - FAILED"), 0, 0);

       return false;

    }

 

    ::ShowWindow(hwnd, SW_SHOW);

    ::UpdateWindow(hwnd);

 

    //

    // Init D3D:

    //

 

    HRESULT hr = 0;

 

    // Step 1: Create the IDirect3D9 object.

 

    IDirect3D9* d3d9 = 0;

    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

 

    if( !d3d9 )

    {

       ::MessageBox(0, _T("Direct3DCreate9() - FAILED"), 0, 0);

       return false;

    }

 

    // Step 2: Check for hardware vp.

 

    D3DCAPS9 caps;

    d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);

 

    int vp = 0;

    if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )

       vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;

    else

       vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

 

    // Step 3: Fill out the D3DPRESENT_PARAMETERS structure.

 

    D3DPRESENT_PARAMETERS d3dpp;

    d3dpp.BackBufferWidth            = width;

    d3dpp.BackBufferHeight           = height;

    d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;

    d3dpp.BackBufferCount            = 1;

    d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;

    d3dpp.MultiSampleQuality         = 0;

    d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;

    d3dpp.hDeviceWindow              = hwnd;

    d3dpp.Windowed                   = windowed;

    d3dpp.EnableAutoDepthStencil     = true;

    d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;

    d3dpp.Flags                      = 0;

    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;

    d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;

 

    // Step 4: Create the device.

 

    hr = d3d9->CreateDevice(

       D3DADAPTER_DEFAULT, // primary adapter

       deviceType,         // device type

       hwnd,               // window associated with device

       vp,                 // vertex processing

        &d3dpp,             // present parameters

        device);            // return created device

 

    if( FAILED(hr) )

    {

       // try again using a 16-bit depth buffer

       d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

      

       hr = d3d9->CreateDevice(

           D3DADAPTER_DEFAULT,

           deviceType,

           hwnd,

           vp,

           &d3dpp,

           device);

 

       if( FAILED(hr) )

       {

           d3d9->Release(); // done with d3d9 object

           ::MessageBox(0, _T("CreateDevice() - FAILED"), 0, 0);

           return false;

       }

    }

 

    d3d9->Release(); // done with d3d9 object

   

    return true;

}

 

int d3d::EnterMsgLoop( bool (*ptr_display)(float timeDelta) )

{

    MSG msg;

    ::ZeroMemory(&msg, sizeof(MSG));

 

    static float lastTime = (float)timeGetTime();

 

    while(msg.message != WM_QUIT)

    {

       if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))

       {

           ::TranslateMessage(&msg);

           ::DispatchMessage(&msg);

       }

       else

        { 

           float currTime  = (float)timeGetTime();

           float timeDelta = (currTime - lastTime)*0.001f;

 

           ptr_display(timeDelta);

 

           lastTime = currTime;

        }

    }

    return msg.wParam;

}

 

 

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:Fixedsys; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-charset:134; mso-generic-font-family:auto; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:"/@Fixedsys"; panose-1:0 0 0 0 0 0 0 0 0 0; mso-font-charset:134; mso-generic-font-family:auto; mso-font-format:other; mso-font-pitch:fixed; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

//////////////////////////////////////////////////////////////////////////////////////////////////

//

// File: d3dinit.cpp

//

// Author: Frank Luna (C) All Rights Reserved

//

// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0

//

// Desc: Demonstrates how to initialize Direct3D, how to use the book's framework

//       functions, and how to clear the screen to black.  Note that the Direct3D

//       initialization code is in the d3dUtility.h/.cpp files.

//         

//////////////////////////////////////////////////////////////////////////////////////////////////

 

#include "d3dUtility.h"

 

//

// Globals

//

 

IDirect3DDevice9* Device = 0;

 

//

// Framework Functions

//

 

bool Setup()

{

    // Nothing to setup in this sample.

 

    return true;

}

 

void Cleanup()

{

    // Nothing to cleanup in this sample.

}

 

bool Display(float timeDelta)

{

    if( Device ) // Only use Device methods if we have a valid device.

    {

       // Instruct the device to set each pixel on the back buffer black -

       // D3DCLEAR_TARGET: 0x00000000 (black) - and to set each pixel on

       // the depth buffer to a value of 1.0 - D3DCLEAR_ZBUFFER: 1.0f.

       Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);

 

       // Swap the back and front buffers.

       Device->Present(0, 0, 0, 0);

    }

    return true;

}

 

 

 

//

// WinMain

//

int WINAPI WinMain(HINSTANCE hinstance,

                 HINSTANCE prevInstance,

                 PSTR cmdLine,

                 int showCmd)

{

    if(!d3d::InitD3D(hinstance,

       640, 480, true, D3DDEVTYPE_HAL, &Device))

    {

       ::MessageBox(0, _T("InitD3D() - FAILED"), 0, 0);

       return 0;

    }

      

    if(!Setup())

    {

       ::MessageBox(0, _T("Setup() - FAILED"), 0, 0);

       return 0;

    }

 

    d3d::EnterMsgLoop( Display );

 

    Cleanup();

 

    Device->Release();

 

    return 0;

}

 

//

// WndProc

//

LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)

{

    switch( msg )

    {

    case WM_DESTROY:

       ::PostQuitMessage(0);

       break;

 

    case WM_KEYDOWN:

       if( wParam == VK_ESCAPE )

           ::DestroyWindow(hwnd);

       break;

    }

    return ::DefWindowProc(hwnd, msg, wParam, lParam);

}


 

 

 

idirect卫星通信系统 产品及技术优势介绍 目录 1. iDirect 设备硬件上的先进性 1.1 iDirect 同一设备支持星状、星/网状、SCPC 1.2 iDirect 单主站机箱支持多网络,支持多达5 颗不同的卫星,或5 个不同的频段: 1.3 iDirect 系统设备功能高度集成 1.4 iDirect 主站硬件符合电信备份标准,提供全面的系统内部备份: 1.5 系统结构紧凑,节省空间效率 1.6 电源消耗 1.7 可伸缩性 1.8 可靠性 1.9 每小站配置可以保存多达15 种不同组合 1.10 小站省电模式 2. iDirect 系统在卫星带宽利用效率方面的先进性 2.1 卫星层次上的效率体现 2.2 IP 层次上的效率体现 3. iDirect 全球网络管理系统(NMS) 4. iDirect 虚拟网络运营商(VNO)、用户网络检察员(CNO) 5. iDirect 与移动通信 6. iDirect 系统其它方面的优势 6.1 小站安装容易 6.2 Rx CRC 关联 6.3 自动波束(网络)切换、小站“漫游” 7. iDirect 产品介绍 7.1 主站5IF HUB 7.2 通用线路卡(Universal Line Card) 7.3 iDirect Private HUB 和 Mini HUB 7.4 iDirect 3100 卫星路由器:星状网远端站 7.5 iDirect 5000 系列卫星路由器:星状、SCPC、星/网状 7.6 iDirect 7000 系列卫星路由器:星状、SCPC、星/网状 8. iDirect 卫星系统工作方式 8.1 小站结构 8.2 主站结构 8.3 高层次网络架构——星形拓扑 8.4 iDirect 网络的下行传输 8.5 iDirect 网络的上行传输 8.6 iDirect 星/网状结构 8.6.1 下行信道
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值