diff --git a/gcw0/Makefile b/gcw0/Makefile new file mode 100644 index 0000000..b17ced2 --- /dev/null +++ b/gcw0/Makefile @@ -0,0 +1,181 @@ +# Makefile for genplus SDL GCW0 +# +# (c) 1999, 2000, 2001, 2002, 2003 Charles MacDonald +# modified by Eke-Eke +# +# Defines : +# -DLSB_FIRST : for little endian systems. +# -DLOGERROR : enable message logging +# -DLOGVDP : enable VDP debug messages +# -DLOGSOUND : enable AUDIO debug messages +# -DLOG_SCD : enable SCD debug messages +# -DLOG_CDD : enable CDD debug messages +# -DLOG_CDC : enable CDC debug messages +# -DLOG_PCM : enable PCM debug messages +# -DLOGSOUND : enable AUDIO debug messages +# -D8BPP_RENDERING - configure for 8-bit pixels (RGB332) +# -D15BPP_RENDERING - configure for 15-bit pixels (RGB555) +# -D16BPP_RENDERING - configure for 16-bit pixels (RGB565) +# -D32BPP_RENDERING - configure for 32-bit pixels (RGB888) + +NAME = gen_gcw0 +SDL-CONFIG = /opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/bin/sdl-config + +CC = /opt/gcw0-toolchain/usr/bin/mipsel-gcw0-linux-uclibc-gcc +#CFLAGS = `$(SDL-CONFIG) --cflags` -03 -fomit-frame-pointer -Wall -ansi -std=c99 +#CFLAGS = `sdl-config --cflags` -O6 -fomit-frame-pointer -Wall -Wno-strict-aliasing -ansi -std=c99 +CFLAGS = `$(SDL-CONFIG) --cflags` -O3 -fomit-frame-pointer -Wall -ansi -std=c99 -march=mips32 -mtune=mips32r2 -mhard-float +#-fomit-frame-pointer +#LDFLAGS = -g +DEFINES = -DLSB_FIRST -DUSE_16BPP_RENDERING -DUSE_LIBVORBIS -DGCWZERO -DALIGN_LONG -DALT_RENDERER + +SRCDIR = ../core +INCLUDES = -I$(SRCDIR) -I$(SRCDIR)/z80 -I$(SRCDIR)/m68k \ + -I$(SRCDIR)/sound -I$(SRCDIR)/input_hw -I$(SRCDIR)/cart_hw -I$(SRCDIR)/cart_hw/svp \ + -I$(SRCDIR)/cd_hw -I$(SRCDIR)/ntsc \ + -I$(SRCDIR)/../gcw0 \ +-I/opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/include/ +# -I$(SRCDIR)/tremor + +LIBS = `$(SDL-CONFIG) --libs --libs` -lz -lm -lSDL_ttf -lSDL_image \ +-L/opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/lib -lvorbisfile + +OBJDIR = ./build_gcw0 + +OBJECTS = $(OBJDIR)/z80.o + +OBJECTS += $(OBJDIR)/m68kcpu.o \ + $(OBJDIR)/s68kcpu.o + +OBJECTS += $(OBJDIR)/genesis.o \ + $(OBJDIR)/vdp_ctrl.o \ + $(OBJDIR)/vdp_render.o \ + $(OBJDIR)/system.o \ + $(OBJDIR)/io_ctrl.o \ + $(OBJDIR)/mem68k.o \ + $(OBJDIR)/memz80.o \ + $(OBJDIR)/membnk.o \ + $(OBJDIR)/state.o \ + $(OBJDIR)/loadrom.o + +OBJECTS += $(OBJDIR)/input.o \ + $(OBJDIR)/gamepad.o \ + $(OBJDIR)/lightgun.o \ + $(OBJDIR)/mouse.o \ + $(OBJDIR)/activator.o \ + $(OBJDIR)/xe_1ap.o \ + $(OBJDIR)/teamplayer.o \ + $(OBJDIR)/paddle.o \ + $(OBJDIR)/sportspad.o \ + $(OBJDIR)/terebi_oekaki.o \ + $(OBJDIR)/graphic_board.o + +OBJECTS += $(OBJDIR)/sound.o \ + $(OBJDIR)/sn76489.o \ + $(OBJDIR)/ym2413.o \ + $(OBJDIR)/ym2612.o + +OBJECTS += $(OBJDIR)/blip_buf.o + +OBJECTS += $(OBJDIR)/eq.o + +OBJECTS += $(OBJDIR)/sram.o \ + $(OBJDIR)/svp.o \ + $(OBJDIR)/ssp16.o \ + $(OBJDIR)/ggenie.o \ + $(OBJDIR)/areplay.o \ + $(OBJDIR)/eeprom_93c.o \ + $(OBJDIR)/eeprom_i2c.o \ + $(OBJDIR)/eeprom_spi.o \ + $(OBJDIR)/md_cart.o \ + $(OBJDIR)/sms_cart.o + +OBJECTS += $(OBJDIR)/scd.o \ + $(OBJDIR)/cdd.o \ + $(OBJDIR)/cdc.o \ + $(OBJDIR)/gfx.o \ + $(OBJDIR)/pcm.o \ + $(OBJDIR)/cd_cart.o + +OBJECTS += $(OBJDIR)/sms_ntsc.o \ + $(OBJDIR)/md_ntsc.o + +OBJECTS += $(OBJDIR)/main.o \ + $(OBJDIR)/config.o \ + $(OBJDIR)/error.o \ + $(OBJDIR)/unzip.o \ + $(OBJDIR)/fileio.o \ + $(OBJDIR)/utils.o #\ +# $(OBJDIR)/menu.o + +#OBJECTS += $(OBJDIR)/block.o \ +# $(OBJDIR)/codebook.o \ +# $(OBJDIR)/floor0.o \ +# $(OBJDIR)/floor1.o \ +# $(OBJDIR)/framing.o \ +# $(OBJDIR)/info.o \ +# $(OBJDIR)/mapping0.o \ +# $(OBJDIR)/mdct.o \ +# $(OBJDIR)/registry.o \ +# $(OBJDIR)/res012.o \ +# $(OBJDIR)/sharedbook.o \ +# $(OBJDIR)/synthesis.o \ +# $(OBJDIR)/vorbisfile.o \ +# $(OBJDIR)/window.o +## 1st $(OBJDIR)/bitwise.o \ + + +all: $(NAME) + +$(NAME): $(OBJDIR) $(OBJECTS) + $(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@ + +$(OBJDIR) : + @[ -d $@ ] || mkdir -p $@ + +$(OBJDIR)/%.o : $(SRCDIR)/%.c $(SRCDIR)/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/sound/%.c $(SRCDIR)/sound/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/input_hw/%.c $(SRCDIR)/input_hw/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/cart_hw/%.c $(SRCDIR)/cart_hw/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/cart_hw/svp/%.c + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/cart_hw/svp/%.c $(SRCDIR)/cart_hw/svp/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/cd_hw/%.c $(SRCDIR)/cd_hw/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/z80/%.c $(SRCDIR)/z80/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/m68k/%.c + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/ntsc/%.c $(SRCDIR)/ntsc/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +#$(OBJDIR)/%.o : $(SRCDIR)/tremor/%.c $(SRCDIR)/tremor/%.h +# $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +#$(OBJDIR)/%.o : $(SRCDIR)/tremor/%.c +# $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + +$(OBJDIR)/%.o : $(SRCDIR)/../gcw0/%.c $(SRCDIR)/../gcw0/%.h + $(CC) -c $(CFLAGS) $(INCLUDES) $(DEFINES) $< -o $@ + + +pack : + strip $(NAME) + upx -9 $(NAME) + +clean: + rm -f $(OBJECTS) $(NAME) diff --git a/gcw0/config.c b/gcw0/config.c new file mode 100644 index 0000000..564cfaf --- /dev/null +++ b/gcw0/config.c @@ -0,0 +1,132 @@ +#include "osd.h" + +t_config config; + +static int config_load(void) +{ + //TODO: extract to function + const char *homedir; + if ((homedir = getenv("HOME")) == NULL) { + homedir = getpwuid(getuid())->pw_dir; + } + + /* open configuration file */ + char fname[MAXPATHLEN]; + sprintf (fname, "%s%s/config.ini", homedir, DEFAULT_PATH); + FILE *fp = fopen(fname, "rb"); + if (fp) + { + /* check file size */ + fseek(fp, 0, SEEK_END); + if (ftell(fp) != sizeof(config)) + { + fclose(fp); + return 0; + } + + /* read file */ + fseek(fp, 0, SEEK_SET); + fread(&config, sizeof(config), 1, fp); + fclose(fp); + return 1; + } + + return 0; +} + + +void set_config_defaults(void) +{ + int i; + /* sound options */ + config.psg_preamp = 150; + config.fm_preamp = 100; + config.hq_fm = 1; + config.psgBoostNoise = 1; + config.filter = 1; + config.low_freq = 200; + config.high_freq = 8000; + config.lg = 1.0; + config.mg = 1.0; + config.hg = 1.0; + config.lp_range = 0x9999; /* 0.6 in 16.16 fixed point */ + config.dac_bits = 14; + config.ym2413 = 1; /* = AUTO (0 = always OFF, 1 = always ON) */ + config.mono = 0; + + /* system options */ + config.system = 0; /* = AUTO (or SYSTEM_SG, SYSTEM_MARKIII, SYSTEM_SMS, SYSTEM_SMS2, SYSTEM_GG, SYSTEM_MD) */ + config.region_detect = 0; /* = AUTO (1 = USA, 2 = EUROPE, 3 = JAPAN/NTSC, 4 = JAPAN/PAL) */ + config.vdp_mode = 0; /* = AUTO (1 = NTSC, 2 = PAL) */ + config.master_clock = 0; /* = AUTO (1 = NTSC, 2 = PAL) */ + config.force_dtack = 0; + config.addr_error = 1; + config.bios = 0; + config.lock_on = 0; /* = OFF (can be TYPE_SK, TYPE_GG & TYPE_AR) */ + config.ntsc = 0; + config.lcd = 0; /* 0.8 fixed point */ + + /* display options */ + config.overscan = 0; /* 3 = all borders (0 = no borders , 1 = vertical borders only, 2 = horizontal borders only) */ + config.gg_extra = 0; /* 1 = show extended Game Gear screen (256x192) */ + config.render = 0; /* 1 = double resolution output (only when interlaced mode 2 is enabled) */ + config.gcw0_fullscreen = 1; /* 1 = use IPU scaling */ + config.keepaspectratio = 1; /* 1 = aspect ratio correct with black bars, 0 = fullscreen without correct aspect ratio */ + config.gg_scanlines = 1; /* 1 = use scanlines on Game Gear */ + config.smsmaskleftbar = 1; /* 1 = Mask left bar on SMS (better for horizontal scrolling) */ + config.sl_autoresume = 1; /* 1 = Automatically resume when saving and loading snapshots */ + config.a_stick = 1; /* 1 = A-Stick on */ + config.lightgun_speed = 1; /* 1 = simple speed multiplier for lightgun */ + config.gcw0_frameskip = 0; /* 0 = off, 1 = skip alternate frames, 2 = skip 2 in 3 frames, etc. Useful for FMV in MCD. */ + + /* controllers options */ + config.cursor = 0; /* different cursor designs */ + input.system[0] = SYSTEM_GAMEPAD; + input.system[1] = SYSTEM_GAMEPAD; + config.gun_cursor[0] = 1; + config.gun_cursor[1] = 1; + config.invert_mouse = 0; + for (i=0; ipw_dir; + } + + /* open configuration file */ + char fname[MAXPATHLEN]; + sprintf (fname, "%s%s/config.ini", homedir, DEFAULT_PATH); + //printf(fname); + FILE *fp = fopen(fname, "wb"); + if (fp) + { + /* write file */ + fwrite(&config, sizeof(config), 1, fp); + fclose(fp); + } +} + diff --git a/gcw0/config.h b/gcw0/config.h new file mode 100644 index 0000000..9b522e1 --- /dev/null +++ b/gcw0/config.h @@ -0,0 +1,78 @@ + +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +#include + +/**************************************************************************** + * Config Option + * + ****************************************************************************/ + +enum {A = 0, B, C, X, Y, Z, START, MODE}; + +typedef struct +{ + uint8 padtype; +} t_input_config; + +typedef struct +{ + uint8 hq_fm; + uint8 filter; + uint8 psgBoostNoise; + uint8 dac_bits; + uint8 ym2413; + int16 psg_preamp; + int16 fm_preamp; + uint32 lp_range; + int16 low_freq; + int16 high_freq; + int16 lg; + int16 mg; + int16 hg; + uint8 mono; + uint8 system; + uint8 region_detect; + uint8 vdp_mode; + uint8 master_clock; + uint8 force_dtack; + uint8 addr_error; + uint8 bios; + uint8 lock_on; + uint8 hot_swap; + uint8 invert_mouse; + uint8 gun_cursor[2]; + uint8 overscan; + uint8 gg_extra; + uint8 ntsc; + uint8 lcd; + uint8 render; + t_input_config input[MAX_INPUTS]; + uint8 gcw0_fullscreen; + uint8 gcw0_frameskip; + uint8 keepaspectratio; + uint8 gg_scanlines; + uint8 smsmaskleftbar; + uint8 sl_autoresume; + uint8 lightgun_speed; + uint8 a_stick; + uint8 cursor; + SDLKey buttons[8]; + //SDLKey button_a; + //SDLKey button_b; + //SDLKey button_c; + //SDLKey button_x; + //SDLKey button_y; + //SDLKey button_z; + //SDLKey button_start; + //SDLKey button_mode; +} t_config; + +/* Global variables */ +extern t_config config; +extern void config_save(void); +extern void set_config_defaults(void); + +#endif /* _CONFIG_H_ */ + diff --git a/gcw0/error.c b/gcw0/error.c new file mode 100644 index 0000000..0502b6d --- /dev/null +++ b/gcw0/error.c @@ -0,0 +1,35 @@ +/* + error.c -- + Error logging +*/ + +#include "osd.h" + +static FILE *error_log; + +void error_init(void) +{ +#ifdef LOGERROR + error_log = fopen("error.log","w"); +#endif +} + +void error_shutdown(void) +{ +#ifdef LOGERROR + if(error_log) fclose(error_log); +#endif +} + +void error(char *format, ...) +{ +#ifdef LOGERROR + if (log_error) + { + va_list ap; + va_start(ap, format); + if(error_log) vfprintf(error_log, format, ap); + va_end(ap); + } +#endif +} diff --git a/gcw0/error.h b/gcw0/error.h new file mode 100644 index 0000000..68c613d --- /dev/null +++ b/gcw0/error.h @@ -0,0 +1,10 @@ +#ifndef _ERROR_H_ +#define _ERROR_H_ + +/* Function prototypes */ +void error_init(void); +void error_shutdown(void); +void error(char *format, ...); + +#endif /* _ERROR_H_ */ + diff --git a/gcw0/fileio.c b/gcw0/fileio.c new file mode 100644 index 0000000..a13e385 --- /dev/null +++ b/gcw0/fileio.c @@ -0,0 +1,158 @@ +/* + * fileio.c + * + * Load a normal file, or ZIP/GZ archive into ROM buffer. + * Returns loaded ROM size (zero if an error occured) + * + * + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Charles Mac Donald + * modified by Eke-Eke (Genesis Plus GX) + * + * Redistribution and use of this code or any derivative works are permitted + * provided that the following conditions are met: + * + * - Redistributions may not be sold, nor may they be used in a commercial + * product or activity. + * + * - Redistributions that are modified from the original source must include the + * complete source code, including the source code for all components used by a + * binary built from the modified sources. However, as a special exception, the + * source code distributed need not include anything that is normally distributed + * (in either source or binary form) with the major components (compiler, kernel, + * and so on) of the operating system on which the executable runs, unless that + * component itself accompanies the executable. + * + * - Redistributions must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************************/ + +#include "shared.h" +#include + +static int check_zip(char *filename); + +int load_archive(char *filename, unsigned char *buffer, int maxsize, char *extension) +{ + int size = 0; + + if(check_zip(filename)) + { + unz_file_info info; + int ret = 0; + char fname[256]; + + /* Attempt to open the archive */ + unzFile *fd = unzOpen(filename); + if (!fd) return 0; + + /* Go to first file in archive */ + ret = unzGoToFirstFile(fd); + if(ret != UNZ_OK) + { + unzClose(fd); + return 0; + } + + /* Get file informations and update filename */ + ret = unzGetCurrentFileInfo(fd, &info, fname, 256, NULL, 0, NULL, 0); + if(ret != UNZ_OK) + { + unzClose(fd); + return 0; + } + + /* Compressed filename extension */ + if (extension) + { + strncpy(extension, &fname[strlen(fname) - 3], 3); + extension[3] = 0; + } + + /* Open the file for reading */ + ret = unzOpenCurrentFile(fd); + if(ret != UNZ_OK) + { + unzClose(fd); + return 0; + } + + /* Retrieve uncompressed file size */ + size = info.uncompressed_size; + if(size > maxsize) + { + size = maxsize; + } + + /* Read (decompress) the file */ + ret = unzReadCurrentFile(fd, buffer, size); + if(ret != size) + { + unzCloseCurrentFile(fd); + unzClose(fd); + return 0; + } + + /* Close the current file */ + ret = unzCloseCurrentFile(fd); + if(ret != UNZ_OK) + { + unzClose(fd); + return 0; + } + + /* Close the archive */ + ret = unzClose(fd); + if(ret != UNZ_OK) return 0; + } + else + { + /* Open file */ + gzFile *gd = gzopen(filename, "rb"); + if (!gd) return 0; + + /* Read file data */ + size = gzread(gd, buffer, maxsize); + + /* filename extension */ + if (extension) + { + strncpy(extension, &filename[strlen(filename) - 3], 3); + extension[3] = 0; + } + + /* Close file */ + gzclose(gd); + } + + /* Return loaded ROM size */ + return size; +} + +/* + Verifies if a file is a ZIP archive or not. + Returns: 1= ZIP archive, 0= not a ZIP archive +*/ +static int check_zip(char *filename) +{ + uint8 buf[2]; + FILE *fd = fopen(filename, "rb"); + if(!fd) return (0); + fread(buf, 2, 1, fd); + fclose(fd); + if(memcmp(buf, "PK", 2) == 0) return (1); + return (0); +} diff --git a/gcw0/fileio.h b/gcw0/fileio.h new file mode 100644 index 0000000..f95a8e8 --- /dev/null +++ b/gcw0/fileio.h @@ -0,0 +1,48 @@ +/* + * fileio.c + * + * Load a normal file, or ZIP/GZ archive. + * Returns loaded ROM size (zero if an error occured) + * + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Charles Mac Donald + * modified by Eke-Eke (Genesis Plus GX) + * + * Redistribution and use of this code or any derivative works are permitted + * provided that the following conditions are met: + * + * - Redistributions may not be sold, nor may they be used in a commercial + * product or activity. + * + * - Redistributions that are modified from the original source must include the + * complete source code, including the source code for all components used by a + * binary built from the modified sources. However, as a special exception, the + * source code distributed need not include anything that is normally distributed + * (in either source or binary form) with the major components (compiler, kernel, + * and so on) of the operating system on which the executable runs, unless that + * component itself accompanies the executable. + * + * - Redistributions must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************************/ + +#ifndef _FILEIO_H_ +#define _FILEIO_H_ + +/* Function prototypes */ +extern int load_archive(char *filename, unsigned char *buffer, int maxsize, char *extension); + +#endif /* _FILEIO_H_ */ diff --git a/gcw0/main.c b/gcw0/main.c new file mode 100644 index 0000000..06ebe10 --- /dev/null +++ b/gcw0/main.c @@ -0,0 +1,2585 @@ +#ifdef __WIN32__ +#include +#else +#define MessageBox(owner, text, caption, type) printf("%s: %s\n", caption, text) +#endif + +#include "SDL.h" +#include "SDL_thread.h" + +#include "shared.h" +#include "sms_ntsc.h" +#include "md_ntsc.h" +#include "utils.h" + +#ifdef GCWZERO +#include +#include +#include + +static int do_once = 1; +static int gcw0_w = 320; +static int gcw0_h = 240; +static int gotomenu; +static int show_lightgun; +time_t current_time; + +const char *cursor[4]= +{ + "./CLASSIC_01_RED.png", //doesn't flash (for epileptics it's default) + "./CLASSIC_02.png", //square flashing red and white + "./CLASSIC_01.png", + "./SQUARE_02.png", +}; + +#define JOY_DEADZONE 1000 +#endif + +#ifdef GCWZERO +#define SOUND_FREQUENCY 44100 +#else +#define SOUND_FREQUENCY 48000 +#endif +#define SOUND_SAMPLES_SIZE 2048 + +#define VIDEO_WIDTH 320 +#define VIDEO_HEIGHT 240 + +int joynum = 0; + +int log_error = 0; +int debug_on = 0; +int turbo_mode = 0; +int use_sound = 1; +int fullscreen = 1; /* SDL_FULLSCREEN */ + +char rom_filename[256]; + +/* sound */ + +struct +{ + char* current_pos; + char* buffer; + int current_emulated_samples; +} sdl_sound; + + +static uint8 brm_format[0x40] = +{ + 0x5f,0x5f,0x5f,0x5f,0x5f,0x5f,0x5f,0x5f,0x5f,0x5f,0x5f,0x00,0x00,0x00,0x00,0x40, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x53,0x45,0x47,0x41,0x5f,0x43,0x44,0x5f,0x52,0x4f,0x4d,0x00,0x01,0x00,0x00,0x00, + 0x52,0x41,0x4d,0x5f,0x43,0x41,0x52,0x54,0x52,0x49,0x44,0x47,0x45,0x5f,0x5f,0x5f +}; + + +static short soundframe[SOUND_SAMPLES_SIZE]; + +static void sdl_sound_callback(void *userdata, Uint8 *stream, int len) +{ + if(sdl_sound.current_emulated_samples < len) + { + memset(stream, 0, len); + } + else + { + memcpy(stream, sdl_sound.buffer, len); + /* loop to compensate desync */ + do + { + sdl_sound.current_emulated_samples -= len; + } + while(sdl_sound.current_emulated_samples > 2 * len); + memcpy(sdl_sound.buffer, + sdl_sound.current_pos - sdl_sound.current_emulated_samples, + sdl_sound.current_emulated_samples); + sdl_sound.current_pos = sdl_sound.buffer + sdl_sound.current_emulated_samples; + } +} + +static int sdl_sound_init() +{ + int n; + SDL_AudioSpec as_desired, as_obtained; + + if(SDL_Init(SDL_INIT_AUDIO) < 0) + { + MessageBox(NULL, "SDL Audio initialization failed", "Error", 0); + return 0; + } + + as_desired.freq = SOUND_FREQUENCY; + as_desired.format = AUDIO_S16LSB; + as_desired.channels = 2; + as_desired.samples = SOUND_SAMPLES_SIZE; + as_desired.callback = sdl_sound_callback; + + if(SDL_OpenAudio(&as_desired, &as_obtained) == -1) + { + MessageBox(NULL, "SDL Audio open failed", "Error", 0); + return 0; + } + + if(as_desired.samples != as_obtained.samples) + { + MessageBox(NULL, "SDL Audio wrong setup", "Error", 0); + return 0; + } + + sdl_sound.current_emulated_samples = 0; + n = SOUND_SAMPLES_SIZE * 2 * sizeof(short) * 20; + sdl_sound.buffer = (char*)malloc(n); + if(!sdl_sound.buffer) + { + MessageBox(NULL, "Can't allocate audio buffer", "Error", 0); + return 0; + } + memset(sdl_sound.buffer, 0, n); + sdl_sound.current_pos = sdl_sound.buffer; + return 1; +} + +static void sdl_sound_update(enabled) +{ + int size = audio_update(soundframe) * 2; + + if (enabled) + { + int i; + short *out; + + SDL_LockAudio(); + out = (short*)sdl_sound.current_pos; + for(i = 0; i < size; i++) + { + *out++ = soundframe[i]; + } + sdl_sound.current_pos = (char*)out; + sdl_sound.current_emulated_samples += size * sizeof(short); + SDL_UnlockAudio(); + } +} + +static void sdl_sound_close() +{ + SDL_PauseAudio(1); + SDL_CloseAudio(); + if (sdl_sound.buffer) + free(sdl_sound.buffer); +} + +#ifdef GCWZERO //A-stick support +static void sdl_joystick_init() +{ + if(SDL_Init(SDL_INIT_JOYSTICK) < 0) + { + MessageBox(NULL, "SDL Joystick initialization failed", "Error", 0); + return 0; + } + else + MessageBox(NULL, "SDL Joystick initialisation successful", "Success", 0); + return 1; +} +#endif + +/* video */ +md_ntsc_t *md_ntsc; +sms_ntsc_t *sms_ntsc; + +struct +{ + SDL_Surface* surf_screen; + SDL_Surface* surf_bitmap; + SDL_Rect srect; + SDL_Rect drect; + Uint32 frames_rendered; +} sdl_video; + +static int sdl_video_init() +{ + if(SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) + { + MessageBox(NULL, "SDL Video initialization failed", "Error", 0); + return; + } +#ifdef GCWZERO + sdl_video.surf_screen = SDL_SetVideoMode(VIDEO_WIDTH, VIDEO_HEIGHT, 16, SDL_HWSURFACE | +#else + sdl_video.surf_screen = SDL_SetVideoMode(VIDEO_WIDTH, VIDEO_HEIGHT, 16, SDL_HWSURFACE | fullscreen | +#endif +#ifdef SDL_TRIPLEBUF + SDL_TRIPLEBUF +#else + SDL_DOUBLEBUF +#endif + ); + sdl_video.surf_bitmap = SDL_CreateRGBSurface(SDL_HWSURFACE, 720, 576, 16, 0, 0, 0, 0); + sdl_video.frames_rendered = 0; + SDL_ShowCursor(0); + return; +} + +static void sdl_video_update() +{ +static int test; + if (system_hw == SYSTEM_MCD) + { +#ifdef GCWZERO + if (test >= config.gcw0_frameskip) // >= in case frameskip has just been lowered + { + system_frame_scd(0); //render frame + test = 0; + } else { + system_frame_scd(1); //skip frame render + test ++; + } +#else + system_frame_scd(0); +#endif + } + else if ((system_hw & SYSTEM_PBC) == SYSTEM_MD) +#ifdef GCWZERO + { + if (test >= config.gcw0_frameskip) + { + system_frame_gen(0); + test = 0; + } else { + system_frame_gen(1); + test ++; + } +#else + system_frame_gen(0); +#endif + } + else + { +#ifdef GCWZERO + if (test >= config.gcw0_frameskip) + { + system_frame_sms(0); + test = 0; + } else { + system_frame_sms(1); + test ++; + } +#else + system_frame_sms(0); +#endif + } + + /* viewport size changed */ + if(bitmap.viewport.changed & 1) + { + bitmap.viewport.changed &= ~1; + + /* source bitmap */ +#ifdef GCWZERO //remove left bar bug with SMS roms + if ( (system_hw == SYSTEM_MARKIII) || (system_hw == SYSTEM_SMS) || (system_hw == SYSTEM_SMS2) || (system_hw == SYSTEM_PBC) ) + { + if (config.smsmaskleftbar) + sdl_video.srect.x = 8; + else + sdl_video.srect.x = 0; + + } + else + { + sdl_video.srect.x = 0; + } +#else + sdl_video.srect.x = 0; +#endif + sdl_video.srect.y = 0; + sdl_video.srect.w = bitmap.viewport.w+2*bitmap.viewport.x; + sdl_video.srect.h = bitmap.viewport.h+2*bitmap.viewport.y; + if (sdl_video.srect.w > VIDEO_WIDTH) + { +#ifdef GCWZERO + if ( (system_hw == SYSTEM_MARKIII) || (system_hw == SYSTEM_SMS) || (system_hw == SYSTEM_SMS2) || (system_hw == SYSTEM_PBC) ) + { + if (config.smsmaskleftbar) + sdl_video.srect.x = (sdl_video.srect.w - VIDEO_WIDTH) / 2 + 8; + else + sdl_video.srect.x = (sdl_video.srect.w - VIDEO_WIDTH) / 2; + sdl_video.srect.w = VIDEO_WIDTH; + } + else + { + sdl_video.srect.x = (sdl_video.srect.w - VIDEO_WIDTH) / 2; + sdl_video.srect.w = VIDEO_WIDTH; + } +#else + sdl_video.srect.x = (sdl_video.srect.w - VIDEO_WIDTH) / 2; + sdl_video.srect.w = VIDEO_WIDTH; +#endif + } + if (sdl_video.srect.h > VIDEO_HEIGHT) + { + sdl_video.srect.y = (sdl_video.srect.h - VIDEO_HEIGHT) / 2; + sdl_video.srect.h = VIDEO_HEIGHT; + } + + /* destination bitmap */ + sdl_video.drect.w = sdl_video.srect.w; + sdl_video.drect.h = sdl_video.srect.h; + sdl_video.drect.x = (VIDEO_WIDTH - sdl_video.drect.w) / 2; + sdl_video.drect.y = (VIDEO_HEIGHT - sdl_video.drect.h) / 2; + + /* clear destination surface */ + SDL_FillRect(sdl_video.surf_screen, 0, 0); +#ifdef GCWZERO //triple buffering so stop flicker + SDL_Flip(sdl_video.surf_screen); + SDL_FillRect(sdl_video.surf_screen, 0, 0); + SDL_Flip(sdl_video.surf_screen); + SDL_FillRect(sdl_video.surf_screen, 0, 0); +#endif +#if 0 + if (config.render && (interlaced || config.ntsc)) rect.h *= 2; + if (config.ntsc) rect.w = (reg[12]&1) ? MD_NTSC_OUT_WIDTH(rect.w) : SMS_NTSC_OUT_WIDTH(rect.w); + if (config.ntsc) + { + sms_ntsc = (sms_ntsc_t *)malloc(sizeof(sms_ntsc_t)); + md_ntsc = (md_ntsc_t *)malloc(sizeof(md_ntsc_t)); + + switch (config.ntsc) + { + case 1: + sms_ntsc_init(sms_ntsc, &sms_ntsc_composite); + md_ntsc_init(md_ntsc, &md_ntsc_composite); + break; + case 2: + sms_ntsc_init(sms_ntsc, &sms_ntsc_svideo); + md_ntsc_init(md_ntsc, &md_ntsc_svideo); + break; + case 3: + sms_ntsc_init(sms_ntsc, &sms_ntsc_rgb); + md_ntsc_init(md_ntsc, &md_ntsc_rgb); + break; + } + } + else + { + if (sms_ntsc) + { + free(sms_ntsc); + sms_ntsc = NULL; + } + + if (md_ntsc) + { + free(md_ntsc); + md_ntsc = NULL; + } + } +#endif + } + +//DK IPU scaling for gg/sms roms +#ifdef GCWZERO + if (config.gcw0_fullscreen) + { + if( (gcw0_w != sdl_video.drect.w) || (gcw0_h != sdl_video.drect.h) ) + { + if ( (system_hw == SYSTEM_MARKIII) || (system_hw == SYSTEM_SMS) || (system_hw == SYSTEM_SMS2) || (system_hw == SYSTEM_PBC) ) + { + if (config.smsmaskleftbar) + { + sdl_video.srect.w = sdl_video.srect.w - 8; + sdl_video.drect.w = sdl_video.srect.w; + sdl_video.drect.x = 4; + } + else + { + sdl_video.srect.w = sdl_video.srect.w ; + sdl_video.drect.w = sdl_video.srect.w; + sdl_video.drect.x = 0; + } + + } + else + { + sdl_video.drect.x = 0; + sdl_video.drect.w = sdl_video.srect.w; + } + + sdl_video.drect.h = sdl_video.srect.h; + sdl_video.drect.y = 0; + gcw0_w=sdl_video.drect.w; + gcw0_h=sdl_video.drect.h; + + if ( (system_hw == SYSTEM_MARKIII) || (system_hw == SYSTEM_SMS) || (system_hw == SYSTEM_SMS2) || (system_hw == SYSTEM_PBC) ) + { + sdl_video.surf_screen = SDL_SetVideoMode(256,gcw0_h, 16, SDL_HWSURFACE | +#ifdef SDL_TRIPLEBUF + SDL_TRIPLEBUF); +#else + SDL_DOUBLEBUF); +#endif + } + else + { + sdl_video.surf_screen = SDL_SetVideoMode(gcw0_w,gcw0_h, 16, SDL_HWSURFACE | +#ifdef SDL_TRIPLEBUF + SDL_TRIPLEBUF); +#else + SDL_DOUBLEBUF); +#endif + } + } + } + if (show_lightgun && !config.gcw0_fullscreen) // hack to remove cursor corruption of over game screen edge + { + SDL_FillRect(sdl_video.surf_screen, 0, 0); + } +#endif + SDL_BlitSurface(sdl_video.surf_bitmap, &sdl_video.srect, sdl_video.surf_screen, &sdl_video.drect); + //SDL_UpdateRect(sdl_video.surf_screen, 0, 0, 0, 0); +#ifdef GCWZERO +// Add scanlines to Game Gear games if requested + if ( (system_hw == SYSTEM_GG) && config.gg_scanlines) + { + SDL_Surface *scanlinesSurface; + scanlinesSurface = IMG_Load("./scanlines.png"); + SDL_BlitSurface(scanlinesSurface, NULL, sdl_video.surf_screen, &sdl_video.drect); + SDL_FreeSurface(scanlinesSurface); + } + if (show_lightgun) + { +// Remove previous cursor from black bars + if (config.gcw0_fullscreen) + { + if (config.smsmaskleftbar) + { + if(system_hw == SYSTEM_SMS2) + { + SDL_Rect srect; + srect.x = 0; + srect.y = 0; + srect.w = 4; + srect.h = 192; + SDL_FillRect(sdl_video.surf_screen, &srect, SDL_MapRGB(sdl_video.surf_screen->format, 0, 0, 0)); + srect.x = 252; + SDL_FillRect(sdl_video.surf_screen, &srect, SDL_MapRGB(sdl_video.surf_screen->format, 0, 0, 0)); + } + } + } + /* get mouse coordinates (absolute values) */ + int x,y; + int state = SDL_GetMouseState(&x,&y); + + SDL_Rect lrect; + lrect.x = x-7; + lrect.y = y-7; + lrect.w = 15; + lrect.h = 15; + + SDL_Surface *lightgunSurface; + lightgunSurface = IMG_Load(cursor[config.cursor]); + static lightgun_af = 0; + SDL_Rect srect; + srect.y = 0; + srect.w = 15; + srect.h = 15; + + //only show cursor if movement occurred within 3 seconds. + time_t current_time2; + current_time2 = time(NULL); + + if (lightgun_af >= 10) + { + srect.x = 0; + if((current_time2 - current_time) < 3) + SDL_BlitSurface(lightgunSurface, &srect, sdl_video.surf_screen, &lrect); + } else + { + if (config.cursor != 0) + srect.x = 15; + else + srect.x = 0; + if((current_time2 - current_time) < 3) + SDL_BlitSurface(lightgunSurface, &srect, sdl_video.surf_screen, &lrect); + } + lightgun_af++; + if (lightgun_af == 20) lightgun_af = 0; + SDL_FreeSurface(lightgunSurface); + } //show_lightgun +#endif + + SDL_Flip(sdl_video.surf_screen); + ++sdl_video.frames_rendered; +} + +static void sdl_video_close() +{ + if (sdl_video.surf_bitmap) + SDL_FreeSurface(sdl_video.surf_bitmap); + if (sdl_video.surf_screen) + SDL_FreeSurface(sdl_video.surf_screen); +} + +/* Timer Sync */ + +struct +{ + SDL_sem* sem_sync; + unsigned ticks; +} sdl_sync; + +static Uint32 sdl_sync_timer_callback(Uint32 interval) +{ +#ifdef GCWZERO + if (!gotomenu) + { + SDL_SemPost(sdl_sync.sem_sync); + sdl_sync.ticks++; + } +#else + SDL_SemPost(sdl_sync.sem_sync); + sdl_sync.ticks++; +#endif + if (sdl_sync.ticks == (vdp_pal ? 50 : 20)) + { + SDL_Event event; + SDL_UserEvent userevent; + + userevent.type = SDL_USEREVENT; + userevent.code = vdp_pal ? (sdl_video.frames_rendered / 3) : sdl_video.frames_rendered; + userevent.data1 = NULL; + userevent.data2 = NULL; + sdl_sync.ticks = sdl_video.frames_rendered = 0; + + event.type = SDL_USEREVENT; + event.user = userevent; + + SDL_PushEvent(&event); + } + return interval; +} + +static int sdl_sync_init() +{ + if(SDL_InitSubSystem(SDL_INIT_TIMER|SDL_INIT_EVENTTHREAD) < 0) + { + MessageBox(NULL, "SDL Timer initialization failed", "Error", 0); + return 0; + } + + sdl_sync.sem_sync = SDL_CreateSemaphore(0); + sdl_sync.ticks = 0; + return 1; +} + +static void sdl_sync_close() +{ + if(sdl_sync.sem_sync) + SDL_DestroySemaphore(sdl_sync.sem_sync); +} + +static const uint16 vc_table[4][2] = +{ + /* NTSC, PAL */ + {0xDA , 0xF2}, /* Mode 4 (192 lines) */ + {0xEA , 0x102}, /* Mode 5 (224 lines) */ + {0xDA , 0xF2}, /* Mode 4 (192 lines) */ + {0x106, 0x10A} /* Mode 5 (240 lines) */ +}; + +static int sdl_control_update(SDLKey keystate) +{ + switch (keystate) + { +#ifndef GCWZERO + case SDLK_TAB: + { + system_reset(); + break; + } +#endif + + case SDLK_F1: + { + if (SDL_ShowCursor(-1)) SDL_ShowCursor(0); + else SDL_ShowCursor(1); + break; + } + + case SDLK_F2: + { + if (fullscreen) fullscreen = 0; + else fullscreen = SDL_FULLSCREEN; + sdl_video.surf_screen = SDL_SetVideoMode(VIDEO_WIDTH, VIDEO_HEIGHT, 16, SDL_SWSURFACE | fullscreen); + break; + } + + case SDLK_F3: + { + if (config.bios == 0) config.bios = 3; + else if (config.bios == 3) config.bios = 1; + break; + } + + case SDLK_F4: + { + if (!turbo_mode) use_sound ^= 1; + break; + } + + case SDLK_F5: + { + log_error ^= 1; + break; + } + + case SDLK_F6: + { + if (!use_sound) + { + turbo_mode ^=1; + sdl_sync.ticks = 0; + } + break; + } + + case SDLK_F7: + { + char save_state_file[256]; + sprintf(save_state_file,"%s/%X.gp0", get_save_directory(), rominfo.realchecksum); + FILE *f = fopen(save_state_file,"rb"); + + if (f) + { + uint8 buf[STATE_SIZE]; + fread(&buf, STATE_SIZE, 1, f); + state_load(buf); + fclose(f); + } + break; + } + + case SDLK_F8: + { + char save_state_file[256]; + sprintf(save_state_file,"%s/%X.gp0", get_save_directory(), rominfo.realchecksum); + FILE *f = fopen(save_state_file,"wb"); + if (f) + { + uint8 buf[STATE_SIZE]; + int len = state_save(buf); + fwrite(&buf, len, 1, f); + fclose(f); + } + break; + } + + case SDLK_F9: + { + config.region_detect = (config.region_detect + 1) % 5; + get_region(0); + + /* framerate has changed, reinitialize audio timings */ + audio_init(snd.sample_rate, 0); + + /* system with region BIOS should be reinitialized */ + if ((system_hw == SYSTEM_MCD) || ((system_hw & SYSTEM_SMS) && (config.bios & 1))) + { + system_init(); + system_reset(); + } + else + { + /* reinitialize I/O region register */ + if (system_hw == SYSTEM_MD) + { + io_reg[0x00] = 0x20 | region_code | (config.bios & 1); + } + else + { + io_reg[0x00] = 0x80 | (region_code >> 1); + } + + /* reinitialize VDP */ + if (vdp_pal) + { + status |= 1; + lines_per_frame = 313; + } + else + { + status &= ~1; + lines_per_frame = 262; + } + + /* reinitialize VC max value */ + switch (bitmap.viewport.h) + { + case 192: + vc_max = vc_table[0][vdp_pal]; + break; + case 224: + vc_max = vc_table[1][vdp_pal]; + break; + case 240: + vc_max = vc_table[3][vdp_pal]; + break; + } + } + break; + } + + case SDLK_F10: + { + gen_reset(0); + break; + } + + case SDLK_F11: + { + config.overscan = (config.overscan + 1) & 3; + if ((system_hw == SYSTEM_GG) && !config.gg_extra) + { + bitmap.viewport.x = (config.overscan & 2) ? 14 : -48; + } + else + { + bitmap.viewport.x = (config.overscan & 2) * 7; + } + bitmap.viewport.changed = 3; + break; + } + case SDLK_F12: + { + joynum = (joynum + 1) % MAX_DEVICES; + while (input.dev[joynum] == NO_DEVICE) + { + joynum = (joynum + 1) % MAX_DEVICES; + } + break; + } + + case SDLK_ESCAPE: + { +#ifndef GCWZERO + /* exit */ + return 0; +#endif + } + + default: + break; + } + + return 1; +} + +static void shutdown() +{ + FILE *fp; + + if (system_hw == SYSTEM_MCD) + { + /* save internal backup RAM (if formatted) */ + char brm_file[256]; + if (!memcmp(scd.bram + 0x2000 - 0x20, brm_format + 0x20, 0x20)) + { + sprintf(brm_file,"%s/", get_save_directory(), "scd.brm"); + fp = fopen(brm_file, "wb"); + if (fp!=NULL) + { + fwrite(scd.bram, 0x2000, 1, fp); + fclose(fp); + } + } + + /* save cartridge backup RAM (if formatted) */ + if (scd.cartridge.id) + { + if (!memcmp(scd.cartridge.area + scd.cartridge.mask + 1 - 0x20, brm_format + 0x20, 0x20)) + { + sprintf(brm_file,"%s/", get_save_directory(), "cart.brm"); + fp = fopen(brm_file, "wb"); + if (fp!=NULL) + { + fwrite(scd.cartridge.area, scd.cartridge.mask + 1, 1, fp); + fclose(fp); + } + } + } + } + + if (sram.on) + { + /* save SRAM */ + char save_file[256]; + if (rom_filename[0] != '\0') { + sprintf(save_file,"%s/%s.srm", get_save_directory(), rom_filename); + fp = fopen(save_file, "wb"); + if (fp!=NULL) + { + fwrite(sram.sram,0x10000,1, fp); + fclose(fp); + } + } + } + audio_shutdown(); + error_shutdown(); + + sdl_video_close(); + sdl_sound_close(); + sdl_sync_close(); + SDL_Quit(); +} + +#ifdef GCWZERO //menu! +static int gcw0menu(void) +{ + SDL_PauseAudio(1); + + /* display menu */ +// change video mode + sdl_video.surf_screen = SDL_SetVideoMode(320,240, 32, SDL_HWSURFACE | +#ifdef SDL_TRIPLEBUF + SDL_TRIPLEBUF); +#else + SDL_DOUBLEBUF); +#endif +// blank screen + SDL_FillRect(sdl_video.surf_screen, 0, 0); + + enum {MAINMENU = 0, GRAPHICS_OPTIONS = 1, REMAP_OPTIONS = 2, SAVE_STATE = 3, LOAD_STATE = 4, MISC_OPTIONS = 5}; + static int menustate = MAINMENU; +// Menu text + const char *gcw0menu_mainlist[9]= + { + "Resume game", + "Save state", + "Load state", + "Graphics options", + "Remap buttons", + "Misc. Options", + + "", //spacer + "Reset", + "Quit" + }; + const char *gcw0menu_gfxlist[6]= + { + "Scaling", + "Keep aspect ratio", + "Scanlines (GG)", + "Mask left bar (SMS)", + "Frameskip", + "Return to main menu", + }; + const char *gcw0menu_numericlist[4]= + { + "0", + "1", + "2", + "3", + }; + const char *gcw0menu_onofflist[2]= + { + "Off", + "On", + }; + const char *gcw0menu_remapoptionslist[9]= + { + "A", + "B", + "C", + "X", + "Y", + "Z", + "Start", + "Mode", + "Return to main menu", + }; + const char *gcw0menu_savestate[10]= + { + "Back to main menu", + "Save state 1 (Quicksave)", + "Save state 2", + "Save state 3", + "Save state 4", + "Save state 5", + "Save state 6", + "Save state 7", + "Save state 8", + "Save state 9", + }; + const char *gcw0menu_loadstate[10]= + { + "Back to main menu", + "Load state 1 (Quickload)", + "Load state 2", + "Load state 3", + "Load state 4", + "Load state 5", + "Load state 6", + "Load state 7", + "Load state 8", + "Load state 9", + }; + const char *gcw0menu_misc[7]= + { + "Back to main menu", + "Resume on Save/Load", + "A-stick", + "Lock-on", + "FM sound (SMS)", + "Lightgun speed", + "Lightgun Cursor", + }; + + const char *lock_on_desc[4]= + { + " Off", + " Game Genie", + " Action Replay", + "Sonic & Knuckles", + }; + +// start menu loop + bitmap.viewport.changed=1; //change screen res if required + while(gotomenu) + { +// set up menu surface + SDL_Surface *menuSurface = NULL; + menuSurface = SDL_CreateRGBSurface(SDL_HWSURFACE, 320, 240, 16, 0, 0, 0, 0); + +// identify system we are using to show correct background just cos we can :P + if ( system_hw == SYSTEM_PICO) //Sega Pico + { + SDL_Surface *tempbgSurface; + SDL_Surface *bgSurface; + tempbgSurface = IMG_Load( "./PICO.png" ); + bgSurface = SDL_DisplayFormat( tempbgSurface ); + SDL_BlitSurface(bgSurface, NULL, menuSurface, NULL); + SDL_FreeSurface(tempbgSurface); + SDL_FreeSurface(bgSurface); + } + else if ( (system_hw == SYSTEM_SG) || (system_hw == SYSTEM_SGII) ) //SG-1000 I&II + { + SDL_Surface *tempbgSurface; + SDL_Surface *bgSurface; + tempbgSurface = IMG_Load( "./SG1000.png" ); + bgSurface = SDL_DisplayFormat( tempbgSurface ); + SDL_BlitSurface(bgSurface, NULL, menuSurface, NULL); + SDL_FreeSurface(tempbgSurface); + SDL_FreeSurface(bgSurface); + } + else if ( (system_hw == SYSTEM_MARKIII) || (system_hw == SYSTEM_SMS) || ( system_hw == SYSTEM_GGMS) || (system_hw == SYSTEM_SMS2) || (system_hw == SYSTEM_PBC) ) //Mark III & Sega Master System I&II & Megadrive with power base converter + { + SDL_Surface *tempbgSurface; + SDL_Surface *bgSurface; + tempbgSurface = IMG_Load( "./SMS.png" ); + bgSurface = SDL_DisplayFormat( tempbgSurface ); + SDL_BlitSurface(bgSurface, NULL, menuSurface, NULL); + SDL_FreeSurface(tempbgSurface); + SDL_FreeSurface(bgSurface); + } + else if (system_hw == SYSTEM_GG) + { + SDL_Surface *tempbgSurface; + SDL_Surface *bgSurface; + tempbgSurface = IMG_Load( "./GG.png" ); + bgSurface = SDL_DisplayFormat( tempbgSurface ); + SDL_BlitSurface(bgSurface, NULL, menuSurface, NULL); + SDL_FreeSurface(tempbgSurface); + SDL_FreeSurface(bgSurface); + } + else if ( system_hw == SYSTEM_MD) //Megadrive + { + SDL_Surface *tempbgSurface; + SDL_Surface *bgSurface; + tempbgSurface = IMG_Load( "./MD.png" ); + bgSurface = SDL_DisplayFormat( tempbgSurface ); + SDL_BlitSurface(bgSurface, NULL, menuSurface, NULL); + SDL_FreeSurface(tempbgSurface); + SDL_FreeSurface(bgSurface); + } + else if ( system_hw == SYSTEM_MCD) //MegaCD + { + SDL_Surface *tempbgSurface; + SDL_Surface *bgSurface; + tempbgSurface = IMG_Load( "./MCD.png" ); + bgSurface = SDL_DisplayFormat( tempbgSurface ); + SDL_BlitSurface(bgSurface, NULL, menuSurface, NULL); + SDL_FreeSurface(tempbgSurface); + SDL_FreeSurface(bgSurface); + } + +// show menu + TTF_Init(); + TTF_Font *ttffont = NULL; + SDL_Color text_color = {180, 180, 180}; + SDL_Color selected_text_color = {23, 86, 155}; //selected colour = Sega blue ;) + SDL_Surface *textSurface; + + int i; + static int selectedoption = 0; + +// Fill menu box + SDL_Surface *MenuBackground; + if (menustate == REMAP_OPTIONS) + { + MenuBackground = SDL_CreateRGBSurface(SDL_HWSURFACE, 320, 185, 16, 0, 0, 0, 0); + SDL_Rect rect; + rect.x = 0; + rect.y = 35; + rect.w = 320; + rect.h = 185; + SDL_FillRect(MenuBackground, 0, 0); + SDL_SetAlpha(MenuBackground, SDL_SRCALPHA, 50); + SDL_BlitSurface(MenuBackground, NULL, menuSurface, &rect); + SDL_FreeSurface(MenuBackground); + } + else + { + MenuBackground = SDL_CreateRGBSurface(SDL_HWSURFACE, 180, 185, 16, 0, 0, 0, 0); + SDL_Rect rect; + rect.x = 60; + rect.y = 35; + rect.w = 180; + rect.h = 185; + SDL_FillRect(MenuBackground, 0, 0); + SDL_SetAlpha(MenuBackground, SDL_SRCALPHA, 50); + SDL_BlitSurface(MenuBackground, NULL, menuSurface, &rect); + SDL_FreeSurface(MenuBackground); + } + +// Show title + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + SDL_Rect destination; + destination.x = 80; + destination.y = 40; + destination.w = 100; + destination.h = 50; + textSurface = TTF_RenderText_Solid(ttffont, "Genesis Plus GX", text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + TTF_CloseFont (ttffont); + + if (menustate == MAINMENU) + { + //there's no need to open/close font each cycle :P + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + for(i=0; i<9; i++) + { + SDL_Rect destination; + destination.x = 80; + destination.y = 70+(15*i); + destination.w = 100; + destination.h = 50; + if (i == selectedoption) + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_mainlist[i], selected_text_color); + else + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_mainlist[i], text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + } + TTF_CloseFont (ttffont); + } + else if (menustate == GRAPHICS_OPTIONS) + { + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + for(i=0; i<6; i++) + { + SDL_Rect destination; + destination.x = 80; + destination.y = 70+(15*i); + destination.w = 100; + destination.h = 50; + if ((i+10) == selectedoption) + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_gfxlist[i], selected_text_color); + else + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_gfxlist[i], text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + } + /* Display On/Off */ + SDL_Rect destination; + destination.x = 220; + destination.w = 100; + destination.h = 50; +// Scaling + destination.y = 70+(15*0); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_onofflist[config.gcw0_fullscreen], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); +// Aspect ratio + destination.y = 70+(15*1); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_onofflist[config.keepaspectratio], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); +// Scanlines + destination.y = 70+(15*2); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_onofflist[config.gg_scanlines], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); +// Mask left bar + destination.y = 70+(15*3); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_onofflist[config.smsmaskleftbar], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); +// Frameskip + destination.y = 70+(15*4); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_numericlist[config.gcw0_frameskip], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + + TTF_CloseFont (ttffont); + + } + else if (menustate == REMAP_OPTIONS) + { + char* remap_text[256]; + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + sprintf(remap_text, "%s%25s", "GenPlus", "GCW-Zero"); + SDL_Rect destination = {30, 60, 100, 50}; + textSurface = TTF_RenderText_Solid(ttffont, remap_text, text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + + for(i=0; i < 9; i++) + { + if (i < 8) + { + sprintf(remap_text, "%-5s %-7s", gcw0menu_remapoptionslist[i], gcw0_get_key_name(config.buttons[i])); + } else + { + sprintf(remap_text, gcw0menu_remapoptionslist[i]); // for return option + } + SDL_Rect destination = {30, 80 + (15 * i), 100, 50}; + if ((i+20) == selectedoption) + { + textSurface = TTF_RenderText_Solid(ttffont, remap_text, selected_text_color); + } else + { + textSurface = TTF_RenderText_Solid(ttffont, remap_text, text_color); + } + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + } + TTF_CloseFont (ttffont); + } + else if (menustate == SAVE_STATE) + { + //Show saved BMP as background if available + SDL_Surface* screenshot; + char load_state_screenshot[256]; + sprintf(load_state_screenshot,"%s/%s.%d.bmp", get_save_directory(), rom_filename, selectedoption-30); + screenshot = SDL_LoadBMP(load_state_screenshot); + if (screenshot) + { + SDL_Rect destination; + destination.x = (320 - screenshot->w) / 2; + destination.y = (240 - screenshot->h) / 2; + destination.w = 320; + destination.h = 240; + SDL_BlitSurface(screenshot, NULL, menuSurface, &destination); + +// Fill menu box + SDL_Surface *MenuBackground = SDL_CreateRGBSurface(SDL_HWSURFACE, 180, 185, 16, 0, 0, 0, 0); + SDL_Rect rect; + rect.x = 60; + rect.y = 35; + rect.w = 180; + rect.h = 185; + SDL_FillRect(MenuBackground, 0, 0); + SDL_SetAlpha(MenuBackground, SDL_SRCALPHA, 180); + SDL_BlitSurface(MenuBackground, NULL, menuSurface, &rect); + SDL_FreeSurface(MenuBackground); + } + SDL_FreeSurface(screenshot); + +// Show title + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + SDL_Rect destination; + destination.x = 80; + destination.y = 40; + destination.w = 100; + destination.h = 50; + textSurface = TTF_RenderText_Solid(ttffont, "Genesis Plus GX", text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + TTF_CloseFont (ttffont); + + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + for(i=0; i<10; i++) + { + SDL_Rect destination; + destination.x = 80; + destination.y = 70+(15*i); + destination.w = 100; + destination.h = 50; + if ((i+30) == selectedoption) + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_savestate[i], selected_text_color); + else + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_savestate[i], text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + } + TTF_CloseFont (ttffont); + } + else if (menustate == LOAD_STATE) + { + //Show saved BMP as background if available + SDL_Surface* screenshot; + char load_state_screenshot[256]; + sprintf(load_state_screenshot,"%s/%s.%d.bmp", get_save_directory(), rom_filename, selectedoption-40); + screenshot = SDL_LoadBMP(load_state_screenshot); + if (screenshot) + { + SDL_Rect destination; + destination.x = (320 - screenshot->w) / 2; + destination.y = (240 - screenshot->h) / 2; + destination.w = 320; + destination.h = 240; + SDL_BlitSurface(screenshot, NULL, menuSurface, &destination); + +// Fill menu box + SDL_Surface *MenuBackground = SDL_CreateRGBSurface(SDL_HWSURFACE, 180, 185, 16, 0, 0, 0, 0); + SDL_Rect rect; + rect.x = 60; + rect.y = 35; + rect.w = 180; + rect.h = 185; + SDL_FillRect(MenuBackground, 0, 0); + SDL_SetAlpha(MenuBackground, SDL_SRCALPHA, 180); + SDL_BlitSurface(MenuBackground, NULL, menuSurface, &rect); + SDL_FreeSurface(MenuBackground); + } + SDL_FreeSurface(screenshot); + +// Show title + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + SDL_Rect destination; + destination.x = 80; + destination.y = 40; + destination.w = 100; + destination.h = 50; + textSurface = TTF_RenderText_Solid(ttffont, "Genesis Plus GX", text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + TTF_CloseFont (ttffont); + + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + for(i=0; i<10; i++) + { + SDL_Rect destination; + destination.x = 80; + destination.y = 70+(15*i); + destination.w = 100; + destination.h = 50; + if ((i+40) == selectedoption) + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_loadstate[i], selected_text_color); + else + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_loadstate[i], text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + } + TTF_CloseFont (ttffont); + } + else if (menustate == MISC_OPTIONS) + { + ttffont = TTF_OpenFont("./ProggyTiny.ttf", 16); + for(i=0; i<7; i++) + { + SDL_Rect destination; + destination.x = 80; + destination.y = 70+(15*i); + destination.w = 100; + destination.h = 50; + if ((i+50) == selectedoption) + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_misc[i], selected_text_color); + else + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_misc[i], text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + } + /* Display On/Off */ + SDL_Rect destination; + destination.x = 220; + destination.w = 100; + destination.h = 50; +// Save/load autoresume + destination.y = 70+(15*1); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_onofflist[config.sl_autoresume], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); +// A-stick + destination.y = 70+(15*2); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_onofflist[config.a_stick], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); + /* Display Lock-on Types */ + destination.x = 142; + destination.y = 70+(15*3); + textSurface = TTF_RenderText_Solid(ttffont, lock_on_desc[config.lock_on], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); +// FM sound(SMS) + destination.x = 220; + destination.y = 70+(15*4); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_onofflist[config.ym2413], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); +// Lightgun speed + destination.x = 220; + destination.y = 70+(15*5); + textSurface = TTF_RenderText_Solid(ttffont, gcw0menu_numericlist[config.lightgun_speed], selected_text_color); + SDL_BlitSurface(textSurface, NULL, menuSurface, &destination); + SDL_FreeSurface(textSurface); +// Lightgun Cursor + destination.y = 70+(15*6); + SDL_Surface *lightgunSurface; + lightgunSurface = IMG_Load(cursor[config.cursor]); + static lightgun_af_demo = 0; + SDL_Rect srect; + srect.x = 0; + srect.y = 0; + srect.w = 15; + srect.h = 15; + if (lightgun_af_demo >= 10 && config.cursor != 0) + { + srect.x = 15; + } + lightgun_af_demo++; + if (lightgun_af_demo == 20) lightgun_af_demo = 0; + SDL_BlitSurface(lightgunSurface, &srect, menuSurface, &destination); + SDL_FreeSurface(lightgunSurface); + + TTF_CloseFont (ttffont); + + } + +//TODO other menu's go here + + + /* Update display */ + SDL_Rect dest; + dest.w = 320; + dest.h = 240; + dest.x = 0; + dest.y = 0; + SDL_BlitSurface(menuSurface, NULL, sdl_video.surf_screen, &dest); + SDL_FreeSurface(menuSurface); + SDL_Flip(sdl_video.surf_screen); + /* Check for user input */ + SDL_EnableKeyRepeat(0,0); + static int keyheld=0; + SDL_Event event; + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_KEYDOWN: + sdl_control_update(event.key.keysym.sym); + break; + case SDL_KEYUP: + keyheld = 0; + break; + default: + break; + } + } + if (event.type == SDL_KEYDOWN && !keyheld) + { + keyheld++; + uint8 *keystate2; + keystate2 = SDL_GetKeyState(NULL); + if(keystate2[SDLK_DOWN]) + { + if (selectedoption > 9 && selectedoption < 20) //graphics menu + { + selectedoption++; + if (selectedoption == 16) selectedoption = 10; + } + else if (selectedoption > 19 && selectedoption < 30) //remap menu + { + selectedoption++; + if (selectedoption == 29) selectedoption = 20; + } + else if (selectedoption > 29 && selectedoption < 40) //save menu + { + selectedoption++; + if (selectedoption == 40) selectedoption = 30; + } + else if (selectedoption > 39 && selectedoption < 50) //load menu + { + selectedoption++; + if (selectedoption == 50) selectedoption = 40; + } + else if (selectedoption > 49 && selectedoption < 60) //misc menu + { + selectedoption++; + if (selectedoption == 57) selectedoption = 50; + } + else //main menu + { + selectedoption++; + if (selectedoption == 6) selectedoption = 7; + if (selectedoption > 8) selectedoption=0; + } + SDL_Delay(100); + } + else if(keystate2[SDLK_UP]) + { + if (selectedoption > 9 && selectedoption < 20) //graphics menu + { + selectedoption--; + if (selectedoption == 9) selectedoption = 15; + } + else if (selectedoption > 19 && selectedoption < 30) //remap menu + { + selectedoption--; + if (selectedoption == 19) selectedoption = 28; + } + else if (selectedoption > 29 && selectedoption < 40) //save menu + { + selectedoption--; + if (selectedoption == 29) selectedoption = 39; + } + else if (selectedoption > 39 && selectedoption < 50) //load menu + { + selectedoption--; + if (selectedoption == 39) selectedoption = 49; + } + else if (selectedoption > 49 && selectedoption < 60) //misc menu + { + selectedoption--; + if (selectedoption == 49) selectedoption = 56; + } + else + { //main menu + if (!selectedoption) selectedoption = 8; + else selectedoption--; + if (selectedoption == 6) selectedoption = 5; + } + SDL_Delay(100); + } + else if(keystate2[SDLK_LALT] && menustate != REMAP_OPTIONS) //back to last menu or quit menu + { + if (menustate == GRAPHICS_OPTIONS) + { + menustate = MAINMENU; + selectedoption = 3; + SDL_Delay(130); + } + else if (menustate == SAVE_STATE) + { + menustate = MAINMENU; + selectedoption = 1; + SDL_Delay(130); + } + else if (menustate == LOAD_STATE) + { + menustate = MAINMENU; + selectedoption = 2; + SDL_Delay(130); + } + else if (menustate == MISC_OPTIONS) + { + menustate = MAINMENU; + selectedoption = 5; + SDL_Delay(130); + } + else if (menustate == MAINMENU) + { + gotomenu=0; + selectedoption=0; + SDL_Delay(130); + break; + } + } + else if(keystate2[SDLK_LCTRL] && menustate != REMAP_OPTIONS) + { + if (selectedoption == 0) + { //Resume + gotomenu=0; + selectedoption=0; + SDL_Delay(130); + break; + } + else if (selectedoption == 1) //Save + { + menustate = SAVE_STATE; + selectedoption = 30; + SDL_Delay(130); + } + else if (selectedoption == 2) //Load + { + menustate = LOAD_STATE; + selectedoption = 40; + SDL_Delay(130); + } + else if (selectedoption == 3) //Graphics + { + menustate = GRAPHICS_OPTIONS; + selectedoption = 10; + SDL_Delay(200); + } + else if (selectedoption == 4) //Remap + { + menustate = REMAP_OPTIONS; + selectedoption=20; + SDL_Delay(200); + } + else if (selectedoption == 5) //Misc. + { + menustate = MISC_OPTIONS; + selectedoption=50; + SDL_Delay(200); + } + else if (selectedoption == 7) //Reset + { + gotomenu = 0; + selectedoption=0; + system_reset(); + SDL_Delay(130); + break; + } + else if (selectedoption == 8) //Quit + { + exit(0); + SDL_Delay(130); + break; + } + else if (selectedoption == 10) + { //Scaling + config.gcw0_fullscreen = !config.gcw0_fullscreen; + SDL_Delay(130); + config_save(); + } + else if (selectedoption == 11) + { //Keep aspect ratio + SDL_Delay(130); + config.keepaspectratio = !config.keepaspectratio; + config_save(); + do_once = 1; + } + else if (selectedoption == 12) + { //Scanlines (GG) + SDL_Delay(130); + config.gg_scanlines = !config.gg_scanlines; + config_save(); + } + else if (selectedoption == 13) + { //Mask left bar + SDL_Delay(130); + config.smsmaskleftbar = !config.smsmaskleftbar; + config_save(); + } + else if (selectedoption == 14) + { //Frameskip + SDL_Delay(130); + config.gcw0_frameskip ++; + if (config.gcw0_frameskip == 4) config.gcw0_frameskip = 0; + config_save(); + } + else if (selectedoption == 15) + { //Back to main menu + menustate = MAINMENU; + selectedoption = 3; + SDL_Delay(130); + } + else if (selectedoption == 30) + { + //Return to main menu + menustate = MAINMENU; + selectedoption = 1; + SDL_Delay(130); + } + else if (selectedoption > 30 && selectedoption < 40) + { + //save to selected savestate + char save_state_file[256]; + sprintf(save_state_file,"%s/%s.gp%d", get_save_directory(), rom_filename, selectedoption-30); + FILE *f = fopen(save_state_file,"wb"); + if (f) + { + uint8 buf[STATE_SIZE]; + int len = state_save(buf); + fwrite(&buf, len, 1, f); + fclose(f); + } + + //Save BMP screenshot + char save_state_screenshot[256]; + sprintf(save_state_screenshot,"%s/%s.%d.bmp", get_save_directory(), rom_filename, selectedoption-30); + SDL_Surface* screenshot; + if (!config.gcw0_fullscreen) + { + screenshot = SDL_CreateRGBSurface(SDL_HWSURFACE, sdl_video.srect.w, sdl_video.srect.h, 16, 0, 0, 0, 0); + SDL_Rect temp; + temp.x = 0; + temp.y = 0; + temp.w = sdl_video.srect.w; + temp.h = sdl_video.srect.h; + + SDL_BlitSurface(sdl_video.surf_bitmap, &temp, screenshot, &temp); + SDL_SaveBMP(screenshot, save_state_screenshot); + SDL_FreeSurface(screenshot); + } + else + { + screenshot = SDL_CreateRGBSurface(SDL_HWSURFACE, gcw0_w, gcw0_h, 16, 0, 0, 0, 0); + SDL_Rect temp; + temp.x = 0; + temp.y = 0; + temp.w = gcw0_w; + temp.h = gcw0_h; + + SDL_BlitSurface(sdl_video.surf_bitmap, &temp, screenshot, &temp); + SDL_SaveBMP(screenshot, save_state_screenshot); + SDL_FreeSurface(screenshot); + } + + SDL_Delay(250); + if (config.sl_autoresume) + { + menustate = MAINMENU; + selectedoption = 0; + gotomenu = 0; + break; + } + } + else if (selectedoption == 40) + { + //return to main menu + menustate = MAINMENU; + selectedoption = 2; + SDL_Delay(130); + } + else if (selectedoption > 40 && selectedoption < 50) + { + //load selected loadstate + char save_state_file[256]; + sprintf(save_state_file,"%s/%s.gp%d", get_save_directory(), rom_filename, selectedoption-40 ); + FILE *f = fopen(save_state_file,"rb"); + if (f) + { + uint8 buf[STATE_SIZE]; + fread(&buf, STATE_SIZE, 1, f); + state_load(buf); + fclose(f); + } + if (config.sl_autoresume) + { + gotomenu = 0; + menustate = MAINMENU; + selectedoption = 0; + SDL_Delay(250); + break; + } + } + else if (selectedoption == 50) + { + //return to main menu + menustate = MAINMENU; + selectedoption = 5; + SDL_Delay(130); + } + else if (selectedoption == 51) + { + //toggle auto resume when save/loading + config.sl_autoresume=!config.sl_autoresume; + config_save(); + SDL_Delay(130); + } + else if (selectedoption == 52) + { + //toggle A-Stick + config.a_stick=!config.a_stick; + config_save(); + SDL_Delay(130); + } + else if (selectedoption == 53) + { + config.lock_on = (++config.lock_on == 4)? 0 : config.lock_on; + config_save(); + SDL_Delay(130); + } + else if (selectedoption == 54) + { + config.ym2413 = !config.ym2413; + config_save(); + SDL_Delay(130); + } + else if (selectedoption == 55) + { + config.lightgun_speed++; + if (config.lightgun_speed == 4) + config.lightgun_speed = 1; + config_save(); + SDL_Delay(130); + } + else if (selectedoption == 56) + { + config.cursor++; + if (config.cursor == 4) + config.cursor = 0; + config_save(); + SDL_Delay(130); + } + + } + else if(menustate == REMAP_OPTIONS) + {// REMAP_OPTIONS needs to capture all input + SDLKey pressed_key = 0; + + if (keystate2[SDLK_RETURN]) + pressed_key = SDLK_RETURN; + else if (keystate2[SDLK_LCTRL]) + pressed_key = SDLK_LCTRL; + else if (keystate2[SDLK_LALT]) + pressed_key = SDLK_LALT; + else if (keystate2[SDLK_LSHIFT]) + pressed_key = SDLK_LSHIFT; + else if (keystate2[SDLK_SPACE]) + pressed_key = SDLK_SPACE; + else if (keystate2[SDLK_TAB]) + pressed_key = SDLK_TAB; + else if (keystate2[SDLK_BACKSPACE]) + pressed_key = SDLK_BACKSPACE; + else if (keystate2[SDLK_ESCAPE]) + pressed_key = SDLK_ESCAPE; + + if (pressed_key) + { + if (selectedoption == 20) + { + //button a remap + config.buttons[A] = (pressed_key==SDLK_ESCAPE)? 0: pressed_key; + config_save(); + SDL_Delay(130); + selectedoption++; + } + else if (selectedoption == 21) + { + //button b remap + config.buttons[B] = (pressed_key==SDLK_ESCAPE)? 0: pressed_key; + config_save(); + SDL_Delay(130); + selectedoption++; + } + else if (selectedoption == 22) + { + //button c remap + config.buttons[C] = (pressed_key==SDLK_ESCAPE)? 0: pressed_key; + config_save(); + SDL_Delay(130); + selectedoption++; + } + else if (selectedoption == 23) + { + //button x remap + config.buttons[X] = (pressed_key==SDLK_ESCAPE)? 0: pressed_key; + config_save(); + SDL_Delay(130); + selectedoption++; + } + else if (selectedoption == 24) + { + //button y remap + config.buttons[Y] = (pressed_key==SDLK_ESCAPE)? 0: pressed_key; + config_save(); + SDL_Delay(130); + selectedoption++; + } + else if (selectedoption == 25) + { + //button z remap + config.buttons[Z] = (pressed_key==SDLK_ESCAPE)? 0: pressed_key; + config_save(); + SDL_Delay(130); + selectedoption++; + } + else if (selectedoption == 26) + { + //button start remap + config.buttons[START] = (pressed_key==SDLK_ESCAPE)? 0: pressed_key; + config_save(); + SDL_Delay(130); + selectedoption++; + } + else if (selectedoption == 27) + { + //button mode remap + config.buttons[MODE] = pressed_key; + config_save(); + SDL_Delay(130); + selectedoption++; + } + else if (selectedoption == 28) + { + //return to main menu + menustate = MAINMENU; + selectedoption = 4; + SDL_Delay(130); + } + } + } + } + }//menu loop + SDL_PauseAudio(0); + + if(config.gcw0_fullscreen) { + if ( (system_hw == SYSTEM_MARKIII) || (system_hw == SYSTEM_SMS) || (system_hw == SYSTEM_SMS2) || (system_hw == SYSTEM_PBC) ) + { + gcw0_w=sdl_video.drect.w; + gcw0_h=sdl_video.drect.h; + sdl_video.surf_screen = SDL_SetVideoMode(256,gcw0_h, 16, SDL_HWSURFACE | +#ifdef SDL_TRIPLEBUF + SDL_TRIPLEBUF); +#else + SDL_DOUBLEBUF); +#endif + } + else + { + sdl_video.drect.w = sdl_video.srect.w; + sdl_video.drect.h = sdl_video.srect.h; + sdl_video.drect.x = 0; + sdl_video.drect.y = 0; + gcw0_w=sdl_video.drect.w; + gcw0_h=sdl_video.drect.h; + sdl_video.surf_screen = SDL_SetVideoMode(gcw0_w,gcw0_h, 16, SDL_HWSURFACE | +#ifdef SDL_TRIPLEBUF + SDL_TRIPLEBUF); +#else + SDL_DOUBLEBUF); +#endif + } + } else { + SDL_FillRect(sdl_video.surf_screen, 0, 0); + SDL_Flip(sdl_video.surf_screen); + SDL_FillRect(sdl_video.surf_screen, 0, 0); + SDL_Flip(sdl_video.surf_screen); + SDL_FillRect(sdl_video.surf_screen, 0, 0); + SDL_Flip(sdl_video.surf_screen); + } + return 1; +} +#endif + +int sdl_input_update(void) +{ + uint8 *keystate = SDL_GetKeyState(NULL); + + /* reset input */ + input.pad[joynum] = 0; + if(show_lightgun) + input.pad[4] = 0; //player2: + switch (input.dev[4]) + { + case DEVICE_LIGHTGUN: + show_lightgun = 1; + /* get mouse coordinates (absolute values) */ + int x,y; + int state = SDL_GetMouseState(&x,&y); + + if (config.gcw0_fullscreen) + { + input.analog[4][0] = x; + input.analog[4][1] = y; + } else + { + input.analog[4][0] = x - (VIDEO_WIDTH-bitmap.viewport.w)/2; + input.analog[4][1] = y - (VIDEO_HEIGHT-bitmap.viewport.h)/2; + } + if (config.smsmaskleftbar) x += 8; + /* TRIGGER, B, C (Menacer only), START (Menacer & Justifier only) */ + if(keystate[SDLK_ESCAPE]) input.pad[4] |= INPUT_START; + default: + break; + } + switch (input.dev[joynum]) + { + case DEVICE_LIGHTGUN: + { + +#ifdef GCWZERO + show_lightgun = 2; + /* get mouse coordinates (absolute values) */ + int x,y; + int state = SDL_GetMouseState(&x,&y); + + if (config.gcw0_fullscreen) + { + input.analog[0][0] = x; + input.analog[0][1] = y; + } else + { + input.analog[0][0] = x - (VIDEO_WIDTH-bitmap.viewport.w)/2; + input.analog[0][1] = y - (VIDEO_HEIGHT-bitmap.viewport.h)/2; + } + if (config.smsmaskleftbar) x += 8; + /* TRIGGER, B, C (Menacer only), START (Menacer & Justifier only) */ + if(state & SDL_BUTTON_LMASK) input.pad[joynum] |= INPUT_A; + if(state & SDL_BUTTON_RMASK) input.pad[joynum] |= INPUT_B; + if(state & SDL_BUTTON_MMASK) input.pad[joynum] |= INPUT_C; + if(keystate[SDLK_ESCAPE]) input.pad[0] |= INPUT_START; +#else + /* get mouse coordinates (absolute values) */ + int x,y; + int state = SDL_GetMouseState(&x,&y); + + /* X axis */ + input.analog[joynum][0] = x - (VIDEO_WIDTH-bitmap.viewport.w)/2; + + /* Y axis */ + input.analog[joynum][1] = y - (VIDEO_HEIGHT-bitmap.viewport.h)/2; + + /* TRIGGER, B, C (Menacer only), START (Menacer & Justifier only) */ + if(state & SDL_BUTTON_LMASK) input.pad[joynum] |= INPUT_A; + if(state & SDL_BUTTON_RMASK) input.pad[joynum] |= INPUT_B; + if(state & SDL_BUTTON_MMASK) input.pad[joynum] |= INPUT_C; + if(keystate[SDLK_f]) input.pad[joynum] |= INPUT_START; + break; +#endif + } +#ifndef GCWZERO + case DEVICE_PADDLE: + { + /* get mouse (absolute values) */ + int x; + int state = SDL_GetMouseState(&x, NULL); + + /* Range is [0;256], 128 being middle position */ + input.analog[joynum][0] = x * 256 /VIDEO_WIDTH; + + /* Button I -> 0 0 0 0 0 0 0 I*/ + if(state & SDL_BUTTON_LMASK) input.pad[joynum] |= INPUT_B; + + break; + } + + case DEVICE_SPORTSPAD: + { + /* get mouse (relative values) */ + int x,y; + int state = SDL_GetRelativeMouseState(&x,&y); + + /* Range is [0;256] */ + input.analog[joynum][0] = (unsigned char)(-x & 0xFF); + input.analog[joynum][1] = (unsigned char)(-y & 0xFF); + + /* Buttons I & II -> 0 0 0 0 0 0 II I*/ + if(state & SDL_BUTTON_LMASK) input.pad[joynum] |= INPUT_B; + if(state & SDL_BUTTON_RMASK) input.pad[joynum] |= INPUT_C; + + break; + } + + case DEVICE_MOUSE: + { + SDL_ShowCursor(1); + /* get mouse (relative values) */ + int x,y; + int state = SDL_GetRelativeMouseState(&x,&y); + + /* Sega Mouse range is [-256;+256] */ + input.analog[joynum][0] = x * 2; + input.analog[joynum][1] = y * 2; + + /* Vertical movement is upsidedown */ + if (!config.invert_mouse) + input.analog[joynum][1] = 0 - input.analog[joynum][1]; + + /* Start,Left,Right,Middle buttons -> 0 0 0 0 START MIDDLE RIGHT LEFT */ + if(state & SDL_BUTTON_LMASK) input.pad[joynum] |= INPUT_B; + if(state & SDL_BUTTON_RMASK) input.pad[joynum] |= INPUT_C; + if(state & SDL_BUTTON_MMASK) input.pad[joynum] |= INPUT_A; + if(keystate[SDLK_f]) input.pad[joynum] |= INPUT_START; + + break; + } + + case DEVICE_XE_1AP: + { + /* A,B,C,D,Select,START,E1,E2 buttons -> E1(?) E2(?) START SELECT(?) A B C D */ + if(keystate[SDLK_a]) input.pad[joynum] |= INPUT_START; + if(keystate[SDLK_s]) input.pad[joynum] |= INPUT_A; + if(keystate[SDLK_d]) input.pad[joynum] |= INPUT_C; + if(keystate[SDLK_f]) input.pad[joynum] |= INPUT_Y; + if(keystate[SDLK_z]) input.pad[joynum] |= INPUT_B; + if(keystate[SDLK_x]) input.pad[joynum] |= INPUT_X; + if(keystate[SDLK_c]) input.pad[joynum] |= INPUT_MODE; + if(keystate[SDLK_v]) input.pad[joynum] |= INPUT_Z; + + /* Left Analog Stick (bidirectional) */ + if(keystate[SDLK_UP]) input.analog[joynum][1]-=2; + else if(keystate[SDLK_DOWN]) input.analog[joynum][1]+=2; + else input.analog[joynum][1] = 128; + if(keystate[SDLK_LEFT]) input.analog[joynum][0]-=2; + else if(keystate[SDLK_RIGHT]) input.analog[joynum][0]+=2; + else input.analog[joynum][0] = 128; + + /* Right Analog Stick (unidirectional) */ + if(keystate[SDLK_KP8]) input.analog[joynum+1][0]-=2; + else if(keystate[SDLK_KP2]) input.analog[joynum+1][0]+=2; + else if(keystate[SDLK_KP4]) input.analog[joynum+1][0]-=2; + else if(keystate[SDLK_KP6]) input.analog[joynum+1][0]+=2; + else input.analog[joynum+1][0] = 128; + + /* Limiters */ + if (input.analog[joynum][0] > 0xFF) input.analog[joynum][0] = 0xFF; + else if (input.analog[joynum][0] < 0) input.analog[joynum][0] = 0; + if (input.analog[joynum][1] > 0xFF) input.analog[joynum][1] = 0xFF; + else if (input.analog[joynum][1] < 0) input.analog[joynum][1] = 0; + if (input.analog[joynum+1][0] > 0xFF) input.analog[joynum+1][0] = 0xFF; + else if (input.analog[joynum+1][0] < 0) input.analog[joynum+1][0] = 0; + if (input.analog[joynum+1][1] > 0xFF) input.analog[joynum+1][1] = 0xFF; + else if (input.analog[joynum+1][1] < 0) input.analog[joynum+1][1] = 0; + + break; + } + + case DEVICE_PICO: + { + /* get mouse (absolute values) */ + int x,y; + int state = SDL_GetMouseState(&x,&y); + + /* Calculate X Y axis values */ + input.analog[0][0] = 0x3c + (x * (0x17c-0x03c+1)) / VIDEO_WIDTH; + input.analog[0][1] = 0x1fc + (y * (0x2f7-0x1fc+1)) / VIDEO_HEIGHT; + + /* Map mouse buttons to player #1 inputs */ + if(state & SDL_BUTTON_MMASK) pico_current = (pico_current + 1) & 7; + if(state & SDL_BUTTON_RMASK) input.pad[0] |= INPUT_PICO_RED; + if(state & SDL_BUTTON_LMASK) input.pad[0] |= INPUT_PICO_PEN; + + break; + } + + case DEVICE_TEREBI: + { + /* get mouse (absolute values) */ + int x,y; + int state = SDL_GetMouseState(&x,&y); + + /* Calculate X Y axis values */ + input.analog[0][0] = (x * 250) / VIDEO_WIDTH; + input.analog[0][1] = (y * 250) / VIDEO_HEIGHT; + + /* Map mouse buttons to player #1 inputs */ + if(state & SDL_BUTTON_RMASK) input.pad[0] |= INPUT_B; + + break; + } + + case DEVICE_GRAPHIC_BOARD: + { + /* get mouse (absolute values) */ + int x,y; + int state = SDL_GetMouseState(&x,&y); + + /* Calculate X Y axis values */ + input.analog[0][0] = (x * 255) / VIDEO_WIDTH; + input.analog[0][1] = (y * 255) / VIDEO_HEIGHT; + + /* Map mouse buttons to player #1 inputs */ + if(state & SDL_BUTTON_LMASK) input.pad[0] |= INPUT_GRAPHIC_PEN; + if(state & SDL_BUTTON_RMASK) input.pad[0] |= INPUT_GRAPHIC_MENU; + if(state & SDL_BUTTON_MMASK) input.pad[0] |= INPUT_GRAPHIC_DO; + + break; + } + + case DEVICE_ACTIVATOR: + { + if(keystate[SDLK_g]) input.pad[joynum] |= INPUT_ACTIVATOR_7L; + if(keystate[SDLK_h]) input.pad[joynum] |= INPUT_ACTIVATOR_7U; + if(keystate[SDLK_j]) input.pad[joynum] |= INPUT_ACTIVATOR_8L; + if(keystate[SDLK_k]) input.pad[joynum] |= INPUT_ACTIVATOR_8U; + } +#endif + default: + { +#ifdef GCWZERO + if(keystate[config.buttons[A]]) input.pad[joynum] |= INPUT_A; + if(keystate[config.buttons[B]]) input.pad[joynum] |= INPUT_B; + if(keystate[config.buttons[C]]) input.pad[joynum] |= INPUT_C; + if(keystate[config.buttons[START]]) input.pad[joynum] |= INPUT_START; + if (show_lightgun == 1) + { + if(keystate[config.buttons[X]]) input.pad[4] |= INPUT_A; //player 2 + if(keystate[config.buttons[Y]]) input.pad[4] |= INPUT_B; //player 2 + if(keystate[config.buttons[Z]]) input.pad[4] |= INPUT_C; //player 2 + } else + if (show_lightgun == 2) + { + if(keystate[config.buttons[X]]) input.pad[4] |= INPUT_A; //player 2 + if(keystate[config.buttons[Y]]) input.pad[4] |= INPUT_B; //player 2 + if(keystate[config.buttons[Z]]) input.pad[4] |= INPUT_C; //player 2 + } else + { + if(keystate[config.buttons[X]]) input.pad[joynum] |= INPUT_X; + if(keystate[config.buttons[Y]]) input.pad[joynum] |= INPUT_Y; + if(keystate[config.buttons[Z]]) input.pad[joynum] |= INPUT_Z; + } + if(keystate[config.buttons[MODE]]) input.pad[joynum] |= INPUT_MODE; + if (keystate[SDLK_ESCAPE] && keystate[SDLK_RETURN]) + { + gotomenu=1; + } + if (keystate[SDLK_ESCAPE] && keystate[SDLK_TAB]) + { + //save to quicksave slot + char save_state_file[256]; + sprintf(save_state_file,"%s/%s.gp1", get_save_directory(), rom_filename); + FILE *f = fopen(save_state_file,"wb"); + if (f) + { + uint8 buf[STATE_SIZE]; + int len = state_save(buf); + fwrite(&buf, len, 1, f); + fclose(f); + } + //Save BMP screenshot + char save_state_screenshot[256]; + sprintf(save_state_screenshot,"%s/%s.1.bmp", get_save_directory(), rom_filename); + SDL_Surface* screenshot; + if (!config.gcw0_fullscreen) + { + screenshot = SDL_CreateRGBSurface(SDL_HWSURFACE, sdl_video.srect.w, sdl_video.srect.h, 16, 0, 0, 0, 0); + SDL_Rect temp; + temp.x = 0; + temp.y = 0; + temp.w = sdl_video.srect.w; + temp.h = sdl_video.srect.h; + + SDL_BlitSurface(sdl_video.surf_bitmap, &temp, screenshot, &temp); + SDL_SaveBMP(screenshot, save_state_screenshot); + SDL_FreeSurface(screenshot); + } + else + { + screenshot = SDL_CreateRGBSurface(SDL_HWSURFACE, gcw0_w, gcw0_h, 16, 0, 0, 0, 0); + SDL_Rect temp; + temp.x = 0; + temp.y = 0; + temp.w = gcw0_w; + temp.h = gcw0_h; + + SDL_BlitSurface(sdl_video.surf_bitmap, &temp, screenshot, &temp); + SDL_SaveBMP(screenshot, save_state_screenshot); + SDL_FreeSurface(screenshot); + } + + SDL_Delay(250); + } + if (keystate[SDLK_ESCAPE] && keystate[SDLK_BACKSPACE]) + { + //load quicksave slot + char save_state_file[256]; + sprintf(save_state_file,"%s/%s.gp1", get_save_directory(), rom_filename ); + FILE *f = fopen(save_state_file,"rb"); + if (f) + { + uint8 buf[STATE_SIZE]; + fread(&buf, STATE_SIZE, 1, f); + state_load(buf); + fclose(f); + } + SDL_Delay(250); + + } +#else + if(keystate[SDLK_a]) input.pad[joynum] |= INPUT_A; + if(keystate[SDLK_s]) input.pad[joynum] |= INPUT_B; + if(keystate[SDLK_d]) input.pad[joynum] |= INPUT_C; + if(keystate[SDLK_f]) input.pad[joynum] |= INPUT_START; + if(keystate[SDLK_z]) input.pad[joynum] |= INPUT_X; + if(keystate[SDLK_x]) input.pad[joynum] |= INPUT_Y; + if(keystate[SDLK_c]) input.pad[joynum] |= INPUT_Z; + if(keystate[SDLK_v]) input.pad[joynum] |= INPUT_MODE; +#endif + +#ifdef GCWZERO //A-stick support + static int MoveLeft = 0; + static int MoveRight = 0; + static int MoveUp = 0; + static int MoveDown = 0; + Sint16 x_move = 0; + Sint16 y_move = 0; + static int lg_left = 0; + static int lg_right = 0; + static int lg_up = 0; + static int lg_down = 0; + SDL_Joystick* joy; + if(SDL_NumJoysticks() > 0) + { + joy = SDL_JoystickOpen(0); + x_move = SDL_JoystickGetAxis(joy, 0); + y_move = SDL_JoystickGetAxis(joy, 1); + } + +// Control lightgun with A-stick if activated + if (show_lightgun) + { + lg_left = 0; + lg_right = 0; + lg_up = 0; + lg_down = 0; + + if (x_move < -1000 || x_move > 1000) + { + if (x_move < -1000 ) lg_left = 1; + if (x_move < -20000) lg_left = 3; + if (x_move > 1000 ) lg_right = 1; + if (x_move > 20000) lg_right = 3; + current_time = time(NULL); //cursor disappears after 3 seconds... + } + if (y_move < -1000 || y_move > 1000) + { + if (y_move < -1000 ) lg_up = 1; + if (y_move < -20000) lg_up = 3; + if (y_move > 1000 ) lg_down = 1; + if (y_move > 20000) lg_down = 3; + current_time = time(NULL); + } +// Keep mouse within screen, wrap around! + int x,y; + int state = SDL_GetMouseState(&x,&y); + if (!config.gcw0_fullscreen) + { + if ((x - lg_left ) < sdl_video.drect.x ) x = VIDEO_WIDTH - sdl_video.drect.x; + if ((y - lg_up ) < sdl_video.drect.y ) y = VIDEO_HEIGHT - sdl_video.drect.y; +// if ((x + lg_right) > 288) x = 288; +// if ((y + lg_down ) > 216) y = 216; +// if ((x + lg_right) > 320) x = 320; +// if ((y + lg_down ) > 240) y = 240; + if ((x + lg_right) > VIDEO_WIDTH - sdl_video.drect.x) x = sdl_video.drect.x; + if ((y + lg_down ) > VIDEO_HEIGHT - sdl_video.drect.y) y = sdl_video.drect.y; + +// sdl_video.drect.x = (VIDEO_WIDTH - sdl_video.drect.w) / 2; + // sdl_video.drect.y = (VIDEO_HEIGHT - sdl_video.drect.h) / 2; + + + } else //scaling on + { + if ((x - lg_left) < 0) x = gcw0_w; + if ((y - lg_up ) < 0) y = gcw0_h; + if ((x + lg_right) > gcw0_w) x = 0; + if ((y + lg_down ) > gcw0_h) y = 0; + } + SDL_WarpMouse( ( x+ ( ( lg_right - lg_left ) * config.lightgun_speed ) ) , + ( y+ ( ( lg_down - lg_up ) * config.lightgun_speed ) ) ); + + } else +// otherwise it's just mirroring the D-pad controls + if (config.a_stick) + { + MoveLeft = 0; + MoveRight = 0; + MoveUp = 0; + MoveDown = 0; + if (x_move < -1000 || x_move > 1000) + { + if (x_move < -1000) MoveLeft = 1; + if (x_move > 1000) MoveRight = 1; + } + if (y_move < -1000 || y_move > 1000) + { + if (y_move < -1000) MoveUp = 1; + if (y_move > 1000) MoveDown = 1; + } + } +if(show_lightgun == 1) //Genesis/MD D-pad controls player 2 +{ + if(MoveUp == 1) input.pad[4] |= INPUT_UP; + if(MoveDown == 1) input.pad[4] |= INPUT_DOWN; + if(MoveLeft == 1) input.pad[4] |= INPUT_LEFT; + if(MoveRight == 1) input.pad[4] |= INPUT_RIGHT; + if(keystate[SDLK_UP] == 1) input.pad[joynum] |= INPUT_UP; + if(keystate[SDLK_DOWN] == 1) input.pad[joynum] |= INPUT_DOWN; + if(keystate[SDLK_LEFT] == 1) input.pad[joynum] |= INPUT_LEFT; + if(keystate[SDLK_RIGHT]== 1) input.pad[joynum] |= INPUT_RIGHT; +} else +if(show_lightgun == 2) //SMS D-pad controls player 2 +{ + if(MoveUp == 1) input.pad[joynum] |= INPUT_UP; + if(MoveDown == 1) input.pad[joynum] |= INPUT_DOWN; + if(MoveLeft == 1) input.pad[joynum] |= INPUT_LEFT; + if(MoveRight == 1) input.pad[joynum] |= INPUT_RIGHT; + if(keystate[SDLK_UP] == 1) input.pad[4] |= INPUT_UP; + if(keystate[SDLK_DOWN] == 1) input.pad[4] |= INPUT_DOWN; + if(keystate[SDLK_LEFT] == 1) input.pad[4] |= INPUT_LEFT; + if(keystate[SDLK_RIGHT]== 1) input.pad[4] |= INPUT_RIGHT; +} else +{ + if (keystate[SDLK_UP] || MoveUp == 1) input.pad[joynum] |= INPUT_UP; + else if(keystate[SDLK_DOWN] || MoveDown == 1) input.pad[joynum] |= INPUT_DOWN; + if (keystate[SDLK_LEFT] || MoveLeft == 1) input.pad[joynum] |= INPUT_LEFT; + else if(keystate[SDLK_RIGHT] || MoveRight == 1) input.pad[joynum] |= INPUT_RIGHT; +} +#else + if (keystate[SDLK_UP] ) input.pad[joynum] |= INPUT_UP; + else if(keystate[SDLK_DOWN] ) input.pad[joynum] |= INPUT_DOWN; + if (keystate[SDLK_LEFT] ) input.pad[joynum] |= INPUT_LEFT; + else if(keystate[SDLK_RIGHT]) input.pad[joynum] |= INPUT_RIGHT; +#endif + } + } + return 1; +} + +int main (int argc, char **argv) +{ + FILE *fp; + int running = 1; + atexit(shutdown); + + /* Print help if no game specified */ + if(argc < 2) + { + char caption[256]; + sprintf(caption, "Genesis Plus GX\\SDL\nusage: %s gamename\n", argv[0]); + MessageBox(NULL, caption, "Information", 0); + exit(1); + } + + error_init(); + create_default_directories(); + /* set default config */ + set_config_defaults(); + + /* using rom file name instead of crc code to save files */ + sprintf(rom_filename, "%s", get_file_name(argv[1])); + + /* mark all BIOS as unloaded */ + system_bios = 0; + + /* Genesis BOOT ROM support (2KB max) */ + memset(boot_rom, 0xFF, 0x800); + fp = fopen(MD_BIOS, "rb"); + if (fp != NULL) + { + int i; + + /* read BOOT ROM */ + fread(boot_rom, 1, 0x800, fp); + fclose(fp); + + /* check BOOT ROM */ + if (!memcmp((char *)(boot_rom + 0x120),"GENESIS OS", 10)) + { + /* mark Genesis BIOS as loaded */ + system_bios = SYSTEM_MD; + } + + /* Byteswap ROM */ + for (i=0; i<0x800; i+=2) + { + uint8 temp = boot_rom[i]; + boot_rom[i] = boot_rom[i+1]; + boot_rom[i+1] = temp; + } + } + + /* initialize SDL */ + if(SDL_Init(0) < 0) + { + char caption[256]; + sprintf(caption, "SDL initialization failed"); + MessageBox(NULL, caption, "Error", 0); + exit(1); + } +#ifdef GCWZERO + sdl_joystick_init(); +#endif + sdl_video_init(); + if (use_sound) sdl_sound_init(); + sdl_sync_init(); + + /* initialize Genesis virtual system */ + SDL_LockSurface(sdl_video.surf_bitmap); + memset(&bitmap, 0, sizeof(t_bitmap)); + bitmap.width = 720; + bitmap.height = 576; +#if defined(USE_8BPP_RENDERING) + bitmap.pitch = (bitmap.width * 1); +#elif defined(USE_15BPP_RENDERING) + bitmap.pitch = (bitmap.width * 2); +#elif defined(USE_16BPP_RENDERING) + bitmap.pitch = (bitmap.width * 2); +#elif defined(USE_32BPP_RENDERING) + bitmap.pitch = (bitmap.width * 4); +#endif + bitmap.data = sdl_video.surf_bitmap->pixels; + SDL_UnlockSurface(sdl_video.surf_bitmap); + bitmap.viewport.changed = 3; + + /* Load game file */ + if(!load_rom(argv[1])) + { + char caption[256]; + sprintf(caption, "Error loading file `%s'.", argv[1]); + MessageBox(NULL, caption, "Error", 0); + exit(1); + } + + + /* initialize system hardware */ +//NOTE gcw0 second value determines framerate + audio_init(SOUND_FREQUENCY, 0); + system_init(); + + /* Mega CD specific */ + char brm_file[256]; + if (system_hw == SYSTEM_MCD) + { + /* load internal backup RAM */ + sprintf(brm_file,"%s/", get_save_directory(), "scd.brm"); + fp = fopen(brm_file, "rb"); + if (fp!=NULL) + { + fread(scd.bram, 0x2000, 1, fp); + fclose(fp); + } + + /* check if internal backup RAM is formatted */ + if (memcmp(scd.bram + 0x2000 - 0x20, brm_format + 0x20, 0x20)) + { + /* clear internal backup RAM */ + memset(scd.bram, 0x00, 0x200); + + /* Internal Backup RAM size fields */ + brm_format[0x10] = brm_format[0x12] = brm_format[0x14] = brm_format[0x16] = 0x00; + brm_format[0x11] = brm_format[0x13] = brm_format[0x15] = brm_format[0x17] = (sizeof(scd.bram) / 64) - 3; + + /* format internal backup RAM */ + memcpy(scd.bram + 0x2000 - 0x40, brm_format, 0x40); + } + + /* load cartridge backup RAM */ + if (scd.cartridge.id) + { + sprintf(brm_file,"%s/", get_save_directory(), "cart.brm"); + fp = fopen(brm_file, "rb"); + if (fp!=NULL) + { + fread(scd.cartridge.area, scd.cartridge.mask + 1, 1, fp); + fclose(fp); + } + + /* check if cartridge backup RAM is formatted */ + if (memcmp(scd.cartridge.area + scd.cartridge.mask + 1 - 0x20, brm_format + 0x20, 0x20)) + { + /* clear cartridge backup RAM */ + memset(scd.cartridge.area, 0x00, scd.cartridge.mask + 1); + + /* Cartridge Backup RAM size fields */ + brm_format[0x10] = brm_format[0x12] = brm_format[0x14] = brm_format[0x16] = (((scd.cartridge.mask + 1) / 64) - 3) >> 8; + brm_format[0x11] = brm_format[0x13] = brm_format[0x15] = brm_format[0x17] = (((scd.cartridge.mask + 1) / 64) - 3) & 0xff; + + /* format cartridge backup RAM */ + memcpy(scd.cartridge.area + scd.cartridge.mask + 1 - sizeof(brm_format), brm_format, sizeof(brm_format)); + } + } + } + + if (sram.on) + { + /* load SRAM */ + char save_file[256]; + sprintf(save_file,"%s/%s.srm", get_save_directory(), rom_filename); + fp = fopen(save_file, "rb"); + if (fp!=NULL) + { + fread(sram.sram,0x10000,1, fp); + fclose(fp); + } + } + + /* reset system hardware */ + system_reset(); + + if(use_sound) SDL_PauseAudio(0); + + /* 3 frames = 50 ms (60hz) or 60 ms (50hz) */ + if(sdl_sync.sem_sync) + SDL_SetTimer(vdp_pal ? 60 : 50, sdl_sync_timer_callback); + + /* emulation loop */ + while(running) + { + SDL_Event event; + if (SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_USEREVENT: + { + char caption[100]; + sprintf(caption,"Genesis Plus GX - %d fps - %s)", event.user.code, (rominfo.international[0] != 0x20) ? rominfo.international : rominfo.domestic); + SDL_WM_SetCaption(caption, NULL); + break; + } + + case SDL_QUIT: + { + running = 0; + break; + } + + case SDL_KEYDOWN: + { + running = sdl_control_update(event.key.keysym.sym); + break; + } + } + } +#ifdef GCWZERO + if (do_once) + { + do_once--; //don't waste write cycles! + if (config.keepaspectratio) + { + FILE* aspect_ratio_file = fopen("/sys/devices/platform/jz-lcd.0/keep_aspect_ratio", "w"); + if (aspect_ratio_file) + { + fwrite("Y", 1, 1, aspect_ratio_file); + fclose(aspect_ratio_file); + } + } + if (!config.keepaspectratio) + { + FILE* aspect_ratio_file = fopen("/sys/devices/platform/jz-lcd.0/keep_aspect_ratio", "w"); + if (aspect_ratio_file) + { + fwrite("N", 1, 1, aspect_ratio_file); + fclose(aspect_ratio_file); + } + } + } + +#endif + sdl_video_update(); + sdl_sound_update(use_sound); + + if(!turbo_mode && sdl_sync.sem_sync && sdl_video.frames_rendered % 3 == 0) + { + if (!config.gcw0_frameskip || (config.gcw0_frameskip && (system_hw != SYSTEM_MCD))) //we really only need this for fmv sequences + if(!gotomenu) + SDL_SemWait(sdl_sync.sem_sync); +#ifdef GCWZERO + } + else + { + if (gotomenu) + gcw0menu(); +#endif + } + } + + return 0; + +} + diff --git a/gcw0/main.h b/gcw0/main.h new file mode 100644 index 0000000..e36ee17 --- /dev/null +++ b/gcw0/main.h @@ -0,0 +1,11 @@ + +#ifndef _MAIN_H_ +#define _MAIN_H_ + +#define MAX_INPUTS 8 + +extern int debug_on; +extern int log_error; +extern int sdl_input_update(void); + +#endif /* _MAIN_H_ */ diff --git a/gcw0/opk-data/CLASSIC_01.png b/gcw0/opk-data/CLASSIC_01.png new file mode 100644 index 0000000..5c4c6ce Binary files /dev/null and b/gcw0/opk-data/CLASSIC_01.png differ diff --git a/gcw0/opk-data/CLASSIC_01_RED.png b/gcw0/opk-data/CLASSIC_01_RED.png new file mode 100644 index 0000000..5ebfa57 Binary files /dev/null and b/gcw0/opk-data/CLASSIC_01_RED.png differ diff --git a/gcw0/opk-data/CLASSIC_02.png b/gcw0/opk-data/CLASSIC_02.png new file mode 100644 index 0000000..292e15d Binary files /dev/null and b/gcw0/opk-data/CLASSIC_02.png differ diff --git a/gcw0/opk-data/GG.png b/gcw0/opk-data/GG.png new file mode 100644 index 0000000..871ce9b Binary files /dev/null and b/gcw0/opk-data/GG.png differ diff --git a/gcw0/opk-data/LICENSE.txt b/gcw0/opk-data/LICENSE.txt new file mode 100644 index 0000000..be32bbd --- /dev/null +++ b/gcw0/opk-data/LICENSE.txt @@ -0,0 +1,621 @@ + +Unless otherwise explicitly stated, all code in Genesis Plus GX is released +under the following license: + +Copyright (c) 1998-2003 Charles MacDonald +Some portions copyright Nicola Salmoria and the MAME team +All rights reserved. + +Copyright (c) 2007-2015 Eke-Eke +All rights reserved. + +Redistribution and use of this code or any derivative works are permitted +provided that the following conditions are met: + +* Redistributions may not be sold, nor may they be used in a commercial +product or activity. + +* Redistributions that are modified from the original source must include the +complete source code, including the source code for all components used by a +binary built from the modified sources. However, as a special exception, the +source code distributed need not include anything that is normally distributed +(in either source or binary form) with the major components (compiler, kernel, +and so on) of the operating system on which the executable runs, unless that +component itself accompanies the executable. + +* Redistributions must reproduce the above copyright notice, this list of +conditions and the following disclaimer in the documentation and/or other +materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + + +---------------------------------------------------------------------------------------- + +TREMOR library is distributed under the following license: + +Copyright (c) 2002, Xiph.org Foundation + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +- Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +- Neither the name of the Xiph.org Foundation nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +---------------------------------------------------------------------------------------- + + +NTSC Filter and Blip Buffer libraries are distributed under the +terms of the GNU Lesser General Public License (LGPL) + + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + +---------------------------------------------------------------------------------------- + +Gamecube & Wii ports are linked with LIBASND library and includes code distributed under +the following license: + +Copyright (c) 2008 Hermes +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are +permitted provided that the following conditions are met: + +- Redistributions of source code must retain the above copyright notice, this list of + conditions and the following disclaimer. +- Redistributions in binary form must reproduce the above copyright notice, this list + of conditions and the following disclaimer in the documentation and/or other + materials provided with the distribution. +- The names of the contributors may not be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +---------------------------------------------------------------------------------------- + + + diff --git a/gcw0/opk-data/MCD.png b/gcw0/opk-data/MCD.png new file mode 100644 index 0000000..a37c3fd Binary files /dev/null and b/gcw0/opk-data/MCD.png differ diff --git a/gcw0/opk-data/MD.png b/gcw0/opk-data/MD.png new file mode 100644 index 0000000..a1e0211 Binary files /dev/null and b/gcw0/opk-data/MD.png differ diff --git a/gcw0/opk-data/PICO.png b/gcw0/opk-data/PICO.png new file mode 100644 index 0000000..22c1541 Binary files /dev/null and b/gcw0/opk-data/PICO.png differ diff --git a/gcw0/opk-data/ProggyTiny.ttf b/gcw0/opk-data/ProggyTiny.ttf new file mode 100644 index 0000000..1c4312c Binary files /dev/null and b/gcw0/opk-data/ProggyTiny.ttf differ diff --git a/gcw0/opk-data/SG1000.png b/gcw0/opk-data/SG1000.png new file mode 100644 index 0000000..70b6084 Binary files /dev/null and b/gcw0/opk-data/SG1000.png differ diff --git a/gcw0/opk-data/SMS.png b/gcw0/opk-data/SMS.png new file mode 100644 index 0000000..3fcac93 Binary files /dev/null and b/gcw0/opk-data/SMS.png differ diff --git a/gcw0/opk-data/SQUARE_02.png b/gcw0/opk-data/SQUARE_02.png new file mode 100644 index 0000000..cd43683 Binary files /dev/null and b/gcw0/opk-data/SQUARE_02.png differ diff --git a/gcw0/opk-data/default.gcw0.desktop b/gcw0/opk-data/default.gcw0.desktop new file mode 100644 index 0000000..8e126cf --- /dev/null +++ b/gcw0/opk-data/default.gcw0.desktop @@ -0,0 +1,11 @@ +[Desktop Entry] +Name=Genesis Plus GX +Comment=Genesis Plus GX for GCW-Zero +MimeType=application/zip;application/x-genesis-rom;application/x-megadrive-rom;application/x-sms-rom;application/x-cd-image; +Exec=gen_gcw0 %f +Terminal=false +Type=Application +StartupNotify=true +Icon=md +Categories=emulators; +X-OD-Manual=gcw0readme.txt diff --git a/gcw0/opk-data/gcw0readme.txt b/gcw0/opk-data/gcw0readme.txt new file mode 100644 index 0000000..5babee5 --- /dev/null +++ b/gcw0/opk-data/gcw0readme.txt @@ -0,0 +1,154 @@ +Genesis Plus GX port for the GCW0 Handheld Console + +GCW0 port by: +Shin-NiL +David Knight + +Source code available at: +https://bitbucket.org/shin_nil/genesis-plus-gx-gcw0/ + +Genesis Plus GX is an open-source emulator focused on accuracy and portability, now emulating SG-1000, Master System, Game Gear, Mega Drive / Genesis and Sega / Mega CD hardware. Initially ported and designed to be running on Gamecube / Wii consoles through libogc / devkitPPC, it is also available for many other platforms through various frontends. + +The source code is based on Genesis Plus 1.3, originally developped by Charles MacDonald (http://cgfm2.emuviews.com). It has been heavily modified, with respect to initial goals and design, in order to improve accuracy of emulation, implementing new features and adding support for extra peripherals, cartridge & systems hardware. + +The result is that Genesis Plus GX is now more a continuation of the original project than a simple port, providing very accurate emulation and 100% compatibility with Genesis / Mega Drive, Sega / Mega CD, Master System, Game Gear & SG-1000 software (including all unlicensed or pirate known dumps), also emulating backwards compatibility modes when available. + +Multi-platform sourcecode is available through SVN and GIT so that other Genesis Plus ports can take advantage of it. The sourcecode is released under a specific non-commercial license, see LICENSE.txt for more informations. + +INSTRUCTIONS + +For SG-1000, Game Gear (GG), Master System (SMS) and Mega Drive (Genesis, MD) you don't need to do anything. Just locate the rom file you wish to use (zip files are supported) and run it through the gMenu. + +For Sega CD games you will need the correct bios files. These are not included with this distribution for copyright reasons. Once you have located the correct bios you will need to rename them according to the correct region: +"/usr/local/home/.genplus/bios/bios_CD_U.bin" +"/usr/local/home/.genplus/bios/bios_CD_E.bin" +"/usr/local/home/.genplus/bios/bios_CD_J.bin" +If everything is setup correctly you will then need to locate your image files and run them. .bin/.cue and .iso files are supported, .ogg compressed tracks are supported, .mp3 is NOT supported for legal reasons. (ogg is better anyway :P) + +To access the menu press SELECT+START. Controls can be reconfigured from there. In the menu A selects option, B will go back to the last menu. + +Default controls are: +D-Pad/A-Stick: Directions +X: A +B: B +A: C +L: X +Y: Y +R: Z +START: Start +SELECT: Mode +SELECT + START: Menu +SELECT + L: Quicksave to Savestate 1 +SELECT + R: Quickload savestate 1 + +In lightgun games for the Master system and Genesis/MD A-stick controls the cursor and A/B/C work as normal. Player 2 controls are mapped to the D-pad and X/Y/Z buttons. +This allows (in theory at least!) two player games. It also allows you to select which control method you prefer. Some games (eg Spacegun) require two controllers. + +CONFIGURATION OPTIONS (Default setting) + +Scaling (On) +The gcw0 has hardware bilinear scaling which when turned on will enlarge the image to fill the screen. By default this keeps aspect ration so you will still see small bars unless the scaled resolution is exactly 320x240. This is particularly useful for SMS and GG games. + +Keep aspect ratio (On) +This does nothing unless Scaling is turned on. If aspect ratio is turned off the hardware scaler will fill the whole screen and there will be no black bars visible. + +Scanlines (GG) (On) +This emulates the vertical scanlines visible on the LCD screen. It is only used when running Game Gear games. + +Mask left bar (SMS) (On) +The Master System often displays a vertical bar on the left of the screen. Setting this to 'On' will remove it. + +Frameskip (0) +In all games except Mega CD this is of no use as the emulation is near perfect speed (with the exception of Virtual Racing). Setting this value will render fewer frames allowing a little speed increase. This improves the performance of FMV scenes in Mega CD games. + +Resume on Save/Load (On) +When saving from the menu, the menu will automatically exit and the game will continue. If turned off it will return you to the menu. + +A-stick (On) +Some users have issues with their analog controls causing erratic movements. Turning off the A-stick will stop this. + +Lock-on (Off) +If enabled, when loading a rom the selected file will be loaded first. +Rename each binary file as follows: + +Game Genie: "/usr/local/home/.genplus/lock-on/ggenie.bin" +Action Replay: "/usr/local/home/.genplus/lock-on/areplay.bin" +Sonic & Knuckles: "/usr/local/home/.genplus/lock-on/sk.bin" and /usr/local/home/.genplus/lock-on/sk2chip.bin additional info + +After changing the lock-on option, you must reload the game rom. + +FM sound (SMS) (On) +Select whether to use enhanced sound on a few Master System titles. A list of compatible titles is at the foot of this readme. + +Lightgun Speed (1) +Higher values will speed up the lightgun cursor for player 1. + +Lightgun Cursor +Change the lightgun cursor. The cursor is only visible in compatible lightgun games. + +FM (enhanced music) Compatible Master system titles (taken from http://segaretro.org/FM_Sound_Unit) + +(Note in most cases, only the Japanese variants are compatible) +After Burner +Alex Kidd BMX Trial +Alex Kidd: The Lost Stars +Alien Syndrome +Aztec Adventure +Blade Eagle 3-D +Bomber Raid +Captain Silver +Cyborg Hunter +Double Dragon +Fantasy Zone II +Fantasy Zone: The Maze +Galactic Protector +Global Defense +Golvellius - Valley of Doom +Great Golf +Hoshi Wo Sagashite... +Kenseiden +Lord of The Sword +Maze Hunter 3-D +Megumi Rescue +Miracle Warriors - Seal of The Dark Lord +Nekkyuu Koushien +OutRun +Parlour Games +Penguin Land +Phantasy Star +Power Strike +R-Type +Shinobi +Solomon no Kagi - Oujo Rihita no Namida +Space Harrier 3-D +SpellCaster +Super Racing +Tensai Bakabon +Thunder Blade +Wonder Boy in Monster Land +Ys: The Vanished Omens +Zaxxon 3-D +Zillion II: The Tri Formation + +The following games have FM soundtracks but were not released in Japan: + +Altered Beast +California Games +Casino Games +Cloud Master +Galaxy Force +Golfamania +OutRun 3-D +Poseidon Wars 3-D +Rampage +Rastan +Rescue Mission +Scramble Spirits +Shanghai +Tennis Ace +Time Soldiers +Ultima IV +Vigilante +Wonder Boy III: The Dragon's Trap + +2015/03/22 diff --git a/gcw0/opk-data/scanlines.png b/gcw0/opk-data/scanlines.png new file mode 100644 index 0000000..5fd37bb Binary files /dev/null and b/gcw0/opk-data/scanlines.png differ diff --git a/gcw0/opk_build.sh b/gcw0/opk_build.sh new file mode 100644 index 0000000..86e682a --- /dev/null +++ b/gcw0/opk_build.sh @@ -0,0 +1,3 @@ +#!/bin/sh +rm -rf genplus.opk +mksquashfs gen_gcw0 opk-data/* genplus.opk -all-root -noappend -no-exports -no-xattrs \ No newline at end of file diff --git a/gcw0/osd.h b/gcw0/osd.h new file mode 100644 index 0000000..1ccc790 --- /dev/null +++ b/gcw0/osd.h @@ -0,0 +1,38 @@ + +#ifndef _OSD_H_ +#define _OSD_H_ + +#include +#include +#include +#include + +#include +#include + +#include "shared.h" +#include "main.h" +#include "config.h" +#include "error.h" +#include "unzip.h" +#include "fileio.h" + +#define MAXPATHLEN 1024 +#define DEFAULT_PATH "/.genplus" +#define osd_input_update sdl_input_update + + +#define GG_ROM "/usr/local/home/.genplus/lock-on/ggenie.bin" +#define AR_ROM "/usr/local/home/.genplus/lock-on/areplay.bin" +#define SK_ROM "/usr/local/home/.genplus/lock-on/sk.bin" +#define SK_UPMEM "/usr/local/home/.genplus/lock-on/sk2chip.bin" +#define CD_BIOS_US "/usr/local/home/.genplus/bios/bios_CD_U.bin" +#define CD_BIOS_EU "/usr/local/home/.genplus/bios/bios_CD_E.bin" +#define CD_BIOS_JP "/usr/local/home/.genplus/bios/bios_CD_J.bin" +#define MD_BIOS "/usr/local/home/.genplus/bios/bios_MD.bin" +#define MS_BIOS_US "/usr/local/home/.genplus/bios/bios_U.sms" +#define MS_BIOS_EU "/usr/local/home/.genplus/bios/bios_E.sms" +#define MS_BIOS_JP "/usr/local/home/.genplus/bios/bios_J.sms" +#define GG_BIOS "/usr/local/home/.genplus/bios/bios.gg" + +#endif /* _OSD_H_ */ diff --git a/gcw0/unzip.c b/gcw0/unzip.c new file mode 100644 index 0000000..85d1031 --- /dev/null +++ b/gcw0/unzip.c @@ -0,0 +1,1294 @@ +/* unzip.c -- IO on .zip files using zlib + Version 0.15 beta, Mar 19th, 1998, + + Read unzip.h for more info +*/ + + +#include +#include +#include +#include +#include "unzip.h" + +#ifdef STDC +# include +# include +# include +#endif +#ifdef NO_ERRNO_H + extern int errno; +#else + #include +#endif + + +#ifndef local + #define local static +#endif +/* compile with -Dlocal if your debugger can't find static symbols */ + + + +#if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \ + !defined(CASESENSITIVITYDEFAULT_NO) +#define CASESENSITIVITYDEFAULT_NO +#endif + + +#ifndef UNZ_BUFSIZE +#define UNZ_BUFSIZE (16384) +#endif + +#ifndef UNZ_MAXFILENAMEINZIP +#define UNZ_MAXFILENAMEINZIP (256) +#endif + +#ifndef ALLOC +# define ALLOC(size) (malloc(size)) +#endif +#ifndef TRYFREE +# define TRYFREE(p) {if (p) free(p);} +#endif + +#define SIZECENTRALDIRITEM (0x2e) +#define SIZEZIPLOCALHEADER (0x1e) + +/* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */ + +#ifndef SEEK_CUR +#define SEEK_CUR 1 +#endif + +#ifndef SEEK_END +#define SEEK_END 2 +#endif + +#ifndef SEEK_SET +#define SEEK_SET 0 +#endif + +const char unz_copyright[] = + " unzip 0.15 Copyright 1998 Gilles Vollant "; + +/* unz_file_info_interntal contain internal info about a file in zipfile*/ +typedef struct unz_file_info_internal_s +{ + uLong offset_curfile;/* relative offset of local header 4 bytes */ +} unz_file_info_internal; + + +/* file_in_zip_read_info_s contain internal information about a file in zipfile, + when reading and decompress it */ +typedef struct +{ + char *read_buffer; /* internal buffer for compressed data */ + z_stream stream; /* zLib stream structure for inflate */ + + uLong pos_in_zipfile; /* position in byte on the zipfile, for fseek*/ + uLong stream_initialised; /* flag set if stream structure is initialised*/ + + uLong offset_local_extrafield;/* offset of the local extra field */ + uInt size_local_extrafield;/* size of the local extra field */ + uLong pos_local_extrafield; /* position in the local extra field in read*/ + + uLong crc32; /* crc32 of all data uncompressed */ + uLong crc32_wait; /* crc32 we must obtain after decompress all */ + uLong rest_read_compressed; /* number of byte to be decompressed */ + uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/ + FILE* file; /* io structore of the zipfile */ + uLong compression_method; /* compression method (0==store) */ + uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/ +} file_in_zip_read_info_s; + + +/* unz_s contain internal information about the zipfile +*/ +typedef struct +{ + FILE* file; /* io structore of the zipfile */ + unz_global_info gi; /* public global information */ + uLong byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx)*/ + uLong num_file; /* number of the current file in the zipfile*/ + uLong pos_in_central_dir; /* pos of the current file in the central dir*/ + uLong current_file_ok; /* flag about the usability of the current file*/ + uLong central_pos; /* position of the beginning of the central dir*/ + + uLong size_central_dir; /* size of the central directory */ + uLong offset_central_dir; /* offset of start of central directory with + respect to the starting disk number */ + + unz_file_info cur_file_info; /* public info about the current file in zip*/ + unz_file_info_internal cur_file_info_internal; /* private info about it*/ + file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current + file if we are decompressing it */ +} unz_s; + + +/* =========================================================================== + Read a byte from a gz_stream; update next_in and avail_in. Return EOF + for end of file. + IN assertion: the stream s has been sucessfully opened for reading. +*/ + + +local int unzlocal_getByte(fin,pi) + FILE *fin; + int *pi; +{ + unsigned char c; + int err = fread(&c, 1, 1, fin); + if (err==1) + { + *pi = (int)c; + return UNZ_OK; + } + else + { + if (ferror(fin)) + return UNZ_ERRNO; + else + return UNZ_EOF; + } +} + + +/* =========================================================================== + Reads a long in LSB order from the given gz_stream. Sets +*/ +local int unzlocal_getShort (fin,pX) + FILE* fin; + uLong *pX; +{ + uLong x ; + int i = 0; + int err; + + err = unzlocal_getByte(fin,&i); + x = (uLong)i; + + if (err==UNZ_OK) + err = unzlocal_getByte(fin,&i); + x += ((uLong)i)<<8; + + if (err==UNZ_OK) + *pX = x; + else + *pX = 0; + return err; +} + +local int unzlocal_getLong (fin,pX) + FILE* fin; + uLong *pX; +{ + uLong x ; + int i = 0; + int err; + + err = unzlocal_getByte(fin,&i); + x = (uLong)i; + + if (err==UNZ_OK) + err = unzlocal_getByte(fin,&i); + x += ((uLong)i)<<8; + + if (err==UNZ_OK) + err = unzlocal_getByte(fin,&i); + x += ((uLong)i)<<16; + + if (err==UNZ_OK) + err = unzlocal_getByte(fin,&i); + x += ((uLong)i)<<24; + + if (err==UNZ_OK) + *pX = x; + else + *pX = 0; + return err; +} + + +/* My own strcmpi / strcasecmp */ +local int strcmpcasenosensitive_internal (fileName1,fileName2) + const char* fileName1; + const char* fileName2; +{ + for (;;) + { + char c1=*(fileName1++); + char c2=*(fileName2++); + if ((c1>='a') && (c1<='z')) + c1 -= 0x20; + if ((c2>='a') && (c2<='z')) + c2 -= 0x20; + if (c1=='\0') + return ((c2=='\0') ? 0 : -1); + if (c2=='\0') + return 1; + if (c1c2) + return 1; + } +} + + +#ifdef CASESENSITIVITYDEFAULT_NO +#define CASESENSITIVITYDEFAULTVALUE 2 +#else +#define CASESENSITIVITYDEFAULTVALUE 1 +#endif + +#ifndef STRCMPCASENOSENTIVEFUNCTION +#define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal +#endif + +/* + Compare two filename (fileName1,fileName2). + If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) + If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi + or strcasecmp) + If iCaseSenisivity = 0, case sensitivity is defaut of your operating system + (like 1 on Unix, 2 on Windows) + +*/ +extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity) + const char* fileName1; + const char* fileName2; + int iCaseSensitivity; +{ + if (iCaseSensitivity==0) + iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE; + + if (iCaseSensitivity==1) + return strcmp(fileName1,fileName2); + + return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2); +} + +#define BUFREADCOMMENT (0x400) + +/* + Locate the Central directory of a zipfile (at the end, just before + the global comment) +*/ +local uLong unzlocal_SearchCentralDir(fin) + FILE *fin; +{ + unsigned char* buf; + uLong uSizeFile; + uLong uBackRead; + uLong uMaxBack=0xffff; /* maximum size of global comment */ + uLong uPosFound=0; + + if (fseek(fin,0,SEEK_END) != 0) + return 0; + + + uSizeFile = ftell( fin ); + + if (uMaxBack>uSizeFile) + uMaxBack = uSizeFile; + + buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4); + if (buf==NULL) + return 0; + + uBackRead = 4; + while (uBackReaduMaxBack) + uBackRead = uMaxBack; + else + uBackRead+=BUFREADCOMMENT; + uReadPos = uSizeFile-uBackRead ; + + uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? + (BUFREADCOMMENT+4) : (uSizeFile-uReadPos); + if (fseek(fin,uReadPos,SEEK_SET)!=0) + break; + + if (fread(buf,(uInt)uReadSize,1,fin)!=1) + break; + + for (i=(int)uReadSize-3; (i--)>0;) + if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && + ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06)) + { + uPosFound = uReadPos+i; + break; + } + + if (uPosFound!=0) + break; + } + TRYFREE(buf); + return uPosFound; +} + +/* + Open a Zip file. path contain the full pathname (by example, + on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer + "zlib/zlib109.zip". + If the zipfile cannot be opened (file don't exist or in not valid), the + return value is NULL. + Else, the return value is a unzFile Handle, usable with other function + of this unzip package. +*/ +extern unzFile ZEXPORT unzOpen (path) + const char *path; +{ + unz_s us; + unz_s *s; + uLong central_pos,uL; + FILE * fin ; + + uLong number_disk; /* number of the current dist, used for + spaning ZIP, unsupported, always 0*/ + uLong number_disk_with_CD; /* number the the disk with central dir, used + for spaning ZIP, unsupported, always 0*/ + uLong number_entry_CD; /* total number of entries in + the central dir + (same than number_entry on nospan) */ + + int err=UNZ_OK; + + if (unz_copyright[0]!=' ') + return NULL; + + fin=fopen(path,"rb"); + if (fin==NULL) + return NULL; + + central_pos = unzlocal_SearchCentralDir(fin); + if (central_pos==0) + err=UNZ_ERRNO; + + if (fseek(fin,central_pos,SEEK_SET)!=0) + err=UNZ_ERRNO; + + /* the signature, already checked */ + if (unzlocal_getLong(fin,&uL)!=UNZ_OK) + err=UNZ_ERRNO; + + /* number of this disk */ + if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK) + err=UNZ_ERRNO; + + /* number of the disk with the start of the central directory */ + if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK) + err=UNZ_ERRNO; + + /* total number of entries in the central dir on this disk */ + if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK) + err=UNZ_ERRNO; + + /* total number of entries in the central dir */ + if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK) + err=UNZ_ERRNO; + + if ((number_entry_CD!=us.gi.number_entry) || + (number_disk_with_CD!=0) || + (number_disk!=0)) + err=UNZ_BADZIPFILE; + + /* size of the central directory */ + if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK) + err=UNZ_ERRNO; + + /* offset of start of central directory with respect to the + starting disk number */ + if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK) + err=UNZ_ERRNO; + + /* zipfile comment length */ + if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK) + err=UNZ_ERRNO; + + if ((central_pospfile_in_zip_read!=NULL) + unzCloseCurrentFile(file); + + fclose(s->file); + TRYFREE(s); + return UNZ_OK; +} + + +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. */ +extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info) + unzFile file; + unz_global_info *pglobal_info; +{ + unz_s* s; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + *pglobal_info=s->gi; + return UNZ_OK; +} + + +/* + Translate date/time from Dos format to tm_unz (readable more easilty) +*/ +local void unzlocal_DosDateToTmuDate (ulDosDate, ptm) + uLong ulDosDate; + tm_unz* ptm; +{ + uLong uDate; + uDate = (uLong)(ulDosDate>>16); + ptm->tm_mday = (uInt)(uDate&0x1f) ; + ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ; + ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ; + + ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800); + ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ; + ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ; +} + +/* + Get Info about the current file in the zipfile, with internal only info +*/ +local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file, + unz_file_info *pfile_info, + unz_file_info_internal + *pfile_info_internal, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize)); + +local int unzlocal_GetCurrentFileInfoInternal (file, + pfile_info, + pfile_info_internal, + szFileName, fileNameBufferSize, + extraField, extraFieldBufferSize, + szComment, commentBufferSize) + unzFile file; + unz_file_info *pfile_info; + unz_file_info_internal *pfile_info_internal; + char *szFileName; + uLong fileNameBufferSize; + void *extraField; + uLong extraFieldBufferSize; + char *szComment; + uLong commentBufferSize; +{ + unz_s* s; + unz_file_info file_info; + unz_file_info_internal file_info_internal; + int err=UNZ_OK; + uLong uMagic; + long lSeek=0; + + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + if (fseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0) + err=UNZ_ERRNO; + + + /* we check the magic */ + if (err==UNZ_OK) + { + if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK) + err=UNZ_ERRNO; + else if (uMagic!=0x02014b50) + err=UNZ_BADZIPFILE; + } + + if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK) + err=UNZ_ERRNO; + + unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date); + + if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK) + err=UNZ_ERRNO; + + lSeek+=file_info.size_filename; + if ((err==UNZ_OK) && (szFileName!=NULL)) + { + uLong uSizeRead ; + if (file_info.size_filename0) && (fileNameBufferSize>0)) + if (fread(szFileName,(uInt)uSizeRead,1,s->file)!=1) + err=UNZ_ERRNO; + lSeek -= uSizeRead; + } + + if ((err==UNZ_OK) && (extraField!=NULL)) + { + uLong uSizeRead ; + if (file_info.size_file_extrafile,lSeek,SEEK_CUR)==0) + lSeek=0; + else + err=UNZ_ERRNO; + } + + if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) + if (fread(extraField,(uInt)uSizeRead,1,s->file)!=1) + err=UNZ_ERRNO; + lSeek += file_info.size_file_extra - uSizeRead; + } + else + lSeek+=file_info.size_file_extra; + + if ((err==UNZ_OK) && (szComment!=NULL)) + { + uLong uSizeRead ; + if (file_info.size_file_commentfile,lSeek,SEEK_CUR)==0) + lSeek=0; + else + err=UNZ_ERRNO; + } + + if ((file_info.size_file_comment>0) && (commentBufferSize>0)) + if (fread(szComment,(uInt)uSizeRead,1,s->file)!=1) + err=UNZ_ERRNO; + lSeek+=file_info.size_file_comment - uSizeRead; + } + else + lSeek+=file_info.size_file_comment; + + if ((err==UNZ_OK) && (pfile_info!=NULL)) + *pfile_info=file_info; + + if ((err==UNZ_OK) && (pfile_info_internal!=NULL)) + *pfile_info_internal=file_info_internal; + + return err; +} + + + +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. +*/ +extern int ZEXPORT unzGetCurrentFileInfo (file, + pfile_info, + szFileName, fileNameBufferSize, + extraField, extraFieldBufferSize, + szComment, commentBufferSize) + unzFile file; + unz_file_info *pfile_info; + char *szFileName; + uLong fileNameBufferSize; + void *extraField; + uLong extraFieldBufferSize; + char *szComment; + uLong commentBufferSize; +{ + return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL, + szFileName,fileNameBufferSize, + extraField,extraFieldBufferSize, + szComment,commentBufferSize); +} + +/* + Set the current file of the zipfile to the first file. + return UNZ_OK if there is no problem +*/ +extern int ZEXPORT unzGoToFirstFile (file) + unzFile file; +{ + int err=UNZ_OK; + unz_s* s; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + s->pos_in_central_dir=s->offset_central_dir; + s->num_file=0; + err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, + &s->cur_file_info_internal, + NULL,0,NULL,0,NULL,0); + s->current_file_ok = (err == UNZ_OK); + return err; +} + + +/* + Set the current file of the zipfile to the next file. + return UNZ_OK if there is no problem + return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. +*/ +extern int ZEXPORT unzGoToNextFile (file) + unzFile file; +{ + unz_s* s; + int err; + + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + if (!s->current_file_ok) + return UNZ_END_OF_LIST_OF_FILE; + if (s->num_file+1==s->gi.number_entry) + return UNZ_END_OF_LIST_OF_FILE; + + s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename + + s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ; + s->num_file++; + err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info, + &s->cur_file_info_internal, + NULL,0,NULL,0,NULL,0); + s->current_file_ok = (err == UNZ_OK); + return err; +} + + +/* + Try locate the file szFileName in the zipfile. + For the iCaseSensitivity signification, see unzipStringFileNameCompare + + return value : + UNZ_OK if the file is found. It becomes the current file. + UNZ_END_OF_LIST_OF_FILE if the file is not found +*/ +extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity) + unzFile file; + const char *szFileName; + int iCaseSensitivity; +{ + unz_s* s; + int err; + + uLong num_fileSaved; + uLong pos_in_central_dirSaved; + + if (file==NULL) + return UNZ_PARAMERROR; + + if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP) + return UNZ_PARAMERROR; + + s=(unz_s*)file; + if (!s->current_file_ok) + return UNZ_END_OF_LIST_OF_FILE; + + num_fileSaved = s->num_file; + pos_in_central_dirSaved = s->pos_in_central_dir; + + err = unzGoToFirstFile(file); + + while (err == UNZ_OK) + { + char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1]; + unzGetCurrentFileInfo(file,NULL, + szCurrentFileName,sizeof(szCurrentFileName)-1, + NULL,0,NULL,0); + if (unzStringFileNameCompare(szCurrentFileName, + szFileName,iCaseSensitivity)==0) + return UNZ_OK; + err = unzGoToNextFile(file); + } + + s->num_file = num_fileSaved ; + s->pos_in_central_dir = pos_in_central_dirSaved ; + return err; +} + + +/* + Read the local header of the current zipfile + Check the coherency of the local header and info in the end of central + directory about this file + store in *piSizeVar the size of extra info in local header + (filename and size of extra field data) +*/ +local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar, + poffset_local_extrafield, + psize_local_extrafield) + unz_s* s; + uInt* piSizeVar; + uLong *poffset_local_extrafield; + uInt *psize_local_extrafield; +{ + uLong uMagic,uData,uFlags; + uLong size_filename; + uLong size_extra_field; + int err=UNZ_OK; + + *piSizeVar = 0; + *poffset_local_extrafield = 0; + *psize_local_extrafield = 0; + + if (fseek(s->file,s->cur_file_info_internal.offset_curfile + + s->byte_before_the_zipfile,SEEK_SET)!=0) + return UNZ_ERRNO; + + + if (err==UNZ_OK) + { + if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK) + err=UNZ_ERRNO; + else if (uMagic!=0x04034b50) + err=UNZ_BADZIPFILE; + } + + if (unzlocal_getShort(s->file,&uData) != UNZ_OK) + err=UNZ_ERRNO; +/* + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion)) + err=UNZ_BADZIPFILE; +*/ + if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK) + err=UNZ_ERRNO; + + if (unzlocal_getShort(s->file,&uData) != UNZ_OK) + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method)) + err=UNZ_BADZIPFILE; + + if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) && + (s->cur_file_info.compression_method!=Z_DEFLATED)) + err=UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */ + err=UNZ_ERRNO; + + if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */ + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && + ((uFlags & 8)==0)) + err=UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */ + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && + ((uFlags & 8)==0)) + err=UNZ_BADZIPFILE; + + if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */ + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && + ((uFlags & 8)==0)) + err=UNZ_BADZIPFILE; + + + if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK) + err=UNZ_ERRNO; + else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename)) + err=UNZ_BADZIPFILE; + + *piSizeVar += (uInt)size_filename; + + if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK) + err=UNZ_ERRNO; + *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile + + SIZEZIPLOCALHEADER + size_filename; + *psize_local_extrafield = (uInt)size_extra_field; + + *piSizeVar += (uInt)size_extra_field; + + return err; +} + +/* + Open for reading data the current file in the zipfile. + If there is no error and the file is opened, the return value is UNZ_OK. +*/ +extern int ZEXPORT unzOpenCurrentFile (file) + unzFile file; +{ + int err=UNZ_OK; + int Store; + uInt iSizeVar; + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + uLong offset_local_extrafield; /* offset of the local extra field */ + uInt size_local_extrafield; /* size of the local extra field */ + + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + if (!s->current_file_ok) + return UNZ_PARAMERROR; + + if (s->pfile_in_zip_read != NULL) + unzCloseCurrentFile(file); + + if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar, + &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK) + return UNZ_BADZIPFILE; + + pfile_in_zip_read_info = (file_in_zip_read_info_s*) + ALLOC(sizeof(file_in_zip_read_info_s)); + if (pfile_in_zip_read_info==NULL) + return UNZ_INTERNALERROR; + + pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE); + pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield; + pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield; + pfile_in_zip_read_info->pos_local_extrafield=0; + + if (pfile_in_zip_read_info->read_buffer==NULL) + { + TRYFREE(pfile_in_zip_read_info); + return UNZ_INTERNALERROR; + } + + pfile_in_zip_read_info->stream_initialised=0; + + if ((s->cur_file_info.compression_method!=0) && + (s->cur_file_info.compression_method!=Z_DEFLATED)) + err=UNZ_BADZIPFILE; + Store = s->cur_file_info.compression_method==0; + + pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc; + pfile_in_zip_read_info->crc32=0; + pfile_in_zip_read_info->compression_method = + s->cur_file_info.compression_method; + pfile_in_zip_read_info->file=s->file; + pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile; + + pfile_in_zip_read_info->stream.total_out = 0; + + if (!Store) + { + pfile_in_zip_read_info->stream.zalloc = (alloc_func)0; + pfile_in_zip_read_info->stream.zfree = (free_func)0; + pfile_in_zip_read_info->stream.opaque = (voidpf)0; + + err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS); + if (err == Z_OK) + pfile_in_zip_read_info->stream_initialised=1; + /* windowBits is passed < 0 to tell that there is no zlib header. + * Note that in this case inflate *requires* an extra "dummy" byte + * after the compressed stream in order to complete decompression and + * return Z_STREAM_END. + * In unzip, i don't wait absolutely Z_STREAM_END because I known the + * size of both compressed and uncompressed data + */ + } + pfile_in_zip_read_info->rest_read_compressed = + s->cur_file_info.compressed_size ; + pfile_in_zip_read_info->rest_read_uncompressed = + s->cur_file_info.uncompressed_size ; + + pfile_in_zip_read_info->pos_in_zipfile = + s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + + iSizeVar; + + pfile_in_zip_read_info->stream.avail_in = (uInt)0; + + + s->pfile_in_zip_read = pfile_in_zip_read_info; + return UNZ_OK; +} + + +/* + Read bytes from the current file. + buf contain buffer where data must be copied + len the size of buf. + + return the number of byte copied if somes bytes are copied + return 0 if the end of file was reached + return <0 with error code if there is an error + (UNZ_ERRNO for IO error, or zLib error for uncompress error) +*/ +extern int ZEXPORT unzReadCurrentFile (file, buf, len) + unzFile file; + voidp buf; + unsigned len; +{ + int err=UNZ_OK; + uInt iRead = 0; + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + if ((pfile_in_zip_read_info->read_buffer == NULL)) + return UNZ_END_OF_LIST_OF_FILE; + if (len==0) + return 0; + + pfile_in_zip_read_info->stream.next_out = (Bytef*)buf; + + pfile_in_zip_read_info->stream.avail_out = (uInt)len; + + if (len>pfile_in_zip_read_info->rest_read_uncompressed) + pfile_in_zip_read_info->stream.avail_out = + (uInt)pfile_in_zip_read_info->rest_read_uncompressed; + + while (pfile_in_zip_read_info->stream.avail_out>0) + { + if ((pfile_in_zip_read_info->stream.avail_in==0) && + (pfile_in_zip_read_info->rest_read_compressed>0)) + { + uInt uReadThis = UNZ_BUFSIZE; + if (pfile_in_zip_read_info->rest_read_compressedrest_read_compressed; + if (uReadThis == 0) + return UNZ_EOF; + if (fseek(pfile_in_zip_read_info->file, + pfile_in_zip_read_info->pos_in_zipfile + + pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0) + return UNZ_ERRNO; + if (fread(pfile_in_zip_read_info->read_buffer,uReadThis,1, + pfile_in_zip_read_info->file)!=1) + return UNZ_ERRNO; + pfile_in_zip_read_info->pos_in_zipfile += uReadThis; + + pfile_in_zip_read_info->rest_read_compressed-=uReadThis; + + pfile_in_zip_read_info->stream.next_in = + (Bytef*)pfile_in_zip_read_info->read_buffer; + pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis; + } + + if (pfile_in_zip_read_info->compression_method==0) + { + uInt uDoCopy,i ; + if (pfile_in_zip_read_info->stream.avail_out < + pfile_in_zip_read_info->stream.avail_in) + uDoCopy = pfile_in_zip_read_info->stream.avail_out ; + else + uDoCopy = pfile_in_zip_read_info->stream.avail_in ; + + for (i=0;istream.next_out+i) = + *(pfile_in_zip_read_info->stream.next_in+i); + + pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, + pfile_in_zip_read_info->stream.next_out, + uDoCopy); + pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy; + pfile_in_zip_read_info->stream.avail_in -= uDoCopy; + pfile_in_zip_read_info->stream.avail_out -= uDoCopy; + pfile_in_zip_read_info->stream.next_out += uDoCopy; + pfile_in_zip_read_info->stream.next_in += uDoCopy; + pfile_in_zip_read_info->stream.total_out += uDoCopy; + iRead += uDoCopy; + } + else + { + uLong uTotalOutBefore,uTotalOutAfter; + const Bytef *bufBefore; + uLong uOutThis; + int flush=Z_SYNC_FLUSH; + + uTotalOutBefore = pfile_in_zip_read_info->stream.total_out; + bufBefore = pfile_in_zip_read_info->stream.next_out; + + /* + if ((pfile_in_zip_read_info->rest_read_uncompressed == + pfile_in_zip_read_info->stream.avail_out) && + (pfile_in_zip_read_info->rest_read_compressed == 0)) + flush = Z_FINISH; + */ + err=inflate(&pfile_in_zip_read_info->stream,flush); + + uTotalOutAfter = pfile_in_zip_read_info->stream.total_out; + uOutThis = uTotalOutAfter-uTotalOutBefore; + + pfile_in_zip_read_info->crc32 = + crc32(pfile_in_zip_read_info->crc32,bufBefore, + (uInt)(uOutThis)); + + pfile_in_zip_read_info->rest_read_uncompressed -= + uOutThis; + + iRead += (uInt)(uTotalOutAfter - uTotalOutBefore); + + if (err==Z_STREAM_END) + return (iRead==0) ? UNZ_EOF : iRead; + if (err!=Z_OK) + break; + } + } + + if (err==Z_OK) + return iRead; + return err; +} + + +/* + Give the current position in uncompressed data +*/ +extern z_off_t ZEXPORT unztell (file) + unzFile file; +{ + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + return (z_off_t)pfile_in_zip_read_info->stream.total_out; +} + + +/* + return 1 if the end of file was reached, 0 elsewhere +*/ +extern int ZEXPORT unzeof (file) + unzFile file; +{ + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + if (pfile_in_zip_read_info->rest_read_uncompressed == 0) + return 1; + else + return 0; +} + +/* + Read extra field from the current file (opened by unzOpenCurrentFile) + This is the local-header version of the extra field (sometimes, there is + more info in the local-header version than in the central-header) + + if buf==NULL, it return the size of the local extra field that can be read + + if buf!=NULL, len is the size of the buffer, the extra header is copied in + buf. + the return value is the number of bytes copied in buf, or (if <0) + the error code +*/ +extern int ZEXPORT unzGetLocalExtrafield (file,buf,len) + unzFile file; + voidp buf; + unsigned len; +{ + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + uInt read_now; + uLong size_to_read; + + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + size_to_read = (pfile_in_zip_read_info->size_local_extrafield - + pfile_in_zip_read_info->pos_local_extrafield); + + if (buf==NULL) + return (int)size_to_read; + + if (len>size_to_read) + read_now = (uInt)size_to_read; + else + read_now = (uInt)len ; + + if (read_now==0) + return 0; + + if (fseek(pfile_in_zip_read_info->file, + pfile_in_zip_read_info->offset_local_extrafield + + pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0) + return UNZ_ERRNO; + + if (fread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1) + return UNZ_ERRNO; + + return (int)read_now; +} + +/* + Close the file in zip opened with unzipOpenCurrentFile + Return UNZ_CRCERROR if all the file was read but the CRC is not good +*/ +extern int ZEXPORT unzCloseCurrentFile (file) + unzFile file; +{ + int err=UNZ_OK; + + unz_s* s; + file_in_zip_read_info_s* pfile_in_zip_read_info; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + pfile_in_zip_read_info=s->pfile_in_zip_read; + + if (pfile_in_zip_read_info==NULL) + return UNZ_PARAMERROR; + + + if (pfile_in_zip_read_info->rest_read_uncompressed == 0) + { + if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait) + err=UNZ_CRCERROR; + } + + + TRYFREE(pfile_in_zip_read_info->read_buffer); + pfile_in_zip_read_info->read_buffer = NULL; + if (pfile_in_zip_read_info->stream_initialised) + inflateEnd(&pfile_in_zip_read_info->stream); + + pfile_in_zip_read_info->stream_initialised = 0; + TRYFREE(pfile_in_zip_read_info); + + s->pfile_in_zip_read=NULL; + + return err; +} + + +/* + Get the global comment string of the ZipFile, in the szComment buffer. + uSizeBuf is the size of the szComment buffer. + return the number of byte copied or an error code <0 +*/ +extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf) + unzFile file; + char *szComment; + uLong uSizeBuf; +{ +/* int err=UNZ_OK; */ + unz_s* s; + uLong uReadThis ; + if (file==NULL) + return UNZ_PARAMERROR; + s=(unz_s*)file; + + uReadThis = uSizeBuf; + if (uReadThis>s->gi.size_comment) + uReadThis = s->gi.size_comment; + + if (fseek(s->file,s->central_pos+22,SEEK_SET)!=0) + return UNZ_ERRNO; + + if (uReadThis>0) + { + *szComment='\0'; + if (fread(szComment,(uInt)uReadThis,1,s->file)!=1) + return UNZ_ERRNO; + } + + if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment)) + *(szComment+s->gi.size_comment)='\0'; + return (int)uReadThis; +} diff --git a/gcw0/unzip.h b/gcw0/unzip.h new file mode 100644 index 0000000..2c4e784 --- /dev/null +++ b/gcw0/unzip.h @@ -0,0 +1,273 @@ +/* unzip.h -- IO for uncompress .zip files using zlib + Version 0.15 beta, Mar 19th, 1998, + + Copyright (C) 1998 Gilles Vollant + + This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g + WinZip, InfoZip tools and compatible. + Encryption and multi volume ZipFile (span) are not supported. + Old compressions used by old PKZip 1.x are not supported + + THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE + CAN CHANGE IN FUTURE VERSION !! + I WAIT FEEDBACK at mail info@winimage.com + Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution + + Condition of use and distribution are the same than zlib : + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +/* for more info about .ZIP format, see + ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip + PkWare has also a specification at : + ftp://ftp.pkware.com/probdesc.zip */ + +#ifndef _unz_H +#define _unz_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _ZLIB_H +#include "zlib.h" +#endif + +#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP) +/* like the STRICT of WIN32, we define a pointer that cannot be converted + from (void*) without cast */ +typedef struct TagunzFile__ { int unused; } unzFile__; +typedef unzFile__ *unzFile; +#else +typedef voidp unzFile; +#endif + + +#define UNZ_OK (0) +#define UNZ_END_OF_LIST_OF_FILE (-100) +#define UNZ_ERRNO (Z_ERRNO) +#define UNZ_EOF (0) +#define UNZ_PARAMERROR (-102) +#define UNZ_BADZIPFILE (-103) +#define UNZ_INTERNALERROR (-104) +#define UNZ_CRCERROR (-105) + +/* tm_unz contain date/time info */ +typedef struct tm_unz_s +{ + uInt tm_sec; /* seconds after the minute - [0,59] */ + uInt tm_min; /* minutes after the hour - [0,59] */ + uInt tm_hour; /* hours since midnight - [0,23] */ + uInt tm_mday; /* day of the month - [1,31] */ + uInt tm_mon; /* months since January - [0,11] */ + uInt tm_year; /* years - [1980..2044] */ +} tm_unz; + +/* unz_global_info structure contain global data about the ZIPfile + These data comes from the end of central dir */ +typedef struct unz_global_info_s +{ + uLong number_entry; /* total number of entries in + the central dir on this disk */ + uLong size_comment; /* size of the global comment of the zipfile */ +} unz_global_info; + + +/* unz_file_info contain information about a file in the zipfile */ +typedef struct unz_file_info_s +{ + uLong version; /* version made by 2 bytes */ + uLong version_needed; /* version needed to extract 2 bytes */ + uLong flag; /* general purpose bit flag 2 bytes */ + uLong compression_method; /* compression method 2 bytes */ + uLong dosDate; /* last mod file date in Dos fmt 4 bytes */ + uLong crc; /* crc-32 4 bytes */ + uLong compressed_size; /* compressed size 4 bytes */ + uLong uncompressed_size; /* uncompressed size 4 bytes */ + uLong size_filename; /* filename length 2 bytes */ + uLong size_file_extra; /* extra field length 2 bytes */ + uLong size_file_comment; /* file comment length 2 bytes */ + + uLong disk_num_start; /* disk number start 2 bytes */ + uLong internal_fa; /* internal file attributes 2 bytes */ + uLong external_fa; /* external file attributes 4 bytes */ + + tm_unz tmu_date; +} unz_file_info; + +extern int ZEXPORT unzStringFileNameCompare OF ((const char* fileName1, + const char* fileName2, + int iCaseSensitivity)); +/* + Compare two filename (fileName1,fileName2). + If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp) + If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi + or strcasecmp) + If iCaseSenisivity = 0, case sensitivity is defaut of your operating system + (like 1 on Unix, 2 on Windows) +*/ + + +extern unzFile ZEXPORT unzOpen OF((const char *path)); +/* + Open a Zip file. path contain the full pathname (by example, + on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer + "zlib/zlib111.zip". + If the zipfile cannot be opened (file don't exist or in not valid), the + return value is NULL. + Else, the return value is a unzFile Handle, usable with other function + of this unzip package. +*/ + +extern int ZEXPORT unzClose OF((unzFile file)); +/* + Close a ZipFile opened with unzipOpen. + If there is files inside the .Zip opened with unzOpenCurrentFile (see later), + these files MUST be closed with unzipCloseCurrentFile before call unzipClose. + return UNZ_OK if there is no problem. */ + +extern int ZEXPORT unzGetGlobalInfo OF((unzFile file, + unz_global_info *pglobal_info)); +/* + Write info about the ZipFile in the *pglobal_info structure. + No preparation of the structure is needed + return UNZ_OK if there is no problem. */ + + +extern int ZEXPORT unzGetGlobalComment OF((unzFile file, + char *szComment, + uLong uSizeBuf)); +/* + Get the global comment string of the ZipFile, in the szComment buffer. + uSizeBuf is the size of the szComment buffer. + return the number of byte copied or an error code <0 +*/ + + +/***************************************************************************/ +/* Unzip package allow you browse the directory of the zipfile */ + +extern int ZEXPORT unzGoToFirstFile OF((unzFile file)); +/* + Set the current file of the zipfile to the first file. + return UNZ_OK if there is no problem +*/ + +extern int ZEXPORT unzGoToNextFile OF((unzFile file)); +/* + Set the current file of the zipfile to the next file. + return UNZ_OK if there is no problem + return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest. +*/ + +extern int ZEXPORT unzLocateFile OF((unzFile file, + const char *szFileName, + int iCaseSensitivity)); +/* + Try locate the file szFileName in the zipfile. + For the iCaseSensitivity signification, see unzStringFileNameCompare + + return value : + UNZ_OK if the file is found. It becomes the current file. + UNZ_END_OF_LIST_OF_FILE if the file is not found +*/ + + +extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file, + unz_file_info *pfile_info, + char *szFileName, + uLong fileNameBufferSize, + void *extraField, + uLong extraFieldBufferSize, + char *szComment, + uLong commentBufferSize)); +/* + Get Info about the current file + if pfile_info!=NULL, the *pfile_info structure will contain somes info about + the current file + if szFileName!=NULL, the filemane string will be copied in szFileName + (fileNameBufferSize is the size of the buffer) + if extraField!=NULL, the extra field information will be copied in extraField + (extraFieldBufferSize is the size of the buffer). + This is the Central-header version of the extra field + if szComment!=NULL, the comment string of the file will be copied in szComment + (commentBufferSize is the size of the buffer) +*/ + +/***************************************************************************/ +/* for reading the content of the current zipfile, you can open it, read data + from it, and close it (you can close it before reading all the file) + */ + +extern int ZEXPORT unzOpenCurrentFile OF((unzFile file)); +/* + Open for reading data the current file in the zipfile. + If there is no error, the return value is UNZ_OK. +*/ + +extern int ZEXPORT unzCloseCurrentFile OF((unzFile file)); +/* + Close the file in zip opened with unzOpenCurrentFile + Return UNZ_CRCERROR if all the file was read but the CRC is not good +*/ + +extern int ZEXPORT unzReadCurrentFile OF((unzFile file, + voidp buf, + unsigned len)); +/* + Read bytes from the current file (opened by unzOpenCurrentFile) + buf contain buffer where data must be copied + len the size of buf. + + return the number of byte copied if somes bytes are copied + return 0 if the end of file was reached + return <0 with error code if there is an error + (UNZ_ERRNO for IO error, or zLib error for uncompress error) +*/ + +extern z_off_t ZEXPORT unztell OF((unzFile file)); +/* + Give the current position in uncompressed data +*/ + +extern int ZEXPORT unzeof OF((unzFile file)); +/* + return 1 if the end of file was reached, 0 elsewhere +*/ + +extern int ZEXPORT unzGetLocalExtrafield OF((unzFile file, + voidp buf, + unsigned len)); +/* + Read extra field from the current file (opened by unzOpenCurrentFile) + This is the local-header version of the extra field (sometimes, there is + more info in the local-header version than in the central-header) + + if buf==NULL, it return the size of the local extra field + + if buf!=NULL, len is the size of the buffer, the extra header is copied in + buf. + the return value is the number of bytes copied in buf, or (if <0) + the error code +*/ + +#ifdef __cplusplus +} +#endif + +#endif /* _unz_H */ diff --git a/gcw0/utils.c b/gcw0/utils.c new file mode 100644 index 0000000..67ac55a --- /dev/null +++ b/gcw0/utils.c @@ -0,0 +1,170 @@ +#include +#include + +#include "utils.h" +#include "osd.h" + +void create_default_directories(void) { + const char *homedir; + if ((homedir = getenv("HOME")) == NULL) { + homedir = getpwuid(getuid())->pw_dir; + } + + char pathname[MAXPATHLEN]; + + /* base directory */ + sprintf (pathname, "%s%s", homedir, DEFAULT_PATH); + DIR *dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + + /* default SRAM & Savestate files directories */ + sprintf (pathname, "%s%s/saves", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/saves/md", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/saves/ms", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/saves/gg", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/saves/sg", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/saves/cd", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + + /* default Snapshot files directories */ + sprintf (pathname, "%s%s/snaps", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/snaps/md", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/snaps/ms", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/snaps/gg", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/snaps/sg", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/snaps/cd", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + + /* default Cheat files directories */ + sprintf (pathname, "%s%s/cheats", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/cheats/md", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/cheats/ms", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/cheats/gg", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/cheats/sg", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + sprintf (pathname, "%s%s/cheats/cd", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + + /* default BIOS ROM files directories */ + sprintf (pathname, "%s%s/bios", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + + /* default LOCK-ON ROM files directories */ + sprintf (pathname, "%s%s/lock-on", homedir, DEFAULT_PATH); + dir = opendir(pathname); + if (dir) closedir(dir); + else mkdir(pathname,S_IRWXU); + +} + +char* get_save_directory(void) { + const char *homedir; + const char *system_dir; + if ((homedir = getenv("HOME")) == NULL) { + homedir = getpwuid(getuid())->pw_dir; + } + + char pathname[MAXPATHLEN]; + + if(system_hw <= SYSTEM_MARKIII){ + system_dir = "/saves/sg"; + } else if (system_hw > SYSTEM_MARKIII && system_hw <= SYSTEM_SMS2) { + system_dir = "/saves/ms"; + } else if (system_hw > SYSTEM_SMS2 && system_hw <= SYSTEM_GGMS) { + system_dir = "/saves/gg"; + } else if (system_hw == SYSTEM_MD) { + system_dir = "/saves/md"; + } else if (system_hw == SYSTEM_MCD) { + system_dir = "/saves/cd"; + } else { + system_dir = "/saves/"; + } + + sprintf (pathname, "%s%s%s", homedir, DEFAULT_PATH, system_dir); + + return pathname; + +} + +char* gcw0_get_key_name(int keycode) +{ + if (keycode == SDLK_UP) return "Up"; + else if (keycode == SDLK_DOWN) return "Down"; + else if (keycode == SDLK_LEFT) return "Left"; + else if (keycode == SDLK_RIGHT) return "Right"; + else if (keycode == SDLK_LCTRL) return "A"; + else if (keycode == SDLK_LALT) return "B"; + else if (keycode == SDLK_LSHIFT) return "X"; + else if (keycode == SDLK_SPACE) return "Y"; + else if (keycode == SDLK_TAB) return "L"; + else if (keycode == SDLK_BACKSPACE) return "R"; + else if (keycode == SDLK_RETURN) return "Start"; + else if (keycode == SDLK_ESCAPE) return "Select"; + else return "..."; +} + +char *get_file_name(char *full_path) { + char file_name[256]; + sprintf(file_name, "%s", basename(full_path)); + + /* remove file extension */ + int i = strlen(file_name) - 1; + while ((i > 0) && (file_name[i] != '.')) i--; + if (i > 0) file_name[i] = 0; + + return file_name; +} + diff --git a/gcw0/utils.h b/gcw0/utils.h new file mode 100644 index 0000000..e6d47ec --- /dev/null +++ b/gcw0/utils.h @@ -0,0 +1,10 @@ +#ifndef _UTILS_H_ +#define _UTILS_H_ + +/* Function prototypes */ +void create_default_directories(void); +char* get_save_directory(void); +char* gcw0_get_key_name(int keycode); +char *get_file_name(char *full_path); +#endif /* _UTILS_H_ */ +