X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=blobdiff_plain;f=src%2Fdos%2Fmain.c;h=5d840b13b290d638950cc2300d5f7bcbb48ff82a;hp=53b8888f8f991e88bc244a3ff674d77f8fc0a641;hb=e8b26db00c934d141f16652cb8dcbeae23b17e48;hpb=0b870b76705b3e597da3f6a11e0499deedbeee30 diff --git a/src/dos/main.c b/src/dos/main.c index 53b8888..5d840b1 100644 --- a/src/dos/main.c +++ b/src/dos/main.c @@ -1,59 +1,107 @@ #include #include +#include #include #include +#include +#include #include "demo.h" #include "keyb.h" #include "mouse.h" #include "timer.h" #include "gfx.h" +#include "vmath.h" +#include "sball.h" +#include "cfgopt.h" #include "logger.h" +#include "tinyfps.h" +#include "cdpmi.h" + +#undef NOKEYB + +static int handle_sball_event(sball_event *ev); +static void recalc_sball_matrix(float *xform); static int quit; -static int use_mouse; static long fbsize; +static int use_sball; +static vec3_t pos = {0, 0, 0}; +static quat_t rot = {0, 0, 0, 1}; + int main(int argc, char **argv) { - fbsize = fb_width * fb_height * fb_bpp / CHAR_BIT; +#ifdef __DJGPP__ + __djgpp_nearptr_enable(); +#endif + + fbsize = FB_WIDTH * FB_HEIGHT * FB_BPP / 8; init_logger("demo.log"); init_timer(100); +#ifndef NOKEYB kb_init(32); +#endif - if((use_mouse = have_mouse())) { - set_mouse_limits(0, 0, fb_width, fb_height); - set_mouse(fb_width / 2, fb_height / 2); - } - - if(!(fb_pixels = malloc(fbsize))) { + /* now start_loadscr sets up fb_pixels to the space used by the loading image, + * so no need to allocate another framebuffer + */ +#if 0 + /* allocate a couple extra rows as a guard band, until we fucking fix the rasterizer */ + if(!(fb_pixels = malloc(fbsize + (FB_WIDTH * FB_BPP / 8) * 2))) { fprintf(stderr, "failed to allocate backbuffer\n"); return 1; } + fb_pixels += FB_WIDTH; +#endif - if(!(vmem_front = set_video_mode(fb_width, fb_height, fb_bpp))) { + if(!(vmem = set_video_mode(FB_WIDTH, FB_HEIGHT, FB_BPP, 1))) { return 1; } - /* TODO implement multiple video memory pages for flipping */ - vmem_back = vmem_front; + + if(opt.mouse) { + if((opt.mouse = have_mouse())) { + printf("initializing mouse input\n"); + set_mouse_limits(0, 0, FB_WIDTH - 1, FB_HEIGHT - 1); + set_mouse(FB_WIDTH / 2, FB_HEIGHT / 2); + } + } if(demo_init(argc, argv) == -1) { set_text_mode(); return 1; } + + if(opt.sball && sball_init() == 0) { + use_sball = 1; + } + reset_timer(); while(!quit) { +#ifndef NOKEYB int key; while((key = kb_getkey()) != -1) { demo_keyboard(key, 1); } +#else + if(kbhit()) { + demo_keyboard(getch(), 1); + } +#endif if(quit) goto break_evloop; - if(use_mouse) { + if(opt.mouse) { mouse_bmask = read_mouse(&mouse_x, &mouse_y); } + if(use_sball && sball_pending()) { + sball_event ev; + while(sball_getevent(&ev)) { + handle_sball_event(&ev); + } + recalc_sball_matrix(sball_matrix); + } time_msec = get_msec(); demo_draw(); @@ -62,7 +110,12 @@ int main(int argc, char **argv) break_evloop: set_text_mode(); demo_cleanup(); +#ifndef NOKEYB kb_shutdown(); +#endif + if(use_sball) { + sball_shutdown(); + } return 0; } @@ -73,9 +126,62 @@ void demo_quit(void) void swap_buffers(void *pixels) { - /* TODO implement page flipping */ - if(pixels) { - /*wait_vsync();*/ - memcpy(vmem_front, pixels, fbsize); + if(!pixels) { + pixels = fb_pixels; } + + demo_post_draw(pixels); + + /* just memcpy to the front buffer */ + if(opt.vsync) { + wait_vsync(); + } + memcpy(vmem, pixels, fbsize); +} + + +#define TX(ev) ((ev)->motion.motion[0]) +#define TY(ev) ((ev)->motion.motion[1]) +#define TZ(ev) ((ev)->motion.motion[2]) +#define RX(ev) ((ev)->motion.motion[3]) +#define RY(ev) ((ev)->motion.motion[4]) +#define RZ(ev) ((ev)->motion.motion[5]) + +static int handle_sball_event(sball_event *ev) +{ + switch(ev->type) { + case SBALL_EV_MOTION: + if(RX(ev) | RY(ev) | RZ(ev)) { + float rx = (float)RX(ev); + float ry = (float)RY(ev); + float rz = (float)RZ(ev); + float axis_len = sqrt(rx * rx + ry * ry + rz * rz); + if(axis_len > 0.0) { + rot = quat_rotate(rot, axis_len * 0.001, -rx / axis_len, + -ry / axis_len, -rz / axis_len); + } + } + + pos.x += TX(ev) * 0.001; + pos.y += TY(ev) * 0.001; + pos.z += TZ(ev) * 0.001; + break; + + case SBALL_EV_BUTTON: + if(ev->button.pressed) { + pos = v3_cons(0, 0, 0); + rot = quat_cons(1, 0, 0, 0); + } + break; + } + + return 0; +} + +void recalc_sball_matrix(float *xform) +{ + quat_to_mat(xform, rot); + xform[12] = pos.x; + xform[13] = pos.y; + xform[14] = pos.z; }