d3dUtility.h #ifndef __d3dUtilityH__ #define __d3dUtilityH__ #include <d3dx9.h> #include <string> 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__ d3dUtility.cpp #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 = "Direct3D9App"; if( !RegisterClass(&wc) ) { ::MessageBox(0, "RegisterClass() - FAILED", 0, 0); return false; } HWND hwnd = 0; hwnd = ::CreateWindow("Direct3D9App", "Direct3D9App", WS_EX_TOPMOST, 0, 0, width, height, 0 /*parent hwnd*/, 0 /* menu */, hInstance, 0 /*extra*/); if( !hwnd ) { ::MessageBox(0, "CreateWindow() - FAILED", 0, 0); return false; } RECT _clientRect, _wndRect; GetClientRect(hwnd, &_clientRect); GetWindowRect(hwnd, &_wndRect); // Set the width and height (set your dimensions here) DWORD _desiredWidth = 1024; DWORD _desiredHeight = 768; DWORD _width = (_wndRect.right - _wndRect.left) + (_desiredWidth - _clientRect.right); DWORD _height = (_wndRect.bottom - _wndRect.top) + (_desiredHeight - _clientRect.bottom); // Set the window's dimensions MoveWindow(hwnd, _wndRect.left, _wndRect.top, _width, _height, TRUE); ::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, "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. D3DDISPLAYMODE playMode; d3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT , &playMode); D3DPRESENT_PARAMETERS d3dpp; memset(&d3dpp, 0, sizeof(d3dpp)); d3dpp.BackBufferWidth = width; d3dpp.BackBufferHeight = height; d3dpp.BackBufferFormat = playMode.Format; d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.MultiSampleQuality = 0; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hwnd; d3dpp.Windowed = windowed; 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, "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; } vertex.h #ifndef __vertexH__ #define __vertexH__ struct Vertex { Vertex(){} Vertex( float x, float y, float z, DWORD color, float u, float v) { _x = x; _y = y; _z = z; _color = color; _u = u; _v = v; } float _x, _y, _z; DWORD _color; float _u, _v; }; #define FVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1) #endif // __vertexH__ test.cpp #include "d3dUtility.h" #include "vertex.h" D3DXMATRIX matView; D3DXMATRIX matProj; D3DXMATRIX matWorld; IDirect3DDevice9* Device = 0; const int Width = 1024; const int Height = 768; IDirect3DVertexBuffer9* BackDropVB = 0; IDirect3DTexture9* BackDropTex = 0; bool Setup() { Vertex v[6]; v[0] = Vertex(0.0f, 0.0f, 0.0f, 0xffffffff, 0.0f, 0.0f); v[2] = Vertex(0.0f, 500.0f, 0.0f, 0xffffffff, 0.0f, 1.0f); v[1] = Vertex(500.0f, 500.0f, 0.0f, 0xffffffff, 1.0f, 1.0f); v[3] = Vertex(0.0f, 0.0f, 0.0f, 0xffffffff, 0.0f, 0.0f); v[5] = Vertex(500.0f, 500.0f, 0.0f, 0xffffffff, 1.0f, 1.0f); v[4] = Vertex(500.0f, 0.0f, 0.0f, 0xffffffff, 1.0f, 0.0f); if(FAILED(Device->CreateVertexBuffer(6*sizeof(Vertex),0,FVF_VERTEX,D3DPOOL_DEFAULT,&BackDropVB,NULL))) { return 0; } VOID *pVertices; if(FAILED(BackDropVB->Lock(0,sizeof(v),(void**)&pVertices,0))) { return 0; } memcpy(pVertices,v,sizeof(v)); BackDropVB->Unlock(); D3DXIMAGE_INFO info; D3DXGetImageInfoFromFile("40000.tga", &info); HRESULT _hr; if((FAILED(_hr = D3DXCreateTextureFromFileEx( (LPDIRECT3DDEVICE9)Device, "40000.tga", info.Width, info.Height, 1, 0, info.Format , D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT, 0, 0, 0, &BackDropTex)))) { int ii = 0; ii++; } Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_NONE); Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_NONE); Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true); Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); Device->SetRenderState(D3DRS_LIGHTING, false); D3DXMatrixIdentity(&matView); Device->SetTransform(D3DTS_VIEW, &matView); D3DVIEWPORT9 _vp; _vp.X = static_cast<DWORD>(0); _vp.Y = static_cast<DWORD>(0); _vp.Width = static_cast<DWORD>(1024); _vp.Height = static_cast<DWORD>(768); _vp.MinZ = 0.0f; _vp.MaxZ = 1.0f; Device->SetViewport(&_vp); const float _fov = 0.523598776f; const float _w = 1024; const float _h = 768; const float _aspect = _w / _h; const float _midx = _w * 0.5f; const float _midy = _h * 0.5f; float viewDistance = _midx / (_aspect * 0.267949192431123f); D3DXVECTOR3 _eye(_midx, _midy, -viewDistance); D3DXVECTOR3 _at(_midx, _midy, 1); D3DXVECTOR3 _up(0, -1, 0); D3DXMATRIX _tmp; D3DXMatrixMultiply(&matProj, D3DXMatrixLookAtRH(&matProj, &_eye, &_at, &_up), D3DXMatrixPerspectiveFovRH(&_tmp, _fov, _aspect, viewDistance * 0.5f, viewDistance * 2.0f)); Device->SetTransform(D3DTS_PROJECTION, &matProj); D3DXMatrixIdentity(&matWorld); Device->SetTransform(D3DTS_WORLD, &matWorld); //Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); return true; } void Cleanup() { d3d::Release<IDirect3DVertexBuffer9*>(BackDropVB); d3d::Release<IDirect3DTexture9*>(BackDropTex); } bool Display(float timeDelta) { if( Device ) { Device->Clear(0, 0, D3DCLEAR_TARGET /*| D3DCLEAR_ZBUFFER*/, 0xffffffff, 1.0f, 0); Device->BeginScene(); Device->SetStreamSource(0, BackDropVB, 0, sizeof(Vertex)); Device->SetFVF(FVF_VERTEX); Device->SetTexture(0, BackDropTex); Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); Device->EndScene(); Device->Present(0, 0, 0, 0); } return true; } 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); } int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) { if(!d3d::InitD3D(hinstance, Width, Height, true, D3DDEVTYPE_HAL, &Device)) { ::MessageBox(0, "InitD3D() - FAILED", 0, 0); return 0; } if(!Setup()) { ::MessageBox(0, "Setup() - FAILED", 0, 0); return 0; } d3d::EnterMsgLoop( Display ); Cleanup(); Device->Release(); return 0; }