1 /**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
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:
12 *
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.
16 *
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.
24 *
25 **************************************************************************/
26
27 #define INITGUID
28 #include "d3d11app.h"
29 #include "stdio.h"
30
31 static d3d11_application* app;
32 static IDXGISwapChain* swap_chain;
33 static unsigned width, height;
34 static DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
35 static ID3D11Device* dev;
36 static ID3D11DeviceContext* ctx;
37 static int frames = 0;
38 static int buffer_count = 1;
39
WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)40 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
41 {
42 switch (message)
43 {
44 case WM_SIZE:
45 width = lParam & 0xffff;
46 height = lParam >> 16;
47
48 swap_chain->ResizeBuffers(buffer_count, width, height, format, 0);
49 frames = 0;
50 break;
51 case WM_DESTROY:
52 PostQuitMessage(0);
53 break;
54 default:
55 return DefWindowProc(hwnd, message, wParam, lParam);
56 }
57 return 0;
58 }
59
main(int argc,char ** argv)60 int main(int argc, char** argv)
61 {
62 HINSTANCE hInstance = GetModuleHandle(NULL);
63 WNDCLASSEXA wcex;
64
65 wcex.cbSize = sizeof(WNDCLASSEX);
66
67 wcex.style = CS_HREDRAW | CS_VREDRAW;
68 wcex.lpfnWndProc = WndProc;
69 wcex.cbClsExtra = 0;
70 wcex.cbWndExtra = 0;
71 wcex.hInstance = hInstance;
72 wcex.hIcon = 0;
73 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
74 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
75 wcex.lpszMenuName = 0;
76 wcex.lpszClassName = "d3d11";
77 wcex.hIconSm = 0;
78
79 RegisterClassExA(&wcex);
80
81 HWND hwnd = CreateWindowA("d3d11", "d3d11", WS_OVERLAPPEDWINDOW,
82 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
83
84 if(!hwnd)
85 return FALSE;
86
87 RECT rc;
88 GetClientRect(hwnd, &rc );
89 width = rc.right - rc.left;
90 height = rc.bottom - rc.top;
91
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;
105
106 D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_10_0;
107
108 HRESULT hr = D3D11CreateDeviceAndSwapChain(
109 NULL,
110 D3D_DRIVER_TYPE_HARDWARE,
111 NULL,
112 D3D11_CREATE_DEVICE_SINGLETHREADED, // | D3D11_CREATE_DEVICE_DEBUG,
113 NULL,
114 0,
115 D3D11_SDK_VERSION,
116 &swap_chain_desc,
117 &swap_chain,
118 &dev,
119 &feature_level,
120 &ctx);
121 if(!SUCCEEDED(hr))
122 {
123 fprintf(stderr, "Failed to create D3D11 device (hresult %08x)\n", hr);
124 return 1;
125 }
126
127 app = d3d11_application_create();
128 if(!app->init(dev, argc, argv))
129 return 1;
130
131 ShowWindow(hwnd, SW_SHOWDEFAULT);
132 UpdateWindow(hwnd);
133
134 LARGE_INTEGER freq;
135 QueryPerformanceFrequency(&freq);
136 double period = 1.0 / (double)freq.QuadPart;
137 LARGE_INTEGER ctime_li;
138 QueryPerformanceCounter(&ctime_li);
139 double start_time = ctime_li.QuadPart * period;
140
141 MSG msg;
142 for(;;)
143 {
144 if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
145 {
146 if(msg.message == WM_QUIT)
147 break;
148 TranslateMessage(&msg);
149 DispatchMessage(&msg);
150 }
151 else if(width && height)
152 {
153 ID3D11Texture2D* tex;
154 static ID3D11RenderTargetView* rtv;
155 ensure(swap_chain->GetBuffer(0, __uuidof(tex), (void**)&tex));
156 ensure(dev->CreateRenderTargetView(tex, NULL, &rtv));
157
158 QueryPerformanceCounter(&ctime_li);
159 double ctime = (double)ctime_li.QuadPart * period - start_time;
160
161 app->draw(ctx, rtv, width, height, ctime);
162 ctx->OMSetRenderTargets(0, 0, 0);
163
164 swap_chain->Present(0, 0);
165 rtv->Release();
166 tex->Release();
167 }
168 else
169 WaitMessage();
170 }
171 return (int) msg.wParam;
172 }
173