d3dut

view src/d3dut.cc @ 1:242535442d04

added license, readme, and gpl headers
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Jun 2013 10:43:12 +0300
parents ecc040281dc9
children
line source
1 /*
2 D3DUT - Simple window creation and event handling for Direct3D 11 applications.
3 Copyright (C) 2013 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <vector>
21 #include "d3dut.h"
22 #include "win.h"
23 #include "logmsg.h"
25 static void d3dut_cleanup();
27 D3DUTAPI ID3D11Device *d3dut_dev;
28 D3DUTAPI ID3D11DeviceContext *d3dut_ctx;
29 D3DUTAPI ID3D11RenderTargetView *d3dut_rtview;
31 static int init_xsz = 640;
32 static int init_ysz = 480;
33 static int init_dmflags = 0;
35 static long init_time = -1;
37 static D3DUT_IdleFunc idle_func;
39 void D3DUTAPI d3dut_init(int *argc, char **argv)
40 {
41 if(init_time >= 0) {
42 warning("already initialized!\n");
43 return;
44 }
46 WNDCLASS wclass;
47 memset(&wclass, 0, sizeof wclass);
48 wclass.hInstance = GetModuleHandle(0);
49 wclass.lpfnWndProc = win_handle_event;
50 wclass.lpszClassName = WINCLASSNAME;
51 wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
52 wclass.hIcon = LoadIcon(0, IDI_APPLICATION);
53 wclass.hCursor = LoadCursor(0, IDC_ARROW);
54 wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
55 RegisterClass(&wclass);
57 // create D3D device
58 D3D_FEATURE_LEVEL feature_level[] = {
59 D3D_FEATURE_LEVEL_11_0,
60 D3D_FEATURE_LEVEL_10_1,
61 D3D_FEATURE_LEVEL_10_0
62 };
63 if(D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, feature_level, 3, D3D11_SDK_VERSION,
64 &d3dut_dev, 0, &d3dut_ctx) != 0) {
65 fatal_error("failed to create D3D11 device\n");
66 }
67 atexit(d3dut_cleanup);
69 init_time = timeGetTime();
70 }
72 static void d3dut_cleanup()
73 {
74 for(size_t i=0; i<windows.size(); i++) {
75 if(windows[i]) {
76 destroy_window(i);
77 }
78 }
79 windows.clear();
81 if(d3dut_dev) {
82 d3dut_dev->Release();
83 d3dut_dev = 0;
84 }
85 if(d3dut_ctx) {
86 d3dut_ctx->Release();
87 d3dut_ctx = 0;
88 }
89 d3dut_rtview = 0;
91 UnregisterClass(WINCLASSNAME, GetModuleHandle(0));
92 init_time = -1;
93 }
95 void D3DUTAPI d3dut_init_display_mode(unsigned int dmflags)
96 {
97 init_dmflags = dmflags;
98 }
100 void D3DUTAPI d3dut_init_window_size(int xsz, int ysz)
101 {
102 init_xsz = xsz;
103 init_ysz = ysz;
104 }
107 int D3DUTAPI d3dut_create_window(const char *title)
108 {
109 return create_window(title, init_xsz, init_ysz, init_dmflags);
110 }
112 void D3DUTAPI d3dut_destroy_window(int win)
113 {
114 destroy_window(win);
115 }
117 void D3DUTAPI d3dut_set_window(int idx)
118 {
119 set_active_win(idx);
120 }
122 int D3DUTAPI d3dut_get_window()
123 {
124 return get_active_win();
125 }
127 void D3DUTAPI d3dut_display_func(D3DUT_DisplayFunc func)
128 {
129 Window *win = get_window();
130 win->display_func = func;
131 }
133 void D3DUTAPI d3dut_idle_func(D3DUT_IdleFunc func)
134 {
135 idle_func = func;
136 }
138 void D3DUTAPI d3dut_reshape_func(D3DUT_ReshapeFunc func)
139 {
140 Window *win = get_window();
141 win->reshape_func = func;
142 }
144 void D3DUTAPI d3dut_keyboard_func(D3DUT_KeyboardFunc func)
145 {
146 Window *win = get_window();
147 win->keyboard_func = func;
148 }
150 void D3DUTAPI d3dut_keyboard_up_func(D3DUT_KeyboardUpFunc func)
151 {
152 Window *win = get_window();
153 win->keyboard_up_func = func;
154 }
156 void D3DUTAPI d3dut_special_func(D3DUT_SpecialFunc func)
157 {
158 Window *win = get_window();
159 win->special_func = func;
160 }
162 void D3DUTAPI d3dut_special_up_func(D3DUT_SpecialUpFunc func)
163 {
164 Window *win = get_window();
165 win->special_up_func = func;
166 }
168 void D3DUTAPI d3dut_mouse_func(D3DUT_MouseFunc func)
169 {
170 Window *win = get_window();
171 win->mouse_func = func;
172 }
174 void D3DUTAPI d3dut_motion_func(D3DUT_MotionFunc func)
175 {
176 Window *win = get_window();
177 win->motion_func = func;
178 }
180 void D3DUTAPI d3dut_passive_motion_func(D3DUT_PassiveMotionFunc func)
181 {
182 Window *win = get_window();
183 win->passive_motion_func = func;
184 }
187 void D3DUTAPI d3dut_post_redisplay()
188 {
189 Window *win = get_window();
190 win->must_redisplay = true;
191 }
193 void D3DUTAPI d3dut_swap_buffers()
194 {
195 Window *win = get_window();
196 win->swap->Present(0, 0);
197 }
199 void D3DUTAPI d3dut_main_loop()
200 {
201 MSG msg;
203 for(;;) {
204 bool must_redisplay = false;
205 for(size_t i=0; i<windows.size(); i++) {
206 Window *win = windows[i];
207 if(win->changed_size && win->reshape_func) {
208 win->changed_size = false;
209 set_active_win(i);
210 win->reshape_func(win->width, win->height);
211 }
212 if(win->must_redisplay) {
213 must_redisplay = true;
214 }
215 }
217 if(idle_func || must_redisplay) {
218 while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
219 TranslateMessage(&msg);
220 DispatchMessage(&msg);
221 if(msg.message == WM_QUIT) {
222 return;
223 }
224 }
226 if(idle_func) { // checking again because a handler might have set this to 0
227 idle_func();
228 }
229 } else {
230 if(!GetMessage(&msg, 0, 0, 0)) {
231 return;
232 }
233 TranslateMessage(&msg);
234 DispatchMessage(&msg);
235 }
237 for(size_t i=0; i<windows.size(); i++) {
238 Window *win = windows[i];
239 if(win->must_redisplay && win->display_func) {
240 win->must_redisplay = false;
241 set_active_win(i);
242 win->display_func();
243 ValidateRect(win->win, 0);
244 }
245 }
246 }
247 }
250 int D3DUTAPI d3dut_get(unsigned int what)
251 {
252 Window *win = get_window();
254 switch(what) {
255 case D3DUT_WINDOW_WIDTH:
256 return win->width;
257 case D3DUT_WINDOW_HEIGHT:
258 return win->height;
260 case D3DUT_ELAPSED_TIME:
261 return (long)timeGetTime() - init_time;
263 default:
264 break;
265 }
266 return 0;
267 }
270 void D3DUTAPI d3dut_solid_sphere(double radius, int slices, int stacks)
271 {
272 }