Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(lib, "d3d11.lib")
- #pragma comment(lib, "d3dx11.lib")
- #pragma comment(lib, "d3dx10.lib")
- #pragma comment (lib, "D3D10_1.lib")
- #pragma comment (lib, "DXGI.lib")
- //#pragma comment (lib, "D2D1.lib")
- //#pragma comment (lib, "dwrite.lib")
- #pragma comment(lib, "DXErr.lib")
- #pragma comment(lib, "dinput8.lib")
- #pragma comment(lib, "dxguid.lib")
- #include <windows.h>
- #include <d3d11.h>
- #include <d3dx11.h>
- #include <d3dx10.h>
- #include <xnamath.h>
- #include <D3D10_1.h>
- #include <DXGI.h>
- #include <DxErr.h>
- #include <dinput.h>
- //The following 4 are for displaying a normal d3d window
- ID3D11Device* d3d11Device;
- IDXGISwapChain* SwapChain;
- ID3D11DeviceContext* d3d11DevCon;
- ID3D11RenderTargetView* renderTargetView;
- //the following few are for displaying a shape
- ID3D11Buffer* triangleVertBuffer;
- ID3D11VertexShader* VS;
- ID3D11PixelShader* PS;
- ID3D10Blob* VS_Buffer;
- ID3D10Blob* PS_Buffer;
- ID3D11InputLayout* vertLayout;
- //Views
- ID3D11Buffer* cbPerObjectBuffer;
- //The following are for input
- IDirectInputDevice8* DIKeyboard;
- IDirectInputDevice8* DIMouse;
- DIMOUSESTATE mouesLastState;
- LPDIRECTINPUT8 DirectInput;
- LPCTSTR WndClassName = "Untitled";
- HWND hwnd = NULL;
- HRESULT hr;
- const int Width = 800; // window width
- const int Height = 600; //window height
- double countsPerSecond = 0.0f;
- int frameCount = 0;
- //View
- XMMATRIX WVP;
- XMMATRIX World;
- XMMATRIX camView;
- XMMATRIX camProjection;
- XMVECTOR camPosition;
- XMVECTOR camTarget;
- XMVECTOR camUp;
- XMMATRIX Translation;
- XMMATRIX Scale;
- XMVECTOR DefaultForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
- XMVECTOR DefaultRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);
- XMVECTOR camForward = XMVectorSet(0.0f,0.0f,1.0f, 0.0f);
- XMVECTOR camRight = XMVectorSet(1.0f,0.0f,0.0f, 0.0f);
- float moveUpDown = 0.0f;
- __int64 frameTimeOld = 0;
- double frameTime;
- //Prototypes
- bool InitializeWindow(HINSTANCE hInstance,
- int ShowWnd,
- int width, int height,
- bool windowed);
- LRESULT CALLBACK WndProc(HWND hWnd,
- UINT msg,
- WPARAM wParam,
- LPARAM lParam);
- //main message loop
- int messageloop();
- //Ini D3D11
- bool InitializeDirect3d11App(HINSTANCE hInstance);
- bool InitScene();
- void ReleaseObjects();
- void UpdateScene(double time);
- void DrawScene();
- bool InitDirectInput(HINSTANCE hInstance);
- void DetectInput(double time);
- double GetFrameTime();
- void Update();
- //cbPerobject
- struct cbPerObject
- {
- XMMATRIX WVP;
- };
- cbPerObject cbPerObj;
- //Vertex info
- struct Vertex
- {
- Vertex(){}
- Vertex(float x, float y, float z)
- : pos(x,y,z){}
- XMFLOAT3 pos;
- };
- D3D11_INPUT_ELEMENT_DESC layout[] =
- {
- {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
- };
- UINT numElements = ARRAYSIZE(layout);
- int WINAPI WinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nShowCmd)
- {
- if(!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
- {
- MessageBox(0, "Window Ini - Failed",
- "Error", MB_OK);
- return 0;
- }
- if(!InitializeDirect3d11App(hInstance))
- {
- MessageBox(0, "Direct3d Ini - failed",
- "Error", MB_OK);
- return 0;
- }
- if(!InitScene())
- {
- MessageBox(0, "Scene Ini - Failed",
- "error", MB_OK);
- return 0;
- }
- if(!InitDirectInput(hInstance))
- {
- MessageBox(0, "DirectInput failed",
- "error", MB_OK);
- return 0;
- }
- messageloop();
- ReleaseObjects();
- return 0;
- }
- bool InitializeWindow(HINSTANCE hInstance, //Initialize our window
- int ShowWnd,
- int width, int height,
- bool windowed)
- {
- //Start creating the window//
- WNDCLASSEX wc; //Create a new extended windows class
- wc.cbSize = sizeof(WNDCLASSEX); //Size of our windows class
- wc.style = CS_HREDRAW | CS_VREDRAW; //class styles
- wc.lpfnWndProc = WndProc; //Default windows procedure function
- wc.cbClsExtra = NULL; //Extra bytes after our wc structure
- wc.cbWndExtra = NULL; //Extra bytes after our windows instance
- wc.hInstance = hInstance; //Instance to current application
- wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); //Title bar Icon
- wc.hCursor = LoadCursor(NULL, IDC_ARROW); //Default mouse Icon
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2); //Window bg color
- wc.lpszMenuName = NULL; //Name of the menu attached to our window
- wc.lpszClassName = WndClassName; //Name of our windows class
- wc.hIconSm = LoadIcon(NULL, IDI_WINLOGO); //Icon in your taskbar
- if (!RegisterClassEx(&wc)) //Register our windows class
- {
- //if registration failed, display error
- MessageBox(NULL, "Error registering class",
- "Error", MB_OK | MB_ICONERROR);
- return 1;
- }
- hwnd = CreateWindowEx( //Create our Extended Window
- NULL, //Extended style
- WndClassName, //Name of our windows class
- "First window", //Name in the title bar of our window
- WS_OVERLAPPEDWINDOW, //style of our window
- CW_USEDEFAULT, CW_USEDEFAULT, //Top left corner of window
- width, //Width of our window
- height, //Height of our window
- NULL, //Handle to parent window
- NULL, //Handle to a Menu
- hInstance, //Specifies instance of current program
- NULL //used for an MDI client window
- );
- if (!hwnd) //Make sure our window has been created
- {
- //If not, display error
- MessageBox(NULL, "Error creating window",
- "Error", MB_OK | MB_ICONERROR);
- return 1;
- }
- ShowWindow(hwnd, ShowWnd); //Shows our window
- UpdateWindow(hwnd); //Its good to update our window
- return true; //if there were no errors, return true
- }
- int messageloop(){ //The message loop
- MSG msg; //Create a new message structure
- ZeroMemory(&msg, sizeof(MSG)); //clear message structure to NULL
- while(true) //while there is a message
- {
- //if there was a windows message
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if (msg.message == WM_QUIT) //if the message was WM_QUIT
- break; //Exit the message loop
- TranslateMessage(&msg); //Translate the message
- //Send the message to default windows procedure
- DispatchMessage(&msg);
- }
- else{ //Otherewise, keep the flow going
- frameTime = GetFrameTime();
- DetectInput(frameTime);
- UpdateScene(frameTime);
- DrawScene();
- }
- }
- return (int)msg.wParam; //return the message
- }
- LRESULT CALLBACK WndProc(HWND hwnd, //Default windows procedure
- UINT msg,
- WPARAM wParam,
- LPARAM lParam)
- {
- switch( msg ) //Check message
- {
- case WM_KEYDOWN: //For a key down
- //if escape key was pressed, display popup box
- if( wParam == VK_ESCAPE ){
- if(MessageBox(0, "Are you sure you want to exit?",
- "Really?", MB_YESNO | MB_ICONQUESTION) == IDYES)
- //Release the windows allocated memory
- DestroyWindow(hwnd);
- }
- //added in for kicks
- //when space bar is pressed a messagebox is presented to the user
- if(wParam == VK_SPACE){
- MessageBox(0, "OKAY","OH REALLY NOW?", MB_OK);
- }
- return 0;
- case WM_DESTROY: //if x button in top right was pressed
- PostQuitMessage(0);
- return 0;
- }
- //return the message for windows to handle it
- return DefWindowProc(hwnd,
- msg,
- wParam,
- lParam);
- }
- bool InitializeDirect3d11App(HINSTANCE hInstance)
- {
- //HRESULT hr;
- //Describe our Buffer
- DXGI_MODE_DESC bufferDesc;
- ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
- bufferDesc.Width = Width;
- bufferDesc.Height = Height;
- bufferDesc.RefreshRate.Numerator = 60;
- bufferDesc.RefreshRate.Denominator = 1;
- bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
- bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
- //Describe our SwapChain
- DXGI_SWAP_CHAIN_DESC swapChainDesc;
- ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
- swapChainDesc.BufferDesc = bufferDesc;
- swapChainDesc.SampleDesc.Count = 1;
- swapChainDesc.SampleDesc.Quality = 0;
- swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- swapChainDesc.BufferCount = 1;
- swapChainDesc.OutputWindow = hwnd;
- swapChainDesc.Windowed = TRUE;
- swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
- //Create our SwapChain
- hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
- D3D11_SDK_VERSION, &swapChainDesc, &SwapChain, &d3d11Device, NULL, &d3d11DevCon);
- if(FAILED(hr))
- {
- MessageBox(NULL, DXGetErrorDescription(hr),
- TEXT(" D3D11CreateDeviceAndSwapChain"), MB_OK);
- return 0;
- }
- //Create our BackBuffer
- ID3D11Texture2D* BackBuffer;
- hr = SwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (void**)&BackBuffer );
- if(FAILED(hr))
- {
- MessageBox(NULL, DXGetErrorDescription(hr),
- TEXT("SwapChain->GetBuffer"), MB_OK);
- return 0;
- }
- //Create our Render Target
- hr = d3d11Device->CreateRenderTargetView( BackBuffer, NULL, &renderTargetView );
- BackBuffer->Release();
- if(FAILED(hr))
- {
- MessageBox(NULL, DXGetErrorDescription(hr),
- TEXT("d3d11Device->CreateRenderTargetView"), MB_OK);
- return 0;
- }
- //Set our Render Target
- d3d11DevCon->OMSetRenderTargets( 1, &renderTargetView, NULL );
- return true;
- }
- void ReleaseObjects()
- {
- //Window
- SwapChain->Release();
- d3d11Device->Release();
- d3d11DevCon->Release();
- renderTargetView->Release();
- //shape and buffers
- triangleVertBuffer->Release();
- VS->Release();
- PS->Release();
- VS_Buffer->Release();
- PS_Buffer->Release();
- vertLayout->Release();
- //Views
- cbPerObjectBuffer->Release();
- //input
- DIKeyboard->Unacquire();
- DIMouse->Unacquire();
- DirectInput->Release();
- }
- bool InitDirectInput(HINSTANCE hInstance)
- {
- hr = DirectInput8Create(hInstance,
- DIRECTINPUT_VERSION,
- IID_IDirectInput8,
- (void**)&DirectInput,
- NULL);
- hr = DirectInput->CreateDevice(GUID_SysKeyboard,
- &DIKeyboard,
- NULL);
- hr = DirectInput->CreateDevice(GUID_SysMouse,
- &DIMouse,
- NULL);
- hr = DIKeyboard->SetDataFormat(&c_dfDIKeyboard);
- hr = DIKeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
- hr = DIMouse->SetDataFormat(&c_dfDIMouse);
- hr = DIMouse->SetCooperativeLevel(hwnd, DISCL_EXCLUSIVE | DISCL_NOWINKEY | DISCL_FOREGROUND);
- return true;
- }
- void DetectInput(double time)
- {
- DIMOUSESTATE mouseCurrState;
- BYTE keyboardState[256];
- DIKeyboard->Acquire();
- DIMouse->Acquire();
- DIMouse->GetDeviceState(sizeof(DIMOUSESTATE), &mouseCurrState);
- DIKeyboard->GetDeviceState(sizeof(keyboardState), (LPVOID)&keyboardState);
- if(keyboardState[DIK_ESCAPE] & 0x80)
- PostMessage(hwnd, WM_DESTROY, 0, 0);
- if(keyboardState[DIK_UP] & 0x80)
- {
- moveUpDown += 1.0f *time;
- }
- Update();
- return;
- }
- void Update()
- {
- XMMATRIX anew;
- anew = XMMatrixIdentity();
- camTarget = XMVector3Normalize(camTarget);
- camUp = XMVector3TransformCoord(camUp, anew);
- camForward = XMVector3TransformCoord(DefaultForward, anew);
- camPosition += moveUpDown*camForward;
- moveUpDown = 0.0f;
- camTarget = camPosition + camTarget;
- camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
- }
- bool InitScene()
- {
- hr = D3DX11CompileFromFile("effects.fx", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
- hr = D3DX11CompileFromFile("effects.fx", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);
- hr = d3d11Device->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
- hr = d3d11Device->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);
- d3d11DevCon->VSSetShader(VS, 0, 0);
- d3d11DevCon->PSSetShader(PS, 0, 0);
- Vertex v[] =
- {
- Vertex(0.0f, 0.5f, 0.5f),
- Vertex(0.5f, -0.5f, 0.5f),
- Vertex(-1.0f, -0.5f, 0.5f),
- };
- D3D11_BUFFER_DESC vertexBufferDesc;
- ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );
- vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
- vertexBufferDesc.ByteWidth = sizeof( Vertex ) * 3;
- vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
- vertexBufferDesc.CPUAccessFlags = 0;
- vertexBufferDesc.MiscFlags = 0;
- D3D11_SUBRESOURCE_DATA vertexBufferData;
- ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
- vertexBufferData.pSysMem = v;
- hr = d3d11Device->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &triangleVertBuffer);
- //Set the vertex buffer
- UINT stride = sizeof( Vertex );
- UINT offset = 0;
- d3d11DevCon->IASetVertexBuffers( 0, 1, &triangleVertBuffer, &stride, &offset );
- //Create the Input Layout
- d3d11Device->CreateInputLayout( layout, numElements, VS_Buffer->GetBufferPointer(),
- VS_Buffer->GetBufferSize(), &vertLayout );
- //Set the Input Layout
- d3d11DevCon->IASetInputLayout( vertLayout );
- //Set Primitive Topology
- d3d11DevCon->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
- //Create the Viewport
- D3D11_VIEWPORT viewport;
- ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
- viewport.TopLeftX = 0;
- viewport.TopLeftY = 0;
- viewport.Width = Width;
- viewport.Height = Height;
- //Set the Viewport
- d3d11DevCon->RSSetViewports(1, &viewport);
- D3D11_BUFFER_DESC cbbd;
- ZeroMemory(&cbbd, sizeof(D3D11_BUFFER_DESC));
- cbbd.Usage = D3D11_USAGE_DEFAULT;
- cbbd.ByteWidth = sizeof(cbPerObject);
- cbbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- cbbd.CPUAccessFlags = 0;
- cbbd.MiscFlags = 0;
- hr = d3d11Device->CreateBuffer(&cbbd, NULL, &cbPerObjectBuffer);
- camPosition = XMVectorSet(0.0f, 0.0f, -0.5f, 0.0f);
- camTarget = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
- camUp = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
- camView = XMMatrixLookAtLH(camPosition, camTarget, camUp);
- camProjection = XMMatrixPerspectiveFovLH(0.4f*3.14, (float)Width/Height, 1.0f, 1000.0f);
- return true;
- }
- void UpdateScene(double time)
- {
- World = XMMatrixIdentity();
- Scale = XMMatrixScaling(500.0, 10.0f, 500.0f);
- Translation = XMMatrixTranslation(0.0f, 10.0f, 0.0f);
- World = Scale * Translation;
- }
- void DrawScene()
- {
- D3DXCOLOR bgColor(0.0f, 0.0f, 1.0f, 0.0f);
- d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);
- World = XMMatrixIdentity();
- WVP = World * camView * camProjection;
- cbPerObj.WVP = XMMatrixTranspose(WVP);
- d3d11DevCon->UpdateSubresource(cbPerObjectBuffer, 0, NULL, &cbPerObj, 0, 0);
- d3d11DevCon->VSSetConstantBuffers(0, 1, &cbPerObjectBuffer);
- d3d11DevCon->Draw(3,0);
- SwapChain->Present(0,0);
- }
- double GetFrameTime()
- {
- LARGE_INTEGER currentTime;
- __int64 tickCount;
- QueryPerformanceCounter(¤tTime);
- tickCount = currentTime.QuadPart-frameTimeOld;
- frameTimeOld = currentTime.QuadPart;
- if(tickCount < 0.0f)
- tickCount = 0.0f;
- return float(tickCount)/countsPerSecond;
- }
Advertisement
Add Comment
Please, Sign In to add comment