minor performance improvements, optional mouse, mouse cursor now drawn
[dosdemo] / src / dos / main.c
index 7564be3..5d840b1 100644 (file)
@@ -3,6 +3,8 @@
 #include <math.h>
 #include <string.h>
 #include <limits.h>
+#include <assert.h>
+#include <conio.h>
 #include "demo.h"
 #include "keyb.h"
 #include "mouse.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;
@@ -26,28 +31,42 @@ 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);
-
-       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))) {
+#endif
+
+       /* 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();
@@ -61,18 +80,23 @@ int main(int argc, char **argv)
        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;
-                       printf("got sball event\n");
                        while(sball_getevent(&ev)) {
                                handle_sball_event(&ev);
                        }
@@ -86,7 +110,9 @@ int main(int argc, char **argv)
 break_evloop:
        set_text_mode();
        demo_cleanup();
+#ifndef NOKEYB
        kb_shutdown();
+#endif
        if(use_sball) {
                sball_shutdown();
        }
@@ -100,14 +126,17 @@ void demo_quit(void)
 
 void swap_buffers(void *pixels)
 {
-       /* TODO implement page flipping */
-       if(pixels) {
-               /*wait_vsync();*/
-               drawFps(pixels);
-               memcpy(vmem_front, pixels, fbsize);
-       } else {
-               drawFps(vmem_back);
+       if(!pixels) {
+               pixels = fb_pixels;
+       }
+
+       demo_post_draw(pixels);
+
+       /* just memcpy to the front buffer */
+       if(opt.vsync) {
+               wait_vsync();
        }
+       memcpy(vmem, pixels, fbsize);
 }