From 00439f4d4925d1ba60a2aaf36849df8aeb00f246 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 23 May 2017 23:18:16 +0300 Subject: [PATCH] SDL port: added drag and drop support, added model switch command, fixed macOS audio --- Makefile | 23 +++--- SDL/drop.bmp | Bin 0 -> 69176 bytes SDL/main.c | 209 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 155 insertions(+), 77 deletions(-) create mode 100644 SDL/drop.bmp diff --git a/Makefile b/Makefile index 27790322..3205ec3b 100755 --- a/Makefile +++ b/Makefile @@ -79,16 +79,16 @@ endif # Define our targets ifeq ($(PLATFORM),windows32) -SDL_TARGET := $(BIN)/sdl/sameboy.exe $(BIN)/sdl/sameboy_debugger.exe $(BIN)/sdl/SDL2.dll +SDL_TARGET := $(BIN)/SDL/sameboy.exe $(BIN)/SDL/sameboy_debugger.exe $(BIN)/SDL/SDL2.dll TESTER_TARGET := $(BIN)/tester/sameboy_tester.exe else -SDL_TARGET := $(BIN)/sdl/sameboy +SDL_TARGET := $(BIN)/SDL/sameboy TESTER_TARGET := $(BIN)/tester/sameboy_tester endif cocoa: $(BIN)/SameBoy.app 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 tester: $(TESTER_TARGET) $(BIN)/tester/dmg_boot.bin $(BIN)/tester/cgb_boot.bin all: cocoa sdl tester @@ -209,7 +209,7 @@ $(BIN)/SameBoy.qlgenerator/Contents/Resources/cgb_boot_fast.bin: $(BIN)/BootROMs # SDL Port # Unix versions build only one binary -$(BIN)/sdl/sameboy: $(CORE_OBJECTS) $(SDL_OBJECTS) +$(BIN)/SDL/sameboy: $(CORE_OBJECTS) $(SDL_OBJECTS) -@$(MKDIR) -p $(dir $@) $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) ifeq ($(CONF), release) @@ -217,11 +217,11 @@ ifeq ($(CONF), release) endif # 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 $@) $(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 $@) $(CC) $^ -o $@ $(LDFLAGS) $(SDL_LDFLAGS) -Wl,/subsystem:console @@ -235,7 +235,7 @@ $(OBJ)/%.res: %.rc # We must provide SDL2.dll with the Windows port. This is an AWFUL HACK to find it. SPACE := SPACE += -$(BIN)/sdl/SDL2.dll: +$(BIN)/SDL/SDL2.dll: @$(eval POTENTIAL_MATCHES := $(subst @@@," ",$(patsubst %,%/SDL2.dll,$(subst ;,$(SPACE),$(subst $(SPACE),@@@,$(lib)))))) @$(eval MATCH := $(shell ls $(POTENTIAL_MATCHES) 2> NUL | head -n 1)) cp "$(MATCH)" $@ @@ -253,7 +253,7 @@ $(BIN)/tester/sameboy_tester.exe: $(CORE_OBJECTS) $(SDL_OBJECTS) -@$(MKDIR) -p $(dir $@) $(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 $@) cp -f $^ $@ @@ -261,10 +261,13 @@ $(BIN)/SameBoy.app/Contents/Resources/%.bin: $(BOOTROMS_DIR)/%.bin -@$(MKDIR) -p $(dir $@) cp -f $^ $@ -$(BIN)/sdl/LICENSE: LICENSE +$(BIN)/SDL/LICENSE: LICENSE 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 $^ $@ # Boot ROMs diff --git a/SDL/drop.bmp b/SDL/drop.bmp new file mode 100644 index 0000000000000000000000000000000000000000..fbdc3748735458d782bf91e36ce42b8838b9bfb0 GIT binary patch literal 69176 zcmeI5`F9+}oxn}9kO#!JWZm~Yx<=AybRTnSW;D7-l4V`KIf9KbI02j(L%0({0Gq== zE=aV&f-$gfbFfR;HJbp2eJ{&p@?!%01KCgYcT{d>Bw4m>7|Y$S)UUd_y1VN8 zsqeSGRbA8a545g0W0~5T`K#dXLH_pecgC_z_RE%ih4ZVRe*|qlS`nuXv zB{*tps;eqXii{h_jy9Wu5c(|gkU(7fFaNw3Umj% zf?a;U*VEb2-qhIOsH-h3$Up0>GZ!z4C3y$ndOfazzMhfD;OIzXb;1VKTvuaHPEJ~qfEHOPS4TTiT0J^EJ~n!4g*B^3#zuz$-QOGbxLq{V^t7}^ zN@M9=j`9u<4M28c&FVx89*?`NwRN6SNrQy)&_I8e&x5&6PO`N4bB)yJ^&p;kWcX#B zf*I~pZ#YB&HmqB_Zte73C4ur7{mNiJopOV-{w&MmG7-kDN=&{L73I!)hcIn!YHDb3 z&c&B>K}?72c6H!ao9cJEM-!88pv#w&olVaQJKTeej0`$i;v3el>*-OE_yA&*xTibR z-rDlDuUUhpXv0oGzSC0^xw*M$A`mCX$BK)JTAG^#Xl+dm7C0WRq<|Q!?)A8{v$IZj zy`8Cl0`i4sX=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$J?nwT$VGR)53lbwlTETi+IFjE>$nT8#$@uAysM57JW zR+(qAsZ%Dz;%?DRLm6|8(-oZ!GbT(NE;XjMx7%jkDS!Ueu<>fccp;SqTvlPjK zrllS97t)Vld|2>9bG6n_e(8K^5~ASPfu^AH$Aq%>$JBt@7*mVC?eJ6r(eMGH3kk#% zh%OZ{*8Bh$`(yFdaEW|b`WsbwG2_^3+;7A2rdwC6qn>tj*hL~ezBmJVm6uw$Wm?LG zRRJ#c2XkcUuSWbO^OYXi+*#v_n}gAJL!TaBIu5hT7t@tUFJtl5a2Kz?m5n8 zBH6eQzgiIEX z52HFgTTt=l=DX1T_hcJqtN&dP$T>32%{SJk zGFxf0e`WEVL$nEzZ1GJ93@5D+7T=R5+Jv~V#Wx`^oU}q%d{3Ha6XM1e--N($(h6bm zJ!zs%h#Omc69U6YE5z~fWwi|NdSrpz7qI79dVP+07#izTjMIPbyw7RzM#Ofcw#)zm=ufeiQ*e0*aSJW_*#79C9t$p;``s? z!h!_R$73o6#kTY}zOBadbvkN~Nw5i$nPOXf7s8j%vDVd87Zv0u2)LsbgQC{esQ*Z7 z@m&aCk{+h{3+~ONAlckiR_>M$Qb)7YrpXyMlrNn44b>XvD z!gq3fj9?bjsTJ4dr#e(>F)~?7*YfczK5MXcdUD%*7?bO668;mMUPf{H!wUihw zrY?M<8HaQ8rVSUIzxn*loA?mid7C$$f?*SdrwUYr>QJf0$Yd#9%l~KRgF+a$i@tH; zwu`r((t#>a5vs#SkCzgo#nk01U-?R{y_#>nng8vS`EyC9N?iU`78Rj7RBACYv97fw zU`fD|fF%J-0+s|U30M-aBw$Ivl7J-vO9GYzED2Z=uq0qfz> 3) { -usage: - fprintf(stderr, "Usage: %s [--dmg] rom\n", argv[0]); - exit(1); +restart: + if (GB_is_inited(&gb)) { + GB_switch_model_and_reset(&gb, !dmg); } - - if (argc == 3) { - if (strcmp(argv[1], "--dmg") == 0) { - dmg = true; + else { + if (dmg) { + GB_init(&gb); } else { - goto usage; + GB_init_cgb(&gb); } + + GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); + GB_set_pixels_output(&gb, pixels); + GB_set_rgb_encode_callback(&gb, rgb_encode); + GB_set_sample_rate(&gb, have_aspec.freq); } - - + if (dmg) { - GB_init(&gb); if (GB_load_boot_rom(&gb, executable_relative_path("dmg_boot.bin"))) { perror("Failed to load boot ROM"); exit(1); } } else { - 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 ); - - window = SDL_CreateWindow("SameBoy v" xstr(VERSION), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - 160, 144, SDL_WINDOW_OPENGL); - 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 Screen */ - GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank); - GB_set_pixels_output(&gb, pixels); - GB_set_rgb_encode_callback(&gb, rgb_encode); - size_t path_length = strlen(filename); - + /* Configure battery */ 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"); battery_save_path_ptr = battery_save_path; GB_load_battery(&gb, battery_save_path); - + /* Configure symbols */ GB_debugger_load_symbol_file(&gb, executable_relative_path("registers.sym")); char symbols_path[path_length + 5]; replace_extension(filename, path_length, symbols_path, ".sym"); 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, have.freq); - /* Start Audio */ - SDL_PauseAudio(false); - /* Run emulation */ while (true) { GB_run(&gb); @@ -362,7 +345,7 @@ usage: else { GB_save_state(&gb, save_path); } - break; + break; } case GB_SDL_RESET_COMMAND: @@ -371,8 +354,100 @@ usage: case GB_SDL_NO_COMMAND: break; + + case GB_SDL_NEW_FILE_COMMAND: + pending_command = GB_SDL_NO_COMMAND; + goto restart; + + case GB_SDL_TOGGLE_MODEL_COMMAND: + dmg = !dmg; + pending_command = GB_SDL_NO_COMMAND; + goto restart; } 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, 144, SDL_WINDOW_OPENGL); + 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; + want_aspec.samples = 2048; + 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)) + { + if (event.type == SDL_QUIT) { + exit(0); + } + else if (event.type == SDL_DROPFILE) { + filename = event.drop.file; + should_free_filename = true; + break; + } + } + } + + run(); // Never returns + return 0; +}