]> git.sesse.net Git - pistorm/blobdiff - platforms/amiga/rtg/rtg-output-raylib.c
Add somewhat proper RTG vsync handling
[pistorm] / platforms / amiga / rtg / rtg-output-raylib.c
index 849db67a9c24daf18f9d485e6876b5549685ed82..ddca531317c3ff72755ac0b52ca46e83151857c5 100644 (file)
 #include <string.h>
 #include <unistd.h>
 
+#define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
+
 //#define DEBUG_RAYLIB_RTG
 
-#define RTG_INIT_ERR(a) { printf(a); *data->running = 0; }
+#ifdef DEBUG_RAYLIB_RTG
+#define DEBUG printf
+#else
+#define DEBUG(...)
+#endif
 
-uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0;
+uint8_t busy = 0, rtg_on = 0, rtg_initialized = 0, emulator_exiting = 0, rtg_output_in_vblank = 0;
 extern uint8_t *rtg_mem;
 extern uint32_t framebuffer_addr;
 extern uint32_t framebuffer_addr_adj;
@@ -28,7 +34,7 @@ extern uint16_t rtg_pitch, rtg_total_rows;
 extern uint16_t rtg_offset_x, rtg_offset_y;
 
 static pthread_t thread_id;
-static uint8_t mouse_cursor_enabled = 0, cursor_image_updated = 0, updating_screen = 0;
+static uint8_t mouse_cursor_enabled = 0, cursor_image_updated = 0, updating_screen = 0, debug_palette = 0, show_fps = 0;
 static uint8_t mouse_cursor_w = 16, mouse_cursor_h = 16;
 static int16_t mouse_cursor_x = 0, mouse_cursor_y = 0;
 
@@ -158,18 +164,16 @@ reinit_raylib:;
 
     raylib_texture = LoadTextureFromImage(raylib_fb);
 
+    printf("Loaded framebuffer texture.\n");
+
     srcrect.x = srcrect.y = 0;
     srcrect.width = width;
     srcrect.height = height;
     dstscale.x = dstscale.y = 0;
     dstscale.width = width;
     dstscale.height = height;
-    scale_x = 1.0f;
-    scale_y = 1.0f;
-    origin.x = 0.0f;
-    origin.y = 0.0f;
 
-    if (dstscale.height * 2 < GetScreenHeight()) {
+    if (dstscale.height * 2 <= GetScreenHeight()) {
         if (width == 320) {
             if (GetScreenHeight() == 720) {
                 dstscale.width = 960;
@@ -177,20 +181,50 @@ reinit_raylib:;
             } else if (GetScreenHeight() == 1080) {
                 dstscale.width = 1440;
                 dstscale.height = 1080;
+            } else if (GetScreenHeight() == 1200) {
+                dstscale.width = 1600;
+                dstscale.height = 1200;
             }
         } else {
-            while (dstscale.height < GetScreenHeight()) {
+            while (dstscale.height + height <= GetScreenHeight()) {
                 dstscale.height += height;
                 dstscale.width += width;
             }
         }
-        scale_x = dstscale.width / (float)width;
-        scale_y = dstscale.height / (float)height;
+    } else if (dstscale.width > GetScreenWidth() || dstscale.height > GetScreenHeight()) {
+        if (dstscale.height > GetScreenHeight()) {
+            DEBUG("[H > SH]\n");
+            DEBUG("Adjusted width from %d to", (int)dstscale.width);
+            dstscale.width = dstscale.width * ((float)GetScreenHeight() / (float)height);
+            DEBUG("%d.\n", (int)dstscale.width);
+            DEBUG("Adjusted height from %d to", (int)dstscale.height);
+            dstscale.height = GetScreenHeight();
+            DEBUG("%d.\n", (int)dstscale.height);
+        }
+        if (dstscale.width > GetScreenWidth()) {
+            // First scaling attempt failed, do not double adjust, re-adjust
+            dstscale.width = width;
+            dstscale.height = height;
+            DEBUG("[W > SW]\n");
+            DEBUG("Adjusted height from %d to", (int)dstscale.height);
+            dstscale.height = dstscale.height * ((float)GetScreenWidth() / (float)width);
+            DEBUG("%d.\n", (int)dstscale.height);
+            DEBUG("Adjusted width from %d to", (int)dstscale.width);
+            dstscale.width = GetScreenWidth();
+            DEBUG("%d.\n", (int)dstscale.width);
+        }
     }
 
+    scale_x = dstscale.width / (float)width;
+    scale_y = dstscale.height / (float)height;
+
+    origin.x = (dstscale.width - GetScreenWidth()) * 0.5;
+    origin.y = (dstscale.height - GetScreenHeight()) * 0.5;
+
     while (1) {
         if (rtg_on) {
             BeginDrawing();
+            rtg_output_in_vblank = 0;
             updating_screen = 1;
 
             switch (format) {
@@ -221,19 +255,20 @@ reinit_raylib:;
                 DrawTexturePro(raylib_cursor_texture, cursor_srcrect, dstrect, origin, 0.0f, RAYWHITE);
             }
 
-#ifdef DEBUG_RAYLIB_RTG
-            if (format == RTGFMT_8BIT) {
-                Rectangle srcrect = { 0, 0, 256, 1 };
-                Rectangle dstrect = { 0, 0, 1024, 8 };
-                //DrawTexture(raylib_clut_texture, 0, 0, RAYWHITE);
-                DrawTexturePro(raylib_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
-                dstrect.y += 8;
-                DrawTexturePro(raylib_cursor_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
+            if (debug_palette) {
+                if (format == RTGFMT_8BIT) {
+                    Rectangle srcrect = { 0, 0, 256, 1 };
+                    Rectangle dstrect = { 0, 0, 1024, 8 };
+                    DrawTexturePro(raylib_clut_texture, srcrect, dstrect, origin, 0.0f, RAYWHITE);
+                }
+            }
+
+            if (show_fps) {
+                DrawFPS(GetScreenWidth() - 128, 0);
             }
-#endif
 
-            DrawFPS(width - 200, 0);
             EndDrawing();
+            rtg_output_in_vblank = 1;
             if (format == RTGFMT_RBG565) {
                 for (int y = 0; y < height; y++) {
                     for (int x = 0; x < width; x++) {
@@ -261,9 +296,9 @@ reinit_raylib:;
             reinit = 1;
             goto shutdown_raylib;
         }
-        /*if (!rtg_on) {
+        if (emulator_exiting) {
             goto shutdown_raylib;
-        }*/
+        }
     }
 
     rtg_initialized = 0;
@@ -357,8 +392,6 @@ void update_mouse_cursor(uint8_t *src) {
         }
     }
 
-    printf ("Updated mouse cursor data.\n");
-
     while (rtg_on && !updating_screen)
         usleep(0);
     cursor_image_updated = 1;
@@ -388,17 +421,20 @@ void rtg_set_mouse_cursor_image(uint8_t *src, uint8_t w, uint8_t h) {
     mouse_cursor_w = w;
     mouse_cursor_h = h;
 
-    if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0) {
-        printf("New cursor data.\n");
+    if (memcmp(src, old_mouse_data, (w / 8 * h)) != 0)
         new_cursor_data = 1;
-    } else {
-        printf("No new cursor data.\n");
-    }
 
     if (old_mouse_w != w || old_mouse_h != h || new_cursor_data) {
-        printf("Set new %dx%d mouse cursor image.\n", mouse_cursor_w, mouse_cursor_h);
         old_mouse_w = w;
         old_mouse_h = h;
         update_mouse_cursor(src);
     }
 }
+
+void rtg_show_fps(uint8_t enable) {
+    show_fps = (enable != 0);
+}
+
+void rtg_palette_debug(uint8_t enable) {
+    debug_palette = (enable != 0);
+}