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
|
#include <D3D10.h>
#include <D3DX10.h>
#include <Windows.h>
#pragma comment (lib,"d3d10.lib")
#pragma comment (lib,"d3dx10.lib")
#pragma comment (lib, "dxgi.lib")
void ReportLastError()
{
LPCTSTR pszCaption = TEXT("Windows SDK Error Report");
DWORD dwError = GetLastError();
if(NOERROR == dwError)
{
MessageBox(NULL, TEXT("No error"), pszCaption, MB_OK);
}
else
{
const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM;
LPVOID pTextBuffer = NULL;
DWORD dwCount = FormatMessage(dwFormatControl,
NULL,
dwError,
0,
(LPTSTR) &pTextBuffer,
0,
NULL);
if(0 != dwCount)
{
MessageBox(NULL, (LPCTSTR)pTextBuffer, pszCaption, MB_OK|MB_ICONERROR);
LocalFree(pTextBuffer);
}
else
{
MessageBox(NULL, TEXT("Unknown error"), pszCaption, MB_OK|MB_ICONERROR);
}
}
}
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
class App{
public:
bool initialize(HWND,int,int);
private:
IDXGISwapChain *m_swapChain;
ID3D10Device *m_device;
ID3D10RenderTargetView *m_renderTargetView;
};
bool App::initialize(HWND hwnd,int scHeight, int scWidth){
HRESULT result;
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ID3D10Texture2D *pBackBuffer;
D3D10_VIEWPORT vs={0,0,scWidth,scHeight,0.0f,1.0f};
ZeroMemory (&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
swapChainDesc.BufferCount=1;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.Height = scHeight;
swapChainDesc.BufferDesc.Width = scWidth;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.OutputWindow = hwnd;
result = D3D10CreateDeviceAndSwapChain (NULL, D3D10_DRIVER_TYPE_HARDWARE, NULL,0, D3D10_SDK_VERSION, &swapChainDesc, &m_swapChain, &m_device);
if (FAILED(result)){
ReportLastError();
return false;
}
result = m_swapChain->GetBuffer (0,__uuidof(ID3D10Texture2D),(LPVOID*)&pBackBuffer);
if(FAILED(result)){
MessageBox(NULL,L"buffer",NULL,NULL);
return false;
}
result = m_device->CreateRenderTargetView(pBackBuffer, NULL, &m_renderTargetView);
if(FAILED (result)){
MessageBox(NULL,L"render view",NULL,NULL);
return false;
}
pBackBuffer->Release();
pBackBuffer = 0;
m_device->OMSetRenderTargets(1, &m_renderTargetView, NULL);
m_device->RSSetViewports (1, &vs);
return true;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
App *app;
HWND hwnd;
int screenWidth = 800, screenHeight= 600;
LPCWSTR szName=L"leClass";
MSG msg;
bool done = false;
WNDCLASS wc;
app= new App;
wc.lpfnWndProc = WndProc;
wc.hInstance = GetModuleHandle(NULL);
wc.hbrBackground = (HBRUSH)(GetStockObject(BLACK_BRUSH));
wc.lpszClassName = szName;
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.style = CS_HREDRAW| CS_VREDRAW;
RegisterClass (&wc);
hwnd = CreateWindow (szName, L"My Project", WS_BORDER | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, screenWidth, screenHeight, NULL,NULL, hInstance, NULL);
if(!hwnd){
MessageBox(NULL,L"gay",L"error",NULL);
}
ShowWindow (hwnd, SW_SHOW);
UpdateWindow(hwnd);
if(!app->initialize(hwnd,screenHeight,screenWidth)){
return -1;
}
ZeroMemory(&msg, sizeof(MSG));
while(!done){
PeekMessage(&msg,NULL,0,0,PM_REMOVE);
if(msg.message == WM_QUIT){
done = true;
}
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return 0;
}
static LRESULT CALLBACK WndProc (HWND hwnd,UINT msg, WPARAM wParam,LPARAM lParam){
switch (msg){
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
|