1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
//Include Windows header files
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3dcompiler.h>
//#include <d3dx11.h> - Deprecated
//#include <d3dx10.h>
//include the Direct3D library file
#pragma comment (lib, "d3dll.lib")
//#pragma comment (lib, "d3dx11.lib")
//#pragma comment (lib, "d3dx10.lib")
//global declarations
IDXGISwapChain *swapChain;
ID3D11Device *dev;
ID3D11DeviceContext *devcon;
//function prototypes
void InitD3D(HWND hWnd);
void CleanD3D(void);
//The WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
//Entry point for any windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
//Handle for the window
HWND hWnd;
//struct holds information for the window class
WNDCLASSEX wc;
//Clear out the wndow class for use
ZeroMemory(&wc, sizeof(WNDCLASSEX));
//fill in the stuct with the needed information
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass1";
//register the window class
RegisterClassEx(&wc);
RECT wr = { 0,0,500,400 }; //Set the size, but not the position
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
//create the window and use the result as the handle
hWnd = CreateWindowEx(NULL, L"WindowClass1", L"First Windows Program",
WS_OVERLAPPEDWINDOW,
300,
300,
wr.right - wr.left, //width of the window
wr.bottom - wr.top, //Height of the window
NULL,
NULL,
hInstance,
NULL);
//display the window on the screen
ShowWindow(hWnd, nCmdShow);
//Setup and initialize Direct3D
InitD3D(hWnd);
//enter the main loop:
//this struct holds windows event messages
MSG msg = { 0 };
//wait for the next message in the queue - store the result in 'msg'
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
//Translate keystroke message into the right format
TranslateMessage(&msg);
//send the message to the WindowProc Function
DispatchMessage(&msg);
//check to see if it's time to quit
if (msg.message == WM_QUIT)
break;
}
else {
// Run Game Code...
// ...
// ...
}
}
//Clean up DirectX and COM
CleanD3D();
//return this part of the WM_QUIT message to windows
return msg.wParam;
}
//this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//sort through and find what code to run for the message given
switch (message)
{
//this message is read when the window is closed
case WM_DESTROY:
{
//Close the application entirely
PostQuitMessage(0);
return 0;
} break;
}
//Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);
}
//This function initializes and prepares Direct3D for use
void InitD3D(HWND hWnd)
{
//Create a struct to hold information about the swap chain
DXGI_SWAP_CHAIN_DESC scd;
//Clear out the struct for use
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
//Fill the swap chain description struct
scd.BufferCount = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = hWnd;
scd.SampleDesc.Count = 4;
scd.Windowed = TRUE;
//create a device, device context and swap chain using the information in the 'scd' struct
D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapChain,
&dev,
NULL,
&devcon);
}
//this is the function that cleans up Direct3D and COM
void CleanD3D(void)
{
//close and release all exisiting COM objects
swapChain->Release();
dev->Release();
devcon->Release();
}
|