Advertisement
Guest User

Untitled

a guest
Oct 26th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <d3d9.h>
  4. #include <d3dx9.h>
  5.  
  6. #include "AxisRender.h"
  7.  
  8. #define WIDTH 800
  9. #define HEIGTH 600
  10.  
  11. #pragma comment (lib, "d3d9.lib")
  12. #pragma comment (lib, "d3dx9d.lib")
  13.  
  14. IDirect3D9* d3d;
  15. IDirect3DDevice9* d3ddev;
  16.  
  17. void initD3D(HWND hWnd);
  18. void render_frame(void);
  19. void cleanD3D(void);
  20.  
  21. AxisRender* axis;
  22.  
  23. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  24.  
  25. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  26. {
  27.     HWND hWnd;
  28.     WNDCLASSEX wc;
  29.  
  30.     memset(&wc, 0, sizeof(WNDCLASSEX));
  31.  
  32.     wc.cbSize = sizeof(WNDCLASSEX);
  33.     wc.style = CS_HREDRAW | CS_VREDRAW;
  34.     wc.lpfnWndProc = WindowProc;
  35.     wc.hInstance = hInstance;
  36.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  37.     wc.lpszClassName = L"WindowClass";
  38.  
  39.     RegisterClassEx(&wc);
  40.  
  41.     hWnd = CreateWindowEx   (  
  42.                                 NULL,
  43.                                 L"WindowClass",
  44.                                 L"D3D",
  45.                                 WS_EX_TOPMOST | WS_POPUP,
  46.                                 0, 0,
  47.                                 WIDTH, HEIGTH,
  48.                                 NULL,
  49.                                 NULL,
  50.                                 hInstance,
  51.                                 NULL
  52.                             );
  53.  
  54.     ShowWindow(hWnd, nCmdShow);
  55.  
  56.     initD3D(hWnd);
  57.  
  58.     MSG msg;
  59.  
  60.     axis = new AxisRender(200.0f, D3DCOLOR_XRGB(200, 200, 200));
  61.     axis->InitVideoMemory(d3ddev);
  62.  
  63.     while(1)
  64.     {
  65.         while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  66.         {
  67.             TranslateMessage(&msg);
  68.             DispatchMessage(&msg);
  69.         }
  70.  
  71.         if(msg.message == WM_QUIT) break;
  72.  
  73.         render_frame();
  74.     }
  75.  
  76.     cleanD3D();
  77.  
  78.     delete axis;
  79.  
  80.     return msg.wParam;
  81. }
  82.  
  83. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  84. {
  85.     switch(message)
  86.     {
  87.         case WM_DESTROY:
  88.             {
  89.                 PostQuitMessage(0);
  90.                 return 0;
  91.             } break;
  92.     }
  93.  
  94.     return DefWindowProc (hWnd, message, wParam, lParam);
  95. }
  96.  
  97. void initD3D(HWND hWnd)
  98. {
  99.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  100.  
  101.     D3DPRESENT_PARAMETERS d3dpp;
  102.  
  103.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  104.     d3dpp.Windowed = TRUE;
  105.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  106.     d3dpp.hDeviceWindow = hWnd;
  107.     d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  108.     d3dpp.BackBufferWidth = WIDTH;
  109.     d3dpp.BackBufferHeight = HEIGTH;
  110.  
  111.  
  112.     d3d->CreateDevice   (
  113.                             D3DADAPTER_DEFAULT,
  114.                             D3DDEVTYPE_HAL,
  115.                             hWnd,
  116.                             D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  117.                             &d3dpp,
  118.                             &d3ddev
  119.                         );
  120.  
  121.     d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
  122. }
  123.  
  124. void render_frame(void)
  125. {
  126.     d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  127.  
  128.     d3ddev->BeginScene();
  129.  
  130.     // camera
  131.     D3DXMATRIX viewMatrix;
  132.     D3DXMatrixLookAtLH(&viewMatrix, &D3DXVECTOR3(10.0f, 4.0f, 5.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
  133.     d3ddev->SetTransform(D3DTS_VIEW, &viewMatrix);
  134.  
  135.     D3DXMATRIX projMatrix;
  136.     D3DXMatrixPerspectiveFovLH(&projMatrix, D3DXToRadian(45), (float)WIDTH / (float)HEIGTH, 1.0f, 100.0f);
  137.     d3ddev->SetTransform(D3DTS_PROJECTION, &projMatrix);
  138.  
  139.     axis->Render();
  140.  
  141.     d3ddev->EndScene();
  142.  
  143.     d3ddev->Present(NULL, NULL, NULL, NULL);
  144. }
  145.  
  146.  
  147. void cleanD3D(void)
  148. {
  149.     d3ddev->Release();
  150.     d3d->Release();
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement