Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. //Every windows application needs to include this
  2. #include "windows.h"
  3.  
  4. //Every Direct3D application this
  5. #include "d3d9.h"
  6. #include "d3dx9.h"
  7.  
  8. bool g_bContinue = true;
  9. LPD3DXEFFECT g_lpEffect = NULL;
  10. LPDIRECT3DVERTEXBUFFER9 g_lpVertexBuffer = NULL;
  11. D3DXMATRIX g_ShaderMatrix;
  12.  
  13. //Definition of the Vertex Format including position and diffuse color
  14. #define D3DFVF_COLOREDVERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
  15.  
  16. struct COLORED_VERTEX
  17. {
  18.     float x, y, z;  //Position
  19.     DWORD color;    //Color
  20. };
  21.  
  22. //Besides the main function, there must be a message processing function
  23. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  24. {
  25.     switch( msg )
  26.     {
  27.         case WM_DESTROY:
  28.             PostQuitMessage( 0 );
  29.             g_bContinue = false;
  30.             return 0;
  31.     }
  32.     return DefWindowProc( hWnd, msg, wParam, lParam );
  33. }
  34.  
  35. //The entry point of a windows application is the WinMain function
  36. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  37. {
  38.     //Create a window class.
  39.     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
  40.                     GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  41.                     "Direct3D Window", NULL };
  42.  
  43.     //Register the window class.
  44.     RegisterClassEx( &wc );
  45.  
  46.     //Create the application's window.
  47.     HWND hWnd = CreateWindow( "Direct3D Window", "DirectXers - D3D9 Tutorial 2",
  48.                             WS_OVERLAPPEDWINDOW, 100, 100, 400, 400,
  49.                             GetDesktopWindow(), NULL, wc.hInstance, NULL );
  50.  
  51.     ShowWindow(hWnd,SW_SHOW);
  52.  
  53.     //Create the Direct3D Object
  54.     LPDIRECT3D9 pD3D = NULL;
  55.     if( NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
  56.       return E_FAIL;
  57.  
  58.     //Setup the device presentation parameters
  59.     D3DPRESENT_PARAMETERS d3dpp;
  60.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  61.     d3dpp.Windowed = TRUE;
  62.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  63.     d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  64.  
  65.     //The final step is to use the IDirect3D9::CreateDevice method to create the Direct3D device, as illustrated in the
  66.     //following code example.
  67.     LPDIRECT3DDEVICE9 pd3dDevice = NULL;
  68.     if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  69.                                     D3DCREATE_HARDWARE_VERTEXPROCESSING,
  70.                                     &d3dpp, &pd3dDevice ) ) )
  71.     {
  72.         MessageBox(hWnd, "No HAL HARDWARE_VERTEXPROCESSING! Sample will exit!", NULL, 0);
  73.         pD3D->Release();
  74.         pD3D = NULL;
  75.         return E_FAIL;
  76.     }
  77.        
  78.     //set the vertex buffer size 4 vertices * vertex structure size
  79.     UINT uiBufferSize = 4*sizeof(COLORED_VERTEX);
  80.  
  81.     //create the buffer
  82.     if( FAILED( pd3dDevice->CreateVertexBuffer( uiBufferSize,
  83.             D3DUSAGE_WRITEONLY, D3DFVF_COLOREDVERTEX, D3DPOOL_DEFAULT, &g_lpVertexBuffer, NULL ) ) )
  84.         return E_FAIL;
  85.    
  86.     COLORED_VERTEX* pVertices;
  87.     //lock the buffer for writing
  88.     if( FAILED( g_lpVertexBuffer->Lock( 0, uiBufferSize, (void**)&pVertices, 0 ) ) )
  89.         return E_FAIL;
  90.  
  91.     //write the vertices. Here a simple rectangle
  92.     pVertices[0].x =  -1.0f; //left
  93.     pVertices[0].y =  -1.0f; //bottom
  94.     pVertices[0].z =   0.0f;
  95.     pVertices[0].color =  0xffff0000; //red
  96.  
  97.     pVertices[1].x =  -1.0f; //left
  98.     pVertices[1].y =   1.0f; //top
  99.     pVertices[1].z =   0.0f;
  100.     pVertices[1].color =  0xff0000ff; //blue
  101.  
  102.     pVertices[2].x =   1.0f; //right
  103.     pVertices[2].y =  -1.0f; //bottom
  104.     pVertices[2].z =   0.0f;
  105.     pVertices[2].color =  0xff00ff00; //green
  106.  
  107.     pVertices[3].x =  1.0f; //right
  108.     pVertices[3].y =  1.0f; //top
  109.     pVertices[3].z =  0.0f;
  110.     pVertices[3].color =  0xffffffff; //white
  111.  
  112.     //unlock the buffer
  113.     g_lpVertexBuffer->Unlock();
  114.  
  115.     //set the Vertex Format
  116.     pd3dDevice->SetFVF( D3DFVF_COLOREDVERTEX );
  117.  
  118.     //transfer the buffer to the gpu
  119.     pd3dDevice->SetStreamSource( 0, g_lpVertexBuffer, 0, sizeof(COLORED_VERTEX) );
  120.  
  121.     //create an effect
  122.     if(FAILED(D3DXCreateEffectFromFile( pd3dDevice, "Effect.fx", NULL,
  123.         NULL, D3DXSHADER_ENABLE_BACKWARDS_COMPATIBILITY, NULL, &g_lpEffect, NULL )))
  124.         return E_FAIL;
  125.  
  126.     D3DXMatrixIdentity(&g_ShaderMatrix);
  127.     g_lpEffect->SetMatrix( "ShaderMatrix", &g_ShaderMatrix );
  128.  
  129.     MSG msg;
  130.     while( g_bContinue )
  131.     {
  132.         //Clear render region with blue
  133.         pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
  134.  
  135.         //before rendering something, you have to call this
  136.         pd3dDevice->BeginScene();
  137.  
  138.         //rendering of scene objects happens here
  139.  
  140.         //begin the effect
  141.         UINT uiPasses = 0;
  142.         g_lpEffect->Begin(&uiPasses, 0);
  143.         for (UINT uiPass = 0; uiPass < uiPasses; uiPass++)
  144.         {
  145.             //render an effect pass
  146.             g_lpEffect->BeginPass(uiPass);
  147.  
  148.             //render the rectangle
  149.             pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
  150.  
  151.             g_lpEffect->EndPass();
  152.         }
  153.         g_lpEffect->End();
  154.  
  155.         //after the scene call
  156.         pd3dDevice->EndScene();
  157.  
  158.         //update screen = swap front and backbuffer
  159.         pd3dDevice->Present(NULL, NULL, NULL, NULL);
  160.  
  161.         // A window has to handle its messages.
  162.         TranslateMessage( &msg );
  163.         DispatchMessage( &msg );
  164.         PeekMessage(&msg, 0, 0, 0, PM_REMOVE);
  165.     }
  166.  
  167.     //Do not forget to clean up here
  168.     pd3dDevice->Release();
  169.     pd3dDevice = NULL;
  170.     pD3D->Release();
  171.     pD3D = NULL;    
  172.     g_lpEffect->Release();
  173.     g_lpEffect = NULL;
  174.     g_lpVertexBuffer->Release();
  175.     g_lpVertexBuffer = NULL;
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement