Advertisement
atm959

main.cpp

Dec 29th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.28 KB | None | 0 0
  1.  
  2.  
  3. //Include and link appropriate libraries and headers//
  4. #pragma comment(lib, "d3d11.lib")
  5. #pragma comment(lib, "d3dx11.lib")
  6. #pragma comment(lib, "d3dx10.lib")
  7.  
  8. #include <windows.h>
  9. #include <d3d11.h>
  10. #include <d3dx11.h>
  11. #include <d3dx10.h>
  12. #include <xnamath.h>
  13.  
  14. #include <time.h>
  15.  
  16. //Global Declarations - Interfaces//
  17. IDXGISwapChain* SwapChain;
  18. ID3D11Device* d3d11Device;
  19. ID3D11DeviceContext* d3d11DevCon;
  20. ID3D11RenderTargetView* renderTargetView;
  21. ID3D11Buffer* squareIndexBuffer;
  22. ID3D11DepthStencilView* depthStencilView;
  23. ID3D11Texture2D* depthStencilBuffer;
  24. ID3D11Buffer* squareVertBuffer;
  25. ID3D11VertexShader* VS;
  26. ID3D11PixelShader* PS;
  27. ID3D10Blob* VS_Buffer;
  28. ID3D10Blob* PS_Buffer;
  29. ID3D11InputLayout* vertLayout;
  30. ID3D11Buffer* cbPerObjectBuffer;
  31. ///////////////**************new**************////////////////////
  32. ID3D11RasterizerState* WireFrame;
  33. ///////////////**************new**************////////////////////
  34.  
  35. //Global Declarations - Others//
  36. LPCTSTR WndClassName = L"firstwindow";
  37. HWND hwnd = NULL;
  38. HRESULT hr;
  39.  
  40. const int Width = 1280;
  41. const int Height = 720;
  42.  
  43. XMMATRIX WVP;
  44. XMMATRIX terrainWorld;
  45. XMMATRIX cube2World;
  46. XMMATRIX camView;
  47. XMMATRIX camProjection;
  48.  
  49. XMVECTOR camPosition;
  50. XMVECTOR camTarget;
  51. XMVECTOR camUp;
  52.  
  53. XMMATRIX Rotation;
  54. XMMATRIX Scale;
  55. XMMATRIX Translation;
  56. float rot = 0.01f;
  57.  
  58. ID3D11ShaderResourceView* tex;
  59. ID3D11ShaderResourceView* tex2;
  60. ID3D11SamplerState* texSamplerState;
  61. D3D11_SAMPLER_DESC sampDesc;
  62.  
  63. //Function Prototypes//
  64. bool InitializeDirect3d11App(HINSTANCE hInstance);
  65. void CleanUp();
  66. bool InitScene();
  67. void UpdateScene();
  68. void DrawScene();
  69.  
  70. bool InitializeWindow(HINSTANCE hInstance,
  71.     int ShowWnd,
  72.     int width, int height,
  73.     bool windowed);
  74. int messageloop();
  75.  
  76. LRESULT CALLBACK WndProc(HWND hWnd,
  77.     UINT msg,
  78.     WPARAM wParam,
  79.     LPARAM lParam);
  80.  
  81. //Create effects constant buffer's structure//
  82. struct cbPerObject
  83. {
  84.     XMMATRIX  WVP;
  85.     float t;
  86. };
  87.  
  88. cbPerObject cbPerObj;
  89.  
  90. //Vertex Structure and Vertex Layout (Input Layout)//
  91. struct Vertex   //Overloaded Vertex Structure
  92. {
  93.     Vertex() {}
  94.     Vertex(float x, float y, float z, float u, float v) : pos(x, y, z), texCoord(u, v) {}
  95.  
  96.     XMFLOAT3 pos;
  97.     XMFLOAT2 texCoord;
  98. };
  99.  
  100. float vertexHeights[65][65];
  101. Vertex terrainVertices[(64 * 64) * 6];
  102.  
  103. D3D11_INPUT_ELEMENT_DESC layout[] =
  104. {
  105.     { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  106.     { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
  107. };
  108. UINT numElements = ARRAYSIZE(layout);
  109.  
  110. int seed;
  111.  
  112. float getNoise(int x, int z) {
  113.     srand(x * 49632 + z * 325176 + seed);
  114.     return ((float)rand() / RAND_MAX) * 2.0f - 1.0f;
  115. }
  116.  
  117. float getSmoothNoise(int x, int z) {
  118.     float corners = (getNoise(x - 1, z - 1) + getNoise(x + 1, z - 1) + getNoise(x - 1, z + 1) + getNoise(x + 1, z + 1)) / 16.0f;
  119.     float sides = (getNoise(x - 1, z) + getNoise(x + 1, z) + getNoise(x, z - 1) + getNoise(x, z + 1)) / 8.0f;
  120.     float center = getNoise(x, z) / 4.0f;
  121.     return corners + sides + center;
  122. }
  123.  
  124. float interpolate(float a, float b, float blend) {
  125.     double theta = blend * XM_PI;
  126.     float f = (float)(1.0f - cos(theta)) * 0.5f;
  127.     return a * (1.0f - f) + b * f;
  128. }
  129.  
  130. float getInterpolatedNoise(float x, float z) {
  131.     int intX = (int)x;
  132.     int intZ = (int)z;
  133.     float fracX = x - intX;
  134.     float fracZ = z - intZ;
  135.  
  136.     float v1 = getSmoothNoise(intX, intZ);
  137.     float v2 = getSmoothNoise(intX + 1, intZ);
  138.     float v3 = getSmoothNoise(intX, intZ + 1);
  139.     float v4 = getSmoothNoise(intX + 1, intZ + 1);
  140.     float i1 = interpolate(v1, v2, fracX);
  141.     float i2 = interpolate(v3, v4, fracX);
  142.     return interpolate(i1, i2, fracZ);
  143. }
  144.  
  145. #define AMPLITUDE 20.0f
  146.  
  147. float generateHeight(int x, int z) {
  148.     return getInterpolatedNoise(x / 8.0f, z / 8.0f) * AMPLITUDE;
  149. }
  150.  
  151. int WINAPI WinMain(HINSTANCE hInstance, //Main windows function
  152.     HINSTANCE hPrevInstance,
  153.     LPSTR lpCmdLine,
  154.     int nShowCmd)
  155. {
  156.     srand(time(NULL));
  157.     seed = rand() % 1000000000;
  158.  
  159.     for (int x = 0; x < 65; x++) {
  160.         for (int z = 0; z < 65; z++) {
  161.             vertexHeights[x][z] = generateHeight(x, z);
  162.         }
  163.     }
  164.  
  165.     if (!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
  166.     {
  167.         MessageBox(0, L"Window Initialization - Failed",
  168.             L"Error", MB_OK);
  169.         return 0;
  170.     }
  171.  
  172.     if (!InitializeDirect3d11App(hInstance))    //Initialize Direct3D
  173.     {
  174.         MessageBox(0, L"Direct3D Initialization - Failed",
  175.             L"Error", MB_OK);
  176.         return 0;
  177.     }
  178.  
  179.     if (!InitScene())   //Initialize our scene
  180.     {
  181.         MessageBox(0, L"Scene Initialization - Failed",
  182.             L"Error", MB_OK);
  183.         return 0;
  184.     }
  185.  
  186.     messageloop();
  187.  
  188.     CleanUp();
  189.  
  190.     return 0;
  191. }
  192.  
  193. bool InitializeWindow(HINSTANCE hInstance,
  194.     int ShowWnd,
  195.     int width, int height,
  196.     bool windowed)
  197. {
  198.     WNDCLASSEX wc;
  199.  
  200.     wc.cbSize = sizeof(WNDCLASSEX);
  201.     wc.style = CS_HREDRAW | CS_VREDRAW;
  202.     wc.lpfnWndProc = WndProc;
  203.     wc.cbClsExtra = NULL;
  204.     wc.cbWndExtra = NULL;
  205.     wc.hInstance = hInstance;
  206.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  207.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  208.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
  209.     wc.lpszMenuName = NULL;
  210.     wc.lpszClassName = WndClassName;
  211.     wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  212.  
  213.     if (!RegisterClassEx(&wc))
  214.     {
  215.         MessageBox(NULL, L"Error registering class",
  216.             L"Error", MB_OK | MB_ICONERROR);
  217.         return 1;
  218.     }
  219.  
  220.     hwnd = CreateWindowEx(
  221.         NULL,
  222.         WndClassName,
  223.         L"Lesson 4 - Begin Drawing",
  224.         WS_OVERLAPPEDWINDOW,
  225.         CW_USEDEFAULT, CW_USEDEFAULT,
  226.         width, height,
  227.         NULL,
  228.         NULL,
  229.         hInstance,
  230.         NULL
  231.     );
  232.  
  233.     if (!hwnd)
  234.     {
  235.         MessageBox(NULL, L"Error creating window",
  236.             L"Error", MB_OK | MB_ICONERROR);
  237.         return 1;
  238.     }
  239.  
  240.     ShowWindow(hwnd, ShowWnd);
  241.     UpdateWindow(hwnd);
  242.  
  243.     return true;
  244. }
  245.  
  246. bool InitializeDirect3d11App(HINSTANCE hInstance)
  247. {
  248.     //Describe our SwapChain Buffer
  249.     DXGI_MODE_DESC bufferDesc;
  250.  
  251.     ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
  252.  
  253.     bufferDesc.Width = Width;
  254.     bufferDesc.Height = Height;
  255.     bufferDesc.RefreshRate.Numerator = 60;
  256.     bufferDesc.RefreshRate.Denominator = 1;
  257.     bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  258.     bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  259.     bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  260.  
  261.     //Describe our SwapChain
  262.     DXGI_SWAP_CHAIN_DESC swapChainDesc;
  263.  
  264.     ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  265.  
  266.     swapChainDesc.BufferDesc = bufferDesc;
  267.     swapChainDesc.SampleDesc.Count = 1;
  268.     swapChainDesc.SampleDesc.Quality = 0;
  269.     swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  270.     swapChainDesc.BufferCount = 1;
  271.     swapChainDesc.OutputWindow = hwnd;
  272.     swapChainDesc.Windowed = TRUE;
  273.     swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  274.  
  275.  
  276.     //Create our SwapChain
  277.     hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
  278.         D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);
  279.  
  280.     //Create our BackBuffer
  281.     ID3D11Texture2D* BackBuffer;
  282.     hr = SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer);
  283.  
  284.     //Create our Render Target
  285.     hr = d3d11Device->CreateRenderTargetView(BackBuffer, NULL, &renderTargetView);
  286.     BackBuffer->Release();
  287.  
  288.     //Describe our Depth/Stencil Buffer
  289.     D3D11_TEXTURE2D_DESC depthStencilDesc;
  290.  
  291.     depthStencilDesc.Width = Width;
  292.     depthStencilDesc.Height = Height;
  293.     depthStencilDesc.MipLevels = 1;
  294.     depthStencilDesc.ArraySize = 1;
  295.     depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  296.     depthStencilDesc.SampleDesc.Count = 1;
  297.     depthStencilDesc.SampleDesc.Quality = 0;
  298.     depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
  299.     depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  300.     depthStencilDesc.CPUAccessFlags = 0;
  301.     depthStencilDesc.MiscFlags = 0;
  302.  
  303.     //Create the Depth/Stencil View
  304.     d3d11Device->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
  305.     d3d11Device->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
  306.  
  307.     //Set our Render Target
  308.     d3d11DevCon->OMSetRenderTargets(1, &renderTargetView, depthStencilView);
  309.  
  310.     return true;
  311. }
  312.  
  313. void CleanUp()
  314. {
  315.     //Release the COM Objects we created
  316.     SwapChain->Release();
  317.     d3d11Device->Release();
  318.     d3d11DevCon->Release();
  319.     renderTargetView->Release();
  320.     squareVertBuffer->Release();
  321.     VS->Release();
  322.     PS->Release();
  323.     VS_Buffer->Release();
  324.     PS_Buffer->Release();
  325.     vertLayout->Release();
  326.     depthStencilView->Release();
  327.     depthStencilBuffer->Release();
  328.     cbPerObjectBuffer->Release();
  329.     ///////////////**************new**************////////////////////
  330.     WireFrame->Release();
  331.     ///////////////**************new**************////////////////////
  332.  
  333.     tex->Release();
  334.     texSamplerState->Release();
  335. }
  336.  
  337. D3D11_RASTERIZER_DESC wfdesc;
  338.  
  339. D3D11_SUBRESOURCE_DATA vertexBufferData;
  340. D3D11_BUFFER_DESC vertexBufferDesc;
  341.  
  342. bool InitScene()
  343. {
  344.     //Compile Shaders from shader file
  345.     hr = D3DX11CompileFromFile(L"Terrain.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
  346.     hr = D3DX11CompileFromFile(L"Terrain.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);
  347.  
  348.     //Create the Shader Objects
  349.     hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
  350.     hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);
  351.  
  352.     //Set Vertex and Pixel Shaders
  353.     d3d11DevCon->VSSetShader(VS, 0, 0);
  354.     d3d11DevCon->PSSetShader(PS, 0, 0);
  355.  
  356.     for (int i = 0; i < 64 * 64; i++) {
  357.         terrainVertices[(i * 6)] = Vertex{(float)(i % 64), vertexHeights[(i % 64)][(i / 64) + 1], (float)((i / 64) + 1), 0.0f, 1.0f};
  358.         terrainVertices[(i * 6) + 1] = Vertex{(float)((i % 64) + 1), vertexHeights[(i % 64) + 1][(i / 64)], (float)(i / 64), 1.0f, 0.0f};
  359.         terrainVertices[(i * 6) + 2] = Vertex{(float)(i % 64), vertexHeights[(i % 64)][(i / 64)], (float)(i / 64), 0.0f, 0.0f,};
  360.         terrainVertices[(i * 6) + 3] = Vertex{(float)(i % 64), vertexHeights[(i % 64)][(i / 64) + 1], (float)((i / 64) + 1), 0.0f, 1.0f};
  361.         terrainVertices[(i * 6) + 4] = Vertex{(float)((i % 64) + 1), vertexHeights[(i % 64) + 1][(i / 64) + 1], (float)((i / 64) + 1), 1.0f, 1.0f};
  362.         terrainVertices[(i * 6) + 5] = Vertex{(float)((i % 64) + 1), vertexHeights[(i % 64) + 1][(i / 64)], (float)(i / 64), 1.0f, 0.0f};
  363.     }
  364.  
  365.     ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
  366.  
  367.     vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  368.     vertexBufferDesc.ByteWidth = sizeof(Vertex) * (64*64)*6;
  369.     vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  370.     vertexBufferDesc.CPUAccessFlags = 0;
  371.     vertexBufferDesc.MiscFlags = 0;
  372.  
  373.     ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
  374.     vertexBufferData.pSysMem = terrainVertices;
  375.     hr = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &squareVertBuffer);
  376.  
  377.     //Set the vertex buffer
  378.     UINT stride = sizeof(Vertex);
  379.     UINT offset = 0;
  380.     d3d11DevCon->IASetVertexBuffers(0, 1, &squareVertBuffer, &stride, &offset);
  381.  
  382.     //Create the Input Layout
  383.     hr = d3d11Device->CreateInputLayout(layout, numElements, VS_Buffer->GetBufferPointer(),
  384.         VS_Buffer->GetBufferSize(), &vertLayout);
  385.    
  386.     //Set the Input Layout
  387.     d3d11DevCon->IASetInputLayout(vertLayout);
  388.  
  389.     //Set Primitive Topology
  390.     d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
  391.  
  392.     //Create the Viewport
  393.     D3D11_VIEWPORT viewport;
  394.     ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  395.  
  396.     viewport.TopLeftX = 0;
  397.     viewport.TopLeftY = 0;
  398.     viewport.Width = Width;
  399.     viewport.Height = Height;
  400.     viewport.MinDepth = 0.0f;
  401.     viewport.MaxDepth = 1.0f;
  402.  
  403.     //Set the Viewport
  404.     d3d11DevCon->RSSetViewports(1, &viewport);
  405.  
  406.     //Create the buffer to send to the cbuffer in effect file
  407.     D3D11_BUFFER_DESC cbbd;
  408.     ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));
  409.  
  410.     cbbd.Usage = D3D11_USAGE_DEFAULT;
  411.     cbbd.ByteWidth = sizeof(cbPerObject);
  412.     cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  413.     cbbd.CPUAccessFlags = 0;
  414.     cbbd.MiscFlags = 0;
  415.  
  416.     hr = d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);
  417.  
  418.     //Camera information
  419.     camPosition = XMVectorSet(0.0f, 10.0f, 0.0f, 0.0f);
  420.     camTarget = XMVectorSet(32.0f, 0.0f, 32.0f, 0.0f);
  421.     camUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  422.  
  423.     //Set the View matrix
  424.     camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
  425.  
  426.     //Set the Projection matrix
  427.     camProjection = XMMatrixPerspectiveFovLH(0.4f*3.14f, Width / Height, 1.0f, 1000.0f);
  428.     //camProjection._11 = -(camProjection._11);
  429.  
  430.     ///////////////**************new**************////////////////////
  431.     ZeroMemory(&wfdesc, sizeof(D3D11_RASTERIZER_DESC));
  432.     wfdesc.FillMode = D3D11_FILL_SOLID;
  433.     wfdesc.CullMode = D3D11_CULL_NONE;
  434.     hr = d3d11Device->CreateRasterizerState(&wfdesc, &WireFrame);
  435.  
  436.     d3d11DevCon->RSSetState(WireFrame);
  437.     ///////////////**************new**************////////////////////
  438.  
  439.     ZeroMemory(&sampDesc, sizeof(sampDesc));
  440.     sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  441.     sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  442.     sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  443.     sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  444.     sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
  445.     sampDesc.MinLOD = 0;
  446.     sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
  447.  
  448.     d3d11Device->CreateSamplerState(&sampDesc, &texSamplerState);
  449.  
  450.     D3DX11CreateShaderResourceViewFromFile(d3d11Device, L"res/seamless.png",NULL, NULL, &tex, NULL);
  451.     D3DX11CreateShaderResourceViewFromFile(d3d11Device, L"res/shadowmap.png", NULL, NULL, &tex2, NULL);
  452.  
  453.     return true;
  454. }
  455.  
  456. void UpdateScene()
  457. {
  458.     //Keep the cubes rotating
  459.     rot += .005f;
  460.  
  461.     camPosition = XMVectorSet(32 + (32 * sin(rot * 0.05f)), 10.0f, 32 + (32 * cos(rot * 0.05f)), 0.0f);
  462.     //camPosition = XMVectorSet(0.0f, 100.0f, 0.0f, 0.0f);
  463.     //Set the View matrix
  464.     camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
  465.  
  466.     //Define cube1's world space matrix
  467.     XMVECTOR rotaxis = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  468.  
  469.     //Reset cube2World
  470.     cube2World = XMMatrixIdentity();
  471.  
  472.     //Define cube2's world space matrix
  473.     Rotation = XMMatrixRotationAxis(rotaxis, 0.0f);
  474.     Scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
  475.     //Scale = XMMatrixScaling(100.0f, 100.0f, 100.0f);
  476.  
  477.     //Set cube2's world space matrix
  478.     cube2World = Rotation * Scale;
  479. }
  480.  
  481. float t = 0.0f;
  482. void DrawScene()
  483. {
  484.     //Clear our backbuffer
  485.     float bgColor[4] = { (0.0f, 0.0f, 0.0f, 0.0f) };
  486.     d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);
  487.  
  488.     //Refresh the Depth/Stencil view
  489.     d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
  490.  
  491.     t += 0.00001f;
  492.  
  493.     WVP = cube2World * camView * camProjection;
  494.     cbPerObj.WVP = XMMatrixTranspose(WVP);
  495.     cbPerObj.t = t;
  496.     d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
  497.     d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);
  498.  
  499.     d3d11DevCon->PSSetShaderResources(0, 1, &tex);
  500.     d3d11DevCon->PSSetShaderResources(1, 1, &tex2);
  501.     d3d11DevCon->PSSetSamplers(0, 1, &texSamplerState);
  502.  
  503.     d3d11DevCon->Draw((64 * 64)*6, 0);
  504.  
  505.     //Present the backbuffer to the screen
  506.     SwapChain->Present(0, 0);
  507. }
  508.  
  509. int messageloop() {
  510.     MSG msg;
  511.     ZeroMemory(&msg, sizeof(MSG));
  512.     while (true)
  513.     {
  514.         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  515.         {
  516.             if (msg.message == WM_QUIT)
  517.                 break;
  518.             TranslateMessage(&msg);
  519.             DispatchMessage(&msg);
  520.         } else {
  521.             // run game code            
  522.             UpdateScene();
  523.             DrawScene();
  524.         }
  525.     }
  526.     return msg.wParam;
  527. }
  528.  
  529. LRESULT CALLBACK WndProc(HWND hwnd,
  530.     UINT msg,
  531.     WPARAM wParam,
  532.     LPARAM lParam)
  533. {
  534.     switch (msg)
  535.     {
  536.     case WM_KEYDOWN:
  537.         if (wParam == VK_ESCAPE) {
  538.             DestroyWindow(hwnd);
  539.         }
  540.         return 0;
  541.  
  542.     case WM_DESTROY:
  543.         PostQuitMessage(0);
  544.         return 0;
  545.     }
  546.     return DefWindowProc(hwnd,
  547.         msg,
  548.         wParam,
  549.         lParam);
  550. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement