Guest User

Untitled

a guest
Jan 15th, 2013
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.49 KB | None | 0 0
  1. #pragma comment(lib, "d3d11.lib")
  2. #pragma comment(lib, "d3dx11.lib")
  3. #pragma comment(lib, "d3dx10.lib")
  4. #pragma comment (lib, "D3D10_1.lib")
  5. #pragma comment (lib, "DXGI.lib")
  6. //#pragma comment (lib, "D2D1.lib")
  7. //#pragma comment (lib, "dwrite.lib")
  8. #pragma comment(lib, "DXErr.lib")
  9. #pragma comment(lib, "dinput8.lib")
  10. #pragma comment(lib, "dxguid.lib")
  11.  
  12.  
  13. #include <windows.h>
  14. #include <d3d11.h>
  15. #include <d3dx11.h>
  16. #include <d3dx10.h>
  17. #include <xnamath.h>
  18. #include <D3D10_1.h>
  19. #include <DXGI.h>
  20. #include <DxErr.h>
  21. #include <dinput.h>
  22. //The following 4 are for displaying a normal d3d window
  23. ID3D11Device* d3d11Device;
  24. IDXGISwapChain* SwapChain;
  25. ID3D11DeviceContext* d3d11DevCon;
  26. ID3D11RenderTargetView* renderTargetView;
  27.  
  28. //the following few are for displaying a shape
  29. ID3D11Buffer* triangleVertBuffer;
  30. ID3D11VertexShader* VS;
  31. ID3D11PixelShader* PS;
  32. ID3D10Blob* VS_Buffer;
  33. ID3D10Blob* PS_Buffer;
  34. ID3D11InputLayout* vertLayout;
  35.  
  36. //Views
  37. ID3D11Buffer* cbPerObjectBuffer;
  38.  
  39.  
  40. //The following are for input
  41. IDirectInputDevice8* DIKeyboard;
  42. IDirectInputDevice8* DIMouse;
  43. DIMOUSESTATE mouesLastState;
  44. LPDIRECTINPUT8 DirectInput;
  45.  
  46. LPCTSTR WndClassName = "Untitled";
  47. HWND hwnd = NULL;
  48. HRESULT hr;
  49.  
  50. const int Width = 800; // window width
  51. const int Height = 600; //window height
  52. double countsPerSecond = 0.0f;
  53. int frameCount = 0;
  54.  
  55. //View
  56. XMMATRIX WVP;
  57. XMMATRIX World;
  58. XMMATRIX camView;
  59. XMMATRIX camProjection;
  60. XMVECTOR camPosition;
  61. XMVECTOR camTarget;
  62. XMVECTOR camUp;
  63.  
  64. XMMATRIX Translation;
  65. XMMATRIX Scale;
  66.  
  67. XMVECTOR DefaultForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
  68. XMVECTOR DefaultRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);
  69. XMVECTOR camForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
  70. XMVECTOR camRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);
  71.  
  72.  
  73.  
  74. float moveUpDown = 0.0f;
  75.  
  76. __int64 frameTimeOld = 0;
  77. double frameTime;
  78.  
  79.  
  80. //Prototypes
  81. bool InitializeWindow(HINSTANCE hInstance,
  82. int ShowWnd,
  83. int width, int height,
  84. bool windowed);
  85.  
  86. LRESULT CALLBACK WndProc(HWND hWnd,
  87. UINT msg,
  88. WPARAM wParam,
  89. LPARAM lParam);
  90.  
  91. //main message loop
  92. int messageloop();
  93.  
  94.  
  95. //Ini D3D11
  96. bool InitializeDirect3d11App(HINSTANCE hInstance);
  97.  
  98. bool InitScene();
  99.  
  100. void ReleaseObjects();
  101.  
  102. void UpdateScene(double time);
  103.  
  104. void DrawScene();
  105.  
  106. bool InitDirectInput(HINSTANCE hInstance);
  107.  
  108. void DetectInput(double time);
  109.  
  110. double GetFrameTime();
  111.  
  112. void Update();
  113.  
  114.  
  115. //cbPerobject
  116. struct cbPerObject
  117. {
  118. XMMATRIX WVP;
  119. };
  120.  
  121. cbPerObject cbPerObj;
  122.  
  123. //Vertex info
  124. struct Vertex
  125. {
  126. Vertex(){}
  127. Vertex(float x, float y, float z)
  128. : pos(x,y,z){}
  129. XMFLOAT3 pos;
  130. };
  131.  
  132. D3D11_INPUT_ELEMENT_DESC layout[] =
  133. {
  134. {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
  135. };
  136.  
  137. UINT numElements = ARRAYSIZE(layout);
  138.  
  139.  
  140.  
  141.  
  142. int WINAPI WinMain(HINSTANCE hInstance,
  143. HINSTANCE hPrevInstance,
  144. LPSTR lpCmdLine,
  145. int nShowCmd)
  146. {
  147. if(!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
  148. {
  149. MessageBox(0, "Window Ini - Failed",
  150. "Error", MB_OK);
  151. return 0;
  152. }
  153.  
  154. if(!InitializeDirect3d11App(hInstance))
  155. {
  156. MessageBox(0, "Direct3d Ini - failed",
  157. "Error", MB_OK);
  158. return 0;
  159. }
  160.  
  161. if(!InitScene())
  162. {
  163. MessageBox(0, "Scene Ini - Failed",
  164. "error", MB_OK);
  165. return 0;
  166. }
  167.  
  168. if(!InitDirectInput(hInstance))
  169. {
  170. MessageBox(0, "DirectInput failed",
  171. "error", MB_OK);
  172. return 0;
  173. }
  174.  
  175. messageloop();
  176.  
  177. ReleaseObjects();
  178.  
  179. return 0;
  180. }
  181.  
  182. bool InitializeWindow(HINSTANCE hInstance, //Initialize our window
  183. int ShowWnd,
  184. int width, int height,
  185. bool windowed)
  186. {
  187. //Start creating the window//
  188.  
  189. WNDCLASSEX wc; //Create a new extended windows class
  190.  
  191. wc.cbSize = sizeof(WNDCLASSEX); //Size of our windows class
  192. wc.style = CS_HREDRAW | CS_VREDRAW; //class styles
  193. wc.lpfnWndProc = WndProc; //Default windows procedure function
  194. wc.cbClsExtra = NULL; //Extra bytes after our wc structure
  195. wc.cbWndExtra = NULL; //Extra bytes after our windows instance
  196. wc.hInstance = hInstance; //Instance to current application
  197. wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); //Title bar Icon
  198. wc.hCursor = LoadCursor(NULL, IDC_ARROW); //Default mouse Icon
  199. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2); //Window bg color
  200. wc.lpszMenuName = NULL; //Name of the menu attached to our window
  201. wc.lpszClassName = WndClassName; //Name of our windows class
  202. wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); //Icon in your taskbar
  203.  
  204. if (!RegisterClassEx(&wc)) //Register our windows class
  205. {
  206. //if registration failed, display error
  207. MessageBox(NULL, "Error registering class",
  208. "Error", MB_OK | MB_ICONERROR);
  209. return 1;
  210. }
  211.  
  212. hwnd = CreateWindowEx( //Create our Extended Window
  213. NULL, //Extended style
  214. WndClassName, //Name of our windows class
  215. "First window", //Name in the title bar of our window
  216. WS_OVERLAPPEDWINDOW, //style of our window
  217. CW_USEDEFAULT, CW_USEDEFAULT, //Top left corner of window
  218. width, //Width of our window
  219. height, //Height of our window
  220. NULL, //Handle to parent window
  221. NULL, //Handle to a Menu
  222. hInstance, //Specifies instance of current program
  223. NULL //used for an MDI client window
  224. );
  225.  
  226. if (!hwnd) //Make sure our window has been created
  227. {
  228. //If not, display error
  229. MessageBox(NULL, "Error creating window",
  230. "Error", MB_OK | MB_ICONERROR);
  231. return 1;
  232. }
  233.  
  234. ShowWindow(hwnd, ShowWnd); //Shows our window
  235. UpdateWindow(hwnd); //Its good to update our window
  236.  
  237. return true; //if there were no errors, return true
  238. }
  239.  
  240.  
  241.  
  242. int messageloop(){ //The message loop
  243.  
  244. MSG msg; //Create a new message structure
  245. ZeroMemory(&msg, sizeof(MSG)); //clear message structure to NULL
  246.  
  247. while(true) //while there is a message
  248. {
  249. //if there was a windows message
  250. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  251. {
  252. if (msg.message == WM_QUIT) //if the message was WM_QUIT
  253. break; //Exit the message loop
  254.  
  255. TranslateMessage(&msg); //Translate the message
  256.  
  257. //Send the message to default windows procedure
  258. DispatchMessage(&msg);
  259. }
  260.  
  261. else{ //Otherewise, keep the flow going
  262. frameTime = GetFrameTime();
  263. DetectInput(frameTime);
  264. UpdateScene(frameTime);
  265. DrawScene();
  266. }
  267.  
  268.  
  269. }
  270.  
  271. return (int)msg.wParam; //return the message
  272.  
  273. }
  274.  
  275.  
  276. LRESULT CALLBACK WndProc(HWND hwnd, //Default windows procedure
  277. UINT msg,
  278. WPARAM wParam,
  279. LPARAM lParam)
  280. {
  281. switch( msg ) //Check message
  282. {
  283.  
  284. case WM_KEYDOWN: //For a key down
  285. //if escape key was pressed, display popup box
  286. if( wParam == VK_ESCAPE ){
  287. if(MessageBox(0, "Are you sure you want to exit?",
  288. "Really?", MB_YESNO | MB_ICONQUESTION) == IDYES)
  289.  
  290. //Release the windows allocated memory
  291. DestroyWindow(hwnd);
  292. }
  293.  
  294. //added in for kicks
  295. //when space bar is pressed a messagebox is presented to the user
  296. if(wParam == VK_SPACE){
  297. MessageBox(0, "OKAY","OH REALLY NOW?", MB_OK);
  298. }
  299. return 0;
  300.  
  301. case WM_DESTROY: //if x button in top right was pressed
  302. PostQuitMessage(0);
  303. return 0;
  304. }
  305. //return the message for windows to handle it
  306. return DefWindowProc(hwnd,
  307. msg,
  308. wParam,
  309. lParam);
  310. }
  311.  
  312. bool InitializeDirect3d11App(HINSTANCE hInstance)
  313. {
  314. //HRESULT hr;
  315.  
  316. //Describe our Buffer
  317. DXGI_MODE_DESC bufferDesc;
  318.  
  319. ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
  320.  
  321. bufferDesc.Width = Width;
  322. bufferDesc.Height = Height;
  323. bufferDesc.RefreshRate.Numerator = 60;
  324. bufferDesc.RefreshRate.Denominator = 1;
  325. bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  326. bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  327. bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  328.  
  329. //Describe our SwapChain
  330. DXGI_SWAP_CHAIN_DESC swapChainDesc;
  331.  
  332. ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
  333.  
  334. swapChainDesc.BufferDesc = bufferDesc;
  335. swapChainDesc.SampleDesc.Count = 1;
  336. swapChainDesc.SampleDesc.Quality = 0;
  337. swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  338. swapChainDesc.BufferCount = 1;
  339. swapChainDesc.OutputWindow = hwnd;
  340. swapChainDesc.Windowed = TRUE;
  341. swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  342.  
  343.  
  344. //Create our SwapChain
  345. hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
  346. D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);
  347. if(FAILED(hr))
  348. {
  349. MessageBox(NULL, DXGetErrorDescription(hr),
  350. TEXT(" D3D11CreateDeviceAndSwapChain"), MB_OK);
  351. return 0;
  352. }
  353. //Create our BackBuffer
  354. ID3D11Texture2D* BackBuffer;
  355. hr = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (void**)&BackBuffer );
  356. if(FAILED(hr))
  357. {
  358. MessageBox(NULL, DXGetErrorDescription(hr),
  359. TEXT("SwapChain->GetBuffer"), MB_OK);
  360. return 0;
  361. }
  362. //Create our Render Target
  363. hr = d3d11Device->CreateRenderTargetView( BackBuffer, NULL, &renderTargetView );
  364. BackBuffer->Release();
  365. if(FAILED(hr))
  366. {
  367. MessageBox(NULL, DXGetErrorDescription(hr),
  368. TEXT("d3d11Device->CreateRenderTargetView"), MB_OK);
  369. return 0;
  370. }
  371. //Set our Render Target
  372. d3d11DevCon->OMSetRenderTargets( 1, &renderTargetView, NULL );
  373.  
  374. return true;
  375. }
  376.  
  377.  
  378. void ReleaseObjects()
  379. {
  380. //Window
  381. SwapChain->Release();
  382. d3d11Device->Release();
  383. d3d11DevCon->Release();
  384. renderTargetView->Release();
  385.  
  386. //shape and buffers
  387. triangleVertBuffer->Release();
  388. VS->Release();
  389. PS->Release();
  390. VS_Buffer->Release();
  391. PS_Buffer->Release();
  392. vertLayout->Release();
  393.  
  394. //Views
  395. cbPerObjectBuffer->Release();
  396.  
  397. //input
  398. DIKeyboard->Unacquire();
  399. DIMouse->Unacquire();
  400. DirectInput->Release();
  401. }
  402.  
  403. bool InitDirectInput(HINSTANCE hInstance)
  404. {
  405. hr = DirectInput8Create(hInstance,
  406. DIRECTINPUT_VERSION,
  407. IID_IDirectInput8,
  408. (void**)&DirectInput,
  409. NULL);
  410.  
  411. hr = DirectInput->CreateDevice(GUID_SysKeyboard,
  412. &DIKeyboard,
  413. NULL);
  414.  
  415. hr = DirectInput->CreateDevice(GUID_SysMouse,
  416. &DIMouse,
  417. NULL);
  418.  
  419. hr = DIKeyboard->SetDataFormat(&c_dfDIKeyboard);
  420. hr = DIKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
  421.  
  422. hr = DIMouse->SetDataFormat(&c_dfDIMouse);
  423. hr = DIMouse->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND);
  424.  
  425. return true;
  426. }
  427.  
  428. void DetectInput(double time)
  429. {
  430. DIMOUSESTATE mouseCurrState;
  431.  
  432. BYTE keyboardState[256];
  433. DIKeyboard->Acquire();
  434. DIMouse->Acquire();
  435.  
  436. DIMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseCurrState);
  437. DIKeyboard->GetDeviceState(sizeof(keyboardState), (LPVOID)&keyboardState);
  438.  
  439. if(keyboardState[DIK_ESCAPE] & 0x80)
  440. PostMessage(hwnd, WM_DESTROY, 0, 0);
  441.  
  442. if(keyboardState[DIK_UP] & 0x80)
  443. {
  444. moveUpDown += 1.0f *time;
  445. }
  446.  
  447. Update();
  448. return;
  449. }
  450.  
  451. void Update()
  452. {
  453. XMMATRIX anew;
  454. anew = XMMatrixIdentity();
  455.  
  456. camTarget = XMVector3Normalize(camTarget);
  457.  
  458. camUp = XMVector3TransformCoord(camUp, anew);
  459. camForward = XMVector3TransformCoord(DefaultForward, anew);
  460.  
  461.  
  462. camPosition += moveUpDown*camForward;
  463. moveUpDown = 0.0f;
  464.  
  465. camTarget = camPosition + camTarget;
  466.  
  467. camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
  468. }
  469.  
  470.  
  471. bool InitScene()
  472. {
  473. hr = D3DX11CompileFromFile("effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
  474. hr = D3DX11CompileFromFile("effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);
  475.  
  476. hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
  477. hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);
  478.  
  479.  
  480.  
  481. d3d11DevCon->VSSetShader(VS, 0, 0);
  482. d3d11DevCon->PSSetShader(PS, 0, 0);
  483.  
  484. Vertex v[] =
  485. {
  486. Vertex(0.0f, 0.5f, 0.5f),
  487. Vertex(0.5f, -0.5f, 0.5f),
  488. Vertex(-1.0f, -0.5f, 0.5f),
  489. };
  490.  
  491. D3D11_BUFFER_DESC vertexBufferDesc;
  492. ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );
  493.  
  494. vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  495. vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 3;
  496. vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  497. vertexBufferDesc.CPUAccessFlags = 0;
  498. vertexBufferDesc.MiscFlags = 0;
  499.  
  500. D3D11_SUBRESOURCE_DATA vertexBufferData;
  501.  
  502. ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
  503. vertexBufferData.pSysMem = v;
  504. hr = d3d11Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &triangleVertBuffer);
  505.  
  506. //Set the vertex buffer
  507. UINT stride = sizeof( Vertex );
  508. UINT offset = 0;
  509. d3d11DevCon->IASetVertexBuffers( 0, 1, &triangleVertBuffer, &stride, &offset );
  510.  
  511. //Create the Input Layout
  512. d3d11Device->CreateInputLayout( layout, numElements, VS_Buffer->GetBufferPointer(),
  513. VS_Buffer->GetBufferSize(), &vertLayout );
  514.  
  515. //Set the Input Layout
  516. d3d11DevCon->IASetInputLayout( vertLayout );
  517.  
  518. //Set Primitive Topology
  519. d3d11DevCon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  520.  
  521. //Create the Viewport
  522. D3D11_VIEWPORT viewport;
  523. ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  524.  
  525. viewport.TopLeftX = 0;
  526. viewport.TopLeftY = 0;
  527. viewport.Width = Width;
  528. viewport.Height = Height;
  529.  
  530. //Set the Viewport
  531. d3d11DevCon->RSSetViewports(1, &viewport);
  532.  
  533. D3D11_BUFFER_DESC cbbd;
  534. ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));
  535.  
  536. cbbd.Usage = D3D11_USAGE_DEFAULT;
  537. cbbd.ByteWidth = sizeof(cbPerObject);
  538. cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  539. cbbd.CPUAccessFlags = 0;
  540. cbbd.MiscFlags = 0;
  541.  
  542. hr = d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);
  543.  
  544. camPosition = XMVectorSet(0.0f, 0.0f, -0.5f, 0.0f);
  545. camTarget = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
  546. camUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
  547.  
  548. camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
  549.  
  550. camProjection = XMMatrixPerspectiveFovLH(0.4f*3.14, (float)Width/Height, 1.0f, 1000.0f);
  551.  
  552.  
  553. return true;
  554. }
  555.  
  556. void UpdateScene(double time)
  557. {
  558. World = XMMatrixIdentity();
  559.  
  560. Scale = XMMatrixScaling(500.0, 10.0f, 500.0f);
  561.  
  562. Translation = XMMatrixTranslation(0.0f, 10.0f, 0.0f);
  563.  
  564. World = Scale * Translation;
  565. }
  566.  
  567. void DrawScene()
  568. {
  569. D3DXCOLOR bgColor(0.0f, 0.0f, 1.0f, 0.0f);
  570.  
  571. d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);
  572.  
  573. World = XMMatrixIdentity();
  574.  
  575. WVP = World * camView * camProjection;
  576.  
  577. cbPerObj.WVP = XMMatrixTranspose(WVP);
  578.  
  579. d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
  580.  
  581. d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);
  582.  
  583. d3d11DevCon->Draw(3,0);
  584.  
  585. SwapChain->Present(0,0);
  586. }
  587.  
  588. double GetFrameTime()
  589. {
  590. LARGE_INTEGER currentTime;
  591. __int64 tickCount;
  592. QueryPerformanceCounter(&currentTime);
  593.  
  594. tickCount = currentTime.QuadPart-frameTimeOld;
  595. frameTimeOld = currentTime.QuadPart;
  596.  
  597. if(tickCount < 0.0f)
  598. tickCount = 0.0f;
  599.  
  600. return float(tickCount)/countsPerSecond;
  601. }
Advertisement
Add Comment
Please, Sign In to add comment