Introduction to 3D Game Programming with DirectX 9.0 学习笔记-chapter1
本书的chapter1通过一个简单的例子实现Direct3D的Initialize,源代码如下:
1: //////////////////////////////////////////////////////////////////////////////////////////////////2: //3: // File: d3dUtility.h4: //5: // Author: Frank Luna (C) All Rights Reserved6: //7: // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.08: //9: // Desc: Provides utility functions for simplifying common tasks.10: //11: //////////////////////////////////////////////////////////////////////////////////////////////////12:13: #ifndef __d3dUtilityH__14: #define __d3dUtilityH__15:16: #include <d3dx9.h>17: #include <string>18:19: namespace d3d20: {21: bool InitD3D(22: HINSTANCE hInstance, // [in] Application instance.23: int width, int height, // [in] Backbuffer dimensions.24: bool windowed, // [in] Windowed (true)or full screen (false).25: D3DDEVTYPE deviceType, // [in] HAL or REF26: IDirect3DDevice9** device);// [out]The created device.27:28: int EnterMsgLoop(29: bool (*ptr_display)(float timeDelta));30:31: LRESULT CALLBACK WndProc(32: HWND hwnd,33: UINT msg,34: WPARAM wParam,35: LPARAM lParam);36:37: template<class T> void Release(T t)38: {39: if( t )40: {41: t->Release();42: t = 0;43: }44: }45:46: template<class T> void Delete(T t)47: {48: if( t )49: {50: delete t;51: t = 0;52: }53: }54: }55:56: #endif // __d3dUtilityH__1: //////////////////////////////////////////////////////////////////////////////////////////////////2: //3: // File: d3dinit.cpp4: //5: // Author: Frank Luna (C) All Rights Reserved6: //7: // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.08: //9: // Desc: Demonstrates how to initialize Direct3D, how to use the book's framework10: // functions, and how to clear the screen to black. Note that the Direct3D11: // initialization code is in the d3dUtility.h/.cpp files.12: //13: //////////////////////////////////////////////////////////////////////////////////////////////////14:15: #pragma comment(lib,"d3d9.lib")16: // //#pragma comment(lib,"d3dx9.lib")17: // //#pragma comment(lib,"d3dxof.lib")18: // //#pragma comment(lib,"dxguid.lib")19: #pragma comment(lib,"winmm.lib")20: // //#pragma (lib,"comctl32.lib")21:22:23: #include "d3dUtility.h"24:25: //26: // Globals27: //28:29: IDirect3DDevice9* Device = 0;30:31: //32: // Framework Functions33: //34:35: //This function is where we set up anything that needs to be set up for this sample, such as allocating resources,36: // checking device capabilities, and setting application states.37: bool Setup()38: {39: // Nothing to setup in this sample.40:41: return true;42: }43:44: // This function is where we free anything that we allocated in the Setup function, such as deallocating memory45: void Cleanup()46: {47: // Nothing to cleanup in this sample.48: }49:50:51: // This function is where we implement all of our drawing code and code that occurs on a frame-by-frame basis52: bool Display(float timeDelta)53: {54: if( Device ) // Only use Device methods if we have a valid device.55: {56: // Instruct the device to set each pixel on the back buffer black -57: // D3DCLEAR_TARGET: 0x00000000 (black) - and to set each pixel on58: // the depth buffer to a value of 1.0 - D3DCLEAR_ZBUFFER: 1.0f.59: Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00000000, 1.0f, 0);60:61: // Swap the back and front buffers.62: Device->Present(0, 0, 0, 0);63: }64: return true;65: }66:67: //68: // WndProc69: //70: LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)71: {72: switch( msg )73: {74: case WM_DESTROY:75: ::PostQuitMessage(0);76: break;77:78: case WM_KEYDOWN:79: if( wParam == VK_ESCAPE )80: ::DestroyWindow(hwnd);81: break;82: }83: return ::DefWindowProc(hwnd, msg, wParam, lParam);84: }85:86: //87: // WinMain performs the following 4 steps88: //1. Initializes the main display window and Direct3D(to draw)89: //2. Calls the Setup routine to set up the application90: //3. Enters the message loop using Display as the display function91: //4. Cleans up the application and finally releases the IDirect3DDevice9 object92: int WINAPI WinMain(HINSTANCE hinstance,93: HINSTANCE prevInstance,94: PSTR cmdLine,95: int showCmd)96: {97: // InitD3D最主要的功能就是创建一个IDirect3DDevice9对象进行绘图98: if(!d3d::InitD3D(hinstance,99: 640, 480, true, D3DDEVTYPE_HAL, &Device))100: {101: ::MessageBox(0, "InitD3D() - FAILED", 0, 0);102: return 0;103: }104:105: if(!Setup())106: {107: ::MessageBox(0, "Setup() - FAILED", 0, 0);108: return 0;109: }110: //开始消息循环,当消息队列中木有消息时,就调用Display(),显示一个黑窗口111: d3d::EnterMsgLoop( Display );112:113: Cleanup();114:115: Device->Release();116:117: return 0;118: }1: //////////////////////////////////////////////////////////////////////////////////////////////////2: //3: // File: d3dUtility.cpp4: //5: // Author: Frank Luna (C) All Rights Reserved6: //7: // System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.08: //9: // Desc: Provides utility functions for simplifying common tasks.10: //11: //////////////////////////////////////////////////////////////////////////////////////////////////12:13: #include "d3dUtility.h"14:15: // InitD3D()的作用:16: // 1.创建应用程序主窗口,然后创建一个IDirect3D9对象(所有设备的属性都在这个对象里面)17: // 2.通过IDirect3D9对象获取设备的属性,检查设备是否支持 HARDWARE_VERTEXPROCESSING18: // 3.构造一个D3DPRESENT_PARAMETERS对象(在创建IDirect3DDevice9对象时要用到这个参数,其中,IDirect3DDevice9的主要功能就是提供绘图之类的操作)19: // 4.最后利用IDirect3D9对象的CreateDevice()函数创建IDirect3DDevice9对象,以进行绘图20: // 5.最后的最后释放IDirect3D9对象,因为创建完IDirect3DDevice9对象之后,IDirect3D9就没啥用了(Release():release COM interfaces and set them to null.)21:22: bool d3d::InitD3D(23: HINSTANCE hInstance,24: int width, int height,25: bool windowed,26: D3DDEVTYPE deviceType,27: IDirect3DDevice9** device)28: {29: //30: // Create the main application window.31: //32:33: WNDCLASS wc;34:35: wc.style = CS_HREDRAW | CS_VREDRAW;36: wc.lpfnWndProc = (WNDPROC)d3d::WndProc;37: wc.cbClsExtra = 0;38: wc.cbWndExtra = 0;39: wc.hInstance = hInstance;40: wc.hIcon = LoadIcon(0, IDI_APPLICATION);41: wc.hCursor = LoadCursor(0, IDC_ARROW);42: wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);43: wc.lpszMenuName = 0;44: wc.lpszClassName = "Direct3D9App";45:46: if( !RegisterClass(&wc) )47: {48: ::MessageBox(0, "RegisterClass() - FAILED", 0, 0);49: return false;50: }51:52: HWND hwnd = 0;53: hwnd = ::CreateWindow("Direct3D9App", "Direct3D9App",54: WS_EX_TOPMOST,55: 0, 0, width, height,56: 0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/);57:58: if( !hwnd )59: {60: ::MessageBox(0, "CreateWindow() - FAILED", 0, 0);61: return false;62: }63:64: ::ShowWindow(hwnd, SW_SHOW);65: ::UpdateWindow(hwnd);66:67: //68: // Init D3D:69: //70:71: HRESULT hr = 0;72:73: // Step 1: Create the IDirect3D9 object.74:75: IDirect3D9* d3d9 = 0;76: d3d9 = Direct3DCreate9(D3D_SDK_VERSION);77:78: if( !d3d9 )79: {80: ::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0);81: return false;82: }83:84: // Step 2: Check for hardware vp.85:86: D3DCAPS9 caps;87: d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);88:89: int vp = 0;90: if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )91: vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;92: else93: vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;94:95: // Step 3: Fill out the D3DPRESENT_PARAMETERS structure.96:97: D3DPRESENT_PARAMETERS d3dpp;98: d3dpp.BackBufferWidth = width;99: d3dpp.BackBufferHeight = height;100: d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;101: d3dpp.BackBufferCount = 1;102: d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;103: d3dpp.MultiSampleQuality = 0;104: d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;105: d3dpp.hDeviceWindow = hwnd;106: d3dpp.Windowed = windowed;107: d3dpp.EnableAutoDepthStencil = true;108: d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;109: d3dpp.Flags = 0;110: d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;111: d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;112:113: // Step 4: Create the device.114:115: hr = d3d9->CreateDevice(116: D3DADAPTER_DEFAULT, // primary adapter117: deviceType, // device type118: hwnd, // window associated with device119: vp, // vertex processing120: &d3dpp, // present parameters121: device); // return created device122:123: if( FAILED(hr) )124: {125: // try again using a 16-bit depth buffer126: d3dpp.AutoDepthStencilFormat = D3DFMT_D16;127:128: hr = d3d9->CreateDevice(129: D3DADAPTER_DEFAULT,130: deviceType,131: hwnd,132: vp,133: &d3dpp,134: device);135:136: if( FAILED(hr) )137: {138: d3d9->Release(); // done with d3d9 object139: ::MessageBox(0, "CreateDevice() - FAILED", 0, 0);140: return false;141: }142: }143:144: d3d9->Release(); // done with d3d9 object145:146: return true;147: }148:149: int d3d::EnterMsgLoop( bool (*ptr_display)(float timeDelta) )150: {151: MSG msg;152: ::ZeroMemory(&msg, sizeof(MSG));153:154: static float lastTime = (float)timeGetTime();155:156: while(msg.message != WM_QUIT)157: {158: if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))159: {160: ::TranslateMessage(&msg);161: ::DispatchMessage(&msg);162: }163: else164: {165: float currTime = (float)timeGetTime();166: float timeDelta = (currTime - lastTime)*0.001f;167:168: ptr_display(timeDelta);169:170: lastTime = currTime;171: }172: }173: return msg.wParam;174: }175:176:

浙公网安备 33010602011771号