fixed blitfb32 for banked video modes with scanline padding (cirrus logic vlb)
[retroray] / src / dos / main.c
index ad64f4c..8bcc718 100644 (file)
@@ -18,15 +18,26 @@ 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"
+
+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;
@@ -35,8 +46,9 @@ int main(int argc, char **argv)
 {
        int i;
        int vmidx;
-       int mx, my, bnstate, bndiff;
-       static int prev_mx, prev_my, prev_bnstate;
+       int mx, my, mdx, mdy, prev_mx, prev_my, bnstate, bndiff;
+       static int prev_bnstate;
+       char *env;
 
 #ifdef __DJGPP__
        __djgpp_nearptr_enable();
@@ -48,25 +60,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);
-       set_mouse(320, 240);
 
-       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;
        }
 
@@ -79,30 +96,63 @@ int main(int argc, char **argv)
        }
        disp_pending = 1;
 
+       app_reshape(win_width, win_height);
+       mx = win_width / 2;
+       my = win_height / 2;
+
        for(;;) {
                int key;
+
+               modkeys = 0;
+               if(kb_isdown(KEY_ALT)) {
+                       modkeys |= KEY_MOD_ALT;
+               }
+               if(kb_isdown(KEY_CTRL)) {
+                       modkeys |= KEY_MOD_CTRL;
+               }
+               if(kb_isdown(KEY_SHIFT)) {
+                       modkeys |= KEY_MOD_SHIFT;
+               }
+
                while((key = kb_getkey()) != -1) {
                        app_keyboard(key, 1);
                        if(quit) goto break_evloop;
                }
 
-               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(prev_mx, prev_my);
+               draw_cursor(mx, my);
+
+               app_swap_buffers();
        }
 
 break_evloop:
        app_shutdown();
-       set_text_mode();
-       cleanup_video();
+       vid_cleanup();
        kb_shutdown();
        return 0;
 }
@@ -119,7 +169,10 @@ void app_redisplay(void)
 
 void app_swap_buffers(void)
 {
-       blit_frame(framebuf, opt.vsync);
+       if(opt.vsync) {
+               vid_vsync();
+       }
+       vid_blitfb32(framebuf, 0);
 }
 
 void app_quit(void)
@@ -138,3 +191,17 @@ void app_fullscreen(int fs)
 void app_vsync(int vsync)
 {
 }
+
+static void draw_cursor(int x, int y)
+{
+       int i;
+       uint32_t *fbptr = framebuf + y * win_width + x;
+
+       for(i=0; i<3; i++) {
+               int offs = i + 1;
+               if(y > offs) fbptr[-win_width * offs] ^= 0xffffff;
+               if(y < win_height - offs - 1) fbptr[win_width * offs] ^= 0xffffff;
+               if(x > offs) fbptr[-offs] ^= 0xffffff;
+               if(x < win_width - offs - 1) fbptr[offs] ^= 0xffffff;
+       }
+}