stop appending repeat keys to the input buffer
[retroray] / src / dos / main.c
index 16f53ba..e3242f1 100644 (file)
@@ -21,6 +21,7 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #include <ctype.h>
 #include <time.h>
 #include "app.h"
+#include "timer.h"
 #include "keyb.h"
 #include "vidsys.h"
 #include "cdpmi.h"
@@ -39,17 +40,20 @@ static INLINE int clamp(int x, int a, int b)
 }
 
 static void draw_cursor(int x, int y);
+static void draw_rband(rtk_rect *r);
 
 static uint32_t *vmem;
-static int quit, disp_pending, dirty_valid;
+static int quit, dirty_valid;
 static rtk_rect dirty;
-static int mx, my;
+static int mx, my, prev_mx, prev_my;
+static rtk_rect rband, prev_rband;
+
 
 int main(int argc, char **argv)
 {
        int i;
        int vmidx;
-       int mdx, mdy, prev_mx, prev_my, bnstate, bndiff;
+       int mdx, mdy, bnstate, bndiff;
        static int prev_bnstate;
        char *env;
 
@@ -57,18 +61,21 @@ int main(int argc, char **argv)
        __djgpp_nearptr_enable();
 #endif
 
+       if(!have_mouse()) {
+               fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
+               return 1;
+       }
+
        init_logger();
 
        if(read_cpuid(&cpuid) == 0) {
                print_cpuid(&cpuid);
        }
 
+       init_timer(0);
        kb_init();
 
-       if(!have_mouse()) {
-               fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
-               return 1;
-       }
+       load_options(CFGFILE);
 
        if((env = getenv("RRLOG"))) {
                if(tolower(env[0]) == 'c' && tolower(env[1]) == 'o' && tolower(env[2]) == 'm'
@@ -83,15 +90,15 @@ int main(int argc, char **argv)
                return 1;
        }
 
-       if((vmidx = vid_findmode(640, 480, 32)) == -1) {
+       if((vmidx = vid_findmode(opt.xres, opt.yres, opt.bpp)) == -1) {
                return 1;
        }
        if(!(vmem = vid_setmode(vmidx))) {
                return 1;
        }
 
-       win_width = 640;
-       win_height = 480;
+       win_width = opt.xres;
+       win_height = opt.yres;
        win_aspect = (float)win_width / (float)win_height;
 
        if(app_init() == -1) {
@@ -102,6 +109,7 @@ int main(int argc, char **argv)
        app_reshape(win_width, win_height);
        mx = win_width / 2;
        my = win_height / 2;
+       prev_mx = prev_my = -1;
 
        for(;;) {
                int key;
@@ -131,8 +139,6 @@ int main(int argc, char **argv)
                prev_bnstate = bnstate;
 
                read_mouse_rel(&mdx, &mdy);
-               prev_mx = mx;
-               prev_my = my;
                mx = clamp(mx + mdx, 0, win_width - 1);
                my = clamp(my + mdy, 0, win_height - 1);
                mdx = mx - prev_mx;
@@ -146,15 +152,8 @@ int main(int argc, char **argv)
                        app_motion(mx, my);
                }
 
-               if(disp_pending) {
-                       disp_pending = 0;
-                       app_display();
-               }
-
+               app_display();
                app_swap_buffers();
-
-               draw_cursor(prev_mx, prev_my);
-               draw_cursor(mx, my);
        }
 
 break_evloop:
@@ -164,11 +163,6 @@ break_evloop:
        return 0;
 }
 
-long app_getmsec(void)
-{
-       return time(0) * 1000;  /* TODO */
-}
-
 void app_redisplay(int x, int y, int w, int h)
 {
        rtk_rect r;
@@ -189,7 +183,6 @@ void app_redisplay(int x, int y, int w, int h)
        } else {
                dirty = r;
        }
-       disp_pending = 1;
        dirty_valid = 1;
 }
 
@@ -198,20 +191,29 @@ void app_swap_buffers(void)
        if(opt.vsync) {
                vid_vsync();
        }
-       if(!dirty_valid) return;
-       if(dirty.width < win_width || dirty.height < win_height) {
-               uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
-               vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
-
-               if(mx >= dirty.x && my >= dirty.y && mx < dirty.x + dirty.width && my < dirty.y + dirty.height) {
-                       draw_cursor(mx, my);
+       if(dirty_valid) {
+               if(dirty.width < win_width || dirty.height < win_height) {
+                       uint32_t *src = framebuf + dirty.y * win_width + dirty.x;
+                       vid_blit32(dirty.x, dirty.y, dirty.width, dirty.height, src, 0);
+               } else {
+                       vid_blitfb32(framebuf, 0);
                }
-       } else {
-               vid_blitfb32(framebuf, 0);
-               draw_cursor(mx, my);
+               dirty_valid = 0;
        }
+       if(prev_mx >= 0) {
+               draw_cursor(prev_mx, prev_my);
+       }
+       draw_cursor(mx, my);
+       prev_mx = mx;
+       prev_my = my;
 
-       dirty_valid = 0;
+       if(prev_rband.width) {
+               draw_rband(&prev_rband);
+       }
+       if(rband.width) {
+               draw_rband(&rband);
+       }
+       prev_rband = rband;
 }
 
 void app_quit(void)
@@ -231,6 +233,18 @@ void app_vsync(int vsync)
 {
 }
 
+void app_rband(int x, int y, int w, int h)
+{
+       if(!(w | h)) {
+               w = h = 0;
+       }
+
+       rband.x = x;
+       rband.y = y;
+       rband.width = w;
+       rband.height = h;
+}
+
 static void draw_cursor(int x, int y)
 {
        int i;
@@ -244,3 +258,31 @@ static void draw_cursor(int x, int y)
                if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
        }
 }
+
+static void draw_rband(rtk_rect *r)
+{
+       int i;
+       rtk_rect rect;
+       uint32_t *fbptr, *bptr;
+
+       rect = *r;
+       rtk_fix_rect(&rect);
+
+       if(rect.width <= 0 || rect.height <= 0) {
+               return;
+       }
+
+       fbptr = vmem + rect.y * win_width + rect.x;
+       bptr = fbptr + win_width * (rect.height - 1);
+
+       for(i=0; i<rect.width; i++) {
+               fbptr[i] ^= 0xffffff;
+               bptr[i] ^= 0xffffff;
+       }
+       fbptr += win_width;
+       for(i=0; i<rect.height-2; i++) {
+               fbptr[0] ^= 0xffffff;
+               fbptr[rect.width - 1] ^= 0xffffff;
+               fbptr += win_width;
+       }
+}