backported fixes from rtxon: BSP tree construction and mouse handling
[dosdemo] / src / sdl / main.c
index 48852f5..358d774 100644 (file)
@@ -3,12 +3,14 @@
 #include <limits.h>
 #include <SDL/SDL.h>
 #include "demo.h"
+#include "tinyfps.h"
+#include "timer.h"
+#include "cfgopt.h"
 
 static void handle_event(SDL_Event *ev);
 static void toggle_fullscreen(void);
 
 static int quit;
-static long start_time;
 static SDL_Surface *fbsurf;
 
 static int fbscale = 2;
@@ -29,13 +31,14 @@ int main(int argc, char **argv)
        xsz = fb_width * fbscale;
        ysz = fb_height * fbscale;
 
-       if(!(fb_pixels = malloc(fb_width * fb_height * fb_bpp / CHAR_BIT))) {
+       /* allocate 1 extra row as a guard band, until we fucking fix the rasterizer */
+       if(!(fb_pixels = malloc(fb_width * (fb_height + 1) * fb_bpp / CHAR_BIT))) {
                fprintf(stderr, "failed to allocate virtual framebuffer\n");
                return 1;
        }
        vmem_front = vmem_back = fb_pixels;
 
-       SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
+       SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE);
        if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, fb_bpp, sdl_flags))) {
                fprintf(stderr, "failed to set video mode %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
                free(fb_pixels);
@@ -50,7 +53,7 @@ int main(int argc, char **argv)
                SDL_Quit();
                return 1;
        }
-       start_time = SDL_GetTicks();
+       reset_timer();
 
        while(!quit) {
                SDL_Event ev;
@@ -59,8 +62,9 @@ int main(int argc, char **argv)
                        if(quit) goto break_evloop;
                }
 
-               time_msec = SDL_GetTicks() - start_time;
+               time_msec = get_msec();
                demo_draw();
+               drawFps(fb_pixels);
 
                if(SDL_MUSTLOCK(fbsurf)) {
                        SDL_LockSurface(fbsurf);
@@ -100,9 +104,34 @@ void demo_quit(void)
        quit = 1;
 }
 
+void wait_vsync(void)
+{
+       unsigned long start = SDL_GetTicks();
+       unsigned long until = (start | 0xf) + 1;
+       while(SDL_GetTicks() <= until);
+}
+
 void swap_buffers(void *pixels)
 {
        /* do nothing, all pointers point to the same buffer */
+       if(opt.vsync) {
+               wait_vsync();
+       }
+}
+
+static int bnmask(int sdlbn)
+{
+       switch(sdlbn) {
+       case SDL_BUTTON_LEFT:
+               return MOUSE_LEFT;
+       case SDL_BUTTON_RIGHT:
+               return MOUSE_RIGHT;
+       case SDL_BUTTON_MIDDLE:
+               return MOUSE_MIDDLE;
+       default:
+               break;
+       }
+       return 0;
 }
 
 static void handle_event(SDL_Event *ev)
@@ -114,10 +143,9 @@ static void handle_event(SDL_Event *ev)
 
        case SDL_KEYDOWN:
        case SDL_KEYUP:
-               if(ev->key.keysym.sym == 'f') {
-                       if(ev->key.state == SDL_PRESSED) {
-                               toggle_fullscreen();
-                       }
+               if(ev->key.keysym.sym == SDLK_RETURN && (SDL_GetModState() & KMOD_ALT) &&
+                               ev->key.state == SDL_PRESSED) {
+                       toggle_fullscreen();
                        break;
                }
                demo_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED ? 1 : 0);
@@ -129,10 +157,10 @@ static void handle_event(SDL_Event *ev)
                break;
 
        case SDL_MOUSEBUTTONDOWN:
-               mouse_bmask |= 1 << (ev->button.button - SDL_BUTTON_LEFT);
+               mouse_bmask |= bnmask(ev->button.button);
                if(0) {
        case SDL_MOUSEBUTTONUP:
-                       mouse_bmask &= ~(1 << (ev->button.button - SDL_BUTTON_LEFT));
+                       mouse_bmask &= ~bnmask(ev->button.button);
                }
                mouse_x = ev->button.x / fbscale;
                mouse_y = ev->button.y / fbscale;