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 "d3d10app.h"
29 #include "stdio.h"
30 
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;
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	= "d3d10";
77 	wcex.hIconSm		= 0;
78 
79 	RegisterClassExA(&wcex);
80 
81 	HWND hwnd = CreateWindowA("d3d10", "d3d10", 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 	D3D10_FEATURE_LEVEL1 feature_level = D3D10_FEATURE_LEVEL_10_0;
107 
108 	HRESULT hr;
109 	if(1)
110 	{
111 		hr = D3D10CreateDeviceAndSwapChain(
112 			NULL,
113 			D3D10_DRIVER_TYPE_HARDWARE,
114 			NULL,
115 			D3D10_CREATE_DEVICE_SINGLETHREADED, // | D3D10_CREATE_DEVICE_DEBUG,
116 			D3D10_SDK_VERSION,
117 			&swap_chain_desc,
118 			&swap_chain,
119 			&dev);
120 	}
121 	else
122 	{
123 		hr = D3D10CreateDeviceAndSwapChain1(
124 			NULL,
125 			D3D10_DRIVER_TYPE_HARDWARE,
126 			NULL,
127 			D3D10_CREATE_DEVICE_SINGLETHREADED, // | D3D10_CREATE_DEVICE_DEBUG,
128 			feature_level,
129 			D3D10_SDK_VERSION,
130 			&swap_chain_desc,
131 			&swap_chain,
132 			(ID3D10Device1**)&dev);
133 	}
134 
135 	if(!SUCCEEDED(hr))
136 	{
137 		fprintf(stderr, "Failed to create D3D10 device (hresult %08x)\n", hr);
138 		return 1;
139 	}
140 
141 	ctx = dev;
142 
143 	app = d3d10_application_create();
144 	if(!app->init(dev, argc, argv))
145 		return 1;
146 
147 	ShowWindow(hwnd, SW_SHOWDEFAULT);
148 	UpdateWindow(hwnd);
149 
150 	LARGE_INTEGER freq;
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;
156 
157 	MSG msg;
158 	for(;;)
159 	{
160 		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
161 		{
162 			if(msg.message == WM_QUIT)
163 				break;
164 			TranslateMessage(&msg);
165 			DispatchMessage(&msg);
166 		}
167 		else if(width && height)
168 		{
169 			ID3D10Texture2D* tex;
170 			static ID3D10RenderTargetView* rtv;
171 			ensure(swap_chain->GetBuffer(0, __uuidof(tex), (void**)&tex));
172 			ensure(dev->CreateRenderTargetView(tex, NULL, &rtv));
173 
174 			QueryPerformanceCounter(&ctime_li);
175 			double ctime = (double)ctime_li.QuadPart * period - start_time;
176 
177 			app->draw(ctx, rtv, width, height, ctime);
178 			ctx->OMSetRenderTargets(0, 0, 0);
179 
180 			swap_chain->Present(0, 0);
181 			rtv->Release();
182 			tex->Release();
183 		}
184 		else
185 			WaitMessage();
186 	}
187 	return (int) msg.wParam;
188 }
189