mirror of https://github.com/bsnes-emu/bsnes.git
Merge branch 'sdl2'
This commit is contained in:
commit
096bd8ba94
|
@ -1,5 +1,4 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
26
Core/gb.c
26
Core/gb.c
|
@ -2,12 +2,13 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <sys/time.h>
|
#ifndef _WIN32
|
||||||
|
#include <unistd.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
|
#endif
|
||||||
#include "gb.h"
|
#include "gb.h"
|
||||||
|
|
||||||
void GB_attributed_logv(GB_gameboy_t *gb, GB_log_attributes attributes, const char *fmt, va_list args)
|
void GB_attributed_logv(GB_gameboy_t *gb, GB_log_attributes attributes, const char *fmt, va_list args)
|
||||||
|
@ -34,7 +35,7 @@ void GB_attributed_log(GB_gameboy_t *gb, GB_log_attributes attributes, const cha
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GB_log(GB_gameboy_t *gb,const char *fmt, ...)
|
void GB_log(GB_gameboy_t *gb, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
|
@ -150,7 +151,10 @@ void GB_free(GB_gameboy_t *gb)
|
||||||
int GB_load_boot_rom(GB_gameboy_t *gb, const char *path)
|
int GB_load_boot_rom(GB_gameboy_t *gb, const char *path)
|
||||||
{
|
{
|
||||||
FILE *f = fopen(path, "rb");
|
FILE *f = fopen(path, "rb");
|
||||||
if (!f) return errno;
|
if (!f) {
|
||||||
|
GB_log(gb, "Could not open boot ROM: %s.\n", strerror(errno));
|
||||||
|
return errno;
|
||||||
|
}
|
||||||
fread(gb->boot_rom, sizeof(gb->boot_rom), 1, f);
|
fread(gb->boot_rom, sizeof(gb->boot_rom), 1, f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -159,7 +163,10 @@ int GB_load_boot_rom(GB_gameboy_t *gb, const char *path)
|
||||||
int GB_load_rom(GB_gameboy_t *gb, const char *path)
|
int GB_load_rom(GB_gameboy_t *gb, const char *path)
|
||||||
{
|
{
|
||||||
FILE *f = fopen(path, "rb");
|
FILE *f = fopen(path, "rb");
|
||||||
if (!f) return errno;
|
if (!f) {
|
||||||
|
GB_log(gb, "Could not open ROM: %s.\n", strerror(errno));
|
||||||
|
return errno;
|
||||||
|
}
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
gb->rom_size = (ftell(f) + 0x3FFF) & ~0x3FFF; /* Round to bank */
|
gb->rom_size = (ftell(f) + 0x3FFF) & ~0x3FFF; /* Round to bank */
|
||||||
/* And then round to a power of two */
|
/* And then round to a power of two */
|
||||||
|
@ -201,6 +208,7 @@ int GB_save_state(GB_gameboy_t *gb, const char *path)
|
||||||
{
|
{
|
||||||
FILE *f = fopen(path, "wb");
|
FILE *f = fopen(path, "wb");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
|
GB_log(gb, "Could not open save state: %s.\n", strerror(errno));
|
||||||
return errno;
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,6 +276,7 @@ int GB_load_state(GB_gameboy_t *gb, const char *path)
|
||||||
|
|
||||||
FILE *f = fopen(path, "rb");
|
FILE *f = fopen(path, "rb");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
|
GB_log(gb, "Could not open save state: %s.\n", strerror(errno));
|
||||||
return errno;
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -344,6 +353,7 @@ int GB_save_battery(GB_gameboy_t *gb, const char *path)
|
||||||
if (gb->mbc_ram_size == 0 && !gb->cartridge_type->has_rtc) return 0; /* Claims to have battery, but has no RAM or RTC */
|
if (gb->mbc_ram_size == 0 && !gb->cartridge_type->has_rtc) return 0; /* Claims to have battery, but has no RAM or RTC */
|
||||||
FILE *f = fopen(path, "wb");
|
FILE *f = fopen(path, "wb");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
|
GB_log(gb, "Could not open save state: %s.\n", strerror(errno));
|
||||||
return errno;
|
return errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -651,12 +661,14 @@ void GB_switch_model_and_reset(GB_gameboy_t *gb, bool is_cgb)
|
||||||
void *GB_get_direct_access(GB_gameboy_t *gb, GB_direct_access_t access, size_t *size, uint16_t *bank)
|
void *GB_get_direct_access(GB_gameboy_t *gb, GB_direct_access_t access, size_t *size, uint16_t *bank)
|
||||||
{
|
{
|
||||||
/* Set size and bank to dummy pointers if not set */
|
/* Set size and bank to dummy pointers if not set */
|
||||||
|
size_t dummy_size;
|
||||||
|
uint16_t dummy_bank;
|
||||||
if (!size) {
|
if (!size) {
|
||||||
size = alloca(sizeof(size));
|
size = &dummy_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!bank) {
|
if (!bank) {
|
||||||
bank = alloca(sizeof(bank));
|
bank = &dummy_bank;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
#include "gb.h"
|
#include "gb.h"
|
||||||
#ifdef _WIN32
|
|
||||||
typedef intptr_t ssize_t;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static size_t GB_map_find_symbol_index(GB_symbol_map_t *map, uint16_t addr)
|
static size_t GB_map_find_symbol_index(GB_symbol_map_t *map, uint16_t addr)
|
||||||
{
|
{
|
||||||
|
|
41
Makefile
41
Makefile
|
@ -43,10 +43,10 @@ endif
|
||||||
# Set compilation and linkage flags based on target, platform and configuration
|
# Set compilation and linkage flags based on target, platform and configuration
|
||||||
|
|
||||||
CFLAGS += -Werror -Wall -std=gnu11 -ICore -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES
|
CFLAGS += -Werror -Wall -std=gnu11 -ICore -D_GNU_SOURCE -DVERSION="$(VERSION)" -I. -D_USE_MATH_DEFINES
|
||||||
SDL_LDFLAGS := -lSDL
|
SDL_LDFLAGS := -lSDL2
|
||||||
ifeq ($(PLATFORM),windows32)
|
ifeq ($(PLATFORM),windows32)
|
||||||
CFLAGS += -IWindows
|
CFLAGS += -IWindows
|
||||||
LDFLAGS += -lmsvcrt -lSDLmain -Wl,/MANIFESTFILE:NUL
|
LDFLAGS += -lmsvcrt -lSDL2main -Wl,/MANIFESTFILE:NUL
|
||||||
else
|
else
|
||||||
LDFLAGS += -lc -lm
|
LDFLAGS += -lc -lm
|
||||||
endif
|
endif
|
||||||
|
@ -56,19 +56,16 @@ SYSROOT := $(shell xcodebuild -sdk macosx -version Path 2> /dev/null)
|
||||||
CFLAGS += -F/Library/Frameworks
|
CFLAGS += -F/Library/Frameworks
|
||||||
OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) -mmacosx-version-min=10.9
|
OCFLAGS += -x objective-c -fobjc-arc -Wno-deprecated-declarations -isysroot $(SYSROOT) -mmacosx-version-min=10.9
|
||||||
LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore
|
LDFLAGS += -framework AppKit -framework PreferencePanes -framework Carbon -framework QuartzCore
|
||||||
SDL_LDFLAGS := -F/Library/Frameworks -framework SDL
|
SDL_LDFLAGS := -F/Library/Frameworks -framework SDL2
|
||||||
endif
|
endif
|
||||||
|
CFLAGS += -Wno-deprecated-declarations
|
||||||
ifeq ($(PLATFORM),windows32)
|
ifeq ($(PLATFORM),windows32)
|
||||||
CFLAGS += -Wno-deprecated-declarations # Seems like Microsoft deprecated every single LIBC function
|
CFLAGS += -Wno-deprecated-declarations # Seems like Microsoft deprecated every single LIBC function
|
||||||
LDFLAGS += -Wl,/NODEFAULTLIB:libcmt
|
LDFLAGS += -Wl,/NODEFAULTLIB:libcmt.lib
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(CONF),debug)
|
ifeq ($(CONF),debug)
|
||||||
CFLAGS += -g
|
CFLAGS += -g
|
||||||
ifeq ($(PLATFORM),windows32)
|
|
||||||
LDFLAGS += -Wl,/debug
|
|
||||||
endif
|
|
||||||
else ifeq ($(CONF), release)
|
else ifeq ($(CONF), release)
|
||||||
CFLAGS += -O3 -DNDEBUG
|
CFLAGS += -O3 -DNDEBUG
|
||||||
ifneq ($(PLATFORM),windows32)
|
ifneq ($(PLATFORM),windows32)
|
||||||
|
@ -82,16 +79,16 @@ endif
|
||||||
# Define our targets
|
# Define our targets
|
||||||
|
|
||||||
ifeq ($(PLATFORM),windows32)
|
ifeq ($(PLATFORM),windows32)
|
||||||
SDL_TARGET := $(BIN)/sdl/sameboy.exe $(BIN)/sdl/sameboy_debugger.exe $(BIN)/sdl/SDL.dll
|
SDL_TARGET := $(BIN)/SDL/sameboy.exe $(BIN)/SDL/sameboy_debugger.exe $(BIN)/SDL/SDL2.dll
|
||||||
TESTER_TARGET := $(BIN)/tester/sameboy_tester.exe
|
TESTER_TARGET := $(BIN)/tester/sameboy_tester.exe
|
||||||
else
|
else
|
||||||
SDL_TARGET := $(BIN)/sdl/sameboy
|
SDL_TARGET := $(BIN)/SDL/sameboy
|
||||||
TESTER_TARGET := $(BIN)/tester/sameboy_tester
|
TESTER_TARGET := $(BIN)/tester/sameboy_tester
|
||||||
endif
|
endif
|
||||||
|
|
||||||
cocoa: $(BIN)/SameBoy.app
|
cocoa: $(BIN)/SameBoy.app
|
||||||
quicklook: $(BIN)/SameBoy.qlgenerator
|
quicklook: $(BIN)/SameBoy.qlgenerator
|
||||||
sdl: $(SDL_TARGET) $(BIN)/sdl/dmg_boot.bin $(BIN)/sdl/cgb_boot.bin $(BIN)/sdl/LICENSE $(BIN)/sdl/registers.sym
|
sdl: $(SDL_TARGET) $(BIN)/SDL/dmg_boot.bin $(BIN)/SDL/cgb_boot.bin $(BIN)/SDL/LICENSE $(BIN)/SDL/registers.sym $(BIN)/SDL/drop.bmp
|
||||||
bootroms: $(BIN)/BootROMs/cgb_boot.bin $(BIN)/BootROMs/dmg_boot.bin
|
bootroms: $(BIN)/BootROMs/cgb_boot.bin $(BIN)/BootROMs/dmg_boot.bin
|
||||||
tester: $(TESTER_TARGET) $(BIN)/tester/dmg_boot.bin $(BIN)/tester/cgb_boot.bin
|
tester: $(TESTER_TARGET) $(BIN)/tester/dmg_boot.bin $(BIN)/tester/cgb_boot.bin
|
||||||
all: cocoa sdl tester
|
all: cocoa sdl tester
|
||||||
|
@ -105,7 +102,6 @@ TESTER_SOURCES := $(shell ls Tester/*.c)
|
||||||
ifeq ($(PLATFORM),Darwin)
|
ifeq ($(PLATFORM),Darwin)
|
||||||
COCOA_SOURCES := $(shell ls Cocoa/*.m) $(shell ls HexFiend/*.m)
|
COCOA_SOURCES := $(shell ls Cocoa/*.m) $(shell ls HexFiend/*.m)
|
||||||
QUICKLOOK_SOURCES := $(shell ls QuickLook/*.m) $(shell ls QuickLook/*.c)
|
QUICKLOOK_SOURCES := $(shell ls QuickLook/*.m) $(shell ls QuickLook/*.c)
|
||||||
SDL_SOURCES += $(shell ls SDL/*.m)
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CORE_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(CORE_SOURCES))
|
CORE_OBJECTS := $(patsubst %,$(OBJ)/%.o,$(CORE_SOURCES))
|
||||||
|
@ -213,7 +209,7 @@ $(BIN)/SameBoy.qlgenerator/Contents/Resources/cgb_boot_fast.bin: $(BIN)/BootROMs
|
||||||
# SDL Port
|
# SDL Port
|
||||||
|
|
||||||
# Unix versions build only one binary
|
# Unix versions build only one binary
|
||||||
$(BIN)/sdl/sameboy: $(CORE_OBJECTS) $(SDL_OBJECTS)
|
$(BIN)/SDL/sameboy: $(CORE_OBJECTS) $(SDL_OBJECTS)
|
||||||
-@$(MKDIR) -p $(dir $@)
|
-@$(MKDIR) -p $(dir $@)
|
||||||
$(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS)
|
$(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS)
|
||||||
ifeq ($(CONF), release)
|
ifeq ($(CONF), release)
|
||||||
|
@ -221,11 +217,11 @@ ifeq ($(CONF), release)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Windows version builds two, one with a conole and one without it
|
# Windows version builds two, one with a conole and one without it
|
||||||
$(BIN)/sdl/sameboy.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(OBJ)/Windows/resources.o
|
$(BIN)/SDL/sameboy.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(OBJ)/Windows/resources.o
|
||||||
-@$(MKDIR) -p $(dir $@)
|
-@$(MKDIR) -p $(dir $@)
|
||||||
$(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) -Wl,/subsystem:windows
|
$(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) -Wl,/subsystem:windows
|
||||||
|
|
||||||
$(BIN)/sdl/sameboy_debugger.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(OBJ)/Windows/resources.o
|
$(BIN)/SDL/sameboy_debugger.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) $(OBJ)/Windows/resources.o
|
||||||
-@$(MKDIR) -p $(dir $@)
|
-@$(MKDIR) -p $(dir $@)
|
||||||
$(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) -Wl,/subsystem:console
|
$(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) -Wl,/subsystem:console
|
||||||
|
|
||||||
|
@ -236,11 +232,11 @@ $(OBJ)/%.res: %.rc
|
||||||
%.o: %.res
|
%.o: %.res
|
||||||
cvtres /OUT:"$@" $^
|
cvtres /OUT:"$@" $^
|
||||||
|
|
||||||
# We must provide SDL.dll with the Windows port. This is an AWFUL HACK to find it.
|
# We must provide SDL2.dll with the Windows port. This is an AWFUL HACK to find it.
|
||||||
SPACE :=
|
SPACE :=
|
||||||
SPACE +=
|
SPACE +=
|
||||||
$(BIN)/sdl/SDL.dll:
|
$(BIN)/SDL/SDL2.dll:
|
||||||
@$(eval POTENTIAL_MATCHES := $(subst @@@," ",$(patsubst %,%/SDL.dll,$(subst ;,$(SPACE),$(subst $(SPACE),@@@,$(lib))))))
|
@$(eval POTENTIAL_MATCHES := $(subst @@@," ",$(patsubst %,%/SDL2.dll,$(subst ;,$(SPACE),$(subst $(SPACE),@@@,$(lib))))))
|
||||||
@$(eval MATCH := $(shell ls $(POTENTIAL_MATCHES) 2> NUL | head -n 1))
|
@$(eval MATCH := $(shell ls $(POTENTIAL_MATCHES) 2> NUL | head -n 1))
|
||||||
cp "$(MATCH)" $@
|
cp "$(MATCH)" $@
|
||||||
|
|
||||||
|
@ -257,7 +253,7 @@ $(BIN)/tester/sameboy_tester.exe: $(CORE_OBJECTS) $(SDL_OBJECTS)
|
||||||
-@$(MKDIR) -p $(dir $@)
|
-@$(MKDIR) -p $(dir $@)
|
||||||
$(CC) $^ -o $@ $(LDFLAGS) -Wl,/subsystem:console
|
$(CC) $^ -o $@ $(LDFLAGS) -Wl,/subsystem:console
|
||||||
|
|
||||||
$(BIN)/sdl/%.bin $(BIN)/tester/%.bin: $(BOOTROMS_DIR)/%.bin
|
$(BIN)/SDL/%.bin $(BIN)/tester/%.bin: $(BOOTROMS_DIR)/%.bin
|
||||||
-@$(MKDIR) -p $(dir $@)
|
-@$(MKDIR) -p $(dir $@)
|
||||||
cp -f $^ $@
|
cp -f $^ $@
|
||||||
|
|
||||||
|
@ -265,10 +261,13 @@ $(BIN)/SameBoy.app/Contents/Resources/%.bin: $(BOOTROMS_DIR)/%.bin
|
||||||
-@$(MKDIR) -p $(dir $@)
|
-@$(MKDIR) -p $(dir $@)
|
||||||
cp -f $^ $@
|
cp -f $^ $@
|
||||||
|
|
||||||
$(BIN)/sdl/LICENSE: LICENSE
|
$(BIN)/SDL/LICENSE: LICENSE
|
||||||
cp -f $^ $@
|
cp -f $^ $@
|
||||||
|
|
||||||
$(BIN)/sdl/registers.sym: Misc/registers.sym
|
$(BIN)/SDL/registers.sym: Misc/registers.sym
|
||||||
|
cp -f $^ $@
|
||||||
|
|
||||||
|
$(BIN)/SDL/drop.bmp: SDL/drop.bmp
|
||||||
cp -f $^ $@
|
cp -f $^ $@
|
||||||
|
|
||||||
# Boot ROMs
|
# Boot ROMs
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
/* SDLMain.m - main entry point for our Cocoa-ized SDL app
|
|
||||||
Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
|
|
||||||
Non-NIB-Code & other changes: Max Horn <max@quendi.de>
|
|
||||||
|
|
||||||
Feel free to customize this file to suit your needs
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _SDLMain_h_
|
|
||||||
#define _SDLMain_h_
|
|
||||||
|
|
||||||
#import <Cocoa/Cocoa.h>
|
|
||||||
|
|
||||||
@interface SDLMain : NSObject<NSApplicationDelegate>
|
|
||||||
@end
|
|
||||||
|
|
||||||
#endif /* _SDLMain_h_ */
|
|
382
SDL/SDLMain.m
382
SDL/SDLMain.m
|
@ -1,382 +0,0 @@
|
||||||
/* SDLMain.m - main entry point for our Cocoa-ized SDL app
|
|
||||||
Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
|
|
||||||
Non-NIB-Code & other changes: Max Horn <max@quendi.de>
|
|
||||||
|
|
||||||
Feel free to customize this file to suit your needs
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <SDL/SDL.h>
|
|
||||||
#include "SDLMain.h"
|
|
||||||
#include <sys/param.h> /* for MAXPATHLEN */
|
|
||||||
#include <unistd.h>
|
|
||||||
#import <objc/runtime.h>
|
|
||||||
|
|
||||||
/* For some reaon, Apple removed setAppleMenu from the headers in 10.4,
|
|
||||||
but the method still is there and works. To avoid warnings, we declare
|
|
||||||
it ourselves here. */
|
|
||||||
@interface NSApplication(SDL_Missing_Methods)
|
|
||||||
- (void)setAppleMenu:(NSMenu *)menu;
|
|
||||||
@end
|
|
||||||
|
|
||||||
/* Use this flag to determine whether we use SDLMain.nib or not */
|
|
||||||
#define SDL_USE_NIB_FILE 0
|
|
||||||
|
|
||||||
/* Use this flag to determine whether we use CPS (docking) or not */
|
|
||||||
#define SDL_USE_CPS 1
|
|
||||||
#ifdef SDL_USE_CPS
|
|
||||||
/* Portions of CPS.h */
|
|
||||||
typedef struct CPSProcessSerNum
|
|
||||||
{
|
|
||||||
UInt32 lo;
|
|
||||||
UInt32 hi;
|
|
||||||
} CPSProcessSerNum;
|
|
||||||
|
|
||||||
extern OSErr CPSGetCurrentProcess( CPSProcessSerNum *psn);
|
|
||||||
extern OSErr CPSEnableForegroundOperation( CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
|
|
||||||
extern OSErr CPSSetFrontProcess( CPSProcessSerNum *psn);
|
|
||||||
|
|
||||||
#endif /* SDL_USE_CPS */
|
|
||||||
|
|
||||||
static int gArgc;
|
|
||||||
static char **gArgv;
|
|
||||||
static BOOL gFinderLaunch;
|
|
||||||
static BOOL gCalledAppMainline = FALSE;
|
|
||||||
|
|
||||||
static NSString *getApplicationName(void)
|
|
||||||
{
|
|
||||||
const NSDictionary *dict;
|
|
||||||
NSString *appName = 0;
|
|
||||||
|
|
||||||
/* Determine the application name */
|
|
||||||
dict = (__bridge const NSDictionary *)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
|
|
||||||
if (dict)
|
|
||||||
appName = [dict objectForKey: @"CFBundleName"];
|
|
||||||
|
|
||||||
if (![appName length])
|
|
||||||
appName = [[NSProcessInfo processInfo] processName];
|
|
||||||
|
|
||||||
return appName;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if SDL_USE_NIB_FILE
|
|
||||||
/* A helper category for NSString */
|
|
||||||
@interface NSString (ReplaceSubString)
|
|
||||||
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString;
|
|
||||||
@end
|
|
||||||
#endif
|
|
||||||
|
|
||||||
@interface NSApplication (SDLApplication)
|
|
||||||
@end
|
|
||||||
|
|
||||||
@implementation NSApplication (SDLApplication)
|
|
||||||
/* Invoked from the Quit menu item */
|
|
||||||
- (void)_terminate:(id)sender
|
|
||||||
{
|
|
||||||
/* Post a SDL_QUIT event */
|
|
||||||
SDL_Event event;
|
|
||||||
event.type = SDL_QUIT;
|
|
||||||
SDL_PushEvent(&event);
|
|
||||||
/* Call "super" */
|
|
||||||
[self _terminate:sender];
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Use swizzling to avoid warning and undocumented Obj C runtime behavior. Didn't feel like rewriting SDLMain for this. */
|
|
||||||
+ (void) load
|
|
||||||
{
|
|
||||||
method_exchangeImplementations(class_getInstanceMethod(self, @selector(terminate:)), class_getInstanceMethod(self, @selector(_terminate:)));
|
|
||||||
}
|
|
||||||
@end
|
|
||||||
|
|
||||||
/* The main class of the application, the application's delegate */
|
|
||||||
@implementation SDLMain
|
|
||||||
|
|
||||||
/* Set the working directory to the .app's parent directory */
|
|
||||||
- (void) setupWorkingDirectory:(BOOL)shouldChdir
|
|
||||||
{
|
|
||||||
if (shouldChdir)
|
|
||||||
{
|
|
||||||
char parentdir[MAXPATHLEN];
|
|
||||||
CFURLRef url = CFBundleCopyBundleURL(CFBundleGetMainBundle());
|
|
||||||
CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent(0, url);
|
|
||||||
if (CFURLGetFileSystemRepresentation(url2, 1, (UInt8 *)parentdir, MAXPATHLEN)) {
|
|
||||||
chdir(parentdir); /* chdir to the binary app's parent */
|
|
||||||
}
|
|
||||||
CFRelease(url);
|
|
||||||
CFRelease(url2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if SDL_USE_NIB_FILE
|
|
||||||
|
|
||||||
/* Fix menu to contain the real app name instead of "SDL App" */
|
|
||||||
- (void)fixMenu:(NSMenu *)aMenu withAppName:(NSString *)appName
|
|
||||||
{
|
|
||||||
NSRange aRange;
|
|
||||||
NSEnumerator *enumerator;
|
|
||||||
NSMenuItem *menuItem;
|
|
||||||
|
|
||||||
aRange = [[aMenu title] rangeOfString:@"SDL App"];
|
|
||||||
if (aRange.length != 0)
|
|
||||||
[aMenu setTitle: [[aMenu title] stringByReplacingRange:aRange with:appName]];
|
|
||||||
|
|
||||||
enumerator = [[aMenu itemArray] objectEnumerator];
|
|
||||||
while ((menuItem = [enumerator nextObject]))
|
|
||||||
{
|
|
||||||
aRange = [[menuItem title] rangeOfString:@"SDL App"];
|
|
||||||
if (aRange.length != 0)
|
|
||||||
[menuItem setTitle: [[menuItem title] stringByReplacingRange:aRange with:appName]];
|
|
||||||
if ([menuItem hasSubmenu])
|
|
||||||
[self fixMenu:[menuItem submenu] withAppName:appName];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
static void setApplicationMenu(void)
|
|
||||||
{
|
|
||||||
/* warning: this code is very odd */
|
|
||||||
NSMenu *appleMenu;
|
|
||||||
NSMenuItem *menuItem;
|
|
||||||
NSString *title;
|
|
||||||
NSString *appName;
|
|
||||||
|
|
||||||
appName = getApplicationName();
|
|
||||||
appleMenu = [[NSMenu alloc] initWithTitle:@""];
|
|
||||||
|
|
||||||
/* Add menu items */
|
|
||||||
title = [@"About " stringByAppendingString:appName];
|
|
||||||
[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
|
|
||||||
|
|
||||||
[appleMenu addItem:[NSMenuItem separatorItem]];
|
|
||||||
|
|
||||||
title = [@"Hide " stringByAppendingString:appName];
|
|
||||||
[appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
|
|
||||||
|
|
||||||
menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
|
|
||||||
[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
|
|
||||||
|
|
||||||
[appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
|
|
||||||
|
|
||||||
[appleMenu addItem:[NSMenuItem separatorItem]];
|
|
||||||
|
|
||||||
title = [@"Quit " stringByAppendingString:appName];
|
|
||||||
[appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
|
|
||||||
|
|
||||||
|
|
||||||
/* Put menu into the menubar */
|
|
||||||
menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
|
|
||||||
[menuItem setSubmenu:appleMenu];
|
|
||||||
[[NSApp mainMenu] addItem:menuItem];
|
|
||||||
|
|
||||||
/* Tell the application object that this is now the application menu */
|
|
||||||
[NSApp setAppleMenu:appleMenu];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create a window menu */
|
|
||||||
static void setupWindowMenu(void)
|
|
||||||
{
|
|
||||||
NSMenu *windowMenu;
|
|
||||||
NSMenuItem *windowMenuItem;
|
|
||||||
NSMenuItem *menuItem;
|
|
||||||
|
|
||||||
windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
|
|
||||||
|
|
||||||
/* "Minimize" item */
|
|
||||||
menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
|
|
||||||
[windowMenu addItem:menuItem];
|
|
||||||
|
|
||||||
/* Put menu into the menubar */
|
|
||||||
windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
|
|
||||||
[windowMenuItem setSubmenu:windowMenu];
|
|
||||||
[[NSApp mainMenu] addItem:windowMenuItem];
|
|
||||||
|
|
||||||
/* Tell the application object that this is now the window menu */
|
|
||||||
[NSApp setWindowsMenu:windowMenu];
|
|
||||||
|
|
||||||
/* Finally give up our references to the objects */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Replacement for NSApplicationMain */
|
|
||||||
static void CustomApplicationMain (int argc, char **argv)
|
|
||||||
{
|
|
||||||
SDLMain *sdlMain;
|
|
||||||
|
|
||||||
/* Ensure the application object is initialised */
|
|
||||||
[NSApplication sharedApplication];
|
|
||||||
|
|
||||||
#ifdef SDL_USE_CPS
|
|
||||||
{
|
|
||||||
CPSProcessSerNum PSN;
|
|
||||||
/* Tell the dock about us */
|
|
||||||
if (!CPSGetCurrentProcess(&PSN))
|
|
||||||
if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
|
|
||||||
if (!CPSSetFrontProcess(&PSN))
|
|
||||||
[NSApplication sharedApplication];
|
|
||||||
}
|
|
||||||
#endif /* SDL_USE_CPS */
|
|
||||||
|
|
||||||
/* Set up the menubar */
|
|
||||||
[NSApp setMainMenu:[[NSMenu alloc] init]];
|
|
||||||
setApplicationMenu();
|
|
||||||
setupWindowMenu();
|
|
||||||
|
|
||||||
/* Create SDLMain and make it the app delegate */
|
|
||||||
sdlMain = [[SDLMain alloc] init];
|
|
||||||
[NSApp setDelegate:sdlMain];
|
|
||||||
|
|
||||||
/* Start the main event loop */
|
|
||||||
[NSApp run];
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Catch document open requests...this lets us notice files when the app
|
|
||||||
* was launched by double-clicking a document, or when a document was
|
|
||||||
* dragged/dropped on the app's icon. You need to have a
|
|
||||||
* CFBundleDocumentsType section in your Info.plist to get this message,
|
|
||||||
* apparently.
|
|
||||||
*
|
|
||||||
* Files are added to gArgv, so to the app, they'll look like command line
|
|
||||||
* arguments. Previously, apps launched from the finder had nothing but
|
|
||||||
* an argv[0].
|
|
||||||
*
|
|
||||||
* This message may be received multiple times to open several docs on launch.
|
|
||||||
*
|
|
||||||
* This message is ignored once the app's mainline has been called.
|
|
||||||
*/
|
|
||||||
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
|
|
||||||
{
|
|
||||||
const char *temparg;
|
|
||||||
size_t arglen;
|
|
||||||
char *arg;
|
|
||||||
char **newargv;
|
|
||||||
|
|
||||||
if (!gFinderLaunch) /* MacOS is passing command line args. */
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
if (gCalledAppMainline) /* app has started, ignore this document. */
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
temparg = [filename UTF8String];
|
|
||||||
arglen = SDL_strlen(temparg) + 1;
|
|
||||||
arg = (char *) SDL_malloc(arglen);
|
|
||||||
if (arg == NULL)
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
newargv = (char **) realloc(gArgv, sizeof (char *) * (gArgc + 2));
|
|
||||||
if (newargv == NULL)
|
|
||||||
{
|
|
||||||
SDL_free(arg);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
gArgv = newargv;
|
|
||||||
|
|
||||||
SDL_strlcpy(arg, temparg, arglen);
|
|
||||||
gArgv[gArgc++] = arg;
|
|
||||||
gArgv[gArgc] = NULL;
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* Called when the internal event loop has just started running */
|
|
||||||
- (void) applicationDidFinishLaunching: (NSNotification *) note
|
|
||||||
{
|
|
||||||
int status;
|
|
||||||
|
|
||||||
/* Set the working directory to the .app's parent directory */
|
|
||||||
[self setupWorkingDirectory:gFinderLaunch];
|
|
||||||
|
|
||||||
#if SDL_USE_NIB_FILE
|
|
||||||
/* Set the main menu to contain the real app name instead of "SDL App" */
|
|
||||||
[self fixMenu:[NSApp mainMenu] withAppName:getApplicationName()];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Hand off to main application code */
|
|
||||||
gCalledAppMainline = TRUE;
|
|
||||||
status = SDL_main (gArgc, gArgv);
|
|
||||||
|
|
||||||
/* We're done, thank you for playing */
|
|
||||||
exit(status);
|
|
||||||
}
|
|
||||||
@end
|
|
||||||
|
|
||||||
|
|
||||||
@implementation NSString (ReplaceSubString)
|
|
||||||
|
|
||||||
- (NSString *)stringByReplacingRange:(NSRange)aRange with:(NSString *)aString
|
|
||||||
{
|
|
||||||
unsigned int bufferSize;
|
|
||||||
unsigned int selfLen = [self length];
|
|
||||||
unsigned int aStringLen = [aString length];
|
|
||||||
unichar *buffer;
|
|
||||||
NSRange localRange;
|
|
||||||
NSString *result;
|
|
||||||
|
|
||||||
bufferSize = selfLen + aStringLen - aRange.length;
|
|
||||||
buffer = (unichar *)NSAllocateMemoryPages(bufferSize*sizeof(unichar));
|
|
||||||
|
|
||||||
/* Get first part into buffer */
|
|
||||||
localRange.location = 0;
|
|
||||||
localRange.length = aRange.location;
|
|
||||||
[self getCharacters:buffer range:localRange];
|
|
||||||
|
|
||||||
/* Get middle part into buffer */
|
|
||||||
localRange.location = 0;
|
|
||||||
localRange.length = aStringLen;
|
|
||||||
[aString getCharacters:(buffer+aRange.location) range:localRange];
|
|
||||||
|
|
||||||
/* Get last part into buffer */
|
|
||||||
localRange.location = aRange.location + aRange.length;
|
|
||||||
localRange.length = selfLen - localRange.location;
|
|
||||||
[self getCharacters:(buffer+aRange.location+aStringLen) range:localRange];
|
|
||||||
|
|
||||||
/* Build output string */
|
|
||||||
result = [NSString stringWithCharacters:buffer length:bufferSize];
|
|
||||||
|
|
||||||
NSDeallocateMemoryPages(buffer, bufferSize);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
|
||||||
|
|
||||||
|
|
||||||
void cocoa_disable_filtering(void) {
|
|
||||||
CGContextSetInterpolationQuality([[NSGraphicsContext currentContext] CGContext], kCGInterpolationNone);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef main
|
|
||||||
# undef main
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Main entry point to executable - should *not* be SDL_main! */
|
|
||||||
int main (int argc, char **argv)
|
|
||||||
{
|
|
||||||
/* Copy the arguments into a global variable */
|
|
||||||
/* This is passed if we are launched by double-clicking */
|
|
||||||
if ( argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
|
|
||||||
gArgv = (char **) SDL_malloc(sizeof (char *) * 2);
|
|
||||||
gArgv[0] = argv[0];
|
|
||||||
gArgv[1] = NULL;
|
|
||||||
gArgc = 1;
|
|
||||||
gFinderLaunch = YES;
|
|
||||||
} else {
|
|
||||||
int i;
|
|
||||||
gArgc = argc;
|
|
||||||
gArgv = (char **) SDL_malloc(sizeof (char *) * (argc+1));
|
|
||||||
for (i = 0; i <= argc; i++)
|
|
||||||
gArgv[i] = argv[i];
|
|
||||||
gFinderLaunch = NO;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if SDL_USE_NIB_FILE
|
|
||||||
NSApplicationMain (argc, argv);
|
|
||||||
#else
|
|
||||||
CustomApplicationMain (argc, argv);
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 68 KiB |
652
SDL/main.c
652
SDL/main.c
|
@ -1,42 +1,261 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <SDL/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
#include "gb.h"
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#define AUDIO_FREQUENCY 96000
|
#define AUDIO_FREQUENCY 96000
|
||||||
#else
|
#else
|
||||||
/* Windows (well, at least my VM) can't handle 96KHz sound well :( */
|
/* Windows (well, at least my VM) can't handle 96KHz sound well :( */
|
||||||
#define AUDIO_FREQUENCY 44100
|
#define AUDIO_FREQUENCY 44100
|
||||||
#include <direct.h>
|
|
||||||
#include <windows.h>
|
|
||||||
#define snprintf _snprintf
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "gb.h"
|
|
||||||
|
|
||||||
static bool running = false;
|
|
||||||
static char *filename;
|
|
||||||
static void replace_extension(const char *src, size_t length, char *dest, const char *ext);
|
|
||||||
GB_gameboy_t gb;
|
|
||||||
|
|
||||||
static void GB_update_keys_status(GB_gameboy_t *gb)
|
|
||||||
{
|
|
||||||
static bool ctrl = false;
|
|
||||||
static bool shift = false;
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
static bool cmd = false;
|
#define MODIFIER_NAME "Cmd"
|
||||||
|
#else
|
||||||
|
#define MODIFIER_NAME "Ctrl"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static const char help[] =
|
||||||
|
"Drop a GB or GBC ROM file to play.\n"
|
||||||
|
"\n"
|
||||||
|
"Controls:\n"
|
||||||
|
" D-Pad: Arrow Keys\n"
|
||||||
|
" A: X\n"
|
||||||
|
" B: Z\n"
|
||||||
|
" Start: Enter\n"
|
||||||
|
" Select: Backspace\n"
|
||||||
|
"\n"
|
||||||
|
"Keyboard Shortcuts: \n"
|
||||||
|
" Restart: " MODIFIER_NAME "+R\n"
|
||||||
|
" Pause: " MODIFIER_NAME "+P\n"
|
||||||
|
" Turbo: Space\n"
|
||||||
|
#ifdef __APPLE__
|
||||||
|
" Mute/Unmute: " MODIFIER_NAME "+Shift+M\n"
|
||||||
|
#else
|
||||||
|
" Mute/Unmute: " MODIFIER_NAME "+M\n"
|
||||||
|
#endif
|
||||||
|
" Save state: " MODIFIER_NAME "+Number (0-9)\n"
|
||||||
|
" Load state: " MODIFIER_NAME "+Shift+Number (0-9)\n"
|
||||||
|
" Cycle between DMG/CGB emulation: " MODIFIER_NAME "+T\n"
|
||||||
|
" Cycle scaling modes: Tab"
|
||||||
|
;
|
||||||
|
|
||||||
|
GB_gameboy_t gb;
|
||||||
|
static bool dmg = false;
|
||||||
|
static bool paused = false;
|
||||||
|
static uint32_t pixels[160*144];
|
||||||
|
|
||||||
|
static char *filename = NULL;
|
||||||
|
static bool should_free_filename = false;
|
||||||
|
static char *battery_save_path_ptr;
|
||||||
|
|
||||||
|
static SDL_Window *window = NULL;
|
||||||
|
static SDL_Renderer *renderer = NULL;
|
||||||
|
static SDL_Texture *texture = NULL;
|
||||||
|
static SDL_PixelFormat *pixel_format = NULL;
|
||||||
|
static SDL_AudioSpec want_aspec, have_aspec;
|
||||||
|
|
||||||
|
static char *captured_log = NULL;
|
||||||
|
|
||||||
|
static void log_capture_callback(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes)
|
||||||
|
{
|
||||||
|
size_t current_len = strlen(captured_log);
|
||||||
|
size_t len_to_add = strlen(string);
|
||||||
|
captured_log = realloc(captured_log, current_len + len_to_add + 1);
|
||||||
|
memcpy(captured_log + current_len, string, len_to_add);
|
||||||
|
captured_log[current_len + len_to_add] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void start_capturing_logs(void)
|
||||||
|
{
|
||||||
|
if (captured_log != NULL) {
|
||||||
|
free(captured_log);
|
||||||
|
}
|
||||||
|
captured_log = malloc(1);
|
||||||
|
captured_log[0] = 0;
|
||||||
|
GB_set_log_callback(&gb, log_capture_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char *end_capturing_logs(bool show_popup, bool should_exit)
|
||||||
|
{
|
||||||
|
GB_set_log_callback(&gb, NULL);
|
||||||
|
if (captured_log[0] == 0) {
|
||||||
|
free(captured_log);
|
||||||
|
captured_log = NULL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (show_popup) {
|
||||||
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", captured_log, window);
|
||||||
|
}
|
||||||
|
if (should_exit) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return captured_log;
|
||||||
|
}
|
||||||
|
|
||||||
|
static enum {
|
||||||
|
GB_SDL_NO_COMMAND,
|
||||||
|
GB_SDL_SAVE_STATE_COMMAND,
|
||||||
|
GB_SDL_LOAD_STATE_COMMAND,
|
||||||
|
GB_SDL_RESET_COMMAND,
|
||||||
|
GB_SDL_NEW_FILE_COMMAND,
|
||||||
|
GB_SDL_TOGGLE_MODEL_COMMAND,
|
||||||
|
} pending_command;
|
||||||
|
|
||||||
|
static enum {
|
||||||
|
GB_SDL_SCALING_ENTIRE_WINDOW,
|
||||||
|
GB_SDL_SCALING_KEEP_RATIO,
|
||||||
|
GB_SDL_SCALING_INTEGER_FACTOR,
|
||||||
|
GB_SDL_SCALING_MAX,
|
||||||
|
} scaling_mode = GB_SDL_SCALING_INTEGER_FACTOR;
|
||||||
|
static unsigned command_parameter;
|
||||||
|
|
||||||
|
static void update_viewport(void)
|
||||||
|
{
|
||||||
|
int win_width, win_height;
|
||||||
|
SDL_GetWindowSize(window, &win_width, &win_height);
|
||||||
|
double x_factor = win_width / 160.0;
|
||||||
|
double y_factor = win_height / 144.0;
|
||||||
|
|
||||||
|
if (scaling_mode == GB_SDL_SCALING_INTEGER_FACTOR) {
|
||||||
|
x_factor = (int)(x_factor);
|
||||||
|
y_factor = (int)(y_factor);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scaling_mode != GB_SDL_SCALING_ENTIRE_WINDOW) {
|
||||||
|
if (x_factor > y_factor) {
|
||||||
|
x_factor = y_factor;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
y_factor = x_factor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned new_width = x_factor * 160;
|
||||||
|
unsigned new_height = y_factor * 144;
|
||||||
|
|
||||||
|
SDL_Rect rect = (SDL_Rect){(win_width - new_width) / 2, (win_height - new_height) /2,
|
||||||
|
new_width, new_height};
|
||||||
|
SDL_RenderSetViewport(renderer, &rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cycle_scaling(void)
|
||||||
|
{
|
||||||
|
scaling_mode++;
|
||||||
|
scaling_mode %= GB_SDL_SCALING_MAX;
|
||||||
|
update_viewport();
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_events(GB_gameboy_t *gb)
|
||||||
|
{
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#define MODIFIER KMOD_GUI
|
||||||
|
#else
|
||||||
|
#define MODIFIER KMOD_CTRL
|
||||||
#endif
|
#endif
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event))
|
while (SDL_PollEvent(&event))
|
||||||
{
|
{
|
||||||
switch( event.type ){
|
switch (event.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
running = false;
|
GB_save_battery(gb, battery_save_path_ptr);
|
||||||
|
exit(0);
|
||||||
|
|
||||||
|
case SDL_DROPFILE: {
|
||||||
|
if (should_free_filename) {
|
||||||
|
SDL_free(filename);
|
||||||
|
}
|
||||||
|
filename = event.drop.file;
|
||||||
|
should_free_filename = true;
|
||||||
|
pending_command = GB_SDL_NEW_FILE_COMMAND;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SDL_WINDOWEVENT: {
|
||||||
|
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
||||||
|
update_viewport();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
case SDL_KEYUP:
|
switch (event.key.keysym.sym) {
|
||||||
|
case SDLK_c:
|
||||||
|
if (event.type == SDL_KEYDOWN && (event.key.keysym.mod & KMOD_CTRL)) {
|
||||||
|
GB_debugger_break(gb);
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_r:
|
||||||
|
if (event.key.keysym.mod & MODIFIER) {
|
||||||
|
pending_command = GB_SDL_RESET_COMMAND;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_t:
|
||||||
|
if (event.key.keysym.mod & MODIFIER) {
|
||||||
|
pending_command = GB_SDL_TOGGLE_MODEL_COMMAND;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_p:
|
||||||
|
if (event.key.keysym.mod & MODIFIER) {
|
||||||
|
paused = !paused;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_m:
|
||||||
|
if (event.key.keysym.mod & MODIFIER) {
|
||||||
|
#ifdef __APPLE__
|
||||||
|
// Can't override CMD+M (Minimize) in SDL
|
||||||
|
if (!(event.key.keysym.mod & KMOD_SHIFT)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
SDL_PauseAudio(SDL_GetAudioStatus() == SDL_AUDIO_PLAYING? true : false);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_TAB:
|
||||||
|
cycle_scaling();
|
||||||
|
break;
|
||||||
|
#ifndef __APPLE__
|
||||||
|
case SDLK_F1:
|
||||||
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Help", help, window);
|
||||||
|
break;
|
||||||
|
#else
|
||||||
|
case SDLK_SLASH:
|
||||||
|
if (!(event.key.keysym.sym && (event.key.keysym.mod & KMOD_SHIFT))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SDLK_QUESTION:
|
||||||
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Help", help, window);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
default:
|
||||||
|
/* Save states */
|
||||||
|
if (event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) {
|
||||||
|
if (event.key.keysym.mod & MODIFIER) {
|
||||||
|
command_parameter = event.key.keysym.sym - SDLK_0;
|
||||||
|
|
||||||
|
if (event.key.keysym.mod & KMOD_SHIFT) {
|
||||||
|
pending_command = GB_SDL_LOAD_STATE_COMMAND;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pending_command = GB_SDL_SAVE_STATE_COMMAND;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SDL_KEYUP: // Fallthrough
|
||||||
switch (event.key.keysym.sym) {
|
switch (event.key.keysym.sym) {
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
GB_set_key_state(gb, GB_KEY_RIGHT, event.type == SDL_KEYDOWN);
|
GB_set_key_state(gb, GB_KEY_RIGHT, event.type == SDL_KEYDOWN);
|
||||||
|
@ -65,51 +284,6 @@ static void GB_update_keys_status(GB_gameboy_t *gb)
|
||||||
case SDLK_SPACE:
|
case SDLK_SPACE:
|
||||||
GB_set_turbo_mode(gb, event.type == SDL_KEYDOWN, false);
|
GB_set_turbo_mode(gb, event.type == SDL_KEYDOWN, false);
|
||||||
break;
|
break;
|
||||||
case SDLK_LCTRL:
|
|
||||||
case SDLK_RCTRL:
|
|
||||||
ctrl = event.type == SDL_KEYDOWN;
|
|
||||||
break;
|
|
||||||
case SDLK_LSHIFT:
|
|
||||||
case SDLK_RSHIFT:
|
|
||||||
shift = event.type == SDL_KEYDOWN;
|
|
||||||
break;
|
|
||||||
#ifdef __APPLE__
|
|
||||||
case SDLK_LMETA:
|
|
||||||
case SDLK_RMETA:
|
|
||||||
cmd = event.type == SDL_KEYDOWN;
|
|
||||||
break;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
case SDLK_c:
|
|
||||||
if (ctrl && event.type == SDL_KEYDOWN) {
|
|
||||||
ctrl = false;
|
|
||||||
GB_debugger_break(gb);
|
|
||||||
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
/* Save states */
|
|
||||||
if (event.key.keysym.sym >= SDLK_0 && event.key.keysym.sym <= SDLK_9) {
|
|
||||||
#ifdef __APPLE__
|
|
||||||
if (cmd) {
|
|
||||||
#else
|
|
||||||
if (ctrl) {
|
|
||||||
#endif
|
|
||||||
char save_path[strlen(filename) + 4];
|
|
||||||
char save_extension[] =".s0";
|
|
||||||
save_extension[2] += event.key.keysym.sym - SDLK_0;
|
|
||||||
replace_extension(filename, strlen(filename), save_path, save_extension);
|
|
||||||
|
|
||||||
if (shift) {
|
|
||||||
GB_load_state(gb, save_path);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
GB_save_state(gb, save_path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -120,68 +294,17 @@ static void GB_update_keys_status(GB_gameboy_t *gb)
|
||||||
|
|
||||||
static void vblank(GB_gameboy_t *gb)
|
static void vblank(GB_gameboy_t *gb)
|
||||||
{
|
{
|
||||||
SDL_Surface *screen = GB_get_user_data(gb);
|
SDL_UpdateTexture(texture, NULL, pixels, 160 * sizeof (uint32_t));
|
||||||
SDL_Flip(screen);
|
SDL_RenderClear(renderer);
|
||||||
GB_update_keys_status(gb);
|
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
GB_set_pixels_output(gb, screen->pixels);
|
handle_events(gb);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __APPLE__
|
|
||||||
#include <mach-o/dyld.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static const char *executable_folder(void)
|
|
||||||
{
|
|
||||||
static char path[1024] = {0,};
|
|
||||||
if (path[0]) {
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
/* Ugly unportable code! :( */
|
|
||||||
#ifdef __APPLE__
|
|
||||||
unsigned int length = sizeof(path) - 1;
|
|
||||||
_NSGetExecutablePath(&path[0], &length);
|
|
||||||
#else
|
|
||||||
#ifdef __linux__
|
|
||||||
ssize_t length = readlink("/proc/self/exe", &path[0], sizeof(path) - 1);
|
|
||||||
assert (length != -1);
|
|
||||||
#else
|
|
||||||
#ifdef _WIN32
|
|
||||||
HMODULE hModule = GetModuleHandle(NULL);
|
|
||||||
GetModuleFileName(hModule, path, sizeof(path) - 1);
|
|
||||||
#else
|
|
||||||
/* No OS-specific way, assume running from CWD */
|
|
||||||
getcwd(&path[0], sizeof(path) - 1);
|
|
||||||
return path;
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
size_t pos = strlen(path);
|
|
||||||
while (pos) {
|
|
||||||
pos--;
|
|
||||||
#ifdef _WIN32
|
|
||||||
if (path[pos] == '\\') {
|
|
||||||
#else
|
|
||||||
if (path[pos] == '/') {
|
|
||||||
#endif
|
|
||||||
path[pos] = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *executable_relative_path(const char *filename)
|
|
||||||
{
|
|
||||||
static char path[1024];
|
|
||||||
snprintf(path, sizeof(path), "%s/%s", executable_folder(), filename);
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
static SDL_Surface *screen = NULL;
|
|
||||||
static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
|
static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
|
||||||
{
|
{
|
||||||
return SDL_MapRGB(screen->format, r, g, b);
|
return SDL_MapRGB(pixel_format, r, g, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void debugger_interrupt(int ignore)
|
static void debugger_interrupt(int ignore)
|
||||||
|
@ -196,97 +319,91 @@ static void debugger_interrupt(int ignore)
|
||||||
|
|
||||||
static void audio_callback(void *gb, Uint8 *stream, int len)
|
static void audio_callback(void *gb, Uint8 *stream, int len)
|
||||||
{
|
{
|
||||||
|
if (GB_is_inited(gb)) {
|
||||||
GB_apu_copy_buffer(gb, (GB_sample_t *) stream, len / sizeof(GB_sample_t));
|
GB_apu_copy_buffer(gb, (GB_sample_t *) stream, len / sizeof(GB_sample_t));
|
||||||
}
|
|
||||||
|
|
||||||
static void replace_extension(const char *src, size_t length, char *dest, const char *ext)
|
|
||||||
{
|
|
||||||
memcpy(dest, src, length);
|
|
||||||
dest[length] = 0;
|
|
||||||
|
|
||||||
/* Remove extension */
|
|
||||||
for (size_t i = length; i--;) {
|
|
||||||
if (dest[i] == '/') break;
|
|
||||||
if (dest[i] == '.') {
|
|
||||||
dest[i] = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Add new extension */
|
|
||||||
strcat(dest, ext);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __APPLE__
|
|
||||||
extern void cocoa_disable_filtering(void);
|
|
||||||
#endif
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
bool dmg = false;
|
|
||||||
|
|
||||||
#define str(x) #x
|
|
||||||
#define xstr(x) str(x)
|
|
||||||
fprintf(stderr, "SameBoy v" xstr(VERSION) "\n");
|
|
||||||
|
|
||||||
if (argc == 1 || argc > 3) {
|
|
||||||
usage:
|
|
||||||
fprintf(stderr, "Usage: %s [--dmg] rom\n", argv[0]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argc == 3) {
|
|
||||||
if (strcmp(argv[1], "--dmg") == 0) {
|
|
||||||
dmg = true;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
goto usage;
|
memset(stream, 0, len);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_pending_command(void)
|
||||||
|
{
|
||||||
|
switch (pending_command) {
|
||||||
|
case GB_SDL_LOAD_STATE_COMMAND:
|
||||||
|
case GB_SDL_SAVE_STATE_COMMAND: {
|
||||||
|
char save_path[strlen(filename) + 4];
|
||||||
|
char save_extension[] = ".s0";
|
||||||
|
save_extension[2] += command_parameter;
|
||||||
|
replace_extension(filename, strlen(filename), save_path, save_extension);
|
||||||
|
|
||||||
|
start_capturing_logs();
|
||||||
|
if (pending_command == GB_SDL_LOAD_STATE_COMMAND) {
|
||||||
|
GB_load_state(&gb, save_path);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
GB_save_state(&gb, save_path);
|
||||||
|
}
|
||||||
|
end_capturing_logs(true, false);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case GB_SDL_RESET_COMMAND:
|
||||||
|
GB_reset(&gb);
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case GB_SDL_NO_COMMAND:
|
||||||
|
return false;
|
||||||
|
|
||||||
|
case GB_SDL_NEW_FILE_COMMAND:
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case GB_SDL_TOGGLE_MODEL_COMMAND:
|
||||||
|
dmg = !dmg;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void run(void)
|
||||||
|
{
|
||||||
|
restart:
|
||||||
|
if (GB_is_inited(&gb)) {
|
||||||
|
GB_switch_model_and_reset(&gb, !dmg);
|
||||||
|
}
|
||||||
|
else {
|
||||||
if (dmg) {
|
if (dmg) {
|
||||||
GB_init(&gb);
|
GB_init(&gb);
|
||||||
if (GB_load_boot_rom(&gb, executable_relative_path("dmg_boot.bin"))) {
|
|
||||||
perror("Failed to load boot ROM");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
GB_init_cgb(&gb);
|
GB_init_cgb(&gb);
|
||||||
if (GB_load_boot_rom(&gb, executable_relative_path("cgb_boot.bin"))) {
|
|
||||||
perror("Failed to load boot ROM");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filename = argv[argc - 1];
|
|
||||||
|
|
||||||
if (GB_load_rom(&gb, filename)) {
|
|
||||||
perror("Failed to load ROM");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
signal(SIGINT, debugger_interrupt);
|
|
||||||
|
|
||||||
SDL_Init( SDL_INIT_EVERYTHING );
|
|
||||||
screen = SDL_SetVideoMode(160, 144, 32, SDL_SWSURFACE );
|
|
||||||
SDL_WM_SetCaption("SameBoy v" xstr(VERSION), "SameBoy v" xstr(VERSION));
|
|
||||||
#ifdef __APPLE__
|
|
||||||
cocoa_disable_filtering();
|
|
||||||
#endif
|
|
||||||
/* Configure Screen */
|
|
||||||
SDL_LockSurface(screen);
|
|
||||||
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
||||||
GB_set_user_data(&gb, screen);
|
GB_set_pixels_output(&gb, pixels);
|
||||||
GB_set_pixels_output(&gb, screen->pixels);
|
|
||||||
GB_set_rgb_encode_callback(&gb, rgb_encode);
|
GB_set_rgb_encode_callback(&gb, rgb_encode);
|
||||||
|
GB_set_sample_rate(&gb, have_aspec.freq);
|
||||||
|
}
|
||||||
|
|
||||||
|
start_capturing_logs();
|
||||||
|
if (dmg) {
|
||||||
|
GB_load_boot_rom(&gb, executable_relative_path("dmg_boot.bin"));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
GB_load_boot_rom(&gb, executable_relative_path("cgb_boot.bin"));
|
||||||
|
}
|
||||||
|
end_capturing_logs(true, true);
|
||||||
|
|
||||||
|
start_capturing_logs();
|
||||||
|
GB_load_rom(&gb, filename);
|
||||||
|
end_capturing_logs(true, true);
|
||||||
|
|
||||||
size_t path_length = strlen(filename);
|
size_t path_length = strlen(filename);
|
||||||
|
|
||||||
/* Configure battery */
|
/* Configure battery */
|
||||||
char battery_save_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .sav + NULL */
|
char battery_save_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .sav + NULL */
|
||||||
replace_extension(filename, path_length, battery_save_path, ".sav");
|
replace_extension(filename, path_length, battery_save_path, ".sav");
|
||||||
|
battery_save_path_ptr = battery_save_path;
|
||||||
GB_load_battery(&gb, battery_save_path);
|
GB_load_battery(&gb, battery_save_path);
|
||||||
|
|
||||||
/* Configure symbols */
|
/* Configure symbols */
|
||||||
|
@ -296,29 +413,136 @@ usage:
|
||||||
replace_extension(filename, path_length, symbols_path, ".sym");
|
replace_extension(filename, path_length, symbols_path, ".sym");
|
||||||
GB_debugger_load_symbol_file(&gb, symbols_path);
|
GB_debugger_load_symbol_file(&gb, symbols_path);
|
||||||
|
|
||||||
/* Configure Audio */
|
|
||||||
SDL_AudioSpec want, have;
|
|
||||||
SDL_memset(&want, 0, sizeof(want));
|
|
||||||
want.freq = AUDIO_FREQUENCY;
|
|
||||||
want.format = AUDIO_S16SYS;
|
|
||||||
want.channels = 2;
|
|
||||||
want.samples = 512;
|
|
||||||
want.callback = audio_callback;
|
|
||||||
want.userdata = &gb;
|
|
||||||
SDL_OpenAudio(&want, &have);
|
|
||||||
GB_set_sample_rate(&gb, AUDIO_FREQUENCY);
|
|
||||||
|
|
||||||
/* Start Audio */
|
|
||||||
SDL_PauseAudio(0);
|
|
||||||
|
|
||||||
/* Run emulation */
|
/* Run emulation */
|
||||||
running = true;
|
while (true) {
|
||||||
while (running) {
|
if (paused) {
|
||||||
|
SDL_WaitEvent(NULL);
|
||||||
|
handle_events(&gb);
|
||||||
|
}
|
||||||
|
else {
|
||||||
GB_run(&gb);
|
GB_run(&gb);
|
||||||
}
|
}
|
||||||
SDL_CloseAudio();
|
|
||||||
|
|
||||||
GB_save_battery(&gb, battery_save_path);
|
/* These commands can't run in the handle_event function, because they're not safe in a vblank context. */
|
||||||
return 0;
|
if (handle_pending_command()) {
|
||||||
|
pending_command = GB_SDL_NO_COMMAND;
|
||||||
|
goto restart;
|
||||||
|
}
|
||||||
|
pending_command = GB_SDL_NO_COMMAND;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
#define str(x) #x
|
||||||
|
#define xstr(x) str(x)
|
||||||
|
fprintf(stderr, "SameBoy v" xstr(VERSION) "\n");
|
||||||
|
|
||||||
|
if (argc > 3) {
|
||||||
|
usage:
|
||||||
|
fprintf(stderr, "Usage: %s [--dmg] [rom]\n", argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned i = 1; i < argc; i++) {
|
||||||
|
if (strcmp(argv[i], "--dmg") == 0) {
|
||||||
|
if (dmg) {
|
||||||
|
goto usage;
|
||||||
|
}
|
||||||
|
dmg = true;
|
||||||
|
}
|
||||||
|
else if (!filename) {
|
||||||
|
filename = argv[i];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
goto usage;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
signal(SIGINT, debugger_interrupt);
|
||||||
|
|
||||||
|
SDL_Init( SDL_INIT_EVERYTHING );
|
||||||
|
|
||||||
|
window = SDL_CreateWindow("SameBoy v" xstr(VERSION), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||||
|
160 * 2, 144 * 2, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||||
|
SDL_SetWindowMinimumSize(window, 160, 144);
|
||||||
|
renderer = SDL_CreateRenderer(window, -1, 0);
|
||||||
|
|
||||||
|
texture = SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), SDL_TEXTUREACCESS_STREAMING, 160, 144);
|
||||||
|
|
||||||
|
pixel_format = SDL_AllocFormat(SDL_GetWindowPixelFormat(window));
|
||||||
|
|
||||||
|
/* Configure Audio */
|
||||||
|
memset(&want_aspec, 0, sizeof(want_aspec));
|
||||||
|
want_aspec.freq = AUDIO_FREQUENCY;
|
||||||
|
want_aspec.format = AUDIO_S16SYS;
|
||||||
|
want_aspec.channels = 2;
|
||||||
|
#if SDL_COMPILEDVERSION == 2005 && defined(__APPLE__)
|
||||||
|
/* SDL 2.0.5 on macOS introduced a bug where certain combinations of buffer lengths and frequencies
|
||||||
|
fail to produce audio correctly. This bug was fixed 2.0.6. */
|
||||||
|
want_aspec.samples = 2048;
|
||||||
|
#else
|
||||||
|
want_aspec.samples = 512;
|
||||||
|
#endif
|
||||||
|
want_aspec.callback = audio_callback;
|
||||||
|
want_aspec.userdata = &gb;
|
||||||
|
SDL_OpenAudio(&want_aspec, &have_aspec);
|
||||||
|
|
||||||
|
/* Start Audio */
|
||||||
|
SDL_PauseAudio(false);
|
||||||
|
|
||||||
|
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
|
||||||
|
|
||||||
|
if (filename == NULL) {
|
||||||
|
/* Draw the "Drop file" screen */
|
||||||
|
SDL_Surface *drop_backround = SDL_LoadBMP(executable_relative_path("drop.bmp"));
|
||||||
|
SDL_Surface *drop_converted = SDL_ConvertSurface(drop_backround, pixel_format, 0);
|
||||||
|
SDL_LockSurface(drop_converted);
|
||||||
|
SDL_UpdateTexture(texture, NULL, drop_converted->pixels, drop_converted->pitch);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
SDL_FreeSurface(drop_converted);
|
||||||
|
SDL_FreeSurface(drop_backround);
|
||||||
|
SDL_Event event;
|
||||||
|
while (SDL_WaitEvent(&event))
|
||||||
|
{
|
||||||
|
switch (event.type) {
|
||||||
|
case SDL_QUIT: {
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
case SDL_WINDOWEVENT: {
|
||||||
|
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
|
||||||
|
update_viewport();
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SDL_DROPFILE: {
|
||||||
|
filename = event.drop.file;
|
||||||
|
should_free_filename = true;
|
||||||
|
goto start;
|
||||||
|
}
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
if (event.key.keysym.sym == SDLK_TAB) {
|
||||||
|
cycle_scaling();
|
||||||
|
}
|
||||||
|
#ifndef __APPLE__
|
||||||
|
else if (event.key.keysym.sym == SDLK_F1) {
|
||||||
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Help", help, window);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
else if (event.key.keysym.sym == SDLK_QUESTION || (event.key.keysym.sym && (event.key.keysym.mod & KMOD_SHIFT))) {
|
||||||
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Help", help, window);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
start:
|
||||||
|
run(); // Never returns
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,76 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#ifdef __APPLE__
|
||||||
|
#include <mach-o/dyld.h>
|
||||||
|
#endif
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <direct.h>
|
||||||
|
#include <windows.h>
|
||||||
|
#endif
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
const char *executable_folder(void)
|
||||||
|
{
|
||||||
|
static char path[1024] = {0,};
|
||||||
|
if (path[0]) {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
/* Ugly unportable code! :( */
|
||||||
|
#ifdef __APPLE__
|
||||||
|
unsigned int length = sizeof(path) - 1;
|
||||||
|
_NSGetExecutablePath(&path[0], &length);
|
||||||
|
#else
|
||||||
|
#ifdef __linux__
|
||||||
|
ssize_t length = readlink("/proc/self/exe", &path[0], sizeof(path) - 1);
|
||||||
|
assert (length != -1);
|
||||||
|
#else
|
||||||
|
#ifdef _WIN32
|
||||||
|
HMODULE hModule = GetModuleHandle(NULL);
|
||||||
|
GetModuleFileName(hModule, path, sizeof(path) - 1);
|
||||||
|
#else
|
||||||
|
/* No OS-specific way, assume running from CWD */
|
||||||
|
getcwd(&path[0], sizeof(path) - 1);
|
||||||
|
return path;
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
size_t pos = strlen(path);
|
||||||
|
while (pos) {
|
||||||
|
pos--;
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (path[pos] == '\\') {
|
||||||
|
#else
|
||||||
|
if (path[pos] == '/') {
|
||||||
|
#endif
|
||||||
|
path[pos] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *executable_relative_path(const char *filename)
|
||||||
|
{
|
||||||
|
static char path[1024];
|
||||||
|
snprintf(path, sizeof(path), "%s/%s", executable_folder(), filename);
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void replace_extension(const char *src, size_t length, char *dest, const char *ext)
|
||||||
|
{
|
||||||
|
memcpy(dest, src, length);
|
||||||
|
dest[length] = 0;
|
||||||
|
|
||||||
|
/* Remove extension */
|
||||||
|
for (size_t i = length; i--;) {
|
||||||
|
if (dest[i] == '/') break;
|
||||||
|
if (dest[i] == '.') {
|
||||||
|
dest[i] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add new extension */
|
||||||
|
strcat(dest, ext);
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef utils_h
|
||||||
|
#define utils_h
|
||||||
|
|
||||||
|
const char *executable_folder(void);
|
||||||
|
char *executable_relative_path(const char *filename);
|
||||||
|
void replace_extension(const char *src, size_t length, char *dest, const char *ext);
|
||||||
|
|
||||||
|
#endif /* utils_h */
|
|
@ -0,0 +1,7 @@
|
||||||
|
#pragma once
|
||||||
|
#include_next <math.h>
|
||||||
|
/* "Old" (Pre-2015) Windows headers/libc don't have round. */
|
||||||
|
static inline double round(double f)
|
||||||
|
{
|
||||||
|
return f >= 0? (int)(f + 0.5) : (int)(f - 0.5);
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
#include_next <stdint.h>
|
||||||
|
typedef intptr_t ssize_t;
|
|
@ -67,3 +67,5 @@ static inline size_t getline(char **lineptr, size_t *n, FILE *stream) {
|
||||||
|
|
||||||
return p - bufptr - 1;
|
return p - bufptr - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define snprintf _snprintf
|
|
@ -0,0 +1,3 @@
|
||||||
|
#pragma once
|
||||||
|
#include_next <string.h>
|
||||||
|
#define strdup _strdup
|
Loading…
Reference in New Issue