#pragma comment(lib, "d3d10.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib, "DXErr.lib")
//Include necessary Headers//
#include <windows.h>
#include<DxErr.h>
#include <d3dx11.h>
#include <D3D11.h>
#include <D3D10.h>
#include <D3DX10math.h>
#include <xnamath.h>
#include <string>
///////// INIT D3D
ID3D11Device* d3d11Device;
IDXGISwapChain* SwapChain;
ID3D11DeviceContext* d3d11DevCon;
ID3D11RenderTargetView* renderTargetView;
ID3D11Buffer* triangleVertBuffer;
ID3D11VertexShader* VS;
ID3D11PixelShader* PS;
ID3D10Blob* VS_Buffer;
ID3D10Blob* PS_Buffer;
ID3D11InputLayout* vertLayout;
float red = 0.0f;
float green = 0.0f;
float blue = 0.0f;
int colormodr = 1;
int colormodg = 1;
int colormodb = 1;
//Define variables/constants//
//LP - Pointer
// C - Constant
// T - TCHAR
// STR - String
// WndClassName is the name for the window class
LPCTSTR WndClassName = "firstwindow"; //Define our window class name
HWND hwnd = NULL; //Sets our windows handle to NULL
HRESULT hr;
const int Width = 800; //window width
const int Height = 600; //window height
//
bool InitializeDirect3d11App(HINSTANCE hInstance);
//void ReleaseObjects();
bool InitScene();
void UpdateScene();
void DrawScene();
void CleanUp();
//Functions//
bool InitializeWindow(HINSTANCE hInstance, //Initialize our window
int ShowWnd,
int width, int height,
bool windowed);
int messageloop(); //Main part of the program
LRESULT CALLBACK WndProc(HWND hWnd, //Windows callback procedure
UINT msg,
WPARAM wParam,
LPARAM lParam);
struct Vertex //Overloader vertex Structure
{
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, //Main windows function
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
//Initialize Window//
if(!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
{
//If initialization failed, display an error message
MessageBox(0, "Window Initialization - Failed",
"Error", MB_OK);
return 0;
}
////D3D
if(!InitializeDirect3d11App(hInstance))
{
MessageBox(0, "Direct3D Intialization - Failed",
"Error", MB_OK);
return 0;
}
if(!InitScene())
{
MessageBox(0, "Scene Initialization - Failed",
"Error", MB_OK);
return 0;
}
messageloop(); //Jump into the heart of our program
CleanUp();
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
}
void CleanUp()
{
SwapChain->Release();
d3d11Device->Release();
d3d11DevCon->Release();
renderTargetView->Release();
triangleVertBuffer->Release();
VS->Release();
PS->Release();
VS_Buffer->Release();
PS_Buffer->Release();
vertLayout->Release();
}
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;
}
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);
//set the vertex and pixel shaders
Vertex v[] =
{
//X / Y/ Z
Vertex(0.0f, 0.5f, 0.5f),
Vertex(0.5f, -0.5f, 0.5f),
Vertex(-0.5f, -0.5f, 0.5f),
//Vertex(0.2f, 0.5f, -0.5f),
};
D3D11_BUFFER_DESC vertexBufferDesc;
ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(Vertex) * _countof(v); //was * 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);
return true;
}
void UpdateScene()
{
red += colormodr * 0.00005f;
green += colormodg * 0.00002f;
blue += colormodb * 0.00001f;
if(red >= 1.0f || red <= 0.0f)
colormodr *= -1;
if(green >= 1.0f || green <= 0.0f)
colormodg *= -1;
if(blue >= 1.0f || blue <= 0.0f)
colormodb *= -1;
}
void DrawScene()
{
//Clear backbuffer
D3DXCOLOR bgColor(red, green, blue, 1.0f);
d3d11DevCon->ClearRenderTargetView(renderTargetView, bgColor);
///draw the triangled
d3d11DevCon->Draw(3,0);
SwapChain->Present(0,0);
}
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
UpdateScene();
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);
}
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);
}