Skip to content

Commit

Permalink
add -Wextra to cflags, fix keyboard controls not working, lots of ref…
Browse files Browse the repository at this point in the history
…actoring and cleanup
  • Loading branch information
laamaa committed Aug 24, 2024
1 parent 7f74d42 commit 1c4fa7f
Show file tree
Hide file tree
Showing 22 changed files with 266 additions and 276 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.15)

project(m8c LANGUAGES C)

set(CMAKE_C_FLAGS "-O2 -Wall -Wextra")

set(APP_NAME m8c)

find_package(PkgConfig REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ DEPS = src/serial.h src/slip.h src/command.h src/render.h src/ini.h src/config.h
INCLUDES = $(shell pkg-config --libs sdl2 libserialport | sed 's/-mwindows//')

#Set any compiler flags you want to use (e.g. -I/usr/include/somefolder `pkg-config --cflags gtk+-3.0` ), or leave blank
local_CFLAGS = $(CFLAGS) $(shell pkg-config --cflags sdl2 libserialport) -Wall -O2 -pipe -I.
local_CFLAGS = $(CFLAGS) $(shell pkg-config --cflags sdl2 libserialport) -Wall -Wextra -O2 -pipe -I.

#Set the compiler you are using ( gcc for C or g++ for C++ )
CC = gcc
Expand Down
4 changes: 2 additions & 2 deletions src/SDL2_inprint.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extern void kill_inline_font(void);

extern void inrenderer(SDL_Renderer *renderer);
extern void infont(SDL_Texture *font);
extern void incolor1(SDL_Color *color);
extern void incolor(Uint32 color, Uint32 unused); /* Color must be in 0x00RRGGBB format ! */
extern void incolor1(const SDL_Color *color);
extern void incolor(Uint32 color); /* Color must be in 0x00RRGGBB format ! */
extern void inprint(SDL_Renderer *dst, const char *str, Uint32 x, Uint32 y, Uint32 fgcolor,
Uint32 bgcolor);

Expand Down
12 changes: 5 additions & 7 deletions src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static SDL_AudioDeviceID devid_out = 0;
static unsigned int audio_paused = 0;
static unsigned int audio_initialized = 0;

void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name) {
void toggle_audio(const unsigned int audio_buffer_size, const char *output_device_name) {
if (!audio_initialized) {
audio_init(audio_buffer_size, output_device_name);
return;
Expand All @@ -22,26 +22,24 @@ void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name
SDL_Log(audio_paused ? "Audio paused" : "Audio resumed");
}

void audio_cb_in(void *userdata, uint8_t *stream, int len) {
void audio_cb_in(void *, uint8_t *stream, int len) {
SDL_QueueAudio(devid_out, stream, len);
}

int audio_init(unsigned int audio_buffer_size, const char *output_device_name) {
int audio_init(const unsigned int audio_buffer_size, const char *output_device_name) {

int i = 0;
int m8_device_id = -1;
int devcount_in = 0; // audio input device count

// wait for system to initialize possible new audio devices
SDL_Delay(500);

devcount_in = SDL_GetNumAudioDevices(SDL_TRUE);
const int devcount_in = SDL_GetNumAudioDevices(SDL_TRUE);

if (devcount_in < 1) {
SDL_Log("No audio capture devices, SDL Error: %s", SDL_GetError());
return 0;
}
for (i = 0; i < devcount_in; i++) {
for (int i = 0; i < devcount_in; i++) {
// Check if input device exists before doing anything else
SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
Expand Down
4 changes: 2 additions & 2 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "render.h"

// Convert 2 little-endian 8bit bytes to a 16bit integer
static uint16_t decodeInt16(uint8_t *data, uint8_t start) {
static uint16_t decodeInt16(const uint8_t *data, const uint8_t start) {
return data[start] | (uint16_t)data[start + 1] << 8;
}

Expand All @@ -26,7 +26,7 @@ enum m8_command_bytes {
system_info_command_datalength = 6
};

static inline void dump_packet(uint32_t size, uint8_t *recv_buf) {
static void dump_packet(const uint32_t size, const uint8_t *recv_buf) {
for (uint16_t a = 0; a < size; a++) {
SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "0x%02X ", recv_buf[a]);
}
Expand Down
18 changes: 9 additions & 9 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
#include <assert.h>
#include <stdio.h>

/* Case insensitive string compare from ini.h library */
/* Case-insensitive string compare from ini.h library */
static int strcmpci(const char *a, const char *b) {
for (;;) {
int d = tolower(*a) - tolower(*b);
const int d = tolower(*a) - tolower(*b);
if (d != 0 || !*a) {
return d;
}
Expand Down Expand Up @@ -77,7 +77,7 @@ config_params_s init_config() {
}

// Write config to file
void write_config(config_params_s *conf) {
void write_config(const config_params_s *conf) {

// Open the default config file for writing
char config_path[1024] = {0};
Expand Down Expand Up @@ -165,8 +165,8 @@ void write_config(config_params_s *conf) {

if (rw != NULL) {
// Write ini_values array to config file
for (int i = 0; i < INI_LINE_COUNT; i++) {
size_t len = SDL_strlen(ini_values[i]);
for (unsigned int i = 0; i < INI_LINE_COUNT; i++) {
const size_t len = SDL_strlen(ini_values[i]);
if (SDL_RWwrite(rw, ini_values[i], 1, len) != len) {
SDL_LogDebug(SDL_LOG_CATEGORY_SYSTEM, "Couldn't write line into config file.");
} else {
Expand Down Expand Up @@ -204,7 +204,7 @@ void read_config(config_params_s *conf) {
write_config(conf);
}

void read_audio_config(ini_t *ini, config_params_s *conf) {
void read_audio_config(const ini_t *ini, config_params_s *conf) {
const char *param_audio_enabled = ini_get(ini, "audio", "audio_enabled");
const char *param_audio_buffer_size = ini_get(ini, "audio", "audio_buffer_size");
const char *param_audio_device_name = ini_get(ini, "audio", "audio_device_name");
Expand All @@ -226,7 +226,7 @@ void read_audio_config(ini_t *ini, config_params_s *conf) {
}
}

void read_graphics_config(ini_t *ini, config_params_s *conf) {
void read_graphics_config(const 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");
Expand Down Expand Up @@ -259,7 +259,7 @@ void read_graphics_config(ini_t *ini, config_params_s *conf) {
conf->wait_packets = SDL_atoi(wait_packets);
}

void read_key_config(ini_t *ini, config_params_s *conf) {
void read_key_config(const ini_t *ini, config_params_s *conf) {
// TODO: Some form of validation

const char *key_up = ini_get(ini, "keyboard", "key_up");
Expand Down Expand Up @@ -322,7 +322,7 @@ void read_key_config(ini_t *ini, config_params_s *conf) {
conf->key_jazz_dec_velocity = SDL_atoi(key_toggle_audio);
}

void read_gamepad_config(ini_t *ini, config_params_s *conf) {
void read_gamepad_config(const ini_t *ini, config_params_s *conf) {
// TODO: Some form of validation

const char *gamepad_up = ini_get(ini, "gamepad", "gamepad_up");
Expand Down
60 changes: 30 additions & 30 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@

typedef struct config_params_s {
char *filename;
int init_fullscreen;
int init_use_gpu;
int idle_ms;
int wait_for_device;
int wait_packets;
int audio_enabled;
int audio_buffer_size;
unsigned int init_fullscreen;
unsigned int init_use_gpu;
unsigned int idle_ms;
unsigned int wait_for_device;
unsigned int wait_packets;
unsigned int audio_enabled;
unsigned int audio_buffer_size;
const char *audio_device_name;

int key_up;
int key_left;
int key_down;
int key_right;
int key_select;
int key_select_alt;
int key_start;
int key_start_alt;
int key_opt;
int key_opt_alt;
int key_edit;
int key_edit_alt;
int key_delete;
int key_reset;
int key_jazz_inc_octave;
int key_jazz_dec_octave;
int key_jazz_inc_velocity;
int key_jazz_dec_velocity;
int key_toggle_audio;
unsigned int key_up;
unsigned int key_left;
unsigned int key_down;
unsigned int key_right;
unsigned int key_select;
unsigned int key_select_alt;
unsigned int key_start;
unsigned int key_start_alt;
unsigned int key_opt;
unsigned int key_opt_alt;
unsigned int key_edit;
unsigned int key_edit_alt;
unsigned int key_delete;
unsigned int key_reset;
unsigned int key_jazz_inc_octave;
unsigned int key_jazz_dec_octave;
unsigned int key_jazz_inc_velocity;
unsigned int key_jazz_dec_velocity;
unsigned int key_toggle_audio;

int gamepad_up;
int gamepad_left;
Expand All @@ -62,9 +62,9 @@ typedef struct config_params_s {

config_params_s init_config();
void read_config(config_params_s *conf);
void read_audio_config(ini_t *config, config_params_s *conf);
void read_graphics_config(ini_t *config, config_params_s *conf);
void read_key_config(ini_t *config, config_params_s *conf);
void read_gamepad_config(ini_t *config, config_params_s *conf);
void read_audio_config(const ini_t *ini, config_params_s *conf);
void read_graphics_config(const ini_t *ini, config_params_s *conf);
void read_key_config(const ini_t *ini, config_params_s *conf);
void read_gamepad_config(const ini_t *ini, config_params_s *conf);

#endif
34 changes: 16 additions & 18 deletions src/fx_cube.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "SDL2_inprint.h"
#include "SDL_pixels.h"
#include "SDL_render.h"
#include <SDL.h>
#include <time.h>
#include "SDL2_inprint.h"

static SDL_Texture *texture_cube;
static SDL_Texture *texture_text;
Expand All @@ -20,22 +18,22 @@ static int edges[12][2] = {{0, 1}, {1, 3}, {3, 2}, {2, 0}, {4, 5}, {5, 7},

static float nodes[8][3];

static void scale(float factor0, float factor1, float factor2) {
static void scale(const float factor0, const float factor1, const float factor2) {
for (int i = 0; i < 8; i++) {
nodes[i][0] *= factor0;
nodes[i][1] *= factor1;
nodes[i][2] *= factor2;
}
}

static void rotate_cube(float angle_x, float angle_y) {
float sin_x = SDL_sin(angle_x);
float cos_x = SDL_cos(angle_x);
float sin_y = SDL_sin(angle_y);
float cos_y = SDL_cos(angle_y);
static void rotate_cube(const float angle_x, const float angle_y) {
const float sin_x = SDL_sin(angle_x);
const float cos_x = SDL_cos(angle_x);
const float sin_y = SDL_sin(angle_y);
const float cos_y = SDL_cos(angle_y);
for (int i = 0; i < 8; i++) {
float x = nodes[i][0];
float y = nodes[i][1];
const float x = nodes[i][0];
const float y = nodes[i][1];
float z = nodes[i][2];

nodes[i][0] = x * cos_x - z * sin_x;
Expand All @@ -48,9 +46,9 @@ static void rotate_cube(float angle_x, float angle_y) {
}
}

void fx_cube_init(SDL_Renderer *target_renderer, SDL_Color foreground_color,
unsigned int texture_width, unsigned int texture_height,
unsigned int font_glyph_width) {
void fx_cube_init(SDL_Renderer *target_renderer, const SDL_Color foreground_color,
const unsigned int texture_width, const unsigned int texture_height,
const unsigned int font_glyph_width) {

fx_renderer = target_renderer;
line_color = foreground_color;
Expand Down Expand Up @@ -102,15 +100,15 @@ void fx_cube_update() {
SDL_SetRenderDrawColor(fx_renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(fx_renderer);

unsigned int seconds = SDL_GetTicks() / 1000;
float scalefactor = 1 + (SDL_sin(seconds) * 0.005);
const unsigned int seconds = SDL_GetTicks() / 1000;
const float scalefactor = 1 + SDL_sin(seconds) * 0.005;

scale(scalefactor, scalefactor, scalefactor);
rotate_cube(M_PI / 180, M_PI / 270);

for (int i = 0; i < 12; i++) {
float *p1 = nodes[edges[i][0]];
float *p2 = nodes[edges[i][1]];
const float *p1 = nodes[edges[i][0]];
const float *p2 = nodes[edges[i][1]];
points[points_counter++] = (SDL_Point){p1[0] + center_x, nodes[edges[i][0]][1] + center_y};
points[points_counter++] = (SDL_Point){p2[0] + center_x, p2[1] + center_y};
}
Expand Down
14 changes: 7 additions & 7 deletions src/gamecontrollers.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int gamecontrollers_initialize() {
}

if (db_rw != NULL) {
int mappings = SDL_GameControllerAddMappingsFromRW(db_rw, 1);
const int mappings = SDL_GameControllerAddMappingsFromRW(db_rw, 1);
if (mappings != -1)
SDL_Log("Found %d game controller mappings", mappings);
else
Expand Down Expand Up @@ -66,8 +66,8 @@ void gamecontrollers_close() {
}

// Check whether a button is pressed on a gamepad and return 1 if pressed.
static int get_game_controller_button(config_params_s *conf, SDL_GameController *controller,
int button) {
static int get_game_controller_button(const config_params_s *conf, SDL_GameController *controller,
const int button) {

const int button_mappings[8] = {conf->gamepad_up, conf->gamepad_down, conf->gamepad_left,
conf->gamepad_right, conf->gamepad_opt, conf->gamepad_edit,
Expand Down Expand Up @@ -111,7 +111,7 @@ static int get_game_controller_button(config_params_s *conf, SDL_GameController

// Handle game controllers, simply check all buttons and analog axis on every
// cycle
int gamecontrollers_handle_buttons(config_params_s *conf) {
int gamecontrollers_handle_buttons(const config_params_s *conf) {

const int keycodes[8] = {key_up, key_down, key_left, key_right,
key_opt, key_edit, key_select, key_start};
Expand All @@ -133,18 +133,18 @@ int gamecontrollers_handle_buttons(config_params_s *conf) {
return key;
}

input_msg_s gamecontrollers_handle_special_messages(config_params_s *conf) {
input_msg_s gamecontrollers_handle_special_messages(const config_params_s *conf) {
input_msg_s msg = {0};
// Read special case game controller buttons quit and reset
for (int gc = 0; gc < num_joysticks; gc++) {
if (SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_quit) &&
(SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_select) ||
SDL_GameControllerGetAxis(game_controllers[gc], conf->gamepad_analog_axis_select)))
msg = (input_msg_s){special, msg_quit};
msg = (input_msg_s){special, msg_quit, 0, 0};
else if (SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_reset) &&
(SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_select) ||
SDL_GameControllerGetAxis(game_controllers[gc], conf->gamepad_analog_axis_select)))
msg = (input_msg_s){special, msg_reset_display};
msg = (input_msg_s){special, msg_reset_display, 0, 0};
}
return msg;
}
5 changes: 2 additions & 3 deletions src/gamecontrollers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

#include "config.h"
#include "input.h"
#include <SDL_gamecontroller.h>

#define MAX_CONTROLLERS 4

int gamecontrollers_initialize();
void gamecontrollers_close();
int gamecontrollers_handle_buttons(config_params_s *conf);
input_msg_s gamecontrollers_handle_special_messages(config_params_s *conf);
int gamecontrollers_handle_buttons(const config_params_s *conf);
input_msg_s gamecontrollers_handle_special_messages(const config_params_s *conf);

#endif //GAMECONTROLLERS_H_
Loading

0 comments on commit 1c4fa7f

Please sign in to comment.