2016-03-30 20:07:55 +00:00
|
|
|
#include <stdbool.h>
|
2018-03-28 18:59:27 +00:00
|
|
|
#include <stdio.h>
|
2016-07-17 19:43:23 +00:00
|
|
|
#include <signal.h>
|
2020-04-25 19:48:48 +00:00
|
|
|
#include <ctype.h>
|
2021-05-06 21:33:04 +00:00
|
|
|
#include <errno.h>
|
2019-06-01 11:29:46 +00:00
|
|
|
#include <OpenDialog/open_dialog.h>
|
2019-06-26 03:01:54 +00:00
|
|
|
#include <SDL.h>
|
2017-10-12 21:02:02 +00:00
|
|
|
#include <Core/gb.h>
|
2017-05-24 18:44:43 +00:00
|
|
|
#include "utils.h"
|
2017-10-04 09:43:31 +00:00
|
|
|
#include "gui.h"
|
2017-12-23 15:29:42 +00:00
|
|
|
#include "shader.h"
|
2020-04-27 18:12:30 +00:00
|
|
|
#include "audio/audio.h"
|
2021-09-08 21:13:09 +00:00
|
|
|
#include "console.h"
|
2018-06-26 16:36:14 +00:00
|
|
|
|
2016-08-20 14:51:17 +00:00
|
|
|
#ifndef _WIN32
|
2020-03-28 19:56:19 +00:00
|
|
|
#include <unistd.h>
|
2016-08-20 14:51:17 +00:00
|
|
|
#else
|
2020-01-02 09:50:55 +00:00
|
|
|
#include <Windows.h>
|
2019-07-16 20:14:26 +00:00
|
|
|
#endif
|
|
|
|
|
2021-06-25 14:12:05 +00:00
|
|
|
static bool stop_on_start = false;
|
2017-05-24 18:44:43 +00:00
|
|
|
GB_gameboy_t gb;
|
|
|
|
static bool paused = false;
|
2019-05-18 15:45:31 +00:00
|
|
|
static uint32_t pixel_buffer_1[256 * 224], pixel_buffer_2[256 * 224];
|
2017-12-23 15:41:47 +00:00
|
|
|
static uint32_t *active_pixel_buffer = pixel_buffer_1, *previous_pixel_buffer = pixel_buffer_2;
|
2018-06-22 21:10:28 +00:00
|
|
|
static bool underclock_down = false, rewind_down = false, do_rewind = false, rewind_paused = false, turbo_down = false;
|
|
|
|
static double clock_mutliplier = 1.0;
|
2017-12-23 15:41:47 +00:00
|
|
|
|
2017-05-23 20:18:16 +00:00
|
|
|
static char *filename = NULL;
|
2019-06-01 11:29:46 +00:00
|
|
|
static typeof(free) *free_function = NULL;
|
2021-05-05 21:23:46 +00:00
|
|
|
static char *battery_save_path_ptr = NULL;
|
|
|
|
static SDL_GLContext gl_context = NULL;
|
2021-09-08 21:13:09 +00:00
|
|
|
static bool console_supported = false;
|
2017-05-21 18:44:28 +00:00
|
|
|
|
2021-05-05 21:23:46 +00:00
|
|
|
bool uses_gl(void)
|
|
|
|
{
|
|
|
|
return gl_context;
|
|
|
|
}
|
2018-04-22 13:22:10 +00:00
|
|
|
|
2019-06-01 11:29:46 +00:00
|
|
|
void set_filename(const char *new_filename, typeof(free) *new_free_function)
|
2017-10-04 09:43:31 +00:00
|
|
|
{
|
2019-06-01 11:29:46 +00:00
|
|
|
if (filename && free_function) {
|
|
|
|
free_function(filename);
|
2017-10-04 09:43:31 +00:00
|
|
|
}
|
|
|
|
filename = (char *) new_filename;
|
2019-06-01 11:29:46 +00:00
|
|
|
free_function = new_free_function;
|
2017-10-04 09:43:31 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 21:13:09 +00:00
|
|
|
static char *completer(const char *substring, uintptr_t *context)
|
|
|
|
{
|
2021-09-10 11:17:07 +00:00
|
|
|
if (!GB_is_inited(&gb)) return NULL;
|
2021-09-08 21:13:09 +00:00
|
|
|
char *temp = strdup(substring);
|
|
|
|
char *ret = GB_debugger_complete_substring(&gb, temp, context);
|
|
|
|
free(temp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void log_callback(GB_gameboy_t *gb, const char *string, GB_log_attributes attributes)
|
|
|
|
{
|
|
|
|
CON_attributes_t con_attributes = {0,};
|
|
|
|
con_attributes.bold = attributes & GB_LOG_BOLD;
|
|
|
|
con_attributes.underline = attributes & GB_LOG_UNDERLINE;
|
|
|
|
if (attributes & GB_LOG_DASHED_UNDERLINE) {
|
|
|
|
while (*string) {
|
|
|
|
con_attributes.underline ^= true;
|
|
|
|
CON_attributed_printf("%c", &con_attributes, *string);
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CON_attributed_print(string, &con_attributes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_eof(void)
|
|
|
|
{
|
|
|
|
CON_set_async_prompt("");
|
|
|
|
char *line = CON_readline("Quit? [y]/n > ");
|
|
|
|
if (line[0] == 'n' || line[0] == 'N') {
|
|
|
|
free(line);
|
|
|
|
CON_set_async_prompt("> ");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *input_callback(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
retry: {
|
|
|
|
char *ret = CON_readline("Stopped> ");
|
|
|
|
if (strcmp(ret, CON_EOF) == 0) {
|
|
|
|
handle_eof();
|
|
|
|
free(ret);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CON_attributes_t attributes = {.bold = true};
|
|
|
|
CON_attributed_printf("> %s\n", &attributes, ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *asyc_input_callback(GB_gameboy_t *gb)
|
|
|
|
{
|
|
|
|
retry: {
|
|
|
|
char *ret = CON_readline_async();
|
|
|
|
if (ret && strcmp(ret, CON_EOF) == 0) {
|
|
|
|
handle_eof();
|
|
|
|
free(ret);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
else if (ret) {
|
|
|
|
CON_attributes_t attributes = {.bold = true};
|
|
|
|
CON_attributed_printf("> %s\n", &attributes, ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-25 20:28:48 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-05-06 21:33:04 +00:00
|
|
|
static const char *end_capturing_logs(bool show_popup, bool should_exit, uint32_t popup_flags, const char *title)
|
2017-05-25 20:28:48 +00:00
|
|
|
{
|
2021-09-08 21:13:09 +00:00
|
|
|
GB_set_log_callback(&gb, console_supported? log_callback : NULL);
|
2017-05-25 20:28:48 +00:00
|
|
|
if (captured_log[0] == 0) {
|
|
|
|
free(captured_log);
|
|
|
|
captured_log = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (show_popup) {
|
2021-05-06 21:33:04 +00:00
|
|
|
SDL_ShowSimpleMessageBox(popup_flags, title, captured_log, window);
|
2017-05-25 20:28:48 +00:00
|
|
|
}
|
|
|
|
if (should_exit) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return captured_log;
|
|
|
|
}
|
|
|
|
|
2020-01-29 13:36:19 +00:00
|
|
|
static void update_palette(void)
|
|
|
|
{
|
|
|
|
switch (configuration.dmg_palette) {
|
|
|
|
case 1:
|
|
|
|
GB_set_palette(&gb, &GB_PALETTE_DMG);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
GB_set_palette(&gb, &GB_PALETTE_MGB);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
GB_set_palette(&gb, &GB_PALETTE_GBL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
GB_set_palette(&gb, &GB_PALETTE_GREY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 11:28:46 +00:00
|
|
|
static void screen_size_changed(void)
|
|
|
|
{
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
texture = SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), SDL_TEXTUREACCESS_STREAMING,
|
|
|
|
GB_get_screen_width(&gb), GB_get_screen_height(&gb));
|
|
|
|
|
|
|
|
SDL_SetWindowMinimumSize(window, GB_get_screen_width(&gb), GB_get_screen_height(&gb));
|
|
|
|
|
|
|
|
update_viewport();
|
|
|
|
}
|
|
|
|
|
2018-06-23 19:27:05 +00:00
|
|
|
static void open_menu(void)
|
|
|
|
{
|
2020-04-27 18:12:30 +00:00
|
|
|
bool audio_playing = GB_audio_is_playing();
|
2018-06-23 19:27:05 +00:00
|
|
|
if (audio_playing) {
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_audio_set_paused(true);
|
2018-06-23 19:27:05 +00:00
|
|
|
}
|
2020-02-08 11:28:46 +00:00
|
|
|
size_t previous_width = GB_get_screen_width(&gb);
|
2018-06-23 19:27:05 +00:00
|
|
|
run_gui(true);
|
2019-11-03 03:43:25 +00:00
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2018-06-23 19:27:05 +00:00
|
|
|
if (audio_playing) {
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_audio_set_paused(false);
|
2018-06-23 19:27:05 +00:00
|
|
|
}
|
|
|
|
GB_set_color_correction_mode(&gb, configuration.color_correction_mode);
|
2020-12-25 12:14:17 +00:00
|
|
|
GB_set_light_temperature(&gb, (configuration.color_temperature - 10.0) / 10.0);
|
2020-12-30 22:06:36 +00:00
|
|
|
GB_set_interference_volume(&gb, configuration.interference_volume / 100.0);
|
2020-02-08 11:28:46 +00:00
|
|
|
GB_set_border_mode(&gb, configuration.border_mode);
|
2020-01-29 13:36:19 +00:00
|
|
|
update_palette();
|
2018-06-23 19:27:05 +00:00
|
|
|
GB_set_highpass_filter_mode(&gb, configuration.highpass_mode);
|
2021-02-25 23:04:24 +00:00
|
|
|
GB_set_rewind_length(&gb, configuration.rewind_length);
|
|
|
|
GB_set_rtc_mode(&gb, configuration.rtc_mode);
|
2020-02-08 11:28:46 +00:00
|
|
|
if (previous_width != GB_get_screen_width(&gb)) {
|
|
|
|
screen_size_changed();
|
|
|
|
}
|
2018-06-23 19:27:05 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 18:44:28 +00:00
|
|
|
static void handle_events(GB_gameboy_t *gb)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
2021-05-30 20:39:59 +00:00
|
|
|
while (SDL_PollEvent(&event)) {
|
2017-05-24 17:33:39 +00:00
|
|
|
switch (event.type) {
|
2016-03-30 20:07:55 +00:00
|
|
|
case SDL_QUIT:
|
2017-10-14 11:10:26 +00:00
|
|
|
pending_command = GB_SDL_QUIT_COMMAND;
|
|
|
|
break;
|
2017-05-21 18:44:28 +00:00
|
|
|
|
2017-05-23 20:18:16 +00:00
|
|
|
case SDL_DROPFILE: {
|
2021-04-14 12:20:01 +00:00
|
|
|
if (GB_is_stave_state(event.drop.file)) {
|
|
|
|
dropped_state_file = event.drop.file;
|
|
|
|
pending_command = GB_SDL_LOAD_STATE_FROM_FILE_COMMAND;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set_filename(event.drop.file, SDL_free);
|
|
|
|
pending_command = GB_SDL_NEW_FILE_COMMAND;
|
|
|
|
}
|
2017-05-23 20:18:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-24 17:33:39 +00:00
|
|
|
case SDL_WINDOWEVENT: {
|
2019-11-03 03:31:23 +00:00
|
|
|
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
|
2017-05-24 17:33:39 +00:00
|
|
|
update_viewport();
|
|
|
|
}
|
2017-12-22 22:39:04 +00:00
|
|
|
break;
|
2017-05-24 17:33:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-22 22:39:04 +00:00
|
|
|
case SDL_JOYBUTTONUP:
|
2018-06-23 19:27:05 +00:00
|
|
|
case SDL_JOYBUTTONDOWN: {
|
|
|
|
joypad_button_t button = get_joypad_button(event.jbutton.button);
|
|
|
|
if ((GB_key_t) button < GB_KEY_MAX) {
|
|
|
|
GB_set_key_state(gb, (GB_key_t) button, event.type == SDL_JOYBUTTONDOWN);
|
2017-12-28 18:22:54 +00:00
|
|
|
}
|
2018-06-23 19:27:05 +00:00
|
|
|
else if (button == JOYPAD_BUTTON_TURBO) {
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_audio_clear_queue();
|
2018-06-22 21:10:28 +00:00
|
|
|
turbo_down = event.type == SDL_JOYBUTTONDOWN;
|
|
|
|
GB_set_turbo_mode(gb, turbo_down, turbo_down && rewind_down);
|
2017-12-22 22:39:04 +00:00
|
|
|
}
|
2018-06-23 19:27:05 +00:00
|
|
|
else if (button == JOYPAD_BUTTON_SLOW_MOTION) {
|
|
|
|
underclock_down = event.type == SDL_JOYBUTTONDOWN;
|
|
|
|
}
|
|
|
|
else if (button == JOYPAD_BUTTON_REWIND) {
|
|
|
|
rewind_down = event.type == SDL_JOYBUTTONDOWN;
|
|
|
|
if (event.type == SDL_JOYBUTTONUP) {
|
|
|
|
rewind_paused = false;
|
2017-12-23 19:11:44 +00:00
|
|
|
}
|
2018-06-23 19:27:05 +00:00
|
|
|
GB_set_turbo_mode(gb, turbo_down, turbo_down && rewind_down);
|
|
|
|
}
|
|
|
|
else if (button == JOYPAD_BUTTON_MENU && event.type == SDL_JOYBUTTONDOWN) {
|
|
|
|
open_menu();
|
2017-12-22 22:39:04 +00:00
|
|
|
}
|
2018-06-23 19:27:05 +00:00
|
|
|
}
|
2021-05-30 20:39:59 +00:00
|
|
|
break;
|
2017-12-22 22:39:04 +00:00
|
|
|
|
2018-06-23 19:27:05 +00:00
|
|
|
case SDL_JOYAXISMOTION: {
|
2018-06-26 16:36:14 +00:00
|
|
|
static bool axis_active[2] = {false, false};
|
2018-06-23 19:27:05 +00:00
|
|
|
joypad_axis_t axis = get_joypad_axis(event.jaxis.axis);
|
2018-06-26 16:36:14 +00:00
|
|
|
if (axis == JOYPAD_AXISES_X) {
|
|
|
|
if (event.jaxis.value > JOYSTICK_HIGH) {
|
|
|
|
axis_active[0] = true;
|
|
|
|
GB_set_key_state(gb, GB_KEY_RIGHT, true);
|
|
|
|
GB_set_key_state(gb, GB_KEY_LEFT, false);
|
|
|
|
}
|
|
|
|
else if (event.jaxis.value < -JOYSTICK_HIGH) {
|
|
|
|
axis_active[0] = true;
|
|
|
|
GB_set_key_state(gb, GB_KEY_RIGHT, false);
|
|
|
|
GB_set_key_state(gb, GB_KEY_LEFT, true);
|
|
|
|
}
|
|
|
|
else if (axis_active[0] && event.jaxis.value < JOYSTICK_LOW && event.jaxis.value > -JOYSTICK_LOW) {
|
|
|
|
axis_active[0] = false;
|
|
|
|
GB_set_key_state(gb, GB_KEY_RIGHT, false);
|
|
|
|
GB_set_key_state(gb, GB_KEY_LEFT, false);
|
|
|
|
}
|
2017-12-22 22:39:04 +00:00
|
|
|
}
|
2018-06-26 16:36:14 +00:00
|
|
|
else if (axis == JOYPAD_AXISES_Y) {
|
|
|
|
if (event.jaxis.value > JOYSTICK_HIGH) {
|
|
|
|
axis_active[1] = true;
|
|
|
|
GB_set_key_state(gb, GB_KEY_DOWN, true);
|
|
|
|
GB_set_key_state(gb, GB_KEY_UP, false);
|
|
|
|
}
|
|
|
|
else if (event.jaxis.value < -JOYSTICK_HIGH) {
|
|
|
|
axis_active[1] = true;
|
|
|
|
GB_set_key_state(gb, GB_KEY_DOWN, false);
|
|
|
|
GB_set_key_state(gb, GB_KEY_UP, true);
|
|
|
|
}
|
|
|
|
else if (axis_active[1] && event.jaxis.value < JOYSTICK_LOW && event.jaxis.value > -JOYSTICK_LOW) {
|
|
|
|
axis_active[1] = false;
|
|
|
|
GB_set_key_state(gb, GB_KEY_DOWN, false);
|
|
|
|
GB_set_key_state(gb, GB_KEY_UP, false);
|
|
|
|
}
|
2017-12-22 22:39:04 +00:00
|
|
|
}
|
2018-06-23 19:27:05 +00:00
|
|
|
}
|
2021-05-30 20:39:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_JOYHATMOTION: {
|
2018-06-02 22:21:43 +00:00
|
|
|
uint8_t value = event.jhat.value;
|
|
|
|
int8_t updown =
|
2021-05-30 20:39:59 +00:00
|
|
|
value == SDL_HAT_LEFTUP || value == SDL_HAT_UP || value == SDL_HAT_RIGHTUP ? -1 : (value == SDL_HAT_LEFTDOWN || value == SDL_HAT_DOWN || value == SDL_HAT_RIGHTDOWN ? 1 : 0);
|
2018-06-02 22:21:43 +00:00
|
|
|
int8_t leftright =
|
2021-05-30 20:39:59 +00:00
|
|
|
value == SDL_HAT_LEFTUP || value == SDL_HAT_LEFT || value == SDL_HAT_LEFTDOWN ? -1 : (value == SDL_HAT_RIGHTUP || value == SDL_HAT_RIGHT || value == SDL_HAT_RIGHTDOWN ? 1 : 0);
|
2018-06-02 22:21:43 +00:00
|
|
|
|
|
|
|
GB_set_key_state(gb, GB_KEY_LEFT, leftright == -1);
|
|
|
|
GB_set_key_state(gb, GB_KEY_RIGHT, leftright == 1);
|
|
|
|
GB_set_key_state(gb, GB_KEY_UP, updown == -1);
|
|
|
|
GB_set_key_state(gb, GB_KEY_DOWN, updown == 1);
|
|
|
|
break;
|
2021-05-30 20:39:59 +00:00
|
|
|
};
|
2017-12-22 22:39:04 +00:00
|
|
|
|
2016-03-30 20:07:55 +00:00
|
|
|
case SDL_KEYDOWN:
|
2020-12-23 21:50:19 +00:00
|
|
|
switch (event_hotkey_code(&event)) {
|
2017-12-23 19:11:44 +00:00
|
|
|
case SDL_SCANCODE_ESCAPE: {
|
2018-06-23 19:27:05 +00:00
|
|
|
open_menu();
|
2017-10-13 22:41:51 +00:00
|
|
|
break;
|
2017-12-23 19:11:44 +00:00
|
|
|
}
|
2017-10-14 17:24:13 +00:00
|
|
|
case SDL_SCANCODE_C:
|
2017-05-25 20:28:48 +00:00
|
|
|
if (event.type == SDL_KEYDOWN && (event.key.keysym.mod & KMOD_CTRL)) {
|
2021-09-08 21:13:09 +00:00
|
|
|
CON_print("^C\a\n");
|
2017-05-21 18:44:28 +00:00
|
|
|
GB_debugger_break(gb);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-10-14 17:24:13 +00:00
|
|
|
case SDL_SCANCODE_R:
|
2017-05-25 20:28:48 +00:00
|
|
|
if (event.key.keysym.mod & MODIFIER) {
|
2017-05-21 18:44:28 +00:00
|
|
|
pending_command = GB_SDL_RESET_COMMAND;
|
|
|
|
}
|
|
|
|
break;
|
2019-06-01 11:29:46 +00:00
|
|
|
|
|
|
|
case SDL_SCANCODE_O: {
|
|
|
|
if (event.key.keysym.mod & MODIFIER) {
|
|
|
|
char *filename = do_open_rom_dialog();
|
|
|
|
if (filename) {
|
|
|
|
set_filename(filename, free);
|
|
|
|
pending_command = GB_SDL_NEW_FILE_COMMAND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-05-30 20:39:59 +00:00
|
|
|
|
2017-10-14 17:24:13 +00:00
|
|
|
case SDL_SCANCODE_P:
|
2017-05-25 20:28:48 +00:00
|
|
|
if (event.key.keysym.mod & MODIFIER) {
|
2017-05-24 17:33:39 +00:00
|
|
|
paused = !paused;
|
|
|
|
}
|
|
|
|
break;
|
2017-10-14 17:24:13 +00:00
|
|
|
case SDL_SCANCODE_M:
|
2017-05-25 20:28:48 +00:00
|
|
|
if (event.key.keysym.mod & MODIFIER) {
|
2017-05-21 18:44:28 +00:00
|
|
|
#ifdef __APPLE__
|
2017-05-23 20:18:16 +00:00
|
|
|
// Can't override CMD+M (Minimize) in SDL
|
2017-05-25 20:28:48 +00:00
|
|
|
if (!(event.key.keysym.mod & KMOD_SHIFT)) {
|
2017-05-21 18:44:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_audio_set_paused(GB_audio_is_playing());
|
2018-04-22 13:22:10 +00:00
|
|
|
}
|
2021-05-30 20:39:59 +00:00
|
|
|
break;
|
|
|
|
|
2018-04-18 17:22:08 +00:00
|
|
|
case SDL_SCANCODE_F:
|
|
|
|
if (event.key.keysym.mod & MODIFIER) {
|
|
|
|
if ((SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN_DESKTOP) == false) {
|
|
|
|
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
|
|
|
}
|
2021-05-30 20:39:59 +00:00
|
|
|
else {
|
2018-04-18 17:22:08 +00:00
|
|
|
SDL_SetWindowFullscreen(window, 0);
|
|
|
|
}
|
2019-12-31 22:17:54 +00:00
|
|
|
update_viewport();
|
2018-04-18 17:22:08 +00:00
|
|
|
}
|
|
|
|
break;
|
2017-05-21 18:44:28 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* Save states */
|
2018-04-19 12:40:42 +00:00
|
|
|
if (event.key.keysym.scancode >= SDL_SCANCODE_1 && event.key.keysym.scancode <= SDL_SCANCODE_0) {
|
2017-05-25 20:28:48 +00:00
|
|
|
if (event.key.keysym.mod & MODIFIER) {
|
2018-05-11 09:51:15 +00:00
|
|
|
command_parameter = (event.key.keysym.scancode - SDL_SCANCODE_1 + 1) % 10;
|
2017-05-21 18:44:28 +00:00
|
|
|
|
2017-05-25 20:28:48 +00:00
|
|
|
if (event.key.keysym.mod & KMOD_SHIFT) {
|
2017-05-21 18:44:28 +00:00
|
|
|
pending_command = GB_SDL_LOAD_STATE_COMMAND;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pending_command = GB_SDL_SAVE_STATE_COMMAND;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SDL_KEYUP: // Fallthrough
|
2017-12-22 20:25:53 +00:00
|
|
|
if (event.key.keysym.scancode == configuration.keys[8]) {
|
2018-06-22 21:10:28 +00:00
|
|
|
turbo_down = event.type == SDL_KEYDOWN;
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_audio_clear_queue();
|
2018-06-22 21:10:28 +00:00
|
|
|
GB_set_turbo_mode(gb, turbo_down, turbo_down && rewind_down);
|
|
|
|
}
|
|
|
|
else if (event.key.keysym.scancode == configuration.keys_2[0]) {
|
|
|
|
rewind_down = event.type == SDL_KEYDOWN;
|
|
|
|
if (event.type == SDL_KEYUP) {
|
|
|
|
rewind_paused = false;
|
|
|
|
}
|
|
|
|
GB_set_turbo_mode(gb, turbo_down, turbo_down && rewind_down);
|
|
|
|
}
|
|
|
|
else if (event.key.keysym.scancode == configuration.keys_2[1]) {
|
|
|
|
underclock_down = event.type == SDL_KEYDOWN;
|
2017-12-22 20:25:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (unsigned i = 0; i < GB_KEY_MAX; i++) {
|
|
|
|
if (event.key.keysym.scancode == configuration.keys[i]) {
|
|
|
|
GB_set_key_state(gb, i, event.type == SDL_KEYDOWN);
|
|
|
|
}
|
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 20:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
|
|
|
|
{
|
|
|
|
return SDL_MapRGB(pixel_format, r, g, b);
|
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2016-08-20 14:51:17 +00:00
|
|
|
static void vblank(GB_gameboy_t *gb)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
2018-06-22 21:10:28 +00:00
|
|
|
if (underclock_down && clock_mutliplier > 0.5) {
|
2019-06-19 19:25:59 +00:00
|
|
|
clock_mutliplier -= 1.0/16;
|
2018-06-22 21:10:28 +00:00
|
|
|
GB_set_clock_multiplier(gb, clock_mutliplier);
|
|
|
|
}
|
|
|
|
else if (!underclock_down && clock_mutliplier < 1.0) {
|
2019-06-19 19:25:59 +00:00
|
|
|
clock_mutliplier += 1.0/16;
|
2018-06-22 21:10:28 +00:00
|
|
|
GB_set_clock_multiplier(gb, clock_mutliplier);
|
|
|
|
}
|
2021-05-30 20:39:59 +00:00
|
|
|
|
|
|
|
if (turbo_down) {
|
|
|
|
show_osd_text("Fast forward...");
|
|
|
|
}
|
|
|
|
else if (underclock_down) {
|
|
|
|
show_osd_text("Slow motion...");
|
|
|
|
}
|
|
|
|
else if (rewind_down) {
|
|
|
|
show_osd_text("Rewinding...");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (osd_countdown && configuration.osd) {
|
|
|
|
unsigned width = GB_get_screen_width(gb);
|
|
|
|
unsigned height = GB_get_screen_height(gb);
|
|
|
|
draw_text(active_pixel_buffer,
|
|
|
|
width, height, 8, height - 8 - osd_text_lines * 12, osd_text,
|
|
|
|
rgb_encode(gb, 255, 255, 255), rgb_encode(gb, 0, 0, 0),
|
|
|
|
true);
|
|
|
|
osd_countdown--;
|
|
|
|
}
|
2020-03-26 18:54:18 +00:00
|
|
|
if (configuration.blending_mode) {
|
2017-12-23 15:41:47 +00:00
|
|
|
render_texture(active_pixel_buffer, previous_pixel_buffer);
|
|
|
|
uint32_t *temp = active_pixel_buffer;
|
|
|
|
active_pixel_buffer = previous_pixel_buffer;
|
|
|
|
previous_pixel_buffer = temp;
|
|
|
|
GB_set_pixels_output(gb, active_pixel_buffer);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
render_texture(active_pixel_buffer, NULL);
|
|
|
|
}
|
2018-06-22 21:10:28 +00:00
|
|
|
do_rewind = rewind_down;
|
2017-05-21 18:44:28 +00:00
|
|
|
handle_events(gb);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
|
2020-05-24 20:04:36 +00:00
|
|
|
static void rumble(GB_gameboy_t *gb, double amp)
|
|
|
|
{
|
|
|
|
SDL_HapticRumblePlay(haptic, amp, 250);
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:58:30 +00:00
|
|
|
static void debugger_interrupt(int ignore)
|
|
|
|
{
|
2021-09-08 21:13:09 +00:00
|
|
|
if (!GB_is_inited(&gb)) exit(0);
|
2016-07-18 10:10:19 +00:00
|
|
|
/* ^C twice to exit */
|
2017-04-17 17:16:17 +00:00
|
|
|
if (GB_debugger_is_stopped(&gb)) {
|
2017-10-14 11:10:26 +00:00
|
|
|
GB_save_battery(&gb, battery_save_path_ptr);
|
2016-07-18 10:10:19 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2021-09-08 21:13:09 +00:00
|
|
|
if (console_supported) {
|
|
|
|
CON_print("^C\n");
|
|
|
|
}
|
2017-04-17 17:16:17 +00:00
|
|
|
GB_debugger_break(&gb);
|
2016-04-06 19:58:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 20:22:27 +00:00
|
|
|
static void gb_audio_callback(GB_gameboy_t *gb, GB_sample_t *sample)
|
2019-07-11 22:53:06 +00:00
|
|
|
{
|
|
|
|
if (turbo_down) {
|
|
|
|
static unsigned skip = 0;
|
|
|
|
skip++;
|
2020-04-27 18:12:30 +00:00
|
|
|
if (skip == GB_audio_get_frequency() / 8) {
|
2019-07-11 22:53:06 +00:00
|
|
|
skip = 0;
|
|
|
|
}
|
2020-04-27 18:12:30 +00:00
|
|
|
if (skip > GB_audio_get_frequency() / 16) {
|
2019-07-11 22:53:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:12:30 +00:00
|
|
|
if (GB_audio_get_queue_length() / sizeof(*sample) > GB_audio_get_frequency() / 4) {
|
2019-06-22 16:01:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-07-11 22:53:06 +00:00
|
|
|
|
2020-03-27 16:10:42 +00:00
|
|
|
if (configuration.volume != 100) {
|
|
|
|
sample->left = sample->left * configuration.volume / 100;
|
|
|
|
sample->right = sample->right * configuration.volume / 100;
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_audio_queue_sample(sample);
|
2019-07-11 22:53:06 +00:00
|
|
|
|
2016-05-23 19:22:09 +00:00
|
|
|
}
|
2017-05-24 18:44:43 +00:00
|
|
|
|
|
|
|
static bool handle_pending_command(void)
|
2016-07-17 20:08:07 +00:00
|
|
|
{
|
2017-05-24 18:44:43 +00:00
|
|
|
switch (pending_command) {
|
|
|
|
case GB_SDL_LOAD_STATE_COMMAND:
|
|
|
|
case GB_SDL_SAVE_STATE_COMMAND: {
|
2021-05-06 21:33:04 +00:00
|
|
|
char save_path[strlen(filename) + 5];
|
2017-05-24 18:44:43 +00:00
|
|
|
char save_extension[] = ".s0";
|
|
|
|
save_extension[2] += command_parameter;
|
|
|
|
replace_extension(filename, strlen(filename), save_path, save_extension);
|
|
|
|
|
2017-05-25 20:28:48 +00:00
|
|
|
start_capturing_logs();
|
2021-04-14 23:42:31 +00:00
|
|
|
bool success;
|
2017-05-24 18:44:43 +00:00
|
|
|
if (pending_command == GB_SDL_LOAD_STATE_COMMAND) {
|
2021-05-06 21:33:04 +00:00
|
|
|
int result = GB_load_state(&gb, save_path);
|
|
|
|
if (result == ENOENT) {
|
|
|
|
char save_extension[] = ".sn0";
|
|
|
|
save_extension[3] += command_parameter;
|
|
|
|
replace_extension(filename, strlen(filename), save_path, save_extension);
|
|
|
|
start_capturing_logs();
|
|
|
|
result = GB_load_state(&gb, save_path);
|
|
|
|
}
|
|
|
|
success = result == 0;
|
2017-05-24 18:44:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2021-04-14 23:42:31 +00:00
|
|
|
success = GB_save_state(&gb, save_path) == 0;
|
2017-05-24 18:44:43 +00:00
|
|
|
}
|
2021-05-06 21:33:04 +00:00
|
|
|
end_capturing_logs(true,
|
|
|
|
false,
|
|
|
|
success? SDL_MESSAGEBOX_INFORMATION : SDL_MESSAGEBOX_ERROR,
|
|
|
|
success? "Notice" : "Error");
|
2021-05-30 20:39:59 +00:00
|
|
|
if (success) {
|
|
|
|
show_osd_text(pending_command == GB_SDL_LOAD_STATE_COMMAND? "State loaded" : "State saved");
|
|
|
|
}
|
2017-05-24 18:44:43 +00:00
|
|
|
return false;
|
2016-07-17 20:08:07 +00:00
|
|
|
}
|
2021-04-14 12:20:01 +00:00
|
|
|
|
|
|
|
case GB_SDL_LOAD_STATE_FROM_FILE_COMMAND:
|
|
|
|
start_capturing_logs();
|
2021-04-14 23:42:31 +00:00
|
|
|
bool success = GB_load_state(&gb, dropped_state_file) == 0;
|
2021-05-06 21:33:04 +00:00
|
|
|
end_capturing_logs(true,
|
|
|
|
false,
|
|
|
|
success? SDL_MESSAGEBOX_INFORMATION : SDL_MESSAGEBOX_ERROR,
|
|
|
|
success? "Notice" : "Error");
|
2021-04-14 12:20:01 +00:00
|
|
|
SDL_free(dropped_state_file);
|
2021-05-30 20:39:59 +00:00
|
|
|
if (success) {
|
|
|
|
show_osd_text("State loaded");
|
|
|
|
}
|
2021-04-14 12:20:01 +00:00
|
|
|
return false;
|
2017-05-24 18:44:43 +00:00
|
|
|
|
|
|
|
case GB_SDL_NO_COMMAND:
|
|
|
|
return false;
|
|
|
|
|
2019-11-03 03:50:29 +00:00
|
|
|
case GB_SDL_RESET_COMMAND:
|
2017-05-24 18:44:43 +00:00
|
|
|
case GB_SDL_NEW_FILE_COMMAND:
|
2019-11-03 03:50:29 +00:00
|
|
|
GB_save_battery(&gb, battery_save_path_ptr);
|
2017-05-24 18:44:43 +00:00
|
|
|
return true;
|
|
|
|
|
2017-10-14 11:10:26 +00:00
|
|
|
case GB_SDL_QUIT_COMMAND:
|
|
|
|
GB_save_battery(&gb, battery_save_path_ptr);
|
|
|
|
exit(0);
|
2016-07-17 20:08:07 +00:00
|
|
|
}
|
2017-05-24 18:44:43 +00:00
|
|
|
return false;
|
2016-07-17 20:08:07 +00:00
|
|
|
}
|
2017-05-24 18:44:43 +00:00
|
|
|
|
2020-01-29 18:29:30 +00:00
|
|
|
static void load_boot_rom(GB_gameboy_t *gb, GB_boot_rom_t type)
|
|
|
|
{
|
|
|
|
static const char *const names[] = {
|
|
|
|
[GB_BOOT_ROM_DMG0] = "dmg0_boot.bin",
|
|
|
|
[GB_BOOT_ROM_DMG] = "dmg_boot.bin",
|
|
|
|
[GB_BOOT_ROM_MGB] = "mgb_boot.bin",
|
|
|
|
[GB_BOOT_ROM_SGB] = "sgb_boot.bin",
|
|
|
|
[GB_BOOT_ROM_SGB2] = "sgb2_boot.bin",
|
|
|
|
[GB_BOOT_ROM_CGB0] = "cgb0_boot.bin",
|
|
|
|
[GB_BOOT_ROM_CGB] = "cgb_boot.bin",
|
|
|
|
[GB_BOOT_ROM_AGB] = "agb_boot.bin",
|
|
|
|
};
|
2020-12-26 13:10:11 +00:00
|
|
|
bool use_built_in = true;
|
|
|
|
if (configuration.bootrom_path[0]) {
|
|
|
|
static char path[4096];
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", configuration.bootrom_path, names[type]);
|
|
|
|
use_built_in = GB_load_boot_rom(gb, path);
|
|
|
|
}
|
|
|
|
if (use_built_in) {
|
|
|
|
start_capturing_logs();
|
|
|
|
GB_load_boot_rom(gb, resource_path(names[type]));
|
2021-05-06 21:33:04 +00:00
|
|
|
end_capturing_logs(true, false, SDL_MESSAGEBOX_ERROR, "Error");
|
2020-12-26 13:10:11 +00:00
|
|
|
}
|
2020-01-29 18:29:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 20:18:16 +00:00
|
|
|
static void run(void)
|
2016-03-30 20:07:55 +00:00
|
|
|
{
|
2019-11-03 03:43:25 +00:00
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2019-05-18 17:37:41 +00:00
|
|
|
GB_model_t model;
|
2017-10-13 22:41:51 +00:00
|
|
|
pending_command = GB_SDL_NO_COMMAND;
|
2017-05-23 20:18:16 +00:00
|
|
|
restart:
|
2019-05-18 17:37:41 +00:00
|
|
|
model = (GB_model_t [])
|
|
|
|
{
|
|
|
|
[MODEL_DMG] = GB_MODEL_DMG_B,
|
|
|
|
[MODEL_CGB] = GB_MODEL_CGB_E,
|
|
|
|
[MODEL_AGB] = GB_MODEL_AGB,
|
|
|
|
[MODEL_SGB] = (GB_model_t [])
|
|
|
|
{
|
|
|
|
[SGB_NTSC] = GB_MODEL_SGB_NTSC,
|
|
|
|
[SGB_PAL] = GB_MODEL_SGB_PAL,
|
|
|
|
[SGB_2] = GB_MODEL_SGB2,
|
|
|
|
}[configuration.sgb_revision],
|
|
|
|
}[configuration.model];
|
|
|
|
|
2017-05-23 20:18:16 +00:00
|
|
|
if (GB_is_inited(&gb)) {
|
2019-05-18 17:37:41 +00:00
|
|
|
GB_switch_model_and_reset(&gb, model);
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2017-05-23 20:18:16 +00:00
|
|
|
else {
|
2019-05-18 17:37:41 +00:00
|
|
|
GB_init(&gb, model);
|
2017-05-23 20:18:16 +00:00
|
|
|
|
2020-01-29 18:29:30 +00:00
|
|
|
GB_set_boot_rom_load_callback(&gb, load_boot_rom);
|
2017-05-23 20:18:16 +00:00
|
|
|
GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
|
2017-12-23 15:41:47 +00:00
|
|
|
GB_set_pixels_output(&gb, active_pixel_buffer);
|
2017-05-23 20:18:16 +00:00
|
|
|
GB_set_rgb_encode_callback(&gb, rgb_encode);
|
2020-05-24 20:04:36 +00:00
|
|
|
GB_set_rumble_callback(&gb, rumble);
|
|
|
|
GB_set_rumble_mode(&gb, configuration.rumble_mode);
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_set_sample_rate(&gb, GB_audio_get_frequency());
|
2017-12-22 20:25:53 +00:00
|
|
|
GB_set_color_correction_mode(&gb, configuration.color_correction_mode);
|
2020-12-25 12:14:17 +00:00
|
|
|
GB_set_light_temperature(&gb, (configuration.color_temperature - 10.0) / 10.0);
|
2020-12-30 22:06:36 +00:00
|
|
|
GB_set_interference_volume(&gb, configuration.interference_volume / 100.0);
|
2020-01-29 13:36:19 +00:00
|
|
|
update_palette();
|
2020-02-08 11:28:46 +00:00
|
|
|
if ((unsigned)configuration.border_mode <= GB_BORDER_ALWAYS) {
|
|
|
|
GB_set_border_mode(&gb, configuration.border_mode);
|
|
|
|
}
|
2017-12-23 19:11:44 +00:00
|
|
|
GB_set_highpass_filter_mode(&gb, configuration.highpass_mode);
|
2018-06-22 21:10:28 +00:00
|
|
|
GB_set_rewind_length(&gb, configuration.rewind_length);
|
2021-02-25 23:04:24 +00:00
|
|
|
GB_set_rtc_mode(&gb, configuration.rtc_mode);
|
2019-06-07 15:27:25 +00:00
|
|
|
GB_set_update_input_hint_callback(&gb, handle_events);
|
2019-06-15 20:22:27 +00:00
|
|
|
GB_apu_set_sample_callback(&gb, gb_audio_callback);
|
2021-09-08 21:13:09 +00:00
|
|
|
|
2021-09-10 11:17:07 +00:00
|
|
|
if (console_supported) {
|
2021-09-08 21:13:09 +00:00
|
|
|
CON_set_async_prompt("> ");
|
|
|
|
GB_set_log_callback(&gb, log_callback);
|
|
|
|
GB_set_input_callback(&gb, input_callback);
|
|
|
|
GB_set_async_input_callback(&gb, asyc_input_callback);
|
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
2021-06-25 14:12:05 +00:00
|
|
|
if (stop_on_start) {
|
|
|
|
stop_on_start = false;
|
|
|
|
GB_debugger_break(&gb);
|
|
|
|
}
|
2019-05-18 15:45:31 +00:00
|
|
|
|
2017-05-27 16:16:07 +00:00
|
|
|
bool error = false;
|
2020-04-25 19:48:48 +00:00
|
|
|
GB_debugger_clear_symbols(&gb);
|
2017-05-25 20:28:48 +00:00
|
|
|
start_capturing_logs();
|
2020-04-25 20:11:29 +00:00
|
|
|
size_t path_length = strlen(filename);
|
2020-04-25 19:48:48 +00:00
|
|
|
char extension[4] = {0,};
|
2020-04-25 20:11:29 +00:00
|
|
|
if (path_length > 4) {
|
|
|
|
if (filename[path_length - 4] == '.') {
|
2021-01-26 06:37:46 +00:00
|
|
|
extension[0] = tolower((unsigned char)filename[path_length - 3]);
|
|
|
|
extension[1] = tolower((unsigned char)filename[path_length - 2]);
|
|
|
|
extension[2] = tolower((unsigned char)filename[path_length - 1]);
|
2020-04-25 19:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strcmp(extension, "isx") == 0) {
|
|
|
|
error = GB_load_isx(&gb, filename);
|
2020-04-25 20:11:29 +00:00
|
|
|
/* Configure battery */
|
2020-04-25 20:09:08 +00:00
|
|
|
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, ".ram");
|
|
|
|
battery_save_path_ptr = battery_save_path;
|
|
|
|
GB_load_battery(&gb, battery_save_path);
|
2020-04-25 19:48:48 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
GB_load_rom(&gb, filename);
|
|
|
|
}
|
2021-05-06 21:33:04 +00:00
|
|
|
end_capturing_logs(true, error, SDL_MESSAGEBOX_WARNING, "Warning");
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2021-05-30 20:39:59 +00:00
|
|
|
static char start_text[64];
|
|
|
|
static char title[17];
|
|
|
|
GB_get_rom_title(&gb, title);
|
|
|
|
sprintf(start_text, "SameBoy v" GB_VERSION "\n%s\n%08X", title, GB_get_rom_crc32(&gb));
|
|
|
|
show_osd_text(start_text);
|
|
|
|
|
2017-05-23 20:18:16 +00:00
|
|
|
|
2016-07-17 20:08:07 +00:00
|
|
|
/* Configure battery */
|
|
|
|
char battery_save_path[path_length + 5]; /* At the worst case, size is strlen(path) + 4 bytes for .sav + NULL */
|
2016-07-18 11:37:06 +00:00
|
|
|
replace_extension(filename, path_length, battery_save_path, ".sav");
|
2017-05-21 18:44:28 +00:00
|
|
|
battery_save_path_ptr = battery_save_path;
|
2016-06-18 17:29:11 +00:00
|
|
|
GB_load_battery(&gb, battery_save_path);
|
2017-05-23 20:18:16 +00:00
|
|
|
|
2016-07-17 20:08:07 +00:00
|
|
|
/* Configure symbols */
|
2018-11-09 22:20:57 +00:00
|
|
|
GB_debugger_load_symbol_file(&gb, resource_path("registers.sym"));
|
2017-02-20 12:37:15 +00:00
|
|
|
|
2016-07-17 20:08:07 +00:00
|
|
|
char symbols_path[path_length + 5];
|
2016-07-18 11:37:06 +00:00
|
|
|
replace_extension(filename, path_length, symbols_path, ".sym");
|
2016-07-17 20:08:07 +00:00
|
|
|
GB_debugger_load_symbol_file(&gb, symbols_path);
|
2019-05-18 15:45:31 +00:00
|
|
|
|
2020-02-08 11:28:46 +00:00
|
|
|
screen_size_changed();
|
2019-05-18 15:45:31 +00:00
|
|
|
|
2016-05-23 19:22:09 +00:00
|
|
|
/* Run emulation */
|
2017-05-21 18:44:28 +00:00
|
|
|
while (true) {
|
2018-06-22 21:10:28 +00:00
|
|
|
if (paused || rewind_paused) {
|
2017-05-24 17:33:39 +00:00
|
|
|
SDL_WaitEvent(NULL);
|
|
|
|
handle_events(&gb);
|
|
|
|
}
|
|
|
|
else {
|
2018-06-22 21:10:28 +00:00
|
|
|
if (do_rewind) {
|
|
|
|
GB_rewind_pop(&gb);
|
|
|
|
if (turbo_down) {
|
|
|
|
GB_rewind_pop(&gb);
|
|
|
|
}
|
|
|
|
if (!GB_rewind_pop(&gb)) {
|
|
|
|
rewind_paused = true;
|
|
|
|
}
|
|
|
|
do_rewind = false;
|
|
|
|
}
|
2017-05-24 17:33:39 +00:00
|
|
|
GB_run(&gb);
|
|
|
|
}
|
2017-05-24 18:44:43 +00:00
|
|
|
|
|
|
|
/* These commands can't run in the handle_event function, because they're not safe in a vblank context. */
|
|
|
|
if (handle_pending_command()) {
|
|
|
|
pending_command = GB_SDL_NO_COMMAND;
|
|
|
|
goto restart;
|
2017-05-21 18:44:28 +00:00
|
|
|
}
|
|
|
|
pending_command = GB_SDL_NO_COMMAND;
|
2016-03-30 20:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 15:29:42 +00:00
|
|
|
|
2017-12-28 19:46:59 +00:00
|
|
|
static char prefs_path[1024] = {0, };
|
|
|
|
|
|
|
|
static void save_configuration(void)
|
|
|
|
{
|
|
|
|
FILE *prefs_file = fopen(prefs_path, "wb");
|
|
|
|
if (prefs_file) {
|
|
|
|
fwrite(&configuration, 1, sizeof(configuration), prefs_file);
|
|
|
|
fclose(prefs_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 14:32:45 +00:00
|
|
|
static bool get_arg_flag(const char *flag, int *argc, char **argv)
|
|
|
|
{
|
|
|
|
for (unsigned i = 1; i < *argc; i++) {
|
|
|
|
if (strcmp(argv[i], flag) == 0) {
|
|
|
|
(*argc)--;
|
|
|
|
argv[i] = argv[*argc];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:21:02 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
static void enable_smooth_scrolling(void)
|
|
|
|
{
|
|
|
|
CFPreferencesSetAppValue(CFSTR("AppleMomentumScrollSupported"), kCFBooleanTrue, kCFPreferencesCurrentApplication);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-23 20:18:16 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-12-31 22:27:41 +00:00
|
|
|
#ifdef _WIN32
|
2020-04-24 17:37:57 +00:00
|
|
|
SetProcessDPIAware();
|
2021-09-06 12:21:02 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
|
|
enable_smooth_scrolling();
|
2019-12-31 22:27:41 +00:00
|
|
|
#endif
|
2021-09-10 11:17:07 +00:00
|
|
|
|
2021-06-25 14:12:05 +00:00
|
|
|
bool fullscreen = get_arg_flag("--fullscreen", &argc, argv) || get_arg_flag("-f", &argc, argv);
|
2021-05-05 21:23:46 +00:00
|
|
|
bool nogl = get_arg_flag("--nogl", &argc, argv);
|
2021-06-25 14:12:05 +00:00
|
|
|
stop_on_start = get_arg_flag("--stop-debugger", &argc, argv) || get_arg_flag("-s", &argc, argv);
|
2017-05-23 20:18:16 +00:00
|
|
|
|
2021-05-05 21:26:45 +00:00
|
|
|
if (argc > 2 || (argc == 2 && argv[1][0] == '-')) {
|
2021-09-10 11:17:07 +00:00
|
|
|
fprintf(stderr, "SameBoy v" GB_VERSION "\n");
|
2021-06-25 14:12:05 +00:00
|
|
|
fprintf(stderr, "Usage: %s [--fullscreen|-f] [--nogl] [--stop-debugger|-s] [rom]\n", argv[0]);
|
2017-05-23 20:18:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-01-13 11:49:20 +00:00
|
|
|
if (argc == 2) {
|
2018-01-22 20:25:27 +00:00
|
|
|
filename = argv[1];
|
2017-05-23 20:18:16 +00:00
|
|
|
}
|
2016-03-30 20:07:55 +00:00
|
|
|
|
2017-05-23 20:18:16 +00:00
|
|
|
signal(SIGINT, debugger_interrupt);
|
|
|
|
|
2018-06-16 10:59:33 +00:00
|
|
|
SDL_Init(SDL_INIT_EVERYTHING);
|
2021-09-10 11:17:07 +00:00
|
|
|
if ((console_supported = CON_start(completer))) {
|
|
|
|
CON_set_repeat_empty(true);
|
|
|
|
CON_printf("SameBoy v" GB_VERSION "\n");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "SameBoy v" GB_VERSION "\n");
|
|
|
|
}
|
2017-05-23 20:18:16 +00:00
|
|
|
|
2020-08-06 01:08:19 +00:00
|
|
|
strcpy(prefs_path, resource_path("prefs.bin"));
|
|
|
|
if (access(prefs_path, R_OK | W_OK) != 0) {
|
|
|
|
char *prefs_dir = SDL_GetPrefPath("", "SameBoy");
|
|
|
|
snprintf(prefs_path, sizeof(prefs_path) - 1, "%sprefs.bin", prefs_dir);
|
|
|
|
SDL_free(prefs_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *prefs_file = fopen(prefs_path, "rb");
|
|
|
|
if (prefs_file) {
|
|
|
|
fread(&configuration, 1, sizeof(configuration), prefs_file);
|
|
|
|
fclose(prefs_file);
|
|
|
|
|
|
|
|
/* Sanitize for stability */
|
2021-06-25 16:57:56 +00:00
|
|
|
configuration.color_correction_mode %= GB_COLOR_CORRECTION_LOW_CONTRAST +1;
|
2020-08-06 01:08:19 +00:00
|
|
|
configuration.scaling_mode %= GB_SDL_SCALING_MAX;
|
|
|
|
configuration.default_scale %= GB_SDL_DEFAULT_SCALE_MAX + 1;
|
|
|
|
configuration.blending_mode %= GB_FRAME_BLENDING_MODE_ACCURATE + 1;
|
|
|
|
configuration.highpass_mode %= GB_HIGHPASS_MAX;
|
|
|
|
configuration.model %= MODEL_MAX;
|
|
|
|
configuration.sgb_revision %= SGB_MAX;
|
|
|
|
configuration.dmg_palette %= 3;
|
|
|
|
configuration.border_mode %= GB_BORDER_ALWAYS + 1;
|
|
|
|
configuration.rumble_mode %= GB_RUMBLE_ALL_GAMES + 1;
|
2020-12-25 12:14:17 +00:00
|
|
|
configuration.color_temperature %= 21;
|
2020-12-26 13:10:11 +00:00
|
|
|
configuration.bootrom_path[sizeof(configuration.bootrom_path) - 1] = 0;
|
2020-08-06 01:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (configuration.model >= MODEL_MAX) {
|
|
|
|
configuration.model = MODEL_CGB;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (configuration.default_scale == 0) {
|
|
|
|
configuration.default_scale = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit(save_configuration);
|
|
|
|
|
2017-12-23 15:29:42 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
|
|
|
2021-05-30 17:55:04 +00:00
|
|
|
window = SDL_CreateWindow("SameBoy v" GB_VERSION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
2020-08-06 01:08:19 +00:00
|
|
|
160 * configuration.default_scale, 144 * configuration.default_scale, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
|
2021-02-14 00:31:49 +00:00
|
|
|
if (window == NULL) {
|
|
|
|
fputs(SDL_GetError(), stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-05-24 17:33:39 +00:00
|
|
|
SDL_SetWindowMinimumSize(window, 160, 144);
|
2017-05-23 20:18:16 +00:00
|
|
|
|
2018-11-10 14:32:45 +00:00
|
|
|
if (fullscreen) {
|
|
|
|
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
|
|
|
}
|
|
|
|
|
2021-05-05 21:23:46 +00:00
|
|
|
gl_context = nogl? NULL : SDL_GL_CreateContext(window);
|
2017-12-28 22:43:19 +00:00
|
|
|
|
2017-12-30 15:01:23 +00:00
|
|
|
GLint major = 0, minor = 0;
|
2021-05-05 21:23:46 +00:00
|
|
|
if (gl_context) {
|
|
|
|
glGetIntegerv(GL_MAJOR_VERSION, &major);
|
|
|
|
glGetIntegerv(GL_MINOR_VERSION, &minor);
|
|
|
|
}
|
2017-12-28 22:43:19 +00:00
|
|
|
|
2021-05-05 21:23:46 +00:00
|
|
|
if (gl_context && major * 0x100 + minor < 0x302) {
|
2017-12-28 22:43:19 +00:00
|
|
|
SDL_GL_DeleteContext(gl_context);
|
|
|
|
gl_context = NULL;
|
|
|
|
}
|
|
|
|
|
2017-12-23 15:29:42 +00:00
|
|
|
if (gl_context == NULL) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pixel_format = SDL_AllocFormat(SDL_PIXELFORMAT_ABGR8888);
|
|
|
|
}
|
|
|
|
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_audio_init();
|
2017-05-23 20:18:16 +00:00
|
|
|
|
|
|
|
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
|
|
|
|
|
2017-12-23 15:29:42 +00:00
|
|
|
if (!init_shader_with_name(&shader, configuration.filter)) {
|
|
|
|
init_shader_with_name(&shader, "NearestNeighbor");
|
|
|
|
}
|
|
|
|
update_viewport();
|
|
|
|
|
2017-05-23 20:18:16 +00:00
|
|
|
if (filename == NULL) {
|
2021-06-25 14:12:05 +00:00
|
|
|
stop_on_start = false;
|
2017-10-13 22:41:51 +00:00
|
|
|
run_gui(false);
|
2017-05-23 20:18:16 +00:00
|
|
|
}
|
2018-11-10 17:39:57 +00:00
|
|
|
else {
|
|
|
|
connect_joypad();
|
|
|
|
}
|
2020-04-27 18:12:30 +00:00
|
|
|
GB_audio_set_paused(false);
|
2017-05-23 20:18:16 +00:00
|
|
|
run(); // Never returns
|
|
|
|
return 0;
|
|
|
|
}
|