rubber band in platform-specific code
[retroray] / src / dos / main.c
index e167a21..e6e2346 100644 (file)
@@ -18,27 +18,42 @@ along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #include <time.h>
 #include "app.h"
 #include "keyb.h"
-#include "gfx.h"
+#include "vidsys.h"
 #include "cdpmi.h"
 #include "mouse.h"
 #include "logger.h"
 #include "options.h"
 #include "cpuid.h"
+#include "util.h"
+#include "rtk.h"
+
+static INLINE int clamp(int x, int a, int b)
+{
+       if(x < a) return a;
+       if(x > b) return b;
+       return x;
+}
 
 static void draw_cursor(int x, int y);
 
 static uint32_t *vmem;
-static int quit, disp_pending;
+static int quit, disp_pending, dirty_valid;
+static rtk_rect dirty;
+static int mx, my;
+static rtk_rect rband, prev_rband;
+
 
 int main(int argc, char **argv)
 {
        int i;
        int vmidx;
-       int mx, my, bnstate, bndiff;
-       static int prev_mx, prev_my, prev_bnstate;
+       int mdx, mdy, prev_mx, prev_my, bnstate, bndiff;
+       static int prev_bnstate;
+       char *env;
 
 #ifdef __DJGPP__
        __djgpp_nearptr_enable();
@@ -50,27 +65,30 @@ int main(int argc, char **argv)
                print_cpuid(&cpuid);
        }
 
-       kb_init(32);
+       kb_init();
 
        if(!have_mouse()) {
                fprintf(stderr, "No mouse detected. Make sure the mouse driver is installed\n");
                return 1;
        }
-       set_mouse_limits(0, 0, 639, 479);
-       mx = 320;
-       my = 240;
-       set_mouse(mx, my);
 
-       add_log_file("retroray.log");
+       if((env = getenv("RRLOG"))) {
+               if(tolower(env[0]) == 'c' && tolower(env[1]) == 'o' && tolower(env[2]) == 'm'
+                               && isdigit(env[3])) {
+                       add_log_console(env);
+               } else {
+                       add_log_file(env);
+               }
+       }
 
-       if(init_video() == -1) {
+       if(vid_init() == -1) {
                return 1;
        }
 
-       if((vmidx = match_video_mode(640, 480, 32)) == -1) {
+       if((vmidx = vid_findmode(640, 480, 32)) == -1) {
                return 1;
        }
-       if(!(vmem = set_video_mode(vmidx, 1))) {
+       if(!(vmem = vid_setmode(vmidx))) {
                return 1;
        }
 
@@ -81,14 +99,16 @@ int main(int argc, char **argv)
        if(app_init() == -1) {
                goto break_evloop;
        }
-       disp_pending = 1;
+       app_redisplay(0, 0, 0, 0);
 
        app_reshape(win_width, win_height);
-       read_mouse(&mx, &my);
+       mx = win_width / 2;
+       my = win_height / 2;
 
        for(;;) {
                int key;
 
+               modkeys = 0;
                if(kb_isdown(KEY_ALT)) {
                        modkeys |= KEY_MOD_ALT;
                }
@@ -100,33 +120,48 @@ int main(int argc, char **argv)
                }
 
                while((key = kb_getkey()) != -1) {
-                       app_keyboard(key, 1);
+                       if(key == 'r' && (modkeys & KEY_MOD_CTRL)) {
+                               app_redisplay(0, 0, 0, 0);
+                       } else {
+                               app_keyboard(key, 1);
+                       }
                        if(quit) goto break_evloop;
                }
 
-               draw_cursor(mx, my);
-
-               bnstate = read_mouse(&mx, &my);
+               bnstate = read_mouse_bn();
                bndiff = bnstate ^ prev_bnstate;
                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;
+               mdy = my - prev_my;
+
                if(bndiff & 1) app_mouse(0, bnstate & 1, mx, my);
                if(bndiff & 2) app_mouse(1, bnstate & 2, mx, my);
                if(bndiff & 4) app_mouse(3, bnstate & 4, mx, my);
 
+               if((mdx | mdy) != 0) {
+                       app_motion(mx, my);
+               }
+
                if(disp_pending) {
                        disp_pending = 0;
                        app_display();
                }
 
-               draw_cursor(mx, my);
                app_swap_buffers();
+
+               draw_cursor(prev_mx, prev_my);
+               draw_cursor(mx, my);
        }
 
 break_evloop:
        app_shutdown();
-       set_text_mode();
-       cleanup_video();
+       vid_cleanup();
        kb_shutdown();
        return 0;
 }
@@ -136,14 +171,57 @@ long app_getmsec(void)
        return time(0) * 1000;  /* TODO */
 }
 
-void app_redisplay(void)
+void app_redisplay(int x, int y, int w, int h)
 {
+       rtk_rect r;
+
+       if((w | h) == 0) {
+               r.x = r.y = 0;
+               r.width = win_width;
+               r.height = win_height;
+       } else {
+               r.x = x;
+               r.y = y;
+               r.width = w;
+               r.height = h;
+       }
+
+       if(dirty_valid) {
+               rtk_rect_union(&dirty, &r);
+       } else {
+               dirty = r;
+       }
        disp_pending = 1;
+       dirty_valid = 1;
 }
 
 void app_swap_buffers(void)
 {
-       blit_frame(framebuf, opt.vsync);
+       if(opt.vsync) {
+               vid_vsync();
+       }
+       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);
+
+                       if(mx >= dirty.x && my >= dirty.y && mx < dirty.x + dirty.width && my < dirty.y + dirty.height) {
+                               draw_cursor(mx, my);
+                       }
+               } else {
+                       vid_blitfb32(framebuf, 0);
+                       draw_cursor(mx, my);
+               }
+               dirty_valid = 0;
+       }
+
+       if(rband.width) {
+               if(prev_rband.width) {
+                       draw_rband(&prev_rband);
+               }
+               draw_rband(&rband);
+               prev_rband = rband;
+       }
 }
 
 void app_quit(void)
@@ -163,10 +241,23 @@ void app_vsync(int vsync)
 {
 }
 
+void app_rband(int x, int y, int w, int h)
+{
+       if(!(w | h)) {
+               w = h = 0;
+               prev_rband.width = 0;
+       }
+
+       rband.x = x;
+       rband.y = y;
+       rband.width = w;
+       rband.height = h;
+}
+
 static void draw_cursor(int x, int y)
 {
        int i;
-       uint32_t *fbptr = framebuf + y * win_width + x;
+       uint32_t *fbptr = vmem + y * win_width + x;
 
        for(i=0; i<3; i++) {
                int offs = i + 1;
@@ -176,3 +267,27 @@ 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);
+
+       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;
+       }
+}