Main.cpp
上传用户:whgydz
上传日期:2007-01-12
资源大小:2259k
文件大小:6k
源码类别:

其他书籍

开发平台:

HTML/CSS

  1. #include <d3d8.h>
  2. LPDIRECT3D8 g_pD3D = NULL;
  3. LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
  4. LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Buffer to hold vertices
  5. struct CUSTOMVERTEX
  6. {
  7.     FLOAT x, y, z, rhw; // The transformed position for the vertex.
  8.     DWORD colour; // The vertex colour.
  9. };
  10. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
  11. #define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=NULL;}
  12. HRESULT InitialiseD3D(HWND hWnd)
  13. {
  14.     //First of all, create the main D3D object. If it is created successfully we 
  15.     //should get a pointer to an IDirect3D8 interface.
  16.     g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
  17.     if(g_pD3D == NULL)
  18.     {
  19.         return E_FAIL;
  20.     }
  21.     //Get the current display mode
  22.     D3DDISPLAYMODE d3ddm;
  23.     if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
  24.     {
  25.         return E_FAIL;
  26.     }
  27.     //Create a structure to hold the settings for our device
  28.     D3DPRESENT_PARAMETERS d3dpp; 
  29.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  30.     //Fill the structure. 
  31.     //We want our program to be windowed, and set the back buffer to a format
  32.     //that matches our current display mode
  33.     d3dpp.Windowed = TRUE;
  34.     d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
  35.     d3dpp.BackBufferFormat = d3ddm.Format;
  36.     //Create a Direct3D device.
  37.     if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
  38.                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
  39.     {
  40.         return E_FAIL;
  41.     }
  42.     
  43.     return S_OK;
  44. }
  45. HRESULT InitialiseVertexBuffer()
  46. {
  47. VOID* pVertices;
  48. //Store each point of the triangle together with it's colour
  49. CUSTOMVERTEX cvVertices[] =
  50. {
  51. {250.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),}, //Vertex 1 - Red (250, 100)
  52. {400.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),}, //Vertex 2 - Green (400, 350)
  53. {100.0f, 350.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),}, //Vertex 3 - Blue (100, 350)
  54. };
  55. //Create the vertex buffer from our device
  56. if(FAILED(g_pD3DDevice->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX),
  57.                                                0, D3DFVF_CUSTOMVERTEX,
  58.                                                D3DPOOL_DEFAULT, &g_pVertexBuffer)))
  59. {
  60. return E_FAIL;
  61. }
  62. //Get a pointer to the vertex buffer vertices and lock the vertex buffer
  63. if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
  64. {
  65. return E_FAIL;
  66. }
  67. //Copy our stored vertices values into the vertex buffer
  68. memcpy(pVertices, cvVertices, sizeof(cvVertices));
  69. //Unlock the vertex buffer
  70. g_pVertexBuffer->Unlock();
  71.     return S_OK;
  72. }
  73. void Render()
  74. {
  75.     if(g_pD3DDevice == NULL)
  76.     {
  77.         return;
  78.     }
  79.     //Clear the backbuffer to black
  80.     g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  81.     
  82.     //Begin the scene
  83.     g_pD3DDevice->BeginScene();
  84.     
  85.     //Rendering our triangle
  86. g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(CUSTOMVERTEX));
  87. g_pD3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
  88. g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  89.     //End the scene
  90.     g_pD3DDevice->EndScene();
  91.     
  92.     //Filp the back and front buffers so that whatever has been rendered on the back buffer
  93.     //will now be visible on screen (front buffer).
  94.     g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
  95. }
  96. void CleanUp()
  97. {
  98. SafeRelease(g_pVertexBuffer);
  99. SafeRelease(g_pD3DDevice);
  100. SafeRelease(g_pD3D);
  101. }
  102. void GameLoop()
  103. {
  104.     //Enter the game loop
  105.     MSG msg; 
  106.     BOOL fMessage;
  107.     PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
  108.     
  109.     while(msg.message != WM_QUIT)
  110.     {
  111.         fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
  112.         if(fMessage)
  113.         {
  114.             //Process message
  115.             TranslateMessage(&msg);
  116.             DispatchMessage(&msg);
  117.         }
  118.         else
  119.         {
  120.             //No message to process, so render the current scene
  121.             Render();
  122.         }
  123.     }
  124. }
  125. //The windows message handler
  126. LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  127. {
  128.     switch(msg)
  129.     {
  130.         case WM_DESTROY:
  131.             PostQuitMessage(0);
  132.             return 0;
  133.         break;
  134.         case WM_KEYUP: 
  135.             switch (wParam)
  136.             { 
  137.                 case VK_ESCAPE:
  138.                     //User has pressed the escape key, so quit
  139.                     DestroyWindow(hWnd);
  140.                     return 0;
  141.                 break;
  142.             } 
  143.         break;
  144.     }
  145.     return DefWindowProc(hWnd, msg, wParam, lParam);
  146. }
  147. //Application entry point
  148. INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
  149. {
  150.     //Register the window class
  151.     WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L, 
  152.                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  153.                      "DX Project 2", NULL};
  154.     RegisterClassEx(&wc);
  155.     //Create the application's window
  156.     HWND hWnd = CreateWindow("DX Project 2", "www.andypike.com: Tutorial 2", 
  157.                               WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
  158.                               GetDesktopWindow(), NULL, wc.hInstance, NULL);
  159.     //Initialize Direct3D
  160.     if(SUCCEEDED(InitialiseD3D(hWnd)))
  161.     { 
  162.         //Show our window
  163.         ShowWindow(hWnd, SW_SHOWDEFAULT);
  164.         UpdateWindow(hWnd);
  165. //Initialize Vertex Buffer
  166. if(SUCCEEDED(InitialiseVertexBuffer()))
  167. {
  168. //Start game running: Enter the game loop
  169. GameLoop();
  170. }
  171.     }
  172.     
  173.     CleanUp();
  174.     UnregisterClass("DX Project 2", wc.hInstance);
  175.     
  176.     return 0;
  177. }