Advertisement
Guest User

Untitled

a guest
May 3rd, 2013
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.08 KB | None | 0 0
  1. #include <windows.h>
  2. #include <d3d9.h>
  3. #include <d3dx9tex.h>
  4. #include <dxerr.h>
  5.  
  6. // define global constants once here, not every frame
  7. const DWORD D3DFVF_TLVERTEX = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;
  8.  
  9. // define you types once here, not every frame
  10. struct TLVERTEX
  11. {
  12.     float x, y, z, rhw;
  13.     D3DCOLOR color;
  14.     float u;
  15.     float v;
  16. };
  17.  
  18. HWND hWnd = 0;
  19. LPDIRECT3D9 direct3D = NULL;
  20. LPDIRECT3DDEVICE9 direct3DDevice = NULL;
  21.  
  22. // You dont need to create your VB every frame (resource creation is very slow)
  23. // Instead do it once on app initialization.
  24. // Never make uninitialized variables.
  25. // Assign to null and change it later
  26. IDirect3DTexture9* texture = 0;
  27. IDirect3DVertexBuffer9* vertexBuffer = 0;
  28.  
  29. // Make global variable instead using "magic strings" or "magic nunbers"
  30. const wchar_t* pathToMyPicture = L"DirectX.png";
  31.  
  32.  
  33. void Initialize(HINSTANCE hInstance);
  34. IDirect3DVertexBuffer9* CreateVertexBuffer (const D3DCOLOR& vertexColour, const RECT& rDest);
  35. IDirect3DTexture9 *LoadTexture(const wchar_t *fileName);
  36. void Run();
  37. void Render();
  38. void Cleanup();
  39.  
  40. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  41.  
  42. // Here you put things that will
  43. // be done only once in the beginning of your application
  44. void Initialize(HINSTANCE hInstance)
  45. {
  46.     // -------- Here you creating the window-------------------------
  47.     // TODO: move this code to separate function and call this function here
  48.     // Added primitive error checking
  49.    
  50.     WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC,
  51.         WndProc, 0, 0, hInstance, NULL, NULL, (HBRUSH)(COLOR_WINDOW+1),
  52.         NULL, L"DX9_TUTORIAL1_CLASS", NULL};
  53.    
  54.     if(!RegisterClassEx(&wc))
  55.     {
  56.         MessageBox(0, L"RegisterClassEx failed", L"Error!", MB_OK);
  57.     }
  58.  
  59.     hWnd = CreateWindow(L"DX9_TUTORIAL1_CLASS",
  60.         L"DirectX 9 Bare Bones Tutorial 1",
  61.         WS_OVERLAPPEDWINDOW, 100, 100, 800, 600,
  62.         NULL, NULL, hInstance, NULL);
  63.  
  64.     if(!hWnd)
  65.     {
  66.         MessageBox(0, L"RegisterClassEx failed", L"Error!", MB_OK);
  67.     }
  68.  
  69.     ShowWindow(hWnd, SW_SHOW);
  70.     UpdateWindow(hWnd);
  71.  
  72.     // ----------Here you creating D3D9 core interfaces --------------------
  73.     // TODO: move this code to separate function and call this function here
  74.     // Added primitive error checking
  75.    
  76.     direct3D = Direct3DCreate9(D3D_SDK_VERSION);
  77.     if (!direct3D)
  78.     {
  79.         MessageBox(0, L"Direct3DCreate9 failed", L"Error!", MB_OK);
  80.     }
  81.    
  82.     D3DPRESENT_PARAMETERS PresentParams;
  83.     memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS));
  84.  
  85.     PresentParams.Windowed = true;
  86.     PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
  87.     direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  88.         D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParams, &direct3DDevice);
  89.  
  90.     if (!direct3DDevice)
  91.     {
  92.         MessageBox(0, L"CreateDevice failed", L"Error!", MB_OK);
  93.     }
  94.  
  95.     direct3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
  96.     direct3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  97.     direct3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  98.     direct3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  99.     direct3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  100.    
  101.  
  102.  
  103.     // ------------- Here you creating your resources -------------------
  104.     // Resources are: models, textures, shaders, etc.
  105.     // Resource creation is very slow
  106.     // and you dont need to re-create them every frame.
  107.     // Instead, do it once here, on initialization.
  108.     // TODO: move this code to separate function and call this function here
  109.  
  110.  
  111.     D3DCOLOR vertexColor = D3DCOLOR_ARGB(255, 255, 255, 255);
  112.    
  113.     // No need to use dynamic allocation and pointer here
  114.     // You can pass rect to CreateVertexBuffer function by const reference
  115.     RECT rect = {0};
  116.     rect.left = 10;
  117.     rect.top = 10;
  118.     rect.bottom = 60;
  119.     rect.right = 60;
  120.     vertexBuffer = CreateVertexBuffer(vertexColor, rect);
  121.    
  122.     // Your creation functions can return null if failed
  123.     // (check their code),
  124.     // so you must check this exceptional situation.
  125.     // Best way is to throw exception, a
  126.     // simple way is just create MessageBox
  127.     if (!vertexBuffer)
  128.     {
  129.         MessageBox(0, L"CreateVertexBuffer failed", L"Error!", MB_OK);
  130.     }
  131.  
  132.     // Make global variable instead using "magic strings" or "magic nunbers"
  133.     texture = LoadTexture(pathToMyPicture);
  134.     if (!vertexBuffer)
  135.     {
  136.         MessageBox(0, L"LoadTexture failed", L"Error!", MB_OK);
  137.     }
  138.  
  139. }
  140.  
  141. // Here you creating your vertex buffer
  142. IDirect3DVertexBuffer9* CreateVertexBuffer (const D3DCOLOR& vertexColour, const RECT& rDest)
  143. {
  144.     IDirect3DVertexBuffer9* vertexBuffer = 0;
  145.  
  146.     // Set vertex shader.
  147.     direct3DDevice->SetVertexShader(NULL);
  148.     direct3DDevice->SetFVF(D3DFVF_TLVERTEX);
  149.    
  150.     // Create vertex buffer.
  151.     direct3DDevice->CreateVertexBuffer(sizeof(TLVERTEX) * 4, NULL,
  152.         D3DFVF_TLVERTEX, D3DPOOL_MANAGED, &vertexBuffer, NULL);
  153.     direct3DDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(TLVERTEX));
  154.    
  155.     TLVERTEX* vertices;
  156.    
  157.     //Lock the vertex buffer
  158.     vertexBuffer->Lock(0, 0, (void**)&vertices, NULL);
  159.    
  160.     vertices[0].color = vertexColour;
  161.     vertices[0].x = (float) rDest.left - 0.5f;
  162.     vertices[0].y = (float) rDest.top - 0.5f;
  163.     vertices[0].z = 0.0f;
  164.     vertices[0].rhw = 1.0f;
  165.     vertices[0].u = 0.0f;
  166.     vertices[0].v = 0.0f;
  167.    
  168.     vertices[1].color = vertexColour;
  169.     vertices[1].x = (float) rDest.right - 0.5f;
  170.     vertices[1].y = (float) rDest.top - 0.5f;
  171.     vertices[1].z = 0.0f;
  172.     vertices[1].rhw = 1.0f;
  173.     vertices[1].u = 1.0f;
  174.     vertices[1].v = 0.0f;
  175.    
  176.     vertices[2].color = vertexColour;
  177.     vertices[2].x = (float) rDest.right - 0.5f;
  178.     vertices[2].y = (float) rDest.bottom - 0.5f;
  179.     vertices[2].z = 0.0f;
  180.     vertices[2].rhw = 1.0f;
  181.     vertices[2].u = 1.0f;
  182.     vertices[2].v = 1.0f;
  183.    
  184.     vertices[3].color = vertexColour;
  185.     vertices[3].x = (float) rDest.left - 0.5f;
  186.     vertices[3].y = (float) rDest.bottom - 0.5f;
  187.     vertices[3].z = 0.0f;
  188.     vertices[3].rhw = 1.0f;
  189.     vertices[3].u = 0.0f;
  190.     vertices[3].v = 1.0f;
  191.    
  192.     //Unlock the vertex buffer
  193.     vertexBuffer->Unlock();
  194.    
  195.     return vertexBuffer;
  196. }
  197.  
  198. // Pass values as const if you not planning to change it in a function
  199. // (usually, you will want pass most input values as const)
  200. IDirect3DTexture9 *LoadTexture(const wchar_t *fileName)
  201. {
  202.     // Never make uninitialized variables.
  203.     // Assign to null and change it later
  204.     IDirect3DTexture9 *d3dTexture = 0;
  205.  
  206.     D3DCOLOR colorkey = 0; //0xFFFF00FF;
  207.  
  208.     // More advancer error checking but still not perfect
  209.     HRESULT hr = S_OK;
  210.  
  211.     // More simple variant
  212.     //if (FAILED(D3DXCreateTextureFromFile(direct3DDevice, fileName, &d3dTexture)))
  213.  
  214.     if(FAILED(hr = D3DXCreateTextureFromFileEx (direct3DDevice, fileName, 0, 0, 0, 0,
  215.         D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT, colorkey, 0, 0, &d3dTexture)))
  216.     {
  217.         std::wstring errorString = DXGetErrorString(hr); // Here you get error string
  218.         std::wstring errorDesc = DXGetErrorDescription(hr); // Here you get error description
  219.         std::wstring messageString(L"D3DXCreateTextureFromFileEx failed:\n");
  220.         messageString += errorString;
  221.         messageString += L"\n";
  222.         messageString += errorDesc;
  223.  
  224.         MessageBox(0, messageString.c_str(), L"Error!", MB_OK);            
  225.                         // No need to return null here, because if function failed,
  226.                         // your texture is already null (we assigned it to null)
  227.         DebugBreak();
  228.     }
  229.  
  230.  
  231.     return d3dTexture;
  232. }
  233.  
  234. // Main loop (!)
  235. // Heart of your papplication
  236. // Never use WM_PAINT to trigger directx frames
  237. void Run()
  238. {
  239.     MSG msg = {0};
  240.    
  241.     // Until WM_QUIT message
  242.     while(msg.message != WM_QUIT)
  243.     {
  244.         // If there are windows messages
  245.         if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
  246.         {
  247.             // Process them
  248.             TranslateMessage( &msg );
  249.             DispatchMessage( &msg );
  250.         }
  251.         // If there are no messages
  252.         else
  253.         {  
  254.             // Render one frame
  255.             Render();
  256.         }
  257.     }
  258. }
  259.  
  260. // Here you put things that
  261. // you will do every frame
  262. void Render()
  263. {
  264.     RECT* rect = new RECT;
  265.     rect->left = 10;
  266.     rect->top = 10;
  267.     rect->bottom = 60;
  268.     rect->right = 60;
  269.  
  270.     direct3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0),
  271.         1.0f, 0);
  272.  
  273.     direct3DDevice->BeginScene();
  274.  
  275.     direct3DDevice->SetTexture (0, texture);
  276.     direct3DDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
  277.  
  278.     direct3DDevice->EndScene();
  279.  
  280.     direct3DDevice->Present(NULL, NULL, NULL, NULL);
  281. }
  282.  
  283. // Here you put things that will
  284. // be done only once in the beginning of your application
  285. void Cleanup()
  286. {
  287.     // You forget to clean up VB and texture resource
  288.     // It was memory leak
  289.    
  290.     // Also, check if pointer is not null before dereference it
  291.     // to prevent errors
  292.  
  293.     if (vertexBuffer)
  294.     {
  295.         vertexBuffer->Release();
  296.     }
  297.     if (texture)
  298.     {
  299.         texture->Release();
  300.     }
  301.     if (direct3DDevice)
  302.     {
  303.         direct3DDevice->Release();
  304.     }
  305.     if (direct3D)
  306.     {
  307.         direct3D->Release();
  308.     }
  309. }
  310.  
  311. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow)
  312. {
  313.     // Isn't it much cleaner here? =)
  314.     Initialize(hInstance);
  315.     Run();
  316.     Cleanup();
  317.  
  318.     return 0;
  319. }
  320.  
  321. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  322. {
  323.     switch (msg)
  324.     {
  325.         case WM_DESTROY:
  326.         {
  327.             PostQuitMessage(0);
  328.             break;
  329.         }
  330.  
  331.         default:
  332.         {
  333.  
  334.             break;
  335.         }
  336.        break;
  337.     }
  338.     return (DefWindowProc(hWnd, msg, wParam, lParam));
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement