From 3fdb201710eb9ce2fce0001c2caaf38643d6f67c Mon Sep 17 00:00:00 2001 From: Themaister Date: Fri, 24 Dec 2010 01:26:36 +0100 Subject: [PATCH] Some refactoring. Splitting up driver logic and file logic. --- Makefile | 2 +- driver.c | 148 +++++++++++++++++++++++++ driver.h | 21 ++++ file.c | 149 +++++++++++++++++++++++++ file.h | 15 +++ general.h | 19 ++++ ssnes.c | 323 +----------------------------------------------------- 7 files changed, 357 insertions(+), 320 deletions(-) create mode 100644 driver.c create mode 100644 file.c create mode 100644 file.h create mode 100644 general.h diff --git a/Makefile b/Makefile index d4d77d7451..740f056935 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ include config.mk TARGET = ssnes DEFINES = -OBJ = ssnes.o +OBJ = ssnes.o file.o driver.o libsnes = -lsnes LIBS = -lsamplerate $(libsnes) diff --git a/driver.c b/driver.c new file mode 100644 index 0000000000..ead0f443c3 --- /dev/null +++ b/driver.c @@ -0,0 +1,148 @@ +#include "driver.h" +#include "config.h" +#include "general.h" +#include + +void init_drivers(void) +{ + init_video_input(); + init_audio(); +} + +void uninit_drivers(void) +{ + uninit_video_input(); + uninit_audio(); +} + +void init_audio(void) +{ + if (!audio_enable) + { + audio_active = false; + return; + } + + driver.audio_data = driver.audio->init(audio_device, out_rate, out_latency); + if ( driver.audio_data == NULL ) + audio_active = false; + + if (!audio_sync && audio_active) + driver.audio->set_nonblock_state(driver.audio_data, true); + + int err; + source = src_new(SAMPLERATE_QUALITY, 2, &err); + if (!source) + audio_active = false; +} + +void uninit_audio(void) +{ + if (!audio_enable) + { + audio_active = false; + return; + } + + if ( driver.audio_data && driver.audio ) + driver.audio->free(driver.audio_data); + + if ( source ) + src_delete(source); +} + +void init_video_input(void) +{ + int scale; + + // We multiply scales with 2 to allow for hi-res games. +#if VIDEO_FILTER == FILTER_NONE + scale = 2; +#elif VIDEO_FILTER == FILTER_HQ2X + scale = 4; +#elif VIDEO_FILTER == FILTER_HQ4X + scale = 8; +#elif VIDEO_FILTER == FILTER_NTSC + scale = 8; +#elif VIDEO_FILTER == FILTER_GRAYSCALE + scale = 2; +#elif VIDEO_FILTER == FILTER_BLEED + scale = 2; +#else + scale = 2; +#endif + + video_info_t video = { + .width = (fullscreen) ? fullscreen_x : (296 * xscale), + .height = (fullscreen) ? fullscreen_y : (224 * yscale), + .fullscreen = fullscreen, + .vsync = vsync, + .force_aspect = force_aspect, + .smooth = video_smooth, + .input_scale = scale, + }; + + const input_driver_t *tmp = driver.input; + driver.video_data = driver.video->init(&video, &(driver.input)); + + if ( driver.video_data == NULL ) + { + SSNES_ERR("Cannot open video driver... Exiting ...\n"); + exit(1); + } + + if ( driver.input != NULL ) + { + driver.input_data = driver.video_data; + } + else + { + driver.input = tmp; + if (driver.input != NULL) + { + driver.input_data = driver.input->init(); + if ( driver.input_data == NULL ) + exit(1); + } + else + { + SSNES_ERR("Cannot find input driver. Exiting ...\n"); + exit(1); + } + } +} + +void uninit_video_input(void) +{ + if ( driver.video_data && driver.video ) + driver.video->free(driver.video_data); + + if ( driver.input_data != driver.video_data && driver.input ) + driver.input->free(driver.input_data); +} + +bool video_active = true; +bool audio_active = true; + +driver_t driver = { +#if VIDEO_DRIVER == VIDEO_GL + .video = &video_gl, +#else +#error "Define a valid video driver in config.h" +#endif + +#if AUDIO_DRIVER == AUDIO_RSOUND + .audio = &audio_rsound, +#elif AUDIO_DRIVER == AUDIO_OSS + .audio = &audio_oss, +#elif AUDIO_DRIVER == AUDIO_ALSA + .audio = &audio_alsa, +#elif AUDIO_DRIVER == AUDIO_ROAR + .audio = &audio_roar, +#elif AUDIO_DRIVER == AUDIO_AL + .audio = &audio_openal, +#else +#error "Define a valid audio driver in config.h" +#endif +}; + diff --git a/driver.h b/driver.h index 99e4cbce99..d5e215da9f 100644 --- a/driver.h +++ b/driver.h @@ -84,4 +84,25 @@ typedef struct driver void *input_data; } driver_t; +void init_drivers(void); +void uninit_drivers(void); + +void init_video_input(void); +void uninit_video_input(void); +void init_audio(void); +void uninit_audio(void); + +extern bool video_active; +extern bool audio_active; +extern driver_t driver; + +//////////////////////////////////////////////// Backends +extern const audio_driver_t audio_rsound; +extern const audio_driver_t audio_oss; +extern const audio_driver_t audio_alsa; +extern const audio_driver_t audio_roar; +extern const audio_driver_t audio_openal; +extern const video_driver_t video_gl; +//////////////////////////////////////////////// + #endif diff --git a/file.c b/file.c new file mode 100644 index 0000000000..c63f211c84 --- /dev/null +++ b/file.c @@ -0,0 +1,149 @@ +#include "file.h" +#include "general.h" +#include +#include +#include +#include + +ssize_t read_file(FILE* file, void** buf) +{ + ssize_t ret; + if (file == NULL) // stdin + { + SSNES_LOG("Reading ROM from stdin ...\n"); + size_t buf_size = 0xFFFFF; // Some initial guesstimate. + size_t buf_ptr = 0; + char *rom_buf = malloc(buf_size); + if (rom_buf == NULL) + { + SSNES_ERR("Couldn't allocate memory!\n"); + return -1; + } + + for(;;) + { + size_t ret = fread(rom_buf + buf_ptr, 1, buf_size - buf_ptr, stdin); + buf_ptr += ret; + + // We've reached the end + if (buf_ptr < buf_size) + break; + + rom_buf = realloc(rom_buf, buf_size * 2); + if (rom_buf == NULL) + { + SSNES_ERR("Couldn't allocate memory!\n"); + return -1; + } + + buf_size *= 2; + } + + if ((buf_ptr & 0x7fff) == 512) + { + memmove(rom_buf, rom_buf + 512, buf_ptr - 512); + buf_ptr -= 512; + } + + *buf = rom_buf; + ret = buf_ptr; + } + else + { + fseek(file, 0, SEEK_END); + long length = ftell(file); + rewind(file); + if ((length & 0x7fff) == 512) + { + length -= 512; + fseek(file, 512, SEEK_SET); + } + + void *rom_buf = malloc(length); + if ( rom_buf == NULL ) + { + SSNES_ERR("Couldn't allocate memory!\n"); + return -1; + } + + if ( fread(rom_buf, 1, length, file) < length ) + { + SSNES_ERR("Didn't read whole file.\n"); + free(rom_buf); + return -1; + } + *buf = rom_buf; + ret = length; + } + return ret; +} + +void write_file(const char* path, uint8_t* data, size_t size) +{ + FILE *file = fopen(path, "wb"); + if ( file != NULL ) + { + SSNES_LOG("Saving state \"%s\". Size: %d bytes.\n", path, (int)size); + snes_serialize(data, size); + if ( fwrite(data, 1, size, file) != size ) + SSNES_ERR("Did not save state properly.\n"); + fclose(file); + } +} + +void load_state(const char* path, uint8_t* data, size_t size) +{ + SSNES_LOG("Loading state: \"%s\".\n", path); + FILE *file = fopen(path, "rb"); + if ( file != NULL ) + { + //fprintf(stderr, "SSNES: Loading state. Size: %d bytes.\n", (int)size); + if ( fread(data, 1, size, file) != size ) + SSNES_ERR("Did not load state properly.\n"); + fclose(file); + snes_unserialize(data, size); + } + else + { + SSNES_LOG("No state file found. Will create new.\n"); + } +} + +void load_save_file(const char* path, int type) +{ + FILE *file; + + file = fopen(path, "rb"); + if ( !file ) + { + return; + } + + size_t size = snes_get_memory_size(type); + uint8_t *data = snes_get_memory_data(type); + + if (size == 0 || !data) + { + fclose(file); + return; + } + + int rc = fread(data, 1, size, file); + if ( rc != size ) + { + SSNES_ERR("Couldn't load save file.\n"); + } + + SSNES_LOG("Loaded save file: \"%s\"\n", path); + + fclose(file); +} + +void save_file(const char* path, int type) +{ + size_t size = snes_get_memory_size(type); + uint8_t *data = snes_get_memory_data(type); + + if ( data && size > 0 ) + write_file(path, data, size); +} diff --git a/file.h b/file.h new file mode 100644 index 0000000000..4aa0206258 --- /dev/null +++ b/file.h @@ -0,0 +1,15 @@ +#ifndef __SSNES_FILE_H +#define __SSNES_FILE_H + +#include +#include +#include + +ssize_t read_file(FILE *file, void **buf); + +void load_state(const char* path, uint8_t* data, size_t size); +void write_file(const char* path, uint8_t* data, size_t size); +void load_save_file(const char* path, int type); +void save_file(const char* path, int type); + +#endif diff --git a/general.h b/general.h new file mode 100644 index 0000000000..689bbf2a1a --- /dev/null +++ b/general.h @@ -0,0 +1,19 @@ +#ifndef __SSNES_GENERAL_H +#define __SSNES_GENERAL_H + +#include +#include + +#define SSNES_LOG(msg, args...) do { \ + if (verbose) \ + fprintf(stderr, "SSNES: " msg, ##args); \ + } while(0) + +#define SSNES_ERR(msg, args...) do { \ + fprintf(stderr, "SSNES [ERROR] :: " msg, ##args); \ + } while(0) + +extern bool verbose; +extern SRC_STATE *source; + +#endif diff --git a/ssnes.c b/ssnes.c index 2cc3e0deac..c19213a723 100644 --- a/ssnes.c +++ b/ssnes.c @@ -26,68 +26,12 @@ #include #include "config.h" #include "driver.h" +#include "file.h" #include "hqflt/pastlib.h" #include "hqflt/grayscale.h" #include "hqflt/bleed.h" #include "hqflt/ntsc.h" - -static bool video_active = true; -static bool audio_active = true; - -static SRC_STATE* source = NULL; - -//////////////////////////////////////////////// Backends -extern const audio_driver_t audio_rsound; -extern const audio_driver_t audio_oss; -extern const audio_driver_t audio_alsa; -extern const audio_driver_t audio_roar; -extern const audio_driver_t audio_openal; -extern const video_driver_t video_gl; -//////////////////////////////////////////////// - -static driver_t driver = { -#if VIDEO_DRIVER == VIDEO_GL - .video = &video_gl, -#else -#error "Define a valid video driver in config.h" -#endif - -#if AUDIO_DRIVER == AUDIO_RSOUND - .audio = &audio_rsound, -#elif AUDIO_DRIVER == AUDIO_OSS - .audio = &audio_oss, -#elif AUDIO_DRIVER == AUDIO_ALSA - .audio = &audio_alsa, -#elif AUDIO_DRIVER == AUDIO_ROAR - .audio = &audio_roar, -#elif AUDIO_DRIVER == AUDIO_AL - .audio = &audio_openal, -#else -#error "Define a valid audio driver in config.h" -#endif -}; - -static void init_drivers(void); -static void uninit_drivers(void); - -static void init_video_input(void); -static void uninit_video_input(void); -static void init_audio(void); -static void uninit_audio(void); - -static void load_state(const char* path, uint8_t* data, size_t size); -static void write_file(const char* path, uint8_t* data, size_t size); -static void load_save_file(const char* path, int type); -static void save_file(const char* path, int type); - -#define SSNES_LOG(msg, args...) do { \ - if (verbose) \ - fprintf(stderr, "SSNES: " msg, ##args); \ - } while(0) - -#define SSNES_ERR(msg, args...) do { \ - fprintf(stderr, "SSNES [ERROR] :: " msg, ##args); \ - } while(0) +#include "general.h" // To avoid continous switching if we hold the button down, we require that the button must go from pressed, unpressed back to pressed to be able to toggle between then. @@ -114,124 +58,6 @@ void set_fast_forward_button(bool new_button_state) old_button_state = new_button_state; } -static void init_drivers(void) -{ - init_video_input(); - init_audio(); -} - -static void uninit_drivers(void) -{ - uninit_video_input(); - uninit_audio(); -} - -static void init_audio(void) -{ - if (!audio_enable) - { - audio_active = false; - return; - } - - driver.audio_data = driver.audio->init(audio_device, out_rate, out_latency); - if ( driver.audio_data == NULL ) - audio_active = false; - - if (!audio_sync && audio_active) - driver.audio->set_nonblock_state(driver.audio_data, true); - - int err; - source = src_new(SAMPLERATE_QUALITY, 2, &err); - if (!source) - audio_active = false; -} - -static void uninit_audio(void) -{ - if (!audio_enable) - { - audio_active = false; - return; - } - - if ( driver.audio_data && driver.audio ) - driver.audio->free(driver.audio_data); - - if ( source ) - src_delete(source); -} - -static void init_video_input(void) -{ - int scale; - - // We multiply scales with 2 to allow for hi-res games. -#if VIDEO_FILTER == FILTER_NONE - scale = 2; -#elif VIDEO_FILTER == FILTER_HQ2X - scale = 4; -#elif VIDEO_FILTER == FILTER_HQ4X - scale = 8; -#elif VIDEO_FILTER == FILTER_NTSC - scale = 8; -#elif VIDEO_FILTER == FILTER_GRAYSCALE - scale = 2; -#elif VIDEO_FILTER == FILTER_BLEED - scale = 2; -#else - scale = 2; -#endif - - video_info_t video = { - .width = (fullscreen) ? fullscreen_x : (296 * xscale), - .height = (fullscreen) ? fullscreen_y : (224 * yscale), - .fullscreen = fullscreen, - .vsync = vsync, - .force_aspect = force_aspect, - .smooth = video_smooth, - .input_scale = scale, - }; - - const input_driver_t *tmp = driver.input; - driver.video_data = driver.video->init(&video, &(driver.input)); - - if ( driver.video_data == NULL ) - { - SSNES_ERR("Cannot open video driver... Exiting ...\n"); - exit(1); - } - - if ( driver.input != NULL ) - { - driver.input_data = driver.video_data; - } - else - { - driver.input = tmp; - if (driver.input != NULL) - { - driver.input_data = driver.input->init(); - if ( driver.input_data == NULL ) - exit(1); - } - else - { - SSNES_ERR("Cannot find input driver. Exiting ...\n"); - exit(1); - } - } -} - -static void uninit_video_input(void) -{ - if ( driver.video_data && driver.video ) - driver.video->free(driver.video_data); - - if ( driver.input_data != driver.video_data && driver.input ) - driver.input->free(driver.input_data); -} - #if VIDEO_FILTER != FILTER_NONE static inline void process_frame (uint16_t * restrict out, const uint16_t * restrict in, unsigned width, unsigned height) { @@ -296,6 +122,7 @@ static void video_frame(const uint16_t *data, unsigned width, unsigned height) } +SRC_STATE* source = NULL; static void audio_sample(uint16_t left, uint16_t right) { if ( !audio_active ) @@ -374,7 +201,7 @@ static void print_help(void) static FILE* rom_file = NULL; static char savefile_name_srm[256] = {0}; -static bool verbose = false; +bool verbose = false; #ifdef HAVE_CG char cg_shader_path[256] = DEFAULT_CG_SHADER; #endif @@ -472,79 +299,6 @@ static void parse_input(int argc, char *argv[]) } } -static ssize_t read_file(FILE* file, void** buf) -{ - ssize_t ret; - if (file == NULL) // stdin - { - SSNES_LOG("Reading ROM from stdin ...\n"); - size_t buf_size = 0xFFFFF; // Some initial guesstimate. - size_t buf_ptr = 0; - char *rom_buf = malloc(buf_size); - if (rom_buf == NULL) - { - SSNES_ERR("Couldn't allocate memory!\n"); - return -1; - } - - for(;;) - { - size_t ret = fread(rom_buf + buf_ptr, 1, buf_size - buf_ptr, stdin); - buf_ptr += ret; - - // We've reached the end - if (buf_ptr < buf_size) - break; - - rom_buf = realloc(rom_buf, buf_size * 2); - if (rom_buf == NULL) - { - SSNES_ERR("Couldn't allocate memory!\n"); - return -1; - } - - buf_size *= 2; - } - - if ((buf_ptr & 0x7fff) == 512) - { - memmove(rom_buf, rom_buf + 512, buf_ptr - 512); - buf_ptr -= 512; - } - - *buf = rom_buf; - ret = buf_ptr; - } - else - { - fseek(file, 0, SEEK_END); - long length = ftell(file); - rewind(file); - if ((length & 0x7fff) == 512) - { - length -= 512; - fseek(file, 512, SEEK_SET); - } - - void *rom_buf = malloc(length); - if ( rom_buf == NULL ) - { - SSNES_ERR("Couldn't allocate memory!\n"); - return -1; - } - - if ( fread(rom_buf, 1, length, file) < length ) - { - SSNES_ERR("Didn't read whole file.\n"); - free(rom_buf); - return -1; - } - *buf = rom_buf; - ret = length; - } - return ret; -} - int main(int argc, char *argv[]) { snes_init(); @@ -638,72 +392,3 @@ error: return 1; } -static void write_file(const char* path, uint8_t* data, size_t size) -{ - FILE *file = fopen(path, "wb"); - if ( file != NULL ) - { - SSNES_LOG("Saving state \"%s\". Size: %d bytes.\n", path, (int)size); - snes_serialize(data, size); - if ( fwrite(data, 1, size, file) != size ) - SSNES_ERR("Did not save state properly.\n"); - fclose(file); - } -} - -static void load_state(const char* path, uint8_t* data, size_t size) -{ - SSNES_LOG("Loading state: \"%s\".\n", path); - FILE *file = fopen(path, "rb"); - if ( file != NULL ) - { - //fprintf(stderr, "SSNES: Loading state. Size: %d bytes.\n", (int)size); - if ( fread(data, 1, size, file) != size ) - SSNES_ERR("Did not load state properly.\n"); - fclose(file); - snes_unserialize(data, size); - } - else - { - SSNES_LOG("No state file found. Will create new.\n"); - } -} - -static void load_save_file(const char* path, int type) -{ - FILE *file; - - file = fopen(path, "rb"); - if ( !file ) - { - return; - } - - size_t size = snes_get_memory_size(type); - uint8_t *data = snes_get_memory_data(type); - - if (size == 0 || !data) - { - fclose(file); - return; - } - - int rc = fread(data, 1, size, file); - if ( rc != size ) - { - SSNES_ERR("Couldn't load save file.\n"); - } - - SSNES_LOG("Loaded save file: \"%s\"\n", path); - - fclose(file); -} - -static void save_file(const char* path, int type) -{ - size_t size = snes_get_memory_size(type); - uint8_t *data = snes_get_memory_data(type); - - if ( data && size > 0 ) - write_file(path, data, size); -}