dos3d

annotate src/test.c @ 0:f04884489bad

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