shithub: m8c

Download patch

ref: 5a0b6dac22b3f184890201c6c3cc8387b12b70f4
parent: 8f5b0825a828e37725adc2ba72aa266f9c049a11
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Sat May 1 15:24:23 EDT 2021

improve rendering performance on low end systems, clean unnecessary includes, clear renderer on every frame

--- a/README.md
+++ b/README.md
@@ -6,7 +6,9 @@
 
 Many thanks to:
 
-driedfruit for a wonderful little routine to blit inline bitmap fonts (https://github.com/driedfruit/SDL_inprint/)
+driedfruit for a wonderful little routine to blit inline bitmap fonts, https://github.com/driedfruit/SDL_inprint/
+
+marcinbor85 for the slip handling routine, https://github.com/marcinbor85/slip
 
 turbolent for the great Golang-based g0m8 application, which I used as reference on how the M8 serial protocol works.
 
--- a/inprint2.c
+++ b/inprint2.c
@@ -1,8 +1,4 @@
 #include <SDL2/SDL.h>
-#include <SDL2/SDL_blendmode.h>
-#include <SDL2/SDL_rect.h>
-#include <SDL2/SDL_render.h>
-#include <SDL2/SDL_surface.h>
 
 #include "inline_font.h" /* Actual font data */
 
@@ -94,8 +90,10 @@
              Uint32 fgcolor, Uint32 bgcolor) {
   SDL_Rect s_rect;
   SDL_Rect d_rect;
-	SDL_Rect bg_rect;
+  SDL_Rect bg_rect;
 
+  static uint32_t previous_fgcolor;
+
   d_rect.x = x;
   d_rect.y = y;
   s_rect.w = selected_font_w / CHARACTERS_PER_ROW;
@@ -122,14 +120,17 @@
       d_rect.y += s_rect.h;
       continue;
     }
-    incolor(fgcolor, 0);
+    if (fgcolor != previous_fgcolor) {
+      incolor(fgcolor, 0);
+      previous_fgcolor = fgcolor;
+    }
     if (bgcolor != -1) {
       SDL_SetRenderDrawColor(selected_renderer,
                              (Uint8)((bgcolor & 0x00FF0000) >> 16),
                              (Uint8)((bgcolor & 0x0000FF00) >> 8),
                              (Uint8)((bgcolor & 0x000000FF)), 0xFF);
-			bg_rect = d_rect;
-			bg_rect.w = 6;
+      bg_rect = d_rect;
+      bg_rect.w = 6;
       SDL_RenderFillRect(dst, &bg_rect);
     }
     SDL_RenderCopy(dst, selected_font, &s_rect, &d_rect);
--- a/input.c
+++ b/input.c
@@ -1,7 +1,4 @@
-#include <SDL2/SDL_events.h>
-#include <SDL2/SDL_joystick.h>
-#include <SDL2/SDL_keycode.h>
-#include <SDL2/SDL_scancode.h>
+#include <SDL2/SDL.h>
 #include <stdio.h>
 
 #include "input.h"
--- a/main.c
+++ b/main.c
@@ -1,8 +1,8 @@
 #include <stdio.h>
 #include <SDL2/SDL.h>
-#include <SDL2/SDL_timer.h>
 #include <libserialport.h>
 #include <signal.h>
+#include <unistd.h>
 
 #include "serial.h"
 #include "command.h"
@@ -77,7 +77,8 @@
         }
       }
     } else {
-      SDL_Delay(1);
+      render_screen();
+      usleep(100);
     }
 
     // get current inputs
@@ -109,7 +110,6 @@
       break;
     }
 
-    render_screen();
   }
 
   // exit, clean up
--- a/render.c
+++ b/render.c
@@ -1,15 +1,9 @@
 #include "render.h"
 
 #include <SDL2/SDL.h>
-#include <SDL2/SDL_pixels.h>
-#include <SDL2/SDL_render.h>
-#include <SDL2/SDL_rwops.h>
-#include <SDL2/SDL_surface.h>
-#include <SDL2/SDL_ttf.h>
 #include <stdio.h>
 
 #include "SDL2_inprint.h"
-
 #include "command.h"
 
 SDL_Window *win;
@@ -21,7 +15,6 @@
 static uint32_t ticks_fps;
 static int fps;
 #endif
-const int font_size = 8;
 uint8_t fullscreen = 0;
 
 // Initializes SDL and creates a renderer and required surfaces
@@ -81,7 +74,7 @@
                      (command->background.g << 8) | command->background.b;
 
   if (bgcolor == fgcolor) {
-    // when bgcolor and fgcolor are the same, do not render a background
+    // When bgcolor and fgcolor are the same, do not render a background
     inprint(rend, (char *)&command->c, command->pos.x, command->pos.y + 3,
             fgcolor, -1);
   } else {
@@ -108,7 +101,7 @@
 
 void draw_waveform(struct draw_oscilloscope_waveform_command *command) {
 
-  const SDL_Rect wf_rect = {0,0,320,20};
+  const SDL_Rect wf_rect = {0, 0, 320, 20};
 
   SDL_SetRenderDrawColor(rend, 0, 0, 0, 0xFF);
   SDL_RenderFillRect(rend, &wf_rect);
@@ -116,15 +109,22 @@
   SDL_SetRenderDrawColor(rend, command->color.r, command->color.g,
                          command->color.b, 255);
 
+  // Create a SDL_Point array of the waveform pixels for batch drawing
+  SDL_Point waveform_points[command->waveform_size];
+
   for (int i = 0; i < command->waveform_size; i++) {
-    // limit value because the oscilloscope commands seem to glitch occasionally
+    // Limit value because the oscilloscope commands seem to glitch occasionally
     if (command->waveform[i] > 20)
       command->waveform[i] = 20;
-    SDL_RenderDrawPoint(rend, i, command->waveform[i]);
+    waveform_points[i].x = i;
+    waveform_points[i].y = command->waveform[i];
   }
+
+  SDL_RenderDrawPoints(rend, waveform_points, command->waveform_size);
 }
 
 void display_keyjazz_overlay(uint8_t show, uint8_t base_octave) {
+
   if (show) {
     struct draw_rectangle_command drc;
     drc.color = (struct color){255, 0, 0};
@@ -160,11 +160,10 @@
 
   // process every 16ms (roughly 60fps)
   if (SDL_GetTicks() - ticks > 16) {
-
     ticks = SDL_GetTicks();
     SDL_SetRenderTarget(rend, NULL);
-    // SDL_SetRenderDrawColor(rend, 0, 0, 0, 0);
-    // SDL_RenderClear(rend);
+    SDL_SetRenderDrawColor(rend, 0, 0, 0, 0);
+    SDL_RenderClear(rend);
     SDL_RenderCopy(rend, maintexture, NULL, NULL);
     SDL_RenderPresent(rend);
     SDL_SetRenderTarget(rend, maintexture);
--