ref: 6dbc8363153854eee80f9836bbb85db1f5f60a7e
parent: bc2f9d910906338ebcb2dc23a885616cbc92d16a
parent: a002bab6efaa60464f70f33b991eca2a7e1dc49c
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Tue May 24 18:33:20 EDT 2022
Merge pull request #63 from bloopclick/enh-cpu-perf CPU utilization improvements, minor enhancements, and other bug fixes
--- a/config.c
+++ b/config.c
@@ -4,6 +4,7 @@
#include "config.h"
#include "ini.h"
#include <SDL.h>
+#include <assert.h>
/* Case insensitive string compare from ini.h library */
static int strcmpci(const char *a, const char *b) {@@ -23,6 +24,7 @@
c.init_fullscreen = 0; // default fullscreen state at load
c.init_use_gpu = 1; // default to use hardware acceleration
+ c.idle_ms = 10; // default to high performance
c.key_up = SDL_SCANCODE_UP;
c.key_left = SDL_SCANCODE_LEFT;
@@ -70,8 +72,10 @@
SDL_Log("Writing config file to %s", config_path);+ const unsigned int INI_LINE_COUNT = 36;
+
// Entries for the config file
- char ini_values[35][50];
+ char ini_values[INI_LINE_COUNT][50];
int initPointer = 0;
sprintf(ini_values[initPointer++], "[graphics]\n");
sprintf(ini_values[initPointer++], "fullscreen=%s\n",
@@ -78,6 +82,7 @@
conf->init_fullscreen ? "true" : "false");
sprintf(ini_values[initPointer++], "use_gpu=%s\n",
conf->init_use_gpu ? "true" : "false");
+ sprintf(ini_values[initPointer++], "idle_ms=%d\n", conf->idle_ms);
sprintf(ini_values[initPointer++], "[keyboard]\n");
sprintf(ini_values[initPointer++], "key_up=%d\n", conf->key_up);
sprintf(ini_values[initPointer++], "key_left=%d\n", conf->key_left);
@@ -119,9 +124,12 @@
sprintf(ini_values[initPointer++], "gamepad_analog_axis_edit=%d\n",
conf->gamepad_analog_axis_edit);
+ // Ensure we aren't writing off the end of the array
+ assert(initPointer == INI_LINE_COUNT);
+
if (rw != NULL) {// Write ini_values array to config file
- for (int i = 0; i < 34; i++) {+ for (int i = 0; i < INI_LINE_COUNT; i++) {size_t len = SDL_strlen(ini_values[i]);
if (SDL_RWwrite(rw, ini_values[i], 1, len) != len) {SDL_LogDebug(SDL_LOG_CATEGORY_SYSTEM,
@@ -164,6 +172,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 *idle_ms = ini_get(ini, "graphics", "idle_ms");
if (strcmpci(param_fs, "true") == 0) {conf->init_fullscreen = 1;
@@ -176,6 +185,9 @@
} else
conf->init_use_gpu = 0;
}
+
+ if (idle_ms)
+ conf->idle_ms = SDL_atoi(idle_ms);
}
--- a/config.h
+++ b/config.h
@@ -10,6 +10,7 @@
char *filename;
int init_fullscreen;
int init_use_gpu;
+ int idle_ms;
int key_up;
int key_left;
--- a/ini.c
+++ b/ini.c
@@ -194,6 +194,9 @@
/* Get file size */
fseek(fp, 0, SEEK_END);
sz = ftell(fp);
+ if (sz==0) {+ goto fail;
+ }
rewind(fp);
/* Load file content into memory, null terminate, init end var */
--- a/main.c
+++ b/main.c
@@ -115,7 +115,7 @@
while (1) {// read serial port
- int bytes_read = sp_blocking_read(port, serial_buf, serial_read_size, 1);
+ int bytes_read = sp_nonblocking_read(port, serial_buf, serial_read_size);
if (bytes_read < 0) {SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial. \n",
(int)bytes_read);
@@ -140,6 +140,7 @@
}
}
render_screen();
+ SDL_Delay(conf.idle_ms);
}
// exit, clean up
--- a/render.c
+++ b/render.c
@@ -203,7 +203,7 @@
}
void render_screen() {- if (dirty && (SDL_GetTicks() - ticks > 14)) {+ if (dirty) {dirty = 0;
ticks = SDL_GetTicks();
SDL_SetRenderTarget(rend, NULL);
--
⑨