Guest User

Untitled

a guest
Jul 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.20 KB | None | 0 0
  1. // include the basic windows header files and the Direct3D header file
  2. #include <d3d9.h>
  3. #include <d3dx9.h>
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include "Camera.h"
  7.  
  8. #pragma comment (lib, "d3d9.lib")
  9. #pragma comment (lib, "d3dx9.lib")
  10. #pragma comment (lib, "Dinput8.lib")
  11. #pragma comment (lib, "dxguid.lib")
  12. #pragma comment (lib, "winmm.lib")
  13. #pragma comment (lib, "strmiids.lib")
  14.  
  15. // define the screen resolution and keyboard macros
  16. #define SCREEN_WIDTH 1280
  17. #define SCREEN_HEIGHT 720
  18. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  19. #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  20.  
  21. // global declarations
  22. LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
  23. LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
  24. LPDIRECT3DSURFACE9 z_buffer = NULL;    // the pointer to the z-buffer
  25.  
  26. ID3DXMesh*              g_pMesh = NULL;    
  27. IDirect3DTexture9*      g_pMeshTexture = NULL;  // Mesh texture
  28. D3DXMATRIXA16           g_mCenterWorld;
  29. ID3DXEffect*            g_pEffect = NULL;  
  30. float temp;
  31. // function prototypes
  32. void initD3D(HWND hWnd,HINSTANCE hInstance);    // sets up and initializes Direct3D
  33. void render_frame(void);    // renders a single frame
  34. void cleanD3D(void);    // closes Direct3D and releases memory
  35. void init_light(void);
  36.  
  37. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  38.  
  39.  
  40. // the entry point for any Windows program
  41. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
  42.     HWND hWnd;
  43.     WNDCLASSEX wc;
  44.  
  45.     ZeroMemory(&wc, sizeof(WNDCLASSEX));
  46.  
  47.     wc.cbSize = sizeof(WNDCLASSEX);
  48.     wc.style = CS_HREDRAW | CS_VREDRAW;
  49.     wc.lpfnWndProc = (WNDPROC)WindowProc;
  50.     wc.hInstance = hInstance;
  51.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  52.     wc.lpszClassName = "PirateShipWars";
  53.  
  54.     RegisterClassEx(&wc);
  55.  
  56.     hWnd = CreateWindowEx(NULL,
  57.                           "PirateShipWars",
  58.                           "Our Direct3D Program",
  59.                           WS_OVERLAPPEDWINDOW,
  60.                           0, 0,
  61.                           SCREEN_WIDTH, SCREEN_HEIGHT,
  62.                           NULL,
  63.                           NULL,
  64.                           hInstance,
  65.                           NULL);
  66.  
  67.     ShowWindow(hWnd, nCmdShow);
  68.  
  69.     // set up and initialize Direct3D
  70.     initD3D(hWnd,hInstance);
  71.     // enter the main loop:
  72.  
  73.     MSG msg;
  74.  
  75.     while(TRUE)
  76.     {
  77.         DWORD starting_point = GetTickCount();
  78.  
  79.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  80.         {
  81.             if (msg.message == WM_QUIT)
  82.                 break;
  83.  
  84.             TranslateMessage(&msg);
  85.             DispatchMessage(&msg);
  86.         }
  87.  
  88.         render_frame();
  89.  
  90.         if(KEY_DOWN(VK_ESCAPE))
  91.         {
  92.              PostMessage(hWnd, WM_DESTROY, 0, 0);
  93.         }
  94.  
  95.  
  96.         while ((GetTickCount() - starting_point) < 25);
  97.     }
  98.  
  99.     // clean up DirectX and COM
  100.     cleanD3D();
  101.  
  102.     return msg.wParam;
  103. }
  104.  
  105.  
  106. // this is the main message handler for the program
  107. LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  108. {
  109.     switch(message)
  110.     {
  111.         case WM_DESTROY:
  112.             {
  113.                 PostQuitMessage(0);
  114.                 return 0;
  115.             } break;
  116.     }
  117.  
  118.     return DefWindowProc (hWnd, message, wParam, lParam);
  119. }
  120.  
  121.  
  122. // this function initializes and prepares Direct3D for use
  123. void initD3D(HWND hWnd, HINSTANCE hInstance)
  124. {
  125.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  126.     D3DPRESENT_PARAMETERS d3dpp;
  127.    
  128.  
  129.     ZeroMemory(&d3dpp, sizeof(d3dpp));
  130.     d3dpp.Windowed = true;
  131.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  132.     d3dpp.hDeviceWindow = hWnd;
  133.     d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  134.     d3dpp.BackBufferWidth = SCREEN_WIDTH;
  135.     d3dpp.BackBufferHeight = SCREEN_HEIGHT;
  136.     d3dpp.EnableAutoDepthStencil = TRUE;
  137.     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  138.  
  139.     // create a device class using this information and the info from the d3dpp stuct
  140.     d3d->CreateDevice(D3DADAPTER_DEFAULT,
  141.                       D3DDEVTYPE_HAL,
  142.                       hWnd,
  143.                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  144.                       &d3dpp,
  145.                       &d3ddev);
  146.  
  147.  
  148.     // create the z-buffer
  149.     d3ddev->CreateDepthStencilSurface(SCREEN_WIDTH,
  150.                                       SCREEN_HEIGHT,
  151.                                       D3DFMT_D16,
  152.                                       D3DMULTISAMPLE_NONE,
  153.                                       0,
  154.                                       TRUE,
  155.                                       &z_buffer,
  156.                                       NULL);
  157.  
  158.     // using the shader debugger.
  159.     DWORD dwShaderFlags = D3DXFX_NOT_CLONEABLE;
  160.  
  161.     D3DXLoadMeshFromX("box.x", D3DXMESH_MANAGED, d3ddev, NULL, NULL, NULL, NULL, &g_pMesh);
  162.  
  163.     //Test compilation buffer variable
  164.     LPD3DXBUFFER d3dBuffer;
  165.  
  166.     // If this fails, there should be debug output as to
  167.     // why the .fx file failed to compile
  168.     D3DXCreateEffectFromFile( d3ddev, "Ambient.fx", NULL, NULL, dwShaderFlags, NULL, &g_pEffect, &d3dBuffer ); //dwShaderFlags (5)
  169.  
  170.     float temp = 0;
  171.     D3DXVECTOR4 Acolour = D3DXVECTOR4(1,0,1,1);
  172.    
  173.     g_pEffect->SetVector( "AmbientColor" , &Acolour);
  174.  
  175.     float Dtemp = 0.8f;
  176.     D3DXVECTOR4 DColour = D3DXVECTOR4( 0, 1, 0, 1 );
  177.     D3DXVECTOR4 DLight = D3DXVECTOR4( .7f, 1.0f, -.8f, 0 );
  178.     g_pEffect->SetFloat( "DiffuseIntensity", Dtemp);
  179.     g_pEffect->SetVector( "DiffuseColor" , &DColour);
  180.     g_pEffect->SetVector( "DiffuseLightDirection" , &DLight);
  181.  
  182.     D3DXVECTOR3 eye(0, 10, 10);
  183.     D3DXVECTOR3 at(0, 0, 0);
  184.     D3DXVECTOR3 up(0, 1, 0);
  185.  
  186.     // Setup the matrices within the shader (shaders handle matrices on their own unlike the
  187.     // fixed function pipeline)
  188.     D3DXMATRIX worldMatrix;
  189.     D3DXMatrixTranslation(&worldMatrix, 0,0, 0);
  190.  
  191.     D3DXMATRIX viewMatrix;
  192.     D3DXMatrixLookAtLH(&viewMatrix, &eye, &at, &up);
  193.  
  194.     D3DXMATRIX projectionMatrix;
  195.     D3DXMatrixPerspectiveFovLH(&projectionMatrix,
  196.             D3DXToRadian(45),    // the horizontal field of view
  197.             (FLOAT)1920  / (FLOAT)1080, // aspect ratio
  198.             1.0f,   // the near view-plane
  199.             70000.0f);    // the far view-plane
  200.  
  201.     g_pEffect->SetMatrix( "World", &worldMatrix);
  202.     g_pEffect->SetMatrix( "View", &viewMatrix );
  203.     g_pEffect->SetMatrix( "Projection", &projectionMatrix );
  204.  
  205.  
  206.     return;
  207. }
  208.  
  209.  
  210. // this is the function used to render a single frame
  211. void render_frame(void)
  212. {
  213.     if(temp < 1)
  214.     {
  215.         temp += 0.02;
  216.     }
  217.     else
  218.     {
  219.         temp = 0;
  220.     }
  221.     g_pEffect->SetFloat( "AmbientIntensity", temp);
  222.  
  223.     UINT iPass, cPasses;
  224.  
  225.     // Clear the render target and the zbuffer
  226.     d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DXCOLOR(0.0f,0.0,0.0,0.0), 1.0f, 0);
  227.  
  228.  
  229.     d3ddev->BeginScene();
  230.     // Apply the technique contained in the effect
  231.     g_pEffect->Begin(&cPasses, 0);
  232.  
  233.     for (iPass = 0; iPass < cPasses; iPass++)
  234.     {
  235.         g_pEffect->BeginPass(iPass);
  236.  
  237.         g_pMesh->DrawSubset(0);
  238.  
  239.         g_pEffect->EndPass();
  240.     }
  241.     g_pEffect->End();
  242.  
  243.     d3ddev->EndScene();
  244.     d3ddev->Present(NULL, NULL, NULL, NULL);
  245.  
  246.     return;
  247. }
  248.  
  249.  
  250. // this is the function that cleans up Direct3D and COM
  251. void cleanD3D(void)
  252. {
  253.     d3ddev->Release();    // close and release the 3D device
  254.     d3d->Release();    // close and release Direct3D
  255.     return;
  256. }
Add Comment
Please, Sign In to add comment