Advertisement
Guest User

main.cpp (Brynzarsoft tutorial)

a guest
Feb 27th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.84 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. #pragma comment (lib, "D3D10_1.lib")
  8. #pragma comment (lib, "DXGI.lib")
  9. #pragma comment (lib, "D2D1.lib")
  10. #pragma comment (lib, "dwrite.lib")
  11. #pragma comment (lib, "dinput8.lib")
  12. #pragma comment (lib, "dxguid.lib")
  13.  
  14. #include <windows.h>
  15. #include <d3d11.h>
  16. #include <d3dx11.h>
  17. #include <d3dx10.h>
  18. #include <xnamath.h>
  19. #include <D3D10_1.h>
  20. #include <DXGI.h>
  21. #include <D2D1.h>
  22. #include <sstream>
  23. #include <dwrite.h>
  24. #include <dinput.h>
  25. #include "bsp.h"
  26. //Global Declarations - Interfaces//
  27. IDXGISwapChain* SwapChain;
  28. ID3D11Device* d3d11Device;
  29. ID3D11DeviceContext* d3d11DevCon;
  30. ID3D11RenderTargetView* renderTargetView;
  31. ID3D11Buffer* squareIndexBuffer;
  32. ID3D11DepthStencilView* depthStencilView;
  33. ID3D11Texture2D* depthStencilBuffer;
  34. ID3D11Buffer* squareVertBuffer;
  35. ID3D11VertexShader* VS;
  36. ID3D11PixelShader* PS;
  37. ID3D11PixelShader* D2D_PS;
  38. ID3D10Blob* D2D_PS_Buffer;
  39. ID3D10Blob* VS_Buffer;
  40. ID3D10Blob* PS_Buffer;
  41. ID3D11InputLayout* vertLayout;
  42. ID3D11Buffer* cbPerObjectBuffer;
  43. ID3D11BlendState* Transparency;
  44. ID3D11RasterizerState* CCWcullMode;
  45. ID3D11RasterizerState* CWcullMode;
  46. ID3D11ShaderResourceView* CubesTexture;
  47. ID3D11SamplerState* CubesTexSamplerState;
  48. ID3D11Buffer* cbPerFrameBuffer;
  49.  
  50. ID3D10Device1 *d3d101Device;
  51. IDXGIKeyedMutex *keyedMutex11;
  52. IDXGIKeyedMutex *keyedMutex10;
  53. ID2D1RenderTarget *D2DRenderTarget;
  54. ID2D1SolidColorBrush *Brush;
  55. ID3D11Texture2D *BackBuffer11;
  56. ID3D11Texture2D *sharedTex11;
  57. ID3D11Buffer *d2dVertBuffer;
  58. ID3D11Buffer *d2dIndexBuffer;
  59. ID3D11ShaderResourceView *d2dTexture;
  60. IDWriteFactory *DWriteFactory;
  61. IDWriteTextFormat *TextFormat;
  62.  
  63. IDirectInputDevice8* DIKeyboard;
  64. IDirectInputDevice8* DIMouse;
  65.  
  66. std::wstring printText;
  67. std::wstring Unicode(const std::string s)
  68. {
  69. std::wstring wsTmp(s.begin(), s.end());
  70. return wsTmp;
  71. }
  72. //Global Declarations - Others//
  73. LPCTSTR WndClassName = "firstwindow";
  74. HWND hwnd = NULL;
  75. HRESULT hr;
  76.  
  77. int Width = 1920;
  78. int Height = 1200;
  79.  
  80. DIMOUSESTATE mouseLastState;
  81. LPDIRECTINPUT8 DirectInput;
  82.  
  83. float rotx = 0;
  84. float rotz = 0;
  85. float scaleX = 1.0f;
  86. float scaleY = 1.0f;
  87.  
  88.  
  89. float mapx = 0.0f;
  90. float mapy = 0.0f;
  91. float mapz = 0.0f;
  92. XMMATRIX Rotationx;
  93. XMMATRIX Rotationz;
  94.  
  95. XMMATRIX WVP;
  96. XMMATRIX cube1World;
  97. XMMATRIX cube2World;
  98. XMMATRIX camView;
  99. XMMATRIX camProjection;
  100.  
  101. XMMATRIX d2dWorld;
  102.  
  103. XMVECTOR camPosition;
  104. XMVECTOR camTarget;
  105. XMVECTOR camUp;
  106. XMVECTOR DefaultForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
  107. XMVECTOR DefaultRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);
  108. XMVECTOR camForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
  109. XMVECTOR camRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);
  110.  
  111. XMMATRIX camRotationMatrix;
  112. XMMATRIX groundWorld;
  113.  
  114. XMMATRIX mapWorld;
  115.  
  116. float moveLeftRight = 0.0f;
  117. float moveBackForward = 0.0f;
  118.  
  119. float camYaw = 0.0f;
  120. float camPitch = 0.0f;
  121.  
  122. XMMATRIX Rotation;
  123. XMMATRIX Scale;
  124. XMMATRIX Translation;
  125. float rot = 0.01f;
  126.  
  127. double countsPerSecond = 0.0;
  128. __int64 CounterStart = 0;
  129.  
  130. int frameCount = 0;
  131. int fps = 0;
  132.  
  133. __int64 frameTimeOld = 0;
  134. double frameTime;
  135.  
  136. //Function Prototypes//
  137. bool InitializeDirect3d11App(HINSTANCE hInstance);
  138. void CleanUp();
  139. bool InitScene();
  140. void DrawScene();
  141. bool InitD2D_D3D101_DWrite(IDXGIAdapter1 *Adapter);
  142. void InitD2DScreenTexture();
  143. void UpdateScene(double time);
  144.  
  145. void UpdateCamera();
  146.  
  147. void RenderText(std::wstring text, int inInt);
  148.  
  149. void StartTimer();
  150. double GetTime();
  151. double GetFrameTime();
  152.  
  153. bool InitializeWindow(HINSTANCE hInstance,
  154. int ShowWnd,
  155. int width, int height,
  156. bool windowed);
  157. int messageloop();
  158.  
  159. bool InitDirectInput(HINSTANCE hInstance);
  160. void DetectInput(double time);
  161.  
  162. LRESULT CALLBACK WndProc(HWND hWnd,
  163. UINT msg,
  164. WPARAM wParam,
  165. LPARAM lParam);
  166.  
  167. //Create effects constant buffer's structure//
  168. struct cbPerObject
  169. {
  170. XMMATRIX WVP;
  171. XMMATRIX World;
  172. };
  173.  
  174. cbPerObject cbPerObj;
  175.  
  176. ///////////////**************new**************////////////////////
  177. struct Light
  178. {
  179. Light()
  180. {
  181. ZeroMemory(this, sizeof(Light));
  182. }
  183. XMFLOAT3 pos;
  184. float range;
  185. XMFLOAT3 dir;
  186. float cone;
  187. XMFLOAT3 att;
  188. float pad2;
  189. XMFLOAT4 ambient;
  190. XMFLOAT4 diffuse;
  191. };
  192. ///////////////**************new**************////////////////////
  193.  
  194. Light light;
  195. CBSP bsp;
  196. struct cbPerFrame
  197. {
  198. Light light;
  199. };
  200.  
  201. cbPerFrame constbuffPerFrame;
  202.  
  203. struct Vertex //Overloaded Vertex Structure
  204. {
  205. Vertex(){}
  206. Vertex(float x, float y, float z,
  207. float u, float v,
  208. float nx, float ny, float nz)
  209. : pos(x,y,z), texCoord(u, v), normal(nx, ny, nz){}
  210.  
  211. XMFLOAT3 pos;
  212. XMFLOAT2 texCoord;
  213. XMFLOAT3 normal;
  214. };
  215.  
  216. D3D11_INPUT_ELEMENT_DESC layout[] =
  217. {
  218. { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  219. { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
  220. { "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 20, D3D11_INPUT_PER_VERTEX_DATA, 0}
  221. };
  222. UINT numElements = ARRAYSIZE(layout);
  223.  
  224. int WINAPI WinMain(HINSTANCE hInstance, //Main windows function
  225. HINSTANCE hPrevInstance,
  226. LPSTR lpCmdLine,
  227. int nShowCmd)
  228. {
  229.  
  230. if(!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
  231. {
  232. MessageBox(0, "Window Initialization - Failed",
  233. "Error", MB_OK);
  234. return 0;
  235. }
  236.  
  237. if(!InitializeDirect3d11App(hInstance)) //Initialize Direct3D
  238. {
  239. MessageBox(0, "Direct3D Initialization - Failed",
  240. "Error", MB_OK);
  241. return 0;
  242. }
  243.  
  244. if(!InitScene()) //Initialize our scene
  245. {
  246. MessageBox(0, "Scene Initialization - Failed",
  247. "Error", MB_OK);
  248. return 0;
  249. }
  250.  
  251. if(!InitDirectInput(hInstance))
  252. {
  253. MessageBox(0, "Direct Input Initialization - Failed",
  254. "Error", MB_OK);
  255. return 0;
  256. }
  257.  
  258. messageloop();
  259.  
  260. CleanUp();
  261.  
  262. return 0;
  263. }
  264.  
  265. bool InitializeWindow(HINSTANCE hInstance,
  266. int ShowWnd,
  267. int width, int height,
  268. bool windowed)
  269. {
  270. typedef struct _WNDCLASS {
  271. UINT cbSize;
  272. UINT style;
  273. WNDPROC lpfnWndProc;
  274. int cbClsExtra;
  275. int cbWndExtra;
  276. HANDLE hInstance;
  277. HICON hIcon;
  278. HCURSOR hCursor;
  279. HBRUSH hbrBackground;
  280. LPCTSTR lpszMenuName;
  281. LPCTSTR lpszClassName;
  282. } WNDCLASS;
  283.  
  284. WNDCLASSEX wc;
  285.  
  286. wc.cbSize = sizeof(WNDCLASSEX);
  287. wc.style = CS_HREDRAW | CS_VREDRAW;
  288. wc.lpfnWndProc = WndProc;
  289. wc.cbClsExtra = NULL;
  290. wc.cbWndExtra = NULL;
  291. wc.hInstance = hInstance;
  292. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  293. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  294. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  295. wc.lpszMenuName = NULL;
  296. wc.lpszClassName = WndClassName;
  297. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  298.  
  299. if (!RegisterClassEx(&wc))
  300. {
  301. MessageBox(NULL, "Error registering class",
  302. "Error", MB_OK | MB_ICONERROR);
  303. return 1;
  304. }
  305.  
  306. hwnd = CreateWindowEx(
  307. NULL,
  308. WndClassName,
  309. "DEVELOPERS",
  310. WS_OVERLAPPEDWINDOW,
  311. CW_USEDEFAULT, CW_USEDEFAULT,
  312. width, height,
  313. NULL,
  314. NULL,
  315. hInstance,
  316. NULL
  317. );
  318.  
  319. if (!hwnd)
  320. {
  321. MessageBox(NULL, "Error creating window",
  322. "Error", MB_OK | MB_ICONERROR);
  323. return 1;
  324. }
  325.  
  326. ShowWindow(hwnd, ShowWnd);
  327. UpdateWindow(hwnd);
  328.  
  329. return true;
  330. }
  331.  
  332. bool InitializeDirect3d11App(HINSTANCE hInstance)
  333. {
  334. //Describe our SwapChain Buffer
  335. DXGI_MODE_DESC bufferDesc;
  336.  
  337. ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
  338.  
  339. bufferDesc.Width = Width;
  340. bufferDesc.Height = Height;
  341. bufferDesc.RefreshRate.Numerator = 60;
  342. bufferDesc.RefreshRate.Denominator = 1;
  343. bufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  344. bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  345. bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  346.  
  347. //Describe our SwapChain
  348. DXGI_SWAP_CHAIN_DESC swapChainDesc;
  349.  
  350. ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  351.  
  352. swapChainDesc.BufferDesc = bufferDesc;
  353. swapChainDesc.SampleDesc.Count = 1;
  354. swapChainDesc.SampleDesc.Quality = 0;
  355. swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  356. swapChainDesc.BufferCount = 1;
  357. swapChainDesc.OutputWindow = hwnd;
  358. ///////////////**************new**************////////////////////
  359. swapChainDesc.Windowed = true;
  360. ///////////////**************new**************////////////////////
  361. swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  362.  
  363. // Create DXGI factory to enumerate adapters///////////////////////////////////////////////////////////////////////////
  364. IDXGIFactory1 *DXGIFactory;
  365.  
  366. HRESULT hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&DXGIFactory);
  367.  
  368. // Use the first adapter
  369. IDXGIAdapter1 *Adapter;
  370.  
  371. hr = DXGIFactory->EnumAdapters1(0, &Adapter);
  372.  
  373. DXGIFactory->Release();
  374.  
  375. //Create our Direct3D 11 Device and SwapChain//////////////////////////////////////////////////////////////////////////
  376. hr = D3D11CreateDeviceAndSwapChain(Adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, D3D11_CREATE_DEVICE_BGRA_SUPPORT,
  377. NULL, NULL, D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);
  378.  
  379. //Initialize Direct2D, Direct3D 10.1, DirectWrite
  380. InitD2D_D3D101_DWrite(Adapter);
  381.  
  382. //Release the Adapter interface
  383. Adapter->Release();
  384.  
  385. //Create our BackBuffer and Render Target
  386. hr = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (void**)&BackBuffer11 );
  387. hr = d3d11Device->CreateRenderTargetView( BackBuffer11, NULL, &renderTargetView );
  388.  
  389. //Describe our Depth/Stencil Buffer
  390. D3D11_TEXTURE2D_DESC depthStencilDesc;
  391.  
  392. depthStencilDesc.Width = Width;
  393. depthStencilDesc.Height = Height;
  394. depthStencilDesc.MipLevels = 1;
  395. depthStencilDesc.ArraySize = 1;
  396. depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  397. depthStencilDesc.SampleDesc.Count = 1;
  398. depthStencilDesc.SampleDesc.Quality = 0;
  399. depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
  400. depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  401. depthStencilDesc.CPUAccessFlags = 0;
  402. depthStencilDesc.MiscFlags = 0;
  403.  
  404. //Create the Depth/Stencil View
  405. d3d11Device->CreateTexture2D(&depthStencilDesc, NULL, &depthStencilBuffer);
  406. d3d11Device->CreateDepthStencilView(depthStencilBuffer, NULL, &depthStencilView);
  407.  
  408. return true;
  409. }
  410.  
  411. bool InitD2D_D3D101_DWrite(IDXGIAdapter1 *Adapter)
  412. {
  413. //Create our Direc3D 10.1 Device///////////////////////////////////////////////////////////////////////////////////////
  414. hr = D3D10CreateDevice1(Adapter, D3D10_DRIVER_TYPE_HARDWARE, NULL,D3D10_CREATE_DEVICE_BGRA_SUPPORT,
  415. D3D10_FEATURE_LEVEL_9_3, D3D10_1_SDK_VERSION, &d3d101Device );
  416.  
  417. //Create Shared Texture that Direct3D 10.1 will render on//////////////////////////////////////////////////////////////
  418. D3D11_TEXTURE2D_DESC sharedTexDesc;
  419.  
  420. ZeroMemory(&sharedTexDesc, sizeof(sharedTexDesc));
  421.  
  422. sharedTexDesc.Width = Width;
  423. sharedTexDesc.Height = Height;
  424. sharedTexDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
  425. sharedTexDesc.MipLevels = 1;
  426. sharedTexDesc.ArraySize = 1;
  427. sharedTexDesc.SampleDesc.Count = 1;
  428. sharedTexDesc.Usage = D3D11_USAGE_DEFAULT;
  429. sharedTexDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
  430. sharedTexDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
  431.  
  432. hr = d3d11Device->CreateTexture2D(&sharedTexDesc, NULL, &sharedTex11);
  433.  
  434. // Get the keyed mutex for the shared texture (for D3D11)///////////////////////////////////////////////////////////////
  435. hr = sharedTex11->QueryInterface(__uuidof(IDXGIKeyedMutex), (void**)&keyedMutex11);
  436.  
  437. // Get the shared handle needed to open the shared texture in D3D10.1///////////////////////////////////////////////////
  438. IDXGIResource *sharedResource10;
  439. HANDLE sharedHandle10;
  440.  
  441. hr = sharedTex11->QueryInterface(__uuidof(IDXGIResource), (void**)&sharedResource10);
  442.  
  443. hr = sharedResource10->GetSharedHandle(&sharedHandle10);
  444.  
  445. sharedResource10->Release();
  446.  
  447. // Open the surface for the shared texture in D3D10.1///////////////////////////////////////////////////////////////////
  448. IDXGISurface1 *sharedSurface10;
  449.  
  450. hr = d3d101Device->OpenSharedResource(sharedHandle10, __uuidof(IDXGISurface1), (void**)(&sharedSurface10));
  451.  
  452. hr = sharedSurface10->QueryInterface(__uuidof(IDXGIKeyedMutex), (void**)&keyedMutex10);
  453.  
  454. // Create D2D factory///////////////////////////////////////////////////////////////////////////////////////////////////
  455. ID2D1Factory *D2DFactory;
  456. hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, __uuidof(ID2D1Factory), (void**)&D2DFactory);
  457.  
  458. D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties;
  459.  
  460. ZeroMemory(&renderTargetProperties, sizeof(renderTargetProperties));
  461.  
  462. renderTargetProperties.type = D2D1_RENDER_TARGET_TYPE_HARDWARE;
  463. renderTargetProperties.pixelFormat = D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED);
  464.  
  465. hr = D2DFactory->CreateDxgiSurfaceRenderTarget(sharedSurface10, &renderTargetProperties, &D2DRenderTarget);
  466.  
  467. sharedSurface10->Release();
  468. D2DFactory->Release();
  469.  
  470. // Create a solid color brush to draw something with
  471. hr = D2DRenderTarget->CreateSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f), &Brush);
  472.  
  473. //DirectWrite///////////////////////////////////////////////////////////////////////////////////////////////////////////
  474. hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory),
  475. reinterpret_cast<IUnknown**>(&DWriteFactory));
  476.  
  477. hr = DWriteFactory->CreateTextFormat(
  478. Unicode("Script").c_str(),
  479. NULL,
  480. DWRITE_FONT_WEIGHT_REGULAR,
  481. DWRITE_FONT_STYLE_NORMAL,
  482. DWRITE_FONT_STRETCH_NORMAL,
  483. 24.0f,
  484. Unicode("en-us").c_str(),
  485. &TextFormat
  486. );
  487.  
  488. hr = TextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
  489. hr = TextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR);
  490.  
  491. d3d101Device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST);
  492. return true;
  493. }
  494.  
  495. bool InitDirectInput(HINSTANCE hInstance)
  496. {
  497. hr = DirectInput8Create(hInstance,
  498. DIRECTINPUT_VERSION,
  499. IID_IDirectInput8,
  500. (void**)&DirectInput,
  501. NULL);
  502.  
  503. hr = DirectInput->CreateDevice(GUID_SysKeyboard,
  504. &DIKeyboard,
  505. NULL);
  506.  
  507. hr = DirectInput->CreateDevice(GUID_SysMouse,
  508. &DIMouse,
  509. NULL);
  510.  
  511. hr = DIKeyboard->SetDataFormat(&c_dfDIKeyboard);
  512. hr = DIKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
  513.  
  514. hr = DIMouse->SetDataFormat(&c_dfDIMouse);
  515. hr = DIMouse->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND);
  516.  
  517. return true;
  518. }
  519.  
  520. void UpdateCamera()
  521. {
  522. camRotationMatrix = XMMatrixRotationRollPitchYaw(camPitch, camYaw, 0);
  523. camTarget = XMVector3TransformCoord(DefaultForward, camRotationMatrix );
  524. camTarget = XMVector3Normalize(camTarget);
  525.  
  526. XMMATRIX RotateYTempMatrix;
  527. RotateYTempMatrix = XMMatrixRotationY(camYaw);
  528.  
  529. camRight = XMVector3TransformCoord(DefaultRight, RotateYTempMatrix);
  530. camUp = XMVector3TransformCoord(camUp, RotateYTempMatrix);
  531. camForward = XMVector3TransformCoord(DefaultForward, RotateYTempMatrix);
  532.  
  533. camPosition += moveLeftRight*camRight;
  534. camPosition += moveBackForward*camForward;
  535.  
  536. moveLeftRight = 0.0f;
  537. moveBackForward = 0.0f;
  538.  
  539. camTarget = camPosition + camTarget;
  540.  
  541. camView = XMMatrixLookAtLH( camPosition, camTarget, camUp );
  542. }
  543.  
  544. void DetectInput(double time)
  545. {
  546. DIMOUSESTATE mouseCurrState;
  547.  
  548. BYTE keyboardState[256];
  549.  
  550. DIKeyboard->Acquire();
  551. DIMouse->Acquire();
  552.  
  553. DIMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseCurrState);
  554.  
  555. DIKeyboard->GetDeviceState(sizeof(keyboardState),(LPVOID)&keyboardState);
  556.  
  557. if(keyboardState[DIK_ESCAPE] & 0x80)
  558. PostMessage(hwnd, WM_DESTROY, 0, 0);
  559.  
  560. float speed = 150.0f * time;
  561.  
  562. if(keyboardState[DIK_A] & 0x80)
  563. {
  564. moveLeftRight -= speed;
  565. }
  566. if(keyboardState[DIK_D] & 0x80)
  567. {
  568. moveLeftRight += speed;
  569. }
  570. if(keyboardState[DIK_W] & 0x80)
  571. {
  572. moveBackForward += speed;
  573. }
  574. if(keyboardState[DIK_S] & 0x80)
  575. {
  576. moveBackForward -= speed;
  577. }
  578. if (keyboardState[DIK_DOWN] & 0x80)
  579. {
  580. mapy -= 1.0f;
  581. }
  582. if (keyboardState[DIK_UP] & 0x80)
  583. {
  584. mapy += 1.0f;
  585. }
  586. if (keyboardState[DIK_RIGHT] & 0x80)
  587. {
  588. mapz -= 1.0f;
  589. }
  590. if (keyboardState[DIK_LEFT] & 0x80)
  591. {
  592. mapz += 1.0f;
  593. }
  594. if((mouseCurrState.lX != mouseLastState.lX) || (mouseCurrState.lY != mouseLastState.lY))
  595. {
  596. camYaw += mouseLastState.lX * 0.001f;
  597.  
  598. camPitch += mouseCurrState.lY * 0.001f;
  599.  
  600. mouseLastState = mouseCurrState;
  601. }
  602.  
  603. UpdateCamera();
  604.  
  605. return;
  606. }
  607.  
  608.  
  609. void CleanUp()
  610. {
  611. SwapChain->SetFullscreenState(false, NULL);
  612. PostMessage(hwnd, WM_DESTROY, 0, 0);
  613.  
  614. //Release the COM Objects we created
  615. SwapChain->Release();
  616. d3d11Device->Release();
  617. d3d11DevCon->Release();
  618. renderTargetView->Release();
  619. squareVertBuffer->Release();
  620. squareIndexBuffer->Release();
  621. VS->Release();
  622. PS->Release();
  623. VS_Buffer->Release();
  624. PS_Buffer->Release();
  625. vertLayout->Release();
  626. depthStencilView->Release();
  627. depthStencilBuffer->Release();
  628. cbPerObjectBuffer->Release();
  629. Transparency->Release();
  630. CCWcullMode->Release();
  631. CWcullMode->Release();
  632.  
  633. d3d101Device->Release();
  634. keyedMutex11->Release();
  635. keyedMutex10->Release();
  636. D2DRenderTarget->Release();
  637. Brush->Release();
  638. BackBuffer11->Release();
  639. sharedTex11->Release();
  640. DWriteFactory->Release();
  641. TextFormat->Release();
  642. d2dTexture->Release();
  643.  
  644. cbPerFrameBuffer->Release();
  645.  
  646. DIKeyboard->Unacquire();
  647. DIMouse->Unacquire();
  648. DirectInput->Release();
  649. }
  650.  
  651. void InitD2DScreenTexture()
  652. {
  653. //Create the vertex buffer
  654. Vertex v[] =
  655. {
  656. // Front Face
  657. Vertex(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f,-1.0f, -1.0f, -1.0f),
  658. Vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f,-1.0f, 1.0f, -1.0f),
  659. Vertex( 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f),
  660. Vertex( 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f),
  661. };
  662.  
  663. DWORD indices[] = {
  664. // Front Face
  665. 0, 1, 2,
  666. 0, 2, 3,
  667. };
  668.  
  669. D3D11_BUFFER_DESC indexBufferDesc;
  670. ZeroMemory( &indexBufferDesc, sizeof(indexBufferDesc) );
  671.  
  672. indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  673. indexBufferDesc.ByteWidth = sizeof(DWORD) * 2 * 3;
  674. indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  675. indexBufferDesc.CPUAccessFlags = 0;
  676. indexBufferDesc.MiscFlags = 0;
  677.  
  678. D3D11_SUBRESOURCE_DATA iinitData;
  679.  
  680. iinitData.pSysMem = indices;
  681. d3d11Device->CreateBuffer(&indexBufferDesc, &iinitData, &d2dIndexBuffer);
  682.  
  683.  
  684. D3D11_BUFFER_DESC vertexBufferDesc;
  685. ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );
  686.  
  687. vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  688. vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 4;
  689. vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  690. vertexBufferDesc.CPUAccessFlags = 0;
  691. vertexBufferDesc.MiscFlags = 0;
  692.  
  693. D3D11_SUBRESOURCE_DATA vertexBufferData;
  694.  
  695. ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
  696. vertexBufferData.pSysMem = v;
  697. hr = d3d11Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &d2dVertBuffer);
  698.  
  699. //Create A shader resource view from the texture D2D will render to,
  700. //So we can use it to texture a square which overlays our scene
  701. d3d11Device->CreateShaderResourceView(sharedTex11, NULL, &d2dTexture);
  702. }
  703.  
  704. bool InitScene()
  705. {
  706. bsp.Create(d3d11Device, d3d11DevCon, "dxtest.bsp");
  707. InitD2DScreenTexture();
  708.  
  709. //Compile Shaders from shader file
  710. hr = D3DX11CompileFromFile("Effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
  711. hr = D3DX11CompileFromFile("Effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);
  712. hr = D3DX11CompileFromFile("Effects.fx", 0, 0, "D2D_PS", "ps_4_0", 0, 0, 0, &D2D_PS_Buffer, 0, 0);
  713.  
  714. //Create the Shader Objects
  715. hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
  716. hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);
  717. hr = d3d11Device->CreatePixelShader(D2D_PS_Buffer->GetBufferPointer(), D2D_PS_Buffer->GetBufferSize(), NULL, &D2D_PS);
  718.  
  719. //Set Vertex and Pixel Shaders
  720. d3d11DevCon->VSSetShader(VS, 0, 0);
  721. d3d11DevCon->PSSetShader(PS, 0, 0);
  722.  
  723. ///////////////**************new**************////////////////////
  724. light.pos = XMFLOAT3(0.0f, 1.0f, 0.0f);
  725. light.dir = XMFLOAT3(0.0f, 0.0f, 1.0f);
  726. light.range = 1000.0f;
  727. light.cone = 20.0f;
  728. light.att = XMFLOAT3(0.4f, 0.02f, 0.000f);
  729. light.ambient = XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f);
  730. light.diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
  731. ///////////////**************new**************////////////////////
  732.  
  733.  
  734. //Create the vertex buffer
  735. Vertex v[] =
  736. {
  737. // Bottom Face
  738. Vertex(-1.0f, -1.0f, -1.0f, 100.0f, 100.0f, 0.0f, 1.0f, 0.0f),
  739. Vertex( 1.0f, -1.0f, -1.0f, 0.0f, 100.0f, 0.0f, 1.0f, 0.0f),
  740. Vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f),
  741. Vertex(-1.0f, -1.0f, 1.0f, 100.0f, 0.0f, 0.0f, 1.0f, 0.0f),
  742. };
  743.  
  744. DWORD indices[] = {
  745. 0, 1, 2,
  746. 0, 2, 3,
  747. };
  748.  
  749. D3D11_BUFFER_DESC indexBufferDesc;
  750. ZeroMemory( &indexBufferDesc, sizeof(indexBufferDesc) );
  751.  
  752. indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  753. indexBufferDesc.ByteWidth = sizeof(DWORD) * 2 * 3;
  754. indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  755. indexBufferDesc.CPUAccessFlags = 0;
  756. indexBufferDesc.MiscFlags = 0;
  757.  
  758. D3D11_SUBRESOURCE_DATA iinitData;
  759.  
  760. iinitData.pSysMem = indices;
  761. d3d11Device->CreateBuffer(&indexBufferDesc, &iinitData, &squareIndexBuffer);
  762.  
  763. D3D11_BUFFER_DESC vertexBufferDesc;
  764. ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );
  765.  
  766. vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  767. vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 4;
  768. vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  769. vertexBufferDesc.CPUAccessFlags = 0;
  770. vertexBufferDesc.MiscFlags = 0;
  771.  
  772. D3D11_SUBRESOURCE_DATA vertexBufferData;
  773.  
  774. ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
  775. vertexBufferData.pSysMem = v;
  776. hr = d3d11Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &squareVertBuffer);
  777.  
  778. //Create the Input Layout
  779. hr = d3d11Device->CreateInputLayout( layout, numElements, VS_Buffer->GetBufferPointer(),
  780. VS_Buffer->GetBufferSize(), &vertLayout );
  781.  
  782. //Set the Input Layout
  783. d3d11DevCon->IASetInputLayout( vertLayout );
  784.  
  785. //Set Primitive Topology
  786. d3d11DevCon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  787.  
  788. //Create the Viewport
  789. D3D11_VIEWPORT viewport;
  790. ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  791.  
  792. viewport.TopLeftX = 0;
  793. viewport.TopLeftY = 0;
  794. viewport.Width = Width;
  795. viewport.Height = Height;
  796. viewport.MinDepth = 0.0f;
  797. viewport.MaxDepth = 1.0f;
  798.  
  799. //Set the Viewport
  800. d3d11DevCon->RSSetViewports(1, &viewport);
  801.  
  802. //Create the buffer to send to the cbuffer in effect file
  803. D3D11_BUFFER_DESC cbbd;
  804. ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));
  805.  
  806. cbbd.Usage = D3D11_USAGE_DEFAULT;
  807. cbbd.ByteWidth = sizeof(cbPerObject);
  808. cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  809. cbbd.CPUAccessFlags = 0;
  810. cbbd.MiscFlags = 0;
  811.  
  812. hr = d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);
  813.  
  814. //Create the buffer to send to the cbuffer per frame in effect file
  815. ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));
  816.  
  817. cbbd.Usage = D3D11_USAGE_DEFAULT;
  818. cbbd.ByteWidth = sizeof(cbPerFrame);
  819. cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  820. cbbd.CPUAccessFlags = 0;
  821. cbbd.MiscFlags = 0;
  822.  
  823. hr = d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerFrameBuffer);
  824.  
  825. //Camera information
  826. camPosition = XMVectorSet( 0.0f, 5.0f, -8.0f, 0.0f );
  827. camTarget = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f );
  828. camUp = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );
  829.  
  830. //Set the View matrix
  831. camView = XMMatrixLookAtLH( camPosition, camTarget, camUp );
  832.  
  833. //Set the Projection matrix
  834. camProjection = XMMatrixPerspectiveFovLH( 0.4f*3.14f, Width/Height, 1.0f, 1000.0f);
  835.  
  836. D3D11_BLEND_DESC blendDesc;
  837. ZeroMemory( &blendDesc, sizeof(blendDesc) );
  838.  
  839. D3D11_RENDER_TARGET_BLEND_DESC rtbd;
  840. ZeroMemory( &rtbd, sizeof(rtbd) );
  841.  
  842. rtbd.BlendEnable = true;
  843. rtbd.SrcBlend = D3D11_BLEND_SRC_COLOR;
  844. rtbd.DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  845. rtbd.BlendOp = D3D11_BLEND_OP_ADD;
  846. rtbd.SrcBlendAlpha = D3D11_BLEND_ONE;
  847. rtbd.DestBlendAlpha = D3D11_BLEND_ZERO;
  848. rtbd.BlendOpAlpha = D3D11_BLEND_OP_ADD;
  849. rtbd.RenderTargetWriteMask = D3D10_COLOR_WRITE_ENABLE_ALL;
  850.  
  851. blendDesc.AlphaToCoverageEnable = false;
  852. blendDesc.RenderTarget[0] = rtbd;
  853.  
  854. hr = D3DX11CreateShaderResourceViewFromFile( d3d11Device, "grass.jpg",
  855. NULL, NULL, &CubesTexture, NULL );
  856.  
  857. // Describe the Sample State
  858. D3D11_SAMPLER_DESC sampDesc;
  859. ZeroMemory( &sampDesc, sizeof(sampDesc) );
  860. sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  861. sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  862. sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  863. sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  864. sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
  865. sampDesc.MinLOD = 0;
  866. sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
  867.  
  868. //Create the Sample State
  869. hr = d3d11Device->CreateSamplerState( &sampDesc, &CubesTexSamplerState );
  870.  
  871. d3d11Device->CreateBlendState(&blendDesc, &Transparency);
  872.  
  873. D3D11_RASTERIZER_DESC cmdesc;
  874.  
  875. ZeroMemory(&cmdesc, sizeof(D3D11_RASTERIZER_DESC));
  876. cmdesc.FillMode = D3D11_FILL_SOLID;
  877. cmdesc.CullMode = D3D11_CULL_BACK;
  878. cmdesc.FrontCounterClockwise = true;
  879. hr = d3d11Device->CreateRasterizerState(&cmdesc, &CCWcullMode);
  880.  
  881. cmdesc.FrontCounterClockwise = false;
  882.  
  883. hr = d3d11Device->CreateRasterizerState(&cmdesc, &CWcullMode);
  884.  
  885. return true;
  886. }
  887.  
  888. void StartTimer()
  889. {
  890. LARGE_INTEGER frequencyCount;
  891. QueryPerformanceFrequency(&frequencyCount);
  892.  
  893. countsPerSecond = double(frequencyCount.QuadPart);
  894.  
  895. QueryPerformanceCounter(&frequencyCount);
  896. CounterStart = frequencyCount.QuadPart;
  897. }
  898.  
  899. double GetTime()
  900. {
  901. LARGE_INTEGER currentTime;
  902. QueryPerformanceCounter(&currentTime);
  903. return double(currentTime.QuadPart-CounterStart)/countsPerSecond;
  904. }
  905.  
  906. double GetFrameTime()
  907. {
  908. LARGE_INTEGER currentTime;
  909. __int64 tickCount;
  910. QueryPerformanceCounter(&currentTime);
  911.  
  912. tickCount = currentTime.QuadPart-frameTimeOld;
  913. frameTimeOld = currentTime.QuadPart;
  914.  
  915. if(tickCount < 0.0f)
  916. tickCount = 0.0f;
  917.  
  918. return float(tickCount)/countsPerSecond;
  919. }
  920.  
  921. void UpdateScene(double time)
  922. {
  923. //Reset cube1World
  924. groundWorld = XMMatrixIdentity();
  925.  
  926. //Define cube1's world space matrix
  927. Scale = XMMatrixScaling( 500.0f, 10.0f, 500.0f );
  928. Translation = XMMatrixTranslation( 0.0f, 10.0f, 0.0f );
  929.  
  930. //Set cube1's world space using the transformations
  931. groundWorld = Scale * Translation;
  932.  
  933. mapWorld = XMMatrixIdentity();
  934. Scale = XMMatrixScaling(1.0f, 1.0f, 1.0f);
  935. Translation = XMMatrixTranslation(0.0f, 0.0f, 0.0f);
  936. mapWorld = Scale * Translation;
  937. ///////////////**************new**************////////////////////
  938. light.pos.x = XMVectorGetX(camPosition);
  939. light.pos.y = XMVectorGetY(camPosition);
  940. light.pos.z = XMVectorGetZ(camPosition);
  941.  
  942. light.dir.x = XMVectorGetX(camTarget) - light.pos.x;
  943. light.dir.y = XMVectorGetY(camTarget) - light.pos.y;
  944. light.dir.z = XMVectorGetZ(camTarget) - light.pos.z;
  945. ///////////////**************new**************////////////////////
  946. }
  947.  
  948. void RenderText(std::wstring text, int inInt)
  949. {
  950.  
  951. d3d11DevCon->PSSetShader(D2D_PS, 0, 0);
  952.  
  953. //Release the D3D 11 Device
  954. keyedMutex11->ReleaseSync(0);
  955.  
  956. //Use D3D10.1 device
  957. keyedMutex10->AcquireSync(0, 5);
  958.  
  959. //Draw D2D content
  960. D2DRenderTarget->BeginDraw();
  961.  
  962. //Clear D2D Background
  963. D2DRenderTarget->Clear(D2D1::ColorF(0.0f, 0.0f, 0.0f, 0.0f));
  964.  
  965. //Create our string
  966. std::wostringstream printString;
  967. printString << text << inInt;
  968. printText = printString.str();
  969.  
  970. //Set the Font Color
  971. D2D1_COLOR_F FontColor = D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  972.  
  973. //Set the brush color D2D will use to draw with
  974. Brush->SetColor(FontColor);
  975.  
  976. //Create the D2D Render Area
  977. D2D1_RECT_F layoutRect = D2D1::RectF(0, 0, Width, Height);
  978.  
  979. //Draw the Text
  980. D2DRenderTarget->DrawText(
  981. printText.c_str(),
  982. wcslen(printText.c_str()),
  983. TextFormat,
  984. layoutRect,
  985. Brush
  986. );
  987.  
  988. D2DRenderTarget->EndDraw();
  989.  
  990. //Release the D3D10.1 Device
  991. keyedMutex10->ReleaseSync(1);
  992.  
  993. //Use the D3D11 Device
  994. keyedMutex11->AcquireSync(1, 5);
  995.  
  996. //Use the shader resource representing the direct2d render target
  997. //to texture a square which is rendered in screen space so it
  998. //overlays on top of our entire scene. We use alpha blending so
  999. //that the entire background of the D2D render target is "invisible",
  1000. //And only the stuff we draw with D2D will be visible (the text)
  1001.  
  1002. //Set the blend state for D2D render target texture objects
  1003. d3d11DevCon->OMSetBlendState(Transparency, NULL, 0xffffffff);
  1004.  
  1005. //Set the d2d Index buffer
  1006. d3d11DevCon->IASetIndexBuffer( d2dIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
  1007. //Set the d2d vertex buffer
  1008. UINT stride = sizeof( Vertex );
  1009. UINT offset = 0;
  1010. d3d11DevCon->IASetVertexBuffers( 0, 1, &d2dVertBuffer, &stride, &offset );
  1011.  
  1012. WVP = XMMatrixIdentity();
  1013. cbPerObj.WVP = XMMatrixTranspose(WVP);
  1014. d3d11DevCon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
  1015. d3d11DevCon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
  1016. d3d11DevCon->PSSetShaderResources( 0, 1, &d2dTexture );
  1017. d3d11DevCon->PSSetSamplers( 0, 1, &CubesTexSamplerState );
  1018.  
  1019. d3d11DevCon->RSSetState(CWcullMode);
  1020. //Draw the second cube
  1021. d3d11DevCon->DrawIndexed( 6, 0, 0 );
  1022. }
  1023.  
  1024. void DrawScene()
  1025. {
  1026. //Clear our render target and depth/stencil view
  1027. float bgColor[4] = { 0.1f, 0.1f, 0.1f, 1.0f };
  1028. d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);
  1029. d3d11DevCon->ClearDepthStencilView(depthStencilView, D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0);
  1030.  
  1031. constbuffPerFrame.light = light;
  1032. d3d11DevCon->UpdateSubresource( cbPerFrameBuffer, 0, NULL, &constbuffPerFrame, 0, 0 );
  1033. d3d11DevCon->PSSetConstantBuffers(0, 1, &cbPerFrameBuffer);
  1034.  
  1035. //Set our Render Target
  1036. d3d11DevCon->OMSetRenderTargets( 1, &renderTargetView, depthStencilView );
  1037.  
  1038. //Set the default blend state (no blending) for opaque objects
  1039. d3d11DevCon->OMSetBlendState(0, 0, 0xffffffff);
  1040.  
  1041. //Set Vertex and Pixel Shaders
  1042. d3d11DevCon->VSSetShader(VS, 0, 0);
  1043. d3d11DevCon->PSSetShader(PS, 0, 0);
  1044.  
  1045. //Set the cubes index buffer
  1046. d3d11DevCon->IASetIndexBuffer( squareIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
  1047. //Set the cubes vertex buffer
  1048. UINT stride = sizeof( Vertex );
  1049. UINT offset = 0;
  1050. d3d11DevCon->IASetVertexBuffers( 0, 1, &squareVertBuffer, &stride, &offset );
  1051.  
  1052. //Set the WVP matrix and send it to the constant buffer in effect file
  1053. WVP = groundWorld * camView * camProjection;
  1054. cbPerObj.WVP = XMMatrixTranspose(WVP);
  1055. cbPerObj.World = XMMatrixTranspose(groundWorld);
  1056. d3d11DevCon->UpdateSubresource( cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0 );
  1057. d3d11DevCon->VSSetConstantBuffers( 0, 1, &cbPerObjectBuffer );
  1058. d3d11DevCon->PSSetShaderResources( 0, 1, &CubesTexture );
  1059. d3d11DevCon->PSSetSamplers( 0, 1, &CubesTexSamplerState );
  1060.  
  1061. d3d11DevCon->RSSetState(CCWcullMode);
  1062. d3d11DevCon->DrawIndexed( 6, 0, 0 );
  1063. d3d11DevCon->IASetIndexBuffer(bsp.GetIndexBuffer(), DXGI_FORMAT_R32_UINT, 0);
  1064. //Set the BSP vertex/index buffer
  1065. ID3D11Buffer* bspvertexbuffer = bsp.GetVertexBuffer();
  1066. d3d11DevCon->IASetVertexBuffers(0, 1, &bspvertexbuffer, &stride, &offset);
  1067. d3d11DevCon->RSSetState(CCWcullMode);
  1068. WVP = mapWorld * camView * camProjection;
  1069. cbPerObj.WVP = XMMatrixTranspose(WVP);
  1070. cbPerObj.World = XMMatrixTranspose(mapWorld);
  1071.  
  1072. d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
  1073. bsp.PVS_Render(d3d11Device, d3d11DevCon, D3DXVECTOR3(mapx, mapy, mapz));
  1074. RenderText(Unicode("FPS: ").c_str(), fps);
  1075.  
  1076. //Present the backbuffer to the screen
  1077. SwapChain->Present(0, 0);
  1078. }
  1079.  
  1080. int messageloop(){
  1081. MSG msg;
  1082. ZeroMemory(&msg, sizeof(MSG));
  1083. while(true)
  1084. {
  1085. BOOL PeekMessageL(
  1086. LPMSG lpMsg,
  1087. HWND hWnd,
  1088. UINT wMsgFilterMin,
  1089. UINT wMsgFilterMax,
  1090. UINT wRemoveMsg
  1091. );
  1092.  
  1093. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  1094. {
  1095. if (msg.message == WM_QUIT)
  1096. break;
  1097. TranslateMessage(&msg);
  1098. DispatchMessage(&msg);
  1099. }
  1100. else{
  1101. // run game code
  1102. frameCount++;
  1103. if(GetTime() > 1.0f)
  1104. {
  1105. fps = frameCount;
  1106. frameCount = 0;
  1107. StartTimer();
  1108. }
  1109.  
  1110. frameTime = GetFrameTime();
  1111.  
  1112. DetectInput(frameTime);
  1113. UpdateScene(frameTime);
  1114. DrawScene();
  1115. }
  1116. }
  1117. return msg.wParam;
  1118. }
  1119.  
  1120. LRESULT CALLBACK WndProc(HWND hwnd,
  1121. UINT msg,
  1122. WPARAM wParam,
  1123. LPARAM lParam)
  1124. {
  1125. switch( msg )
  1126. {
  1127. case WM_KEYDOWN:
  1128. if( wParam == VK_ESCAPE ){
  1129. DestroyWindow(hwnd);
  1130. }
  1131. return 0;
  1132.  
  1133. case WM_DESTROY:
  1134. PostQuitMessage(0);
  1135. return 0;
  1136. }
  1137. return DefWindowProc(hwnd,
  1138. msg,
  1139. wParam,
  1140. lParam);
  1141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement