-
-
Save anonymous/706d225d441914371b30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "BlankDemo.h" | |
BlankDemo::BlankDemo() { | |
} | |
BlankDemo::~BlankDemo() { | |
} | |
bool BlankDemo::LoadContent() { | |
return true; | |
} | |
void BlankDemo::UnloadContent() { | |
} | |
void BlankDemo::Update(float dt) { | |
} | |
void BlankDemo::Render() { | |
if(d3dContext_ == 0) | |
return; | |
float clearColor[4] = {0.0f, 0.0f, 0.25f, 1.0f}; | |
d3dContext_->ClearRenderTargetView(backBufferTarget_, clearColor); | |
swapChain_->Present(0, 0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _BLANK_DEMO_H_ | |
#define _BLANK_DEMO_H_ | |
#include "DX11DemoBase.h" | |
class BlankDemo : public DX11DemoBase { | |
public: | |
BlankDemo(); | |
virtual ~BlankDemo(); | |
bool LoadContent(); | |
void UnloadContent(); | |
void Update(float dt); | |
void Render(); | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DX11DemoBase.h" | |
DX11DemoBase::DX11DemoBase() : driverType_(D3D_DRIVER_TYPE_NULL), | |
featureLevel_(D3D_FEATURE_LEVEL_11_0), | |
d3dDevice_(0), | |
d3dContext_(0), | |
swapChain_(0), | |
backBufferTarget_(0) { } | |
DX11DemoBase::~DX11DemoBase() { Shutdown(); } | |
bool DX11DemoBase::LoadContent() { | |
//Override with demo specifics, if any... | |
return true; | |
} | |
void DX11DemoBase::UnloadContent() { | |
//Override with demo specifics, if any... | |
} | |
void DX11DemoBase::Shutdown() { | |
UnloadContent(); | |
if(backBufferTarget_) backBufferTarget_->Release(); | |
if(swapChain_) swapChain_->Release(); | |
if(d3dContext_) d3dContext_->Release(); | |
if(d3dDevice_) d3dDevice_->Release(); | |
d3dDevice_ = 0; | |
d3dContext_ = 0; | |
swapChain_ = 0; | |
backBufferTarget_ = 0; | |
} | |
bool DX11DemoBase::initialize(HINSTANCE hInstance, HWND hwnd) { | |
hInstance_ = hInstance; | |
hwnd_ = hwnd; | |
RECT dimensions; | |
GetClientRect(hwnd, &dimensions); | |
unsigned int width = dimensions.right - dimensions.left; | |
unsigned int height = dimensions.bottom - dimensions.top; | |
D3D_DRIVER_TYPE driverTypes[] = { | |
D3D_DRIVER_TYPE_HARDWARE, | |
D3D_DRIVER_TYPE_WARP, | |
D3D_DRIVER_TYPE_SOFTWARE | |
}; | |
unsigned int totalDriverTypes = ARRAYSIZE(driverTypes); | |
D3D_FEATURE_LEVEL featureLevels[] = { | |
D3D_FEATURE_LEVEL_11_0, | |
D3D_FEATURE_LEVEL_10_1, | |
D3D_FEATURE_LEVEL_10_0 | |
}; | |
unsigned int totalFeatureLevels = ARRAYSIZE(featureLevels); | |
DXGI_SWAP_CHAIN_DESC swapChainDesc; | |
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC)); | |
swapChainDesc.BufferCount = 1; | |
swapChainDesc.BufferDesc.Width = width; | |
swapChainDesc.BufferDesc.Height = height; | |
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | |
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60; | |
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1; | |
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; | |
swapChainDesc.OutputWindow = hwnd; | |
swapChainDesc.Windowed = true; | |
swapChainDesc.SampleDesc.Count = 1; | |
swapChainDesc.SampleDesc.Quality = 0; | |
unsigned int creationFlags = 0; | |
#if _DEBUG | |
creationFlags |= D3D11_CREATE_DEVICE_DEBUG; | |
#endif | |
HRESULT result; | |
unsigned int driver = 0; | |
for(driver = 0; driver < totalDriverTypes; ++driver) { | |
result = D3D11CreateDeviceAndSwapChain(0, | |
driverTypes[driver], | |
0, | |
creationFlags, | |
featureLevels, | |
totalFeatureLevels, | |
D3D11_SDK_VERSION, | |
&swapChainDesc, | |
&swapChain_, | |
&d3dDevice_, | |
&featureLevel_, | |
&d3dContext_); | |
if(SUCCEEDED(result)) { | |
driverType_ = driverTypes[driver]; | |
break; | |
} | |
if(FAILED(result)) { | |
DXTRACE_MSG("Failed to create the DirectX3D device!"); | |
return false; | |
} | |
} | |
ID3D11Texture2D *backBufferTexture; | |
result = swapChain_->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferTexture); | |
if(FAILED(result)) { | |
DXTRACE_MSG("Failed to get the swap chain back buffer!"); | |
return false; | |
} | |
result = d3dDevice_->CreateRenderTargetView(backBufferTexture, 0, &backBufferTarget_); | |
if(backBufferTexture) backBufferTexture->Release(); | |
if(FAILED(result)) { | |
DXTRACE_MSG("Failed to create the render target view!"); | |
return false; | |
} | |
d3dContext_->OMSetRenderTargets(1, &backBufferTarget_, 0); | |
D3D11_VIEWPORT viewport; | |
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT)); | |
viewport.Width = static_cast<float>(width); | |
viewport.Height = static_cast<float>(height); | |
viewport.MinDepth = 0.0f; | |
viewport.MaxDepth = 1.0f; | |
viewport.TopLeftX = 0.0f; | |
viewport.TopLeftY = 0.0f; | |
d3dContext_->RSSetViewports(1, &viewport); | |
return LoadContent(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _DX11DEMOBASE_H_ | |
#define _DX11DEMOBASE_H_ | |
//INCLUDES | |
#include <D3D11.h> | |
#include <D3DX11.h> | |
#include <DxErr.h> | |
class DX11DemoBase { | |
public: | |
DX11DemoBase(); | |
virtual ~DX11DemoBase(); | |
bool initialize(HINSTANCE hInstance, HWND hwnd); | |
void Shutdown(); | |
virtual bool LoadContent(); | |
virtual void UnloadContent(); | |
virtual void Update(float dt) = 0; | |
virtual void Render() = 0; | |
protected: | |
HINSTANCE hInstance_; | |
HWND hwnd_; | |
D3D_DRIVER_TYPE driverType_; | |
D3D_FEATURE_LEVEL featureLevel_; | |
ID3D11Device *d3dDevice_; | |
ID3D11DeviceContext *d3dContext_; | |
IDXGISwapChain *swapChain_; | |
ID3D11RenderTargetView *backBufferTarget_; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Windows.h> | |
#include <xnamath.h> | |
#include <memory> | |
#include "BlankDemo.h" | |
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); | |
struct VertexPos { | |
XMFLOAT3 pos; | |
} | |
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmdLine, int cmdShow) { | |
UNREFERENCED_PARAMETER( prevInstance ); | |
UNREFERENCED_PARAMETER( cmdLine ); | |
WNDCLASSEX wndClass = { 0 }; | |
wndClass.cbSize = sizeof( WNDCLASSEX ); | |
wndClass.style = CS_HREDRAW | CS_VREDRAW; | |
wndClass.lpfnWndProc = WndProc; | |
wndClass.hInstance = hInstance; | |
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); | |
wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); | |
wndClass.lpszMenuName = NULL; | |
wndClass.lpszClassName = "DX11BookWindowClass"; | |
if(!RegisterClassEx(&wndClass)) | |
return -1; | |
RECT rc = {0, 0, 640, 480}; | |
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); | |
HWND hwnd = CreateWindowA("DX11BookWindowClass", "Blank Win32 Window", | |
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, | |
rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, | |
hInstance, NULL); | |
if(!hwnd) | |
return -1; | |
ShowWindow(hwnd, cmdShow); | |
//************** DIRECTX 11 CODE GOES RIGHT HERE ******************** | |
std::auto_ptr<DX11DemoBase> demo(new BlankDemo()); | |
//Demo Initialize | |
bool result = demo->initialize(hInstance, hwnd); | |
//Error reporting if there is an issue | |
if(result == false) return -1; | |
VertexPos vertices[] = { | |
XMFLOAT3( 0.5f, 0.5f, 0.5f ), | |
XMFLOAT3( 0.5f, -0.5f, 0.5f ), | |
XMFLOAT3( -0.5f,- 0.5f, 0.5f ) | |
}; | |
D3D11_BUFFER_DESC vertexDesc; | |
ZeroMemory(&vertexDesc, sizeof(vertexDesc)); | |
vertexDesc.Usage = D3D11_USAGE_DEFAULT; | |
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; | |
vertexDesc.ByteWidth = sizeof(VertexPos) * 3; | |
D3D11_SUBRESOURCE_DATA resourceData; | |
ZeroMemory(&resourceData, sizeof(resourceData)); | |
ID3D11Buffer* vertexBuffer; | |
HRESULT result = demo->d3dDevice_->CreateBuffer(&vertexDesc, &resourceData, &vertexBuffer); | |
//************** | |
int counter = 0; | |
MSG msg = { 0 }; | |
while(msg.message != WM_QUIT) { | |
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { | |
TranslateMessage( &msg ); | |
DispatchMessage( &msg ); | |
} | |
else { | |
//************** DIRECTX 11 CODE GOES RIGHT HERE ******************** | |
demo->Update(0.0f); | |
demo->Render(); | |
//************** | |
} | |
} | |
//Demo Shutdown | |
demo->Shutdown(); | |
return static_cast<int>(msg.wParam); | |
} | |
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { | |
PAINTSTRUCT paintStruct; | |
HDC hDC; | |
switch(message) { | |
case WM_PAINT: | |
hDC = BeginPaint(hwnd, &paintStruct); | |
EndPaint(hwnd, &paintStruct); | |
break; | |
case WM_DESTROY: | |
PostQuitMessage(0); | |
break; | |
default: | |
return DefWindowProc(hwnd, message, wParam, lParam); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment