dos3d

annotate src/test.c @ 15:4a0e9ab12ad0

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