deepstone

annotate src/test.c @ 21:00d84ab1ef26

switched to wacom
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 18:17:55 +0300
parents 777be77b6432
children 5ff8ce78059a
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@0 4 #include <signal.h>
nuclear@0 5 #include <conio.h>
nuclear@21 6 #include "wvga.h"
nuclear@0 7 #include "mingl.h"
nuclear@0 8 #include "timer.h"
nuclear@0 9 #include "mouse.h"
nuclear@1 10 #include "palman.h"
nuclear@3 11 #include "texture.h"
nuclear@0 12
nuclear@0 13 static int init(void);
nuclear@0 14 static void shutdown(void);
nuclear@0 15 static void redraw(void);
nuclear@0 16 static void draw_cursor(unsigned char *fb, int xsz, int ysz, int mx, int my, int cidx);
nuclear@0 17 static int keyb(char key);
nuclear@0 18 static void mouse_button(int bn, int x, int y);
nuclear@0 19 static void mouse_motion(int x, int y);
nuclear@0 20 static void sighandler(int s);
nuclear@0 21 static int parse_args(int argc, char **argv);
nuclear@0 22 static void print_perf(void);
nuclear@0 23
nuclear@0 24 static unsigned char *fbuf;
nuclear@0 25
nuclear@3 26 static struct texture *tex;
nuclear@4 27 static char *texfile;
nuclear@3 28
nuclear@1 29 static int white_base, red_base, green_base, blue_base;
nuclear@1 30 static int grad_range;
nuclear@1 31
nuclear@0 32 static int use_vsync = 1;
nuclear@0 33 static int under_windows = 0;
nuclear@0 34 static unsigned long num_frm;
nuclear@0 35
nuclear@0 36 enum { CUBE, SPHERE, TORUS, NUM_PRIMS };
nuclear@0 37 static int prim = SPHERE;
nuclear@0 38 static int auto_rotate = 1;
nuclear@9 39 static float cam_theta, cam_phi, cam_zoom = 4.0;
nuclear@0 40
nuclear@0 41 static int mx, my;
nuclear@0 42
nuclear@0 43 int main(int argc, char **argv)
nuclear@0 44 {
nuclear@0 45 int mbn, prev_mx = -1, prev_my = -1, prev_mbn = 0;
nuclear@0 46
nuclear@0 47 if(parse_args(argc, argv) == -1) {
nuclear@0 48 return 1;
nuclear@0 49 }
nuclear@0 50
nuclear@0 51 if(init() == -1) {
nuclear@0 52 return 1;
nuclear@0 53 }
nuclear@0 54
nuclear@0 55 reset_timer();
nuclear@0 56
nuclear@0 57 for(;;) {
nuclear@0 58 if(kbhit()) {
nuclear@0 59 if(keyb(getch()) == 0) {
nuclear@0 60 break;
nuclear@0 61 }
nuclear@0 62 }
nuclear@0 63
nuclear@0 64 mbn = read_mouse(&mx, &my);
nuclear@0 65 if(mbn != prev_mbn) {
nuclear@0 66 mouse_button(mbn, mx, my);
nuclear@0 67 prev_mbn = mbn;
nuclear@0 68 }
nuclear@0 69 if(mx != prev_mx || my != prev_my) {
nuclear@0 70 if(mbn) {
nuclear@0 71 mouse_motion(mx, my);
nuclear@0 72 }
nuclear@0 73 prev_mx = mx;
nuclear@0 74 prev_my = my;
nuclear@0 75 }
nuclear@0 76
nuclear@0 77 redraw();
nuclear@0 78 }
nuclear@0 79
nuclear@0 80 shutdown();
nuclear@0 81 print_perf();
nuclear@0 82 return 0;
nuclear@0 83 }
nuclear@0 84
nuclear@0 85 static int init(void)
nuclear@0 86 {
nuclear@0 87 int i;
nuclear@1 88 struct palm_color *pal;
nuclear@0 89
nuclear@0 90 init_timer(under_windows ? 0 : 100);
nuclear@0 91
nuclear@0 92 set_video_mode(0x13);
nuclear@0 93
nuclear@0 94 signal(SIGINT, sighandler);
nuclear@0 95 signal(SIGSEGV, sighandler);
nuclear@0 96 signal(SIGFPE, sighandler);
nuclear@0 97 signal(SIGILL, sighandler);
nuclear@0 98 signal(SIGABRT, sighandler);
nuclear@0 99
nuclear@10 100 have_mouse();
nuclear@10 101
nuclear@1 102
nuclear@4 103 if(mgl_init(320, 200) == -1) {
nuclear@4 104 fprintf(stderr, "mgl init failed\n");
nuclear@4 105 return -1;
nuclear@4 106 }
nuclear@4 107 fbuf = mgl_framebuffer();
nuclear@4 108
nuclear@4 109
nuclear@4 110 if(!texfile) {
nuclear@4 111 palm_add_color(255, 255, 255);
nuclear@4 112 palm_add_color(255, 0, 0);
nuclear@4 113 palm_add_color(0, 255, 0);
nuclear@4 114 palm_add_color(0, 0, 255);
nuclear@4 115 palm_build();
nuclear@4 116
nuclear@4 117 white_base = palm_color_base(255, 255, 255);
nuclear@4 118 red_base = palm_color_base(255, 0, 0);
nuclear@4 119 green_base = palm_color_base(0, 255, 0);
nuclear@4 120 blue_base = palm_color_base(0, 0, 255);
nuclear@4 121
nuclear@4 122 tex = tex_gen_checker(64, 64, 3, 3, red_base, blue_base);
nuclear@4 123 } else {
nuclear@4 124 if(!(tex = load_texture(texfile))) {
nuclear@4 125 return -1;
nuclear@4 126 }
nuclear@4 127
nuclear@4 128 palm_build();
nuclear@4 129 get_texture_pixels(tex);
nuclear@4 130
nuclear@4 131 mgl_enable(MGL_TEXTURE_2D);
nuclear@4 132 }
nuclear@4 133
nuclear@1 134 grad_range = palm_color_range();
nuclear@1 135
nuclear@1 136 pal = palm_palette();
nuclear@1 137 for(i=0; i<palm_palette_size(); i++) {
nuclear@21 138 set_pal_entry(i, pal[i].r, pal[i].g, pal[i].b);
nuclear@0 139 }
nuclear@0 140
nuclear@0 141 mgl_enable(MGL_CULL_FACE);
nuclear@9 142 mgl_enable(MGL_DEPTH_TEST);
nuclear@0 143 mgl_enable(MGL_SMOOTH);
nuclear@1 144 mgl_color_range(grad_range - 1); /* gradient range */
nuclear@0 145
nuclear@0 146 mgl_enable(MGL_LIGHTING);
nuclear@0 147 mgl_light_intensity(0, 1.0);
nuclear@13 148 mgl_light_position(0, -0.5, 0.5, 1, 0);
nuclear@0 149
nuclear@0 150 mgl_matrix_mode(MGL_PROJECTION);
nuclear@0 151 mgl_load_identity();
nuclear@0 152 mgl_perspective(45.0, 320.0 / 200.0, 0.5, 100.0);
nuclear@0 153
nuclear@3 154 mgl_teximage(tex->width, tex->height, tex->pixels);
nuclear@3 155
nuclear@0 156 return 0;
nuclear@0 157 }
nuclear@0 158
nuclear@0 159 static void shutdown(void)
nuclear@0 160 {
nuclear@0 161 mgl_free();
nuclear@0 162 set_video_mode(3);
nuclear@0 163 }
nuclear@0 164
nuclear@0 165 static void redraw(void)
nuclear@0 166 {
nuclear@0 167 float angle = get_msec() / 10.0;
nuclear@0 168 mgl_clear(0);
nuclear@9 169 mgl_clear_depth();
nuclear@0 170
nuclear@0 171 mgl_matrix_mode(MGL_MODELVIEW);
nuclear@0 172 mgl_load_identity();
nuclear@18 173 mgl_translate(0, 0, -cam_zoom);
nuclear@0 174 if(auto_rotate) {
nuclear@18 175 mgl_rotate(angle * 0.5, 1, 0, 0);
nuclear@0 176 mgl_rotate(angle, 0, 0, 1);
nuclear@0 177 } else {
nuclear@18 178 mgl_rotate(cam_phi, 1, 0, 0);
nuclear@0 179 mgl_rotate(cam_theta, 0, 1, 0);
nuclear@0 180 }
nuclear@0 181
nuclear@0 182 switch(prim) {
nuclear@0 183 case TORUS:
nuclear@1 184 mgl_index(green_base);
nuclear@0 185 mgl_torus(1.0, 0.25, 16, 8);
nuclear@0 186 break;
nuclear@0 187 case SPHERE:
nuclear@1 188 mgl_index(blue_base);
nuclear@0 189 mgl_sphere(1.0, 16, 8);
nuclear@0 190 break;
nuclear@0 191 case CUBE:
nuclear@1 192 mgl_index(red_base);
nuclear@0 193 mgl_cube(1.0);
nuclear@0 194 }
nuclear@0 195
nuclear@0 196 if(!auto_rotate) {
nuclear@1 197 draw_cursor(fbuf, 320, 200, mx, my, white_base + grad_range - 1);
nuclear@0 198 }
nuclear@0 199
nuclear@0 200 copy_frame(fbuf);
nuclear@0 201 if(use_vsync) {
nuclear@0 202 wait_vsync();
nuclear@0 203 }
nuclear@0 204 num_frm++;
nuclear@0 205 }
nuclear@0 206
nuclear@0 207 static void draw_cursor(unsigned char *fb, int xsz, int ysz, int mx, int my, int cidx)
nuclear@0 208 {
nuclear@0 209 static char img[] =
nuclear@0 210 "oo........"
nuclear@0 211 "oxo......."
nuclear@0 212 "oxxo......"
nuclear@0 213 "oxxxo....."
nuclear@0 214 "oxxxxo...."
nuclear@0 215 "oxxxxxo..."
nuclear@0 216 "oxxxxxxo.."
nuclear@0 217 "oxxxxxxxo."
nuclear@0 218 "oxxxxxxxxo"
nuclear@0 219 "oxxxxxoooo"
nuclear@0 220 "oxxoxxo..."
nuclear@0 221 "oxo.oxxo.."
nuclear@0 222 "oo..oxxo.."
nuclear@0 223 ".....oxxo."
nuclear@0 224 ".....oxxo."
nuclear@0 225 "......oo..";
nuclear@0 226 int i, j, w = 10, h = 16;
nuclear@0 227
nuclear@0 228 if(mx < 0 || my < 0) {
nuclear@0 229 return;
nuclear@0 230 }
nuclear@0 231 if(mx + w >= xsz) {
nuclear@0 232 w = xsz - mx;
nuclear@0 233 }
nuclear@0 234 if(my + h >= ysz) {
nuclear@0 235 h = ysz - my;
nuclear@0 236 }
nuclear@0 237
nuclear@0 238 fb += my * xsz + mx;
nuclear@0 239 for(i=0; i<h; i++) {
nuclear@0 240 for(j=0; j<w; j++) {
nuclear@0 241 char c = img[(i << 3) + (i << 1) + j];
nuclear@0 242 if(c != '.') {
nuclear@0 243 fb[j] = c == 'x' ? 0 : cidx;
nuclear@0 244 }
nuclear@0 245 }
nuclear@0 246 fb += xsz;
nuclear@0 247 }
nuclear@0 248 }
nuclear@0 249
nuclear@0 250 static int keyb(char key)
nuclear@0 251 {
nuclear@0 252 switch(key) {
nuclear@0 253 case 'q':
nuclear@0 254 case 27:
nuclear@0 255 return 0;
nuclear@0 256
nuclear@3 257 case 's':
nuclear@3 258 if(mgl_isenabled(MGL_SMOOTH)) {
nuclear@3 259 mgl_disable(MGL_SMOOTH);
nuclear@3 260 } else {
nuclear@3 261 mgl_enable(MGL_SMOOTH);
nuclear@3 262 }
nuclear@3 263 break;
nuclear@3 264
nuclear@3 265 case 't':
nuclear@3 266 if(mgl_isenabled(MGL_TEXTURE_2D)) {
nuclear@3 267 mgl_disable(MGL_TEXTURE_2D);
nuclear@3 268 } else {
nuclear@3 269 mgl_enable(MGL_TEXTURE_2D);
nuclear@3 270 }
nuclear@3 271 break;
nuclear@3 272
nuclear@0 273 case ' ':
nuclear@0 274 auto_rotate = !auto_rotate;
nuclear@0 275 break;
nuclear@0 276
nuclear@0 277 case 'p':
nuclear@0 278 prim = (prim + 1) % NUM_PRIMS;
nuclear@0 279 break;
nuclear@0 280
nuclear@0 281 default:
nuclear@0 282 break;
nuclear@0 283 }
nuclear@0 284 return 1;
nuclear@0 285 }
nuclear@0 286
nuclear@0 287 static int bnstate;
nuclear@0 288 static int prev_x, prev_y;
nuclear@0 289
nuclear@0 290 static void mouse_button(int bn, int x, int y)
nuclear@0 291 {
nuclear@0 292 bnstate = bn;
nuclear@0 293 prev_x = x;
nuclear@0 294 prev_y = y;
nuclear@0 295 }
nuclear@0 296
nuclear@0 297 static void mouse_motion(int x, int y)
nuclear@0 298 {
nuclear@0 299 int dx, dy;
nuclear@0 300
nuclear@0 301 dx = x - prev_x;
nuclear@0 302 dy = y - prev_y;
nuclear@0 303 prev_x = x;
nuclear@0 304 prev_y = y;
nuclear@0 305
nuclear@9 306 if(bnstate & MOUSE_LEFT) {
nuclear@0 307 cam_theta += dx;
nuclear@0 308 cam_phi += dy;
nuclear@0 309
nuclear@0 310 if(cam_phi > 90) cam_phi = 90;
nuclear@0 311 if(cam_phi < -90) cam_phi = -90;
nuclear@0 312 }
nuclear@9 313 if(bnstate & MOUSE_RIGHT) {
nuclear@9 314 cam_zoom += dy * 0.1;
nuclear@9 315 if(cam_zoom < 0.0) {
nuclear@9 316 cam_zoom = 0.0;
nuclear@9 317 }
nuclear@9 318 }
nuclear@0 319 }
nuclear@0 320
nuclear@0 321 static void sighandler(int s)
nuclear@0 322 {
nuclear@0 323 set_video_mode(3);
nuclear@0 324
nuclear@0 325 switch(s) {
nuclear@0 326 case SIGABRT:
nuclear@0 327 fprintf(stderr, "abort\n");
nuclear@0 328 break;
nuclear@0 329
nuclear@0 330 case SIGILL:
nuclear@0 331 fprintf(stderr, "illegal operation\n");
nuclear@0 332 break;
nuclear@0 333
nuclear@0 334 case SIGSEGV:
nuclear@0 335 fprintf(stderr, "segmentation fault\n");
nuclear@0 336 break;
nuclear@0 337
nuclear@0 338 case SIGINT:
nuclear@0 339 fprintf(stderr, "interrupted\n");
nuclear@0 340 break;
nuclear@0 341
nuclear@0 342 case SIGFPE:
nuclear@0 343 fprintf(stderr, "floating point exception\n");
nuclear@0 344 break;
nuclear@0 345
nuclear@0 346 default:
nuclear@0 347 fprintf(stderr, "unexpected signal\n");
nuclear@0 348 }
nuclear@0 349
nuclear@0 350 exit(1);
nuclear@0 351 }
nuclear@0 352
nuclear@0 353 static int parse_args(int argc, char **argv)
nuclear@0 354 {
nuclear@0 355 int i;
nuclear@0 356
nuclear@0 357 for(i=1; i<argc; i++) {
nuclear@0 358 if(argv[i][0] == '-') {
nuclear@0 359 if(argv[i][2] != 0) {
nuclear@0 360 goto invalid;
nuclear@0 361 }
nuclear@0 362 switch(argv[i][1]) {
nuclear@0 363 case 'a':
nuclear@0 364 auto_rotate = !auto_rotate;
nuclear@0 365 break;
nuclear@0 366
nuclear@4 367 case 't':
nuclear@4 368 texfile = argv[++i];
nuclear@4 369 break;
nuclear@4 370
nuclear@0 371 case 'v':
nuclear@0 372 use_vsync = !use_vsync;
nuclear@0 373 break;
nuclear@0 374
nuclear@0 375 case 'p':
nuclear@0 376 if(strcmp(argv[++i], "cube") == 0) {
nuclear@0 377 prim = CUBE;
nuclear@0 378 } else if(strcmp(argv[i], "sphere") == 0) {
nuclear@0 379 prim = SPHERE;
nuclear@0 380 } else if(strcmp(argv[i], "torus") == 0) {
nuclear@0 381 prim = TORUS;
nuclear@0 382 } else {
nuclear@0 383 goto invalid;
nuclear@0 384 }
nuclear@0 385 break;
nuclear@0 386
nuclear@0 387 case 'w':
nuclear@0 388 under_windows = 1;
nuclear@0 389 break;
nuclear@0 390
nuclear@0 391 case 'h':
nuclear@0 392 printf("Usage %s [options]\n", argv[0]);
nuclear@0 393 printf("options:\n");
nuclear@0 394 printf(" -p select one of (cube|sphere|torus)\n");
nuclear@0 395 printf(" -v use vsync\n");
nuclear@0 396 printf(" -w run under windows\n");
nuclear@0 397 printf(" -h print usage information and exit\n");
nuclear@0 398 exit(0);
nuclear@0 399
nuclear@0 400 default:
nuclear@0 401 goto invalid;
nuclear@0 402 }
nuclear@0 403 } else {
nuclear@0 404 goto invalid;
nuclear@0 405 }
nuclear@0 406 }
nuclear@0 407
nuclear@0 408 return 0;
nuclear@0 409
nuclear@0 410 invalid:
nuclear@0 411 fprintf(stderr, "invalid argument: %s\n", argv[i]);
nuclear@0 412 return -1;
nuclear@0 413 }
nuclear@0 414
nuclear@0 415
nuclear@0 416 static void print_perf(void)
nuclear@0 417 {
nuclear@0 418 unsigned long msec, avg_frame_time;
nuclear@0 419 float sec, fps;
nuclear@0 420
nuclear@0 421 msec = get_msec();
nuclear@0 422 if(!num_frm || msec < 1000) {
nuclear@0 423 printf("leaving so soon? (%lu ms)\n", msec);
nuclear@0 424 return;
nuclear@0 425 }
nuclear@0 426
nuclear@0 427 sec = msec / 1000.0f;
nuclear@0 428 fps = (float)num_frm / sec;
nuclear@0 429 avg_frame_time = msec / num_frm;
nuclear@0 430
nuclear@0 431 printf("%lu frames in %.2f seconds\n", num_frm, sec);
nuclear@0 432 printf(" avg. frame time: %lu ms\n", avg_frame_time);
nuclear@0 433 printf(" avg. framerate: %.2f fps\n", fps);
nuclear@0 434 }