d3dut

diff src/d3dut.cc @ 0:ecc040281dc9

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Jun 2013 10:11:39 +0300
parents
children 242535442d04
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/d3dut.cc	Sat Jun 22 10:11:39 2013 +0300
     1.3 @@ -0,0 +1,255 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <vector>
     1.7 +#include "d3dut.h"
     1.8 +#include "win.h"
     1.9 +#include "logmsg.h"
    1.10 +
    1.11 +static void d3dut_cleanup();
    1.12 +
    1.13 +D3DUTAPI ID3D11Device *d3dut_dev;
    1.14 +D3DUTAPI ID3D11DeviceContext *d3dut_ctx;
    1.15 +D3DUTAPI ID3D11RenderTargetView *d3dut_rtview;
    1.16 +
    1.17 +static int init_xsz = 640;
    1.18 +static int init_ysz = 480;
    1.19 +static int init_dmflags = 0;
    1.20 +
    1.21 +static long init_time = -1;
    1.22 +
    1.23 +static D3DUT_IdleFunc idle_func;
    1.24 +
    1.25 +void D3DUTAPI d3dut_init(int *argc, char **argv)
    1.26 +{
    1.27 +	if(init_time >= 0) {
    1.28 +		warning("already initialized!\n");
    1.29 +		return;
    1.30 +	}
    1.31 +
    1.32 +	WNDCLASS wclass;
    1.33 +	memset(&wclass, 0, sizeof wclass);
    1.34 +	wclass.hInstance = GetModuleHandle(0);
    1.35 +	wclass.lpfnWndProc = win_handle_event;
    1.36 +	wclass.lpszClassName = WINCLASSNAME;
    1.37 +	wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    1.38 +	wclass.hIcon = LoadIcon(0, IDI_APPLICATION);
    1.39 +	wclass.hCursor = LoadCursor(0, IDC_ARROW);
    1.40 +	wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    1.41 +	RegisterClass(&wclass);
    1.42 +
    1.43 +	// create D3D device
    1.44 +	D3D_FEATURE_LEVEL feature_level[] = {
    1.45 +		D3D_FEATURE_LEVEL_11_0,
    1.46 +		D3D_FEATURE_LEVEL_10_1,
    1.47 +		D3D_FEATURE_LEVEL_10_0
    1.48 +	};
    1.49 +	if(D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, feature_level, 3, D3D11_SDK_VERSION,
    1.50 +			&d3dut_dev, 0, &d3dut_ctx) != 0) {
    1.51 +		fatal_error("failed to create D3D11 device\n");
    1.52 +	}
    1.53 +	atexit(d3dut_cleanup);
    1.54 +
    1.55 +	init_time = timeGetTime();
    1.56 +}
    1.57 +
    1.58 +static void d3dut_cleanup()
    1.59 +{
    1.60 +	for(size_t i=0; i<windows.size(); i++) {
    1.61 +		if(windows[i]) {
    1.62 +			destroy_window(i);
    1.63 +		}
    1.64 +	}
    1.65 +	windows.clear();
    1.66 +
    1.67 +	if(d3dut_dev) {
    1.68 +		d3dut_dev->Release();
    1.69 +		d3dut_dev = 0;
    1.70 +	}
    1.71 +	if(d3dut_ctx) {
    1.72 +		d3dut_ctx->Release();
    1.73 +		d3dut_ctx = 0;
    1.74 +	}
    1.75 +	d3dut_rtview = 0;
    1.76 +
    1.77 +	UnregisterClass(WINCLASSNAME, GetModuleHandle(0));
    1.78 +	init_time = -1;
    1.79 +}
    1.80 +
    1.81 +void D3DUTAPI d3dut_init_display_mode(unsigned int dmflags)
    1.82 +{
    1.83 +	init_dmflags = dmflags;
    1.84 +}
    1.85 +
    1.86 +void D3DUTAPI d3dut_init_window_size(int xsz, int ysz)
    1.87 +{
    1.88 +	init_xsz = xsz;
    1.89 +	init_ysz = ysz;
    1.90 +}
    1.91 +
    1.92 +
    1.93 +int D3DUTAPI d3dut_create_window(const char *title)
    1.94 +{
    1.95 +	return create_window(title, init_xsz, init_ysz, init_dmflags);
    1.96 +}
    1.97 +
    1.98 +void D3DUTAPI d3dut_destroy_window(int win)
    1.99 +{
   1.100 +	destroy_window(win);
   1.101 +}
   1.102 +
   1.103 +void D3DUTAPI d3dut_set_window(int idx)
   1.104 +{
   1.105 +	set_active_win(idx);
   1.106 +}
   1.107 +
   1.108 +int D3DUTAPI d3dut_get_window()
   1.109 +{
   1.110 +	return get_active_win();
   1.111 +}
   1.112 +
   1.113 +void D3DUTAPI d3dut_display_func(D3DUT_DisplayFunc func)
   1.114 +{
   1.115 +	Window *win = get_window();
   1.116 +	win->display_func = func;
   1.117 +}
   1.118 +
   1.119 +void D3DUTAPI d3dut_idle_func(D3DUT_IdleFunc func)
   1.120 +{
   1.121 +	idle_func = func;
   1.122 +}
   1.123 +
   1.124 +void D3DUTAPI d3dut_reshape_func(D3DUT_ReshapeFunc func)
   1.125 +{
   1.126 +	Window *win = get_window();
   1.127 +	win->reshape_func = func;
   1.128 +}
   1.129 +
   1.130 +void D3DUTAPI d3dut_keyboard_func(D3DUT_KeyboardFunc func)
   1.131 +{
   1.132 +	Window *win = get_window();
   1.133 +	win->keyboard_func = func;
   1.134 +}
   1.135 +
   1.136 +void D3DUTAPI d3dut_keyboard_up_func(D3DUT_KeyboardUpFunc func)
   1.137 +{
   1.138 +	Window *win = get_window();
   1.139 +	win->keyboard_up_func = func;
   1.140 +}
   1.141 +
   1.142 +void D3DUTAPI d3dut_special_func(D3DUT_SpecialFunc func)
   1.143 +{
   1.144 +	Window *win = get_window();
   1.145 +	win->special_func = func;
   1.146 +}
   1.147 +
   1.148 +void D3DUTAPI d3dut_special_up_func(D3DUT_SpecialUpFunc func)
   1.149 +{
   1.150 +	Window *win = get_window();
   1.151 +	win->special_up_func = func;
   1.152 +}
   1.153 +
   1.154 +void D3DUTAPI d3dut_mouse_func(D3DUT_MouseFunc func)
   1.155 +{
   1.156 +	Window *win = get_window();
   1.157 +	win->mouse_func = func;
   1.158 +}
   1.159 +
   1.160 +void D3DUTAPI d3dut_motion_func(D3DUT_MotionFunc func)
   1.161 +{
   1.162 +	Window *win = get_window();
   1.163 +	win->motion_func = func;
   1.164 +}
   1.165 +
   1.166 +void D3DUTAPI d3dut_passive_motion_func(D3DUT_PassiveMotionFunc func)
   1.167 +{
   1.168 +	Window *win = get_window();
   1.169 +	win->passive_motion_func = func;
   1.170 +}
   1.171 +
   1.172 +
   1.173 +void D3DUTAPI d3dut_post_redisplay()
   1.174 +{
   1.175 +	Window *win = get_window();
   1.176 +	win->must_redisplay = true;
   1.177 +}
   1.178 +
   1.179 +void D3DUTAPI d3dut_swap_buffers()
   1.180 +{
   1.181 +	Window *win = get_window();
   1.182 +	win->swap->Present(0, 0);
   1.183 +}
   1.184 +
   1.185 +void D3DUTAPI d3dut_main_loop()
   1.186 +{
   1.187 +	MSG msg;
   1.188 +
   1.189 +	for(;;) {
   1.190 +		bool must_redisplay = false;
   1.191 +		for(size_t i=0; i<windows.size(); i++) {
   1.192 +			Window *win = windows[i];
   1.193 +			if(win->changed_size && win->reshape_func) {
   1.194 +				win->changed_size = false;
   1.195 +				set_active_win(i);
   1.196 +				win->reshape_func(win->width, win->height);
   1.197 +			}
   1.198 +			if(win->must_redisplay) {
   1.199 +				must_redisplay = true;
   1.200 +			}
   1.201 +		}
   1.202 +
   1.203 +		if(idle_func || must_redisplay) {
   1.204 +			while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
   1.205 +				TranslateMessage(&msg);
   1.206 +				DispatchMessage(&msg);
   1.207 +				if(msg.message == WM_QUIT) {
   1.208 +					return;
   1.209 +				}
   1.210 +			}
   1.211 +
   1.212 +			if(idle_func) { // checking again because a handler might have set this to 0
   1.213 +				idle_func();
   1.214 +			}
   1.215 +		} else {
   1.216 +			if(!GetMessage(&msg, 0, 0, 0)) {
   1.217 +				return;
   1.218 +			}
   1.219 +			TranslateMessage(&msg);
   1.220 +			DispatchMessage(&msg);
   1.221 +		}
   1.222 +
   1.223 +		for(size_t i=0; i<windows.size(); i++) {
   1.224 +			Window *win = windows[i];
   1.225 +			if(win->must_redisplay && win->display_func) {
   1.226 +				win->must_redisplay = false;
   1.227 +				set_active_win(i);
   1.228 +				win->display_func();
   1.229 +				ValidateRect(win->win, 0);
   1.230 +			}
   1.231 +		}
   1.232 +	}
   1.233 +}
   1.234 +
   1.235 +
   1.236 +int D3DUTAPI d3dut_get(unsigned int what)
   1.237 +{
   1.238 +	Window *win = get_window();
   1.239 +
   1.240 +	switch(what) {
   1.241 +	case D3DUT_WINDOW_WIDTH:
   1.242 +		return win->width;
   1.243 +	case D3DUT_WINDOW_HEIGHT:
   1.244 +		return win->height;
   1.245 +
   1.246 +	case D3DUT_ELAPSED_TIME:
   1.247 +		return (long)timeGetTime() - init_time;
   1.248 +
   1.249 +	default:
   1.250 +		break;
   1.251 +	}
   1.252 +	return 0;
   1.253 +}
   1.254 +
   1.255 +
   1.256 +void D3DUTAPI d3dut_solid_sphere(double radius, int slices, int stacks)
   1.257 +{
   1.258 +}