From 4d5dc6a30eacc8e04a3008ddbcdf46c4dd119114 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Wed, 24 May 2017 21:44:43 +0300 Subject: [PATCH] Cleanup --- Core/apu.c | 8 --- Core/display.c | 1 - Core/gb.c | 7 +- Core/symbol_hash.c | 5 +- Makefile | 2 +- SDL/main.c | 163 +++++++++++++------------------------------ SDL/utils.c | 76 ++++++++++++++++++++ SDL/utils.h | 8 +++ Windows/math.h | 7 ++ Windows/stdint.h | 3 + Windows/stdio.h | 4 +- Windows/string.h | 3 + Windows/sys/select.h | 0 Windows/sys/time.h | 0 Windows/unistd.h | 0 15 files changed, 154 insertions(+), 133 deletions(-) create mode 100644 SDL/utils.c create mode 100644 SDL/utils.h create mode 100755 Windows/math.h create mode 100755 Windows/stdint.h create mode 100755 Windows/string.h delete mode 100755 Windows/sys/select.h delete mode 100755 Windows/sys/time.h delete mode 100755 Windows/unistd.h diff --git a/Core/apu.c b/Core/apu.c index 5b43d77..cb87e66 100755 --- a/Core/apu.c +++ b/Core/apu.c @@ -15,14 +15,6 @@ _a > _b ? _a : _b; }) __typeof__ (b) _b = (b); \ _a < _b ? _a : _b; }) -#ifdef _WIN32 -/* "Old" (Pre-2015) Windows headers/libc don't have round. */ -static inline double round(double f) -{ - return f >= 0? (int)(f + 0.5) : (int)(f - 0.5); -} -#endif - #define APU_FREQUENCY 0x80000 static int16_t generate_square(uint64_t phase, uint32_t wave_length, int16_t amplitude, uint8_t duty) diff --git a/Core/display.c b/Core/display.c index 61a31f3..c8c326e 100755 --- a/Core/display.c +++ b/Core/display.c @@ -1,5 +1,4 @@ #include -#include #include #include #include diff --git a/Core/gb.c b/Core/gb.c index cf334b5..a0dc414 100755 --- a/Core/gb.c +++ b/Core/gb.c @@ -2,12 +2,13 @@ #include #include #include -#include #include #include #include -#include +#ifndef _WIN32 +#include #include +#endif #include "gb.h" void GB_attributed_logv(GB_gameboy_t *gb, GB_log_attributes attributes, const char *fmt, va_list args) @@ -34,7 +35,7 @@ void GB_attributed_log(GB_gameboy_t *gb, GB_log_attributes attributes, const cha va_end(args); } -void GB_log(GB_gameboy_t *gb,const char *fmt, ...) +void GB_log(GB_gameboy_t *gb, const char *fmt, ...) { va_list args; va_start(args, fmt); diff --git a/Core/symbol_hash.c b/Core/symbol_hash.c index 37664e1..709421c 100755 --- a/Core/symbol_hash.c +++ b/Core/symbol_hash.c @@ -1,7 +1,4 @@ #include "gb.h" -#ifdef _WIN32 -typedef intptr_t ssize_t; -#endif static size_t GB_map_find_symbol_index(GB_symbol_map_t *map, uint16_t addr) { @@ -106,4 +103,4 @@ const GB_symbol_t *GB_reversed_map_find_symbol(GB_reversed_symbol_map_t *map, co } return NULL; -} \ No newline at end of file +} diff --git a/Makefile b/Makefile index 3205ec3..15f675f 100755 --- a/Makefile +++ b/Makefile @@ -60,7 +60,7 @@ SDL_LDFLAGS := -F/Library/Frameworks -framework SDL2 endif CFLAGS += -Wno-deprecated-declarations ifeq ($(PLATFORM),windows32) -CFLAGS += -Wno-deprecated-declarations -Dstrdup=_strdup # Seems like Microsoft deprecated every single LIBC function +CFLAGS += -Wno-deprecated-declarations # Seems like Microsoft deprecated every single LIBC function LDFLAGS += -Wl,/NODEFAULTLIB:libcmt.lib endif diff --git a/SDL/main.c b/SDL/main.c index c9afe0a..072a97f 100755 --- a/SDL/main.c +++ b/SDL/main.c @@ -1,27 +1,26 @@ -#include #include -#include -#include -#include #include #include +#include "gb.h" + +#include "utils.h" #ifndef _WIN32 #define AUDIO_FREQUENCY 96000 #else /* Windows (well, at least my VM) can't handle 96KHz sound well :( */ #define AUDIO_FREQUENCY 44100 -#include -#include -#define snprintf _snprintf #endif -#include "gb.h" + +GB_gameboy_t gb; +static bool dmg = false; +static bool paused = false; +static uint32_t pixels[160*144]; static char *filename = NULL; static bool should_free_filename = false; static char *battery_save_path_ptr; -static void replace_extension(const char *src, size_t length, char *dest, const char *ext); static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; @@ -29,11 +28,6 @@ static SDL_Texture *texture = NULL; static SDL_PixelFormat *pixel_format = NULL; static SDL_AudioSpec want_aspec, have_aspec; -static bool paused = false; - -static uint32_t pixels[160*144]; -GB_gameboy_t gb; - static enum { GB_SDL_NO_COMMAND, GB_SDL_SAVE_STATE_COMMAND, @@ -59,8 +53,8 @@ static void update_viewport(void) double y_factor = win_height / 144.0; if (scaling_mode == GB_SDL_SCALING_INTEGER_FACTOR) { - x_factor = floor(x_factor); - y_factor = floor(y_factor); + x_factor = (int)(x_factor); + y_factor = (int)(y_factor); } if (scaling_mode != GB_SDL_SCALING_ENTIRE_WINDOW) { @@ -244,57 +238,7 @@ static void vblank(GB_gameboy_t *gb) handle_events(gb); } -#ifdef __APPLE__ -#include -#endif -static const char *executable_folder(void) -{ - static char path[1024] = {0,}; - if (path[0]) { - return path; - } - /* Ugly unportable code! :( */ -#ifdef __APPLE__ - unsigned int length = sizeof(path) - 1; - _NSGetExecutablePath(&path[0], &length); -#else -#ifdef __linux__ - ssize_t length = readlink("/proc/self/exe", &path[0], sizeof(path) - 1); - assert (length != -1); -#else -#ifdef _WIN32 - HMODULE hModule = GetModuleHandle(NULL); - GetModuleFileName(hModule, path, sizeof(path) - 1); -#else - /* No OS-specific way, assume running from CWD */ - getcwd(&path[0], sizeof(path) - 1); - return path; -#endif -#endif -#endif - size_t pos = strlen(path); - while (pos) { - pos--; -#ifdef _WIN32 - if (path[pos] == '\\') { -#else - if (path[pos] == '/') { -#endif - path[pos] = 0; - break; - } - } - return path; -} - -static char *executable_relative_path(const char *filename) -{ - static char path[1024]; - snprintf(path, sizeof(path), "%s/%s", executable_folder(), filename); - return path; -} - static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b) { return SDL_MapRGB(pixel_format, r, g, b); @@ -319,27 +263,43 @@ static void audio_callback(void *gb, Uint8 *stream, int len) memset(stream, 0, len); } } - -static void replace_extension(const char *src, size_t length, char *dest, const char *ext) + +static bool handle_pending_command(void) { - memcpy(dest, src, length); - dest[length] = 0; - - /* Remove extension */ - for (size_t i = length; i--;) { - if (dest[i] == '/') break; - if (dest[i] == '.') { - dest[i] = 0; - break; + switch (pending_command) { + case GB_SDL_LOAD_STATE_COMMAND: + case GB_SDL_SAVE_STATE_COMMAND: { + char save_path[strlen(filename) + 4]; + char save_extension[] = ".s0"; + save_extension[2] += command_parameter; + replace_extension(filename, strlen(filename), save_path, save_extension); + + if (pending_command == GB_SDL_LOAD_STATE_COMMAND) { + GB_load_state(&gb, save_path); + } + else { + GB_save_state(&gb, save_path); + } + return false; } + + case GB_SDL_RESET_COMMAND: + GB_reset(&gb); + return false; + + case GB_SDL_NO_COMMAND: + return false; + + case GB_SDL_NEW_FILE_COMMAND: + return true; + + case GB_SDL_TOGGLE_MODEL_COMMAND: + dmg = !dmg; + return true; } - - /* Add new extension */ - strcat(dest, ext); + return false; } - -static bool dmg = false; - + static void run(void) { restart: @@ -402,38 +362,11 @@ restart: else { GB_run(&gb); } - switch (pending_command) { - case GB_SDL_LOAD_STATE_COMMAND: - case GB_SDL_SAVE_STATE_COMMAND: { - char save_path[strlen(filename) + 4]; - char save_extension[] = ".s0"; - save_extension[2] += command_parameter; - replace_extension(filename, strlen(filename), save_path, save_extension); - - if (pending_command == GB_SDL_LOAD_STATE_COMMAND) { - GB_load_state(&gb, save_path); - } - else { - GB_save_state(&gb, save_path); - } - break; - } - - case GB_SDL_RESET_COMMAND: - GB_reset(&gb); - break; - - case GB_SDL_NO_COMMAND: - break; - - case GB_SDL_NEW_FILE_COMMAND: - pending_command = GB_SDL_NO_COMMAND; - goto restart; - - case GB_SDL_TOGGLE_MODEL_COMMAND: - dmg = !dmg; - pending_command = GB_SDL_NO_COMMAND; - goto restart; + + /* These commands can't run in the handle_event function, because they're not safe in a vblank context. */ + if (handle_pending_command()) { + pending_command = GB_SDL_NO_COMMAND; + goto restart; } pending_command = GB_SDL_NO_COMMAND; } diff --git a/SDL/utils.c b/SDL/utils.c new file mode 100644 index 0000000..56ba7ac --- /dev/null +++ b/SDL/utils.c @@ -0,0 +1,76 @@ +#include +#include +#ifdef __APPLE__ +#include +#endif +#ifdef _WIN32 +#include +#include +#endif +#include "utils.h" + +const char *executable_folder(void) +{ + static char path[1024] = {0,}; + if (path[0]) { + return path; + } + /* Ugly unportable code! :( */ +#ifdef __APPLE__ + unsigned int length = sizeof(path) - 1; + _NSGetExecutablePath(&path[0], &length); +#else +#ifdef __linux__ + ssize_t length = readlink("/proc/self/exe", &path[0], sizeof(path) - 1); + assert (length != -1); +#else +#ifdef _WIN32 + HMODULE hModule = GetModuleHandle(NULL); + GetModuleFileName(hModule, path, sizeof(path) - 1); +#else + /* No OS-specific way, assume running from CWD */ + getcwd(&path[0], sizeof(path) - 1); + return path; +#endif +#endif +#endif + size_t pos = strlen(path); + while (pos) { + pos--; +#ifdef _WIN32 + if (path[pos] == '\\') { +#else + if (path[pos] == '/') { +#endif + path[pos] = 0; + break; + } + } + return path; +} + +char *executable_relative_path(const char *filename) +{ + static char path[1024]; + snprintf(path, sizeof(path), "%s/%s", executable_folder(), filename); + return path; +} + + +void replace_extension(const char *src, size_t length, char *dest, const char *ext) +{ + memcpy(dest, src, length); + dest[length] = 0; + + /* Remove extension */ + for (size_t i = length; i--;) { + if (dest[i] == '/') break; + if (dest[i] == '.') { + dest[i] = 0; + break; + } + } + + /* Add new extension */ + strcat(dest, ext); +} diff --git a/SDL/utils.h b/SDL/utils.h new file mode 100644 index 0000000..21e900a --- /dev/null +++ b/SDL/utils.h @@ -0,0 +1,8 @@ +#ifndef utils_h +#define utils_h + +const char *executable_folder(void); +char *executable_relative_path(const char *filename); +void replace_extension(const char *src, size_t length, char *dest, const char *ext); + +#endif /* utils_h */ diff --git a/Windows/math.h b/Windows/math.h new file mode 100755 index 0000000..41d6d8a --- /dev/null +++ b/Windows/math.h @@ -0,0 +1,7 @@ +#pragma once +#include_next +/* "Old" (Pre-2015) Windows headers/libc don't have round. */ +static inline double round(double f) +{ + return f >= 0? (int)(f + 0.5) : (int)(f - 0.5); +} \ No newline at end of file diff --git a/Windows/stdint.h b/Windows/stdint.h new file mode 100755 index 0000000..cbe84d5 --- /dev/null +++ b/Windows/stdint.h @@ -0,0 +1,3 @@ +#pragma once +#include_next +typedef intptr_t ssize_t; \ No newline at end of file diff --git a/Windows/stdio.h b/Windows/stdio.h index 8ff90e4..33ced45 100755 --- a/Windows/stdio.h +++ b/Windows/stdio.h @@ -66,4 +66,6 @@ static inline size_t getline(char **lineptr, size_t *n, FILE *stream) { *n = size; return p - bufptr - 1; -} \ No newline at end of file +} + +#define snprintf _snprintf \ No newline at end of file diff --git a/Windows/string.h b/Windows/string.h new file mode 100755 index 0000000..b899ca9 --- /dev/null +++ b/Windows/string.h @@ -0,0 +1,3 @@ +#pragma once +#include_next +#define strdup _strdup \ No newline at end of file diff --git a/Windows/sys/select.h b/Windows/sys/select.h deleted file mode 100755 index e69de29..0000000 diff --git a/Windows/sys/time.h b/Windows/sys/time.h deleted file mode 100755 index e69de29..0000000 diff --git a/Windows/unistd.h b/Windows/unistd.h deleted file mode 100755 index e69de29..0000000