shithub: m8c

Download patch

ref: 6a40a900b58bbbb41ca65e47979ffb3d564f2427
parent: 11a290d5dab446e60b6be62393f45486717a7ca8
author: Ryan <duydv2@vng.com.vn>
date: Tue Mar 21 07:18:05 EDT 2023

option to render m8 background color to sdl canvas

--- a/config.c
+++ b/config.c
@@ -26,6 +26,7 @@
 
   c.init_fullscreen = 0; // default fullscreen state at load
   c.init_use_gpu = 1;    // default to use hardware acceleration
+  c.canvas_color = 0;    // default to not render M8 background color to DSL canvas
   c.idle_ms = 10;        // default to high performance
   c.wait_for_device = 1; // default to exit if device disconnected
   c.wait_packets = 1024; // default zero-byte attempts to disconnect (about 2
@@ -83,7 +84,7 @@
 
   SDL_Log("Writing config file to %s", config_path);
 
-  const unsigned int INI_LINE_COUNT = 44;
+  const unsigned int INI_LINE_COUNT = 45;
   const unsigned int LINELEN = 50;
 
   // Entries for the config file
@@ -94,6 +95,8 @@
            conf->init_fullscreen ? "true" : "false");
   snprintf(ini_values[initPointer++], LINELEN, "use_gpu=%s\n",
            conf->init_use_gpu ? "true" : "false");
+  snprintf(ini_values[initPointer++], LINELEN, "canvas_color=%s\n",
+           conf->canvas_color ? "true" : "false");
   snprintf(ini_values[initPointer++], LINELEN, "idle_ms=%d\n", conf->idle_ms);
   snprintf(ini_values[initPointer++], LINELEN, "wait_for_device=%s\n",
            conf->wait_for_device ? "true" : "false");
@@ -243,6 +246,7 @@
 void read_graphics_config(ini_t *ini, config_params_s *conf) {
   const char *param_fs = ini_get(ini, "graphics", "fullscreen");
   const char *param_gpu = ini_get(ini, "graphics", "use_gpu");
+  const char *param_canvas_color = ini_get(ini, "graphics", "canvas_color");
   const char *idle_ms = ini_get(ini, "graphics", "idle_ms");
   const char *param_wait = ini_get(ini, "graphics", "wait_for_device");
   const char *wait_packets = ini_get(ini, "graphics", "wait_packets");
@@ -257,6 +261,13 @@
       conf->init_use_gpu = 1;
     } else
       conf->init_use_gpu = 0;
+  }
+
+  if (param_canvas_color != NULL) {
+    if (strcmpci(param_canvas_color, "true") == 0) {
+      conf->canvas_color = 1;
+    } else
+      conf->canvas_color = 0;
   }
 
   if (idle_ms != NULL)
--- a/config.h
+++ b/config.h
@@ -10,6 +10,7 @@
   char *filename;
   int init_fullscreen;
   int init_use_gpu;
+  int canvas_color;
   int idle_ms;
   int wait_for_device;
   int wait_packets;
--- a/config.ini.sample
+++ b/config.ini.sample
@@ -9,6 +9,8 @@
 ; set this to false to run m8c in software rendering mode (may be useful for Raspberry Pi)
 use_gpu=true
 ; the delay amount in ms in the main loop, decrease value for faster operation, increase value if too much cpu usage
+canvas_color=false
+; set this to true to render m8 background color to canvas color of sdl, set to false to use default color (black)
 idle_ms = 10
 ; show a spinning cube if device is not inserted
 wait_for_device = true
--- a/main.c
+++ b/main.c
@@ -73,7 +73,7 @@
   }
 
   // initialize all SDL systems
-  if (initialize_sdl(conf.init_fullscreen, conf.init_use_gpu) == -1)
+  if (initialize_sdl(conf.init_fullscreen, conf.init_use_gpu, conf.canvas_color) == -1)
     run = QUIT;
 
   // initial scan for (existing) game controllers
--- a/render.c
+++ b/render.c
@@ -15,16 +15,18 @@
 SDL_Window *win;
 SDL_Renderer *rend;
 SDL_Texture *maintexture;
-SDL_Color background_color = (SDL_Color){0, 0, 0, 0};
+SDL_Color background_color = (SDL_Color){.r = 0x00, .g = 0x00, .b = 0x00, .a = 0x00};
 
 static uint32_t ticks_fps;
 static int fps;
+static int render_canvas_color;
+
 uint8_t fullscreen = 0;
 
 static uint8_t dirty = 0;
 
 // Initializes SDL and creates a renderer and required surfaces
-int initialize_sdl(int init_fullscreen, int init_use_gpu) {
+int initialize_sdl(int init_fullscreen, int init_use_gpu, int init_canvas_color) {
   //ticks = SDL_GetTicks();
 
   const int window_width = 640;  // SDL window width
@@ -52,7 +54,14 @@
 
   SDL_SetRenderTarget(rend, maintexture);
 
-  SDL_SetRenderDrawColor(rend, 0x00, 0x00, 0x00, 0x00);
+  render_canvas_color = init_canvas_color;
+  if (render_canvas_color == 0) {
+    SDL_SetRenderDrawColor(rend, 0x00, 0x00, 0x00, 0x00);
+  } else {
+    SDL_SetRenderDrawColor(rend, background_color.r, background_color.g,
+                                background_color.b, background_color.a);
+  }
+
   SDL_RenderClear(rend);
 
   // Initialize a texture for the font and read the inline font bitmap
@@ -228,7 +237,14 @@
     dirty = 0;
     //ticks = SDL_GetTicks();
     SDL_SetRenderTarget(rend, NULL);
-    SDL_SetRenderDrawColor(rend, 0, 0, 0, 0);
+    
+    if (render_canvas_color == 0) {
+      SDL_SetRenderDrawColor(rend, 0x00, 0x00, 0x00, 0x00);
+    } else {
+      SDL_SetRenderDrawColor(rend, background_color.r, background_color.g,
+                                  background_color.b, background_color.a);
+    }
+
     SDL_RenderClear(rend);
     SDL_RenderCopy(rend, maintexture, NULL, NULL);
     SDL_RenderPresent(rend);
--- a/render.h
+++ b/render.h
@@ -6,7 +6,7 @@
 
 #include "command.h"
 
-int initialize_sdl(int init_fullscreen, int init_use_gpu);
+int initialize_sdl(int init_fullscreen, int init_use_gpu, int init_canvas_color);
 void close_renderer();
 
 int process_queues(struct command_queues *queues);
--