1
/**************************************************************************
3
* Copyright 2010 Luca Barbieri
5
* Permission is hereby granted, free of charge, to any person obtaining
6
* a copy of this software and associated documentation files (the
7
* "Software"), to deal in the Software without restriction, including
8
* without limitation the rights to use, copy, modify, merge, publish,
9
* distribute, sublicense, and/or sell copies of the Software, and to
10
* permit persons to whom the Software is furnished to do so, subject to
11
* the following conditions:
13
* The above copyright notice and this permission notice (including the
14
* next paragraph) shall be included in all copies or substantial
15
* portions of the Software.
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
* IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
**************************************************************************/
31
static d3d10_application* app;
32
static IDXGISwapChain* swap_chain;
33
static unsigned width, height;
34
static DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
35
static ID3D10Device* dev;
36
static ID3D10Device* ctx;
37
static int frames = 0;
38
static int buffer_count = 1;
40
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
45
width = lParam & 0xffff;
46
height = lParam >> 16;
48
swap_chain->ResizeBuffers(buffer_count, width, height, format, 0);
55
return DefWindowProc(hwnd, message, wParam, lParam);
60
int main(int argc, char** argv)
62
HINSTANCE hInstance = GetModuleHandle(NULL);
65
wcex.cbSize = sizeof(WNDCLASSEX);
67
wcex.style = CS_HREDRAW | CS_VREDRAW;
68
wcex.lpfnWndProc = WndProc;
71
wcex.hInstance = hInstance;
73
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
74
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
75
wcex.lpszMenuName = 0;
76
wcex.lpszClassName = "d3d10";
79
RegisterClassExA(&wcex);
81
HWND hwnd = CreateWindowA("d3d10", "d3d10", WS_OVERLAPPEDWINDOW,
82
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
88
GetClientRect(hwnd, &rc );
89
width = rc.right - rc.left;
90
height = rc.bottom - rc.top;
92
DXGI_SWAP_CHAIN_DESC swap_chain_desc;
93
memset(&swap_chain_desc, 0, sizeof(swap_chain_desc));
94
swap_chain_desc.BufferDesc.Width = width;
95
swap_chain_desc.BufferDesc.Height = height;
96
swap_chain_desc.BufferDesc.Format = format;
97
swap_chain_desc.SampleDesc.Count = 1;
98
swap_chain_desc.SampleDesc.Quality = 0;
99
swap_chain_desc.OutputWindow = hwnd;
100
swap_chain_desc.Windowed = TRUE;
101
swap_chain_desc.BufferCount = buffer_count;
102
swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
103
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
104
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
106
D3D10_FEATURE_LEVEL1 feature_level = D3D10_FEATURE_LEVEL_10_0;
111
hr = D3D10CreateDeviceAndSwapChain(
113
D3D10_DRIVER_TYPE_HARDWARE,
115
D3D10_CREATE_DEVICE_SINGLETHREADED, // | D3D10_CREATE_DEVICE_DEBUG,
123
hr = D3D10CreateDeviceAndSwapChain1(
125
D3D10_DRIVER_TYPE_HARDWARE,
127
D3D10_CREATE_DEVICE_SINGLETHREADED, // | D3D10_CREATE_DEVICE_DEBUG,
132
(ID3D10Device1**)&dev);
137
fprintf(stderr, "Failed to create D3D10 device (hresult %08x)\n", hr);
143
app = d3d10_application_create();
144
if(!app->init(dev, argc, argv))
147
ShowWindow(hwnd, SW_SHOWDEFAULT);
151
QueryPerformanceFrequency(&freq);
152
double period = 1.0 / (double)freq.QuadPart;
153
LARGE_INTEGER ctime_li;
154
QueryPerformanceCounter(&ctime_li);
155
double start_time = ctime_li.QuadPart * period;
160
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
162
if(msg.message == WM_QUIT)
164
TranslateMessage(&msg);
165
DispatchMessage(&msg);
167
else if(width && height)
169
ID3D10Texture2D* tex;
170
static ID3D10RenderTargetView* rtv;
171
ensure(swap_chain->GetBuffer(0, __uuidof(tex), (void**)&tex));
172
ensure(dev->CreateRenderTargetView(tex, NULL, &rtv));
174
QueryPerformanceCounter(&ctime_li);
175
double ctime = (double)ctime_li.QuadPart * period - start_time;
177
app->draw(ctx, rtv, width, height, ctime);
178
ctx->OMSetRenderTargets(0, 0, 0);
180
swap_chain->Present(0, 0);
187
return (int) msg.wParam;