rayzor
view src/main.cc @ 11:fe94d9e986ae
optimized 16bpp swap_buffers
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 10 Apr 2014 08:42:33 +0300 |
parents | 235c8b764c0b |
children | d94a69933a71 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include "inttypes.h"
6 #include "gfx.h"
7 #include "keyb.h"
8 #include "mouse.h"
9 #include "logger.h"
10 #include "scene.h"
11 #include "rayzor.h"
12 #include "screen.h"
13 #include "modeller.h"
14 #include "renderer.h"
15 #include "scrman.h"
16 #include "timer.h"
18 #ifdef __DOS__
19 #undef USE_ASM_SWAPBUF
20 #endif
22 #ifdef USE_ASM_SWAPBUF
23 // defined in swapbuf.asm
24 extern "C" void swap_buffers_asm(void *dest, void *src, int xsz, int ysz, int bpp);
25 #endif
27 static bool init();
28 static void cleanup();
29 static void display();
30 static void swap_buffers();
31 static void draw_cursor(uint32_t *buf, int mx, int my);
32 static void handle_keyboard();
33 static void handle_mouse();
34 static bool parse_args(int argc, char **argv);
35 static void sig(int s);
37 uint32_t *fb_pixels;
38 int fb_width = 640;
39 int fb_height = 480;
40 int fb_bpp = 32;
41 Scene *scene;
43 static bool novideo;
44 static void *fb;
45 static int rbits, gbits, bbits;
46 static int rshift, gshift, bshift;
47 static unsigned int rmask, gmask, bmask;
49 static bool use_asm_swap = true;
50 static bool use_mouse;
51 static int mouse_x, mouse_y;
52 static bool quit;
54 int main(int argc, char **argv)
55 {
56 unsigned long start_msec, msec;
57 unsigned long nframes = 0;
59 if(!parse_args(argc, argv)) {
60 return 1;
61 }
62 if(!init()) {
63 return 1;
64 }
66 start_msec = get_msec();
68 // main loop
69 for(;;) {
70 handle_keyboard();
71 handle_mouse();
72 if(quit) break;
74 display();
75 ++nframes;
77 if(novideo) break;
78 }
80 msec = get_msec() - start_msec;
82 cleanup();
84 printf("Average framerate: %g\n", (float)nframes / ((float)msec / 1000.0f));
85 printf("Thank you for using Rayzor!\n");
86 return 0;
87 }
89 void quit_app()
90 {
91 quit = true;
92 }
94 static bool init()
95 {
96 signal(SIGINT, sig);
97 signal(SIGSEGV, sig);
98 signal(SIGILL, sig);
99 signal(SIGFPE, sig);
101 init_timer(128);
103 if(!novideo) {
104 if(kb_init(32) == -1) {
105 fprintf(stderr, "failed to initialize keyboard driver\n");
106 return false;
107 }
109 if(!(fb = set_video_mode(fb_width, fb_height, fb_bpp))) {
110 set_text_mode();
111 fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
112 return false;
113 }
114 fb_bpp = get_color_depth();
115 get_color_bits(&rbits, &gbits, &bbits);
116 get_color_shift(&rshift, &gshift, &bshift);
117 get_color_mask(&rmask, &gmask, &bmask);
119 printlog("video resolution: %dx%d\n", fb_width, fb_height);
120 printlog("bpp: %d (%d %d %d)\n", fb_bpp, rbits, gbits, bbits);
121 printlog("shift: %d %d %d\n", rshift, gshift, bshift);
122 printlog("mask: %x %x %x\n", rmask, gmask, bmask);
124 if(have_mouse()) {
125 use_mouse = true;
126 set_mouse_limits(0, 0, fb_width - 1, fb_height - 1);
127 }
128 } else {
129 logger_output(stdout);
130 printlog("novideo (debug) mode\n");
131 fb_bpp = 24;
132 rbits = gbits = bbits = 8;
133 }
135 fb_pixels = new uint32_t[fb_width * fb_height * 4];
136 if(!fb_pixels) {
137 return false;
138 }
140 scene = new Scene;
142 Sphere *sph = new Sphere;
143 scene->add_object(sph);
145 Modeller *modeller = new Modeller;
146 if(!modeller->init()) {
147 return false;
148 }
149 add_screen(modeller);
151 Renderer *renderer = new Renderer;
152 if(!renderer->init()) {
153 return false;
154 }
155 add_screen(renderer);
157 activate_screen(modeller); // start the modeller screen
158 return true;
159 }
161 static void cleanup()
162 {
163 delete scene;
164 delete [] fb_pixels;
166 destroy_screens();
168 if(!novideo) {
169 set_text_mode();
170 kb_shutdown();
171 }
172 }
174 static void display()
175 {
176 Screen *scr = active_screen();
177 if(scr) {
178 scr->update();
179 scr->draw();
180 }
182 // draw the mouse cursor
183 if(use_mouse) {
184 draw_cursor(fb_pixels, mouse_x, mouse_y);
185 }
187 if(!novideo) {
188 wait_vsync();
189 #ifdef USE_ASM_SWAPBUF
190 swap_buffers_asm(fb, fb_pixels, fb_width, fb_height, fb_bpp);
191 #else
192 swap_buffers();
193 #endif
194 }
195 }
197 #define PACK_RGB(r, g, b) \
198 ((((r) << rshift) & rmask) | \
199 (((g) << gshift) & gmask) | \
200 (((b) << bshift) & bmask))
202 #define UNPACK_RED(c) (((c) >> 16) & 0xff)
203 #define UNPACK_GREEN(c) (((c) >> 8) & 0xff)
204 #define UNPACK_BLUE(c) ((c) & 0xff)
206 static void swap_buffers()
207 {
208 uint32_t *src = fb_pixels;
209 int num_pixels = fb_width * fb_height;
211 switch(fb_bpp) {
212 case 32:
213 memcpy(fb, fb_pixels, num_pixels * 4);
214 break;
216 case 24:
217 {
218 unsigned char *dest = (unsigned char*)fb;
219 for(int i=0; i<num_pixels-1; i++) {
220 *((uint32_t*)dest) = *src++;
221 dest += 3;
222 }
223 *dest++ = UNPACK_RED(*src);
224 *dest++ = UNPACK_GREEN(*src);
225 *dest++ = UNPACK_BLUE(*src);
226 }
227 break;
229 case 16:
230 {
231 uint16_t *dest = (uint16_t*)fb;
232 for(int i=0; i<num_pixels; i++) {
233 uint32_t c = *src++;
234 unsigned char r = UNPACK_RED(c);
235 unsigned char g = UNPACK_GREEN(c);
236 unsigned char b = UNPACK_BLUE(c);
238 *dest++ = (((r) << 8) & 0xf800) |
239 (((g) << 3) & 0x7e0) |
240 (((b) >> 3) & 0x1f);
241 }
242 }
243 break;
245 default:
246 break;
247 }
248 }
250 static void draw_cursor(uint32_t *buf, int mx, int my)
251 {
252 uint32_t *cptr = buf + my * fb_width + mx;
253 int i, cw[2] = {4, 4}, ch[2] = {4, 4};
255 if(mx < cw[0]) cw[0] = mx;
256 if(my < ch[0]) ch[0] = my;
257 if(fb_width - mx < cw[1]) cw[1] = fb_width - mx - 1;
258 if(fb_height - my < ch[1]) ch[1] = fb_height - my - 1;
260 for(i=1; i<cw[0]; i++) {
261 int idx = -i;
262 cptr[idx] = 0xffffff;
263 }
264 for(i=1; i<cw[1]; i++) {
265 int idx = i;
266 cptr[idx] = 0xffffff;
267 }
268 for(i=1; i<ch[0]; i++) {
269 int idx = -i * fb_width;
270 cptr[idx] = 0xffffff;
271 }
272 for(i=1; i<ch[1]; i++) {
273 int idx = i * fb_width;
274 cptr[idx] = 0xffffff;
275 }
276 }
278 static void handle_keyboard()
279 {
280 int key;
281 Screen *scr = active_screen();
283 if(novideo) return;
285 while((key = kb_getkey()) != -1) {
286 switch(key) {
287 case '`':
288 use_asm_swap = !use_asm_swap;
289 break;
291 default:
292 break;
293 }
294 scr->handle_keyboard(key, true); // TODO also generate release events...
295 }
296 }
298 static void handle_mouse()
299 {
300 static int prev_mx, prev_my, prev_bnmask, bndiff;
301 int mx, my, bnmask;
302 Screen *scr = active_screen();
304 if(!use_mouse || novideo) return;
306 bnmask = read_mouse(&mx, &my);
307 if(scr && (bndiff = bnmask ^ prev_bnmask)) {
308 for(int i=0; i<8; i++) {
309 int bit = 1 << i;
310 if(bndiff & bit) {
311 scr->handle_mbutton(i, bnmask & bit, mx, my);
312 }
313 }
314 }
315 prev_bnmask = bnmask;
317 if(scr && (mx != prev_mx || my != prev_my)) {
318 scr->handle_mmotion(mx, my);
319 }
320 prev_mx = mx;
321 prev_my = my;
323 mouse_x = mx;
324 mouse_y = my;
325 }
328 static struct {
329 int opt;
330 const char *lopt;
331 const char *desc;
332 } options[] = {
333 {'s', "size", "resolution <xres>x<yres>[:bpp]"},
334 {'n', "novid", "don't switch video mode (for debugging)"},
335 {'h', "help", "print usage information and exit"},
336 {-1, 0, 0}
337 };
339 static void print_usage(const char *argv0)
340 {
341 printf("%s usage\n", argv0);
342 for(int i=0; options[i].opt != -1; i++) {
343 printf(" -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc);
344 }
345 exit(0);
346 }
348 static bool parse_args(int argc, char **argv)
349 {
350 for(int i=1; i<argc; i++) {
351 if(argv[i][0] == '-') {
352 int opt = -1;
354 for(int j=0; options[j].opt != -1; j++) {
355 if(argv[i][2] == 0) {
356 if(argv[i][1] == options[j].opt) {
357 opt = options[j].opt;
358 break;
359 }
360 } else {
361 if(strcmp(argv[i] + 1, options[j].lopt) == 0) {
362 opt = options[j].opt;
363 break;
364 }
365 }
366 }
368 switch(opt) {
369 case 's':
370 if(sscanf(argv[++i], "%dx%d:%d", &fb_width, &fb_height, &fb_bpp) < 2) {
371 fprintf(stderr, "%s must be followed by a resolution: WxH\n", argv[i - 1]);
372 return false;
373 }
374 break;
376 case 'n':
377 novideo = true;
378 break;
380 case 'h':
381 print_usage(argv[0]); // doesn't return
382 break;
384 default:
385 fprintf(stderr, "unknown option: %s\n", argv[i]);
386 return false;
387 }
388 } else {
389 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
390 return false;
391 }
392 }
393 return true;
394 }
396 static void sig(int s)
397 {
398 cleanup();
399 fprintf(stderr, "signal caught: %d\n", s);
400 exit(1);
401 }