Add DMG color palettes (SDL), add scrolling to SDL menus

This commit is contained in:
Lior Halphon 2020-01-29 15:36:19 +02:00
parent 163a5ea20c
commit 046b09052c
3 changed files with 87 additions and 19 deletions

View File

@ -175,8 +175,10 @@ static void draw_char(uint32_t *buffer, unsigned width, unsigned height, unsigne
}
}
static unsigned scroll = 0;
static void draw_unbordered_text(uint32_t *buffer, unsigned width, unsigned height, unsigned x, unsigned y, const char *string, uint32_t color)
{
y -= scroll;
unsigned orig_x = x;
while (*string) {
if (*string == '\n') {
@ -297,6 +299,7 @@ static void return_to_root_menu(unsigned index)
{
current_menu = root_menu;
current_selection = 0;
scroll = 0;
}
static void cycle_model(unsigned index)
@ -407,6 +410,7 @@ static void enter_emulation_menu(unsigned index)
{
current_menu = emulation_menu;
current_selection = 0;
scroll = 0;
}
const char *current_scaling_mode(unsigned index)
@ -421,6 +425,12 @@ const char *current_color_correction_mode(unsigned index)
[configuration.color_correction_mode];
}
const char *current_palette(unsigned index)
{
return (const char *[]){"Greyscale", "Lime (Game Boy)", "Olive (Pocket)", "Teal (Light)"}
[configuration.dmg_palette];
}
void cycle_scaling(unsigned index)
{
configuration.scaling_mode++;
@ -463,6 +473,26 @@ static void cycle_color_correction_backwards(unsigned index)
}
}
static void cycle_palette(unsigned index)
{
if (configuration.dmg_palette == 3) {
configuration.dmg_palette = 0;
}
else {
configuration.dmg_palette++;
}
}
static void cycle_palette_backwards(unsigned index)
{
if (configuration.dmg_palette == 0) {
configuration.dmg_palette = 3;
}
else {
configuration.dmg_palette--;
}
}
struct shader_name {
const char *file_name;
const char *display_name;
@ -556,6 +586,7 @@ static const struct menu_item graphics_menu[] = {
{"Scaling Mode:", cycle_scaling, current_scaling_mode, cycle_scaling_backwards},
{"Scaling Filter:", cycle_filter, current_filter_name, cycle_filter_backwards},
{"Color Correction:", cycle_color_correction, current_color_correction_mode, cycle_color_correction_backwards},
{"Mono Palette:", cycle_palette, current_palette, cycle_palette_backwards},
{"Blend Frames:", toggle_blend_frames, blend_frames_string, toggle_blend_frames},
{"Back", return_to_root_menu},
{NULL,}
@ -565,6 +596,7 @@ static void enter_graphics_menu(unsigned index)
{
current_menu = graphics_menu;
current_selection = 0;
scroll = 0;
}
const char *highpass_filter_string(unsigned index)
@ -601,6 +633,7 @@ static void enter_audio_menu(unsigned index)
{
current_menu = audio_menu;
current_selection = 0;
scroll = 0;
}
static void modify_key(unsigned index)
@ -608,7 +641,6 @@ static void modify_key(unsigned index)
gui_state = WAITING_FOR_KEY;
}
static void enter_controls_menu_2(unsigned index);
static const char *key_name(unsigned index);
static const struct menu_item controls_menu[] = {
@ -620,12 +652,6 @@ static const struct menu_item controls_menu[] = {
{"B:", modify_key, key_name,},
{"Select:", modify_key, key_name,},
{"Start:", modify_key, key_name,},
{"Next Page", enter_controls_menu_2},
{"Back", return_to_root_menu},
{NULL,}
};
static const struct menu_item controls_menu_2[] = {
{"Turbo:", modify_key, key_name,},
{"Rewind:", modify_key, key_name,},
{"Slow-Motion:", modify_key, key_name,},
@ -635,11 +661,11 @@ static const struct menu_item controls_menu_2[] = {
static const char *key_name(unsigned index)
{
if (current_menu == controls_menu_2) {
if (index == 0) {
if (index >= 8) {
if (index == 8) {
return SDL_GetScancodeName(configuration.keys[8]);
}
return SDL_GetScancodeName(configuration.keys_2[index - 1]);
return SDL_GetScancodeName(configuration.keys_2[index - 9]);
}
return SDL_GetScancodeName(configuration.keys[index]);
}
@ -648,12 +674,7 @@ static void enter_controls_menu(unsigned index)
{
current_menu = controls_menu;
current_selection = 0;
}
static void enter_controls_menu_2(unsigned index)
{
current_menu = controls_menu_2;
current_selection = 0;
scroll = 0;
}
static unsigned joypad_index = 0;
@ -744,6 +765,7 @@ static void enter_joypad_menu(unsigned index)
{
current_menu = joypad_menu;
current_selection = 0;
scroll = 0;
}
joypad_button_t get_joypad_button(uint8_t physical_button)
@ -826,6 +848,7 @@ void run_gui(bool is_running)
bool should_render = true;
current_menu = root_menu = is_running? paused_menu : nonpaused_menu;
current_selection = 0;
scroll = 0;
do {
/* Convert Joypad and mouse events (We only generate down events) */
if (gui_state != WAITING_FOR_KEY && gui_state != WAITING_FOR_JBUTTON) {
@ -850,6 +873,7 @@ void run_gui(bool is_running)
y = y * 8 / 7;
y -= 144 / 16;
}
y += scroll;
if (x < 0 || x >= 160 || y < 24) {
continue;
@ -1058,6 +1082,7 @@ void run_gui(bool is_running)
gui_state = SHOWING_DROP_MESSAGE;
}
current_selection = 0;
scroll = 0;
current_menu = root_menu;
should_render = true;
}
@ -1106,12 +1131,12 @@ void run_gui(bool is_running)
should_render = true;
}
else if (gui_state == WAITING_FOR_KEY) {
if (current_menu == controls_menu_2) {
if (current_selection == 0) {
if (current_selection >= 8) {
if (current_selection == 8) {
configuration.keys[8] = event.key.keysym.scancode;
}
else {
configuration.keys_2[current_selection - 1] = event.key.keysym.scancode;
configuration.keys_2[current_selection - 9] = event.key.keysym.scancode;
}
}
else {
@ -1125,6 +1150,7 @@ void run_gui(bool is_running)
if (should_render) {
should_render = false;
rerender:
if (width == 160 && height == 144) {
memcpy(pixels, converted_background->pixels, sizeof(pixels));
}
@ -1144,6 +1170,16 @@ void run_gui(bool is_running)
draw_text_centered(pixels, width, height, 8 + y_offset, "SameBoy", gui_palette_native[3], gui_palette_native[0], false);
unsigned i = 0, y = 24;
for (const struct menu_item *item = current_menu; item->string; item++, i++) {
if (i == current_selection) {
if (y < scroll) {
scroll = y - 4;
goto rerender;
}
}
if (i == current_selection && i == 0 && scroll != 0) {
scroll = 0;
goto rerender;
}
if (item->value_getter && !item->backwards_handler) {
char line[25];
snprintf(line, sizeof(line), "%s%*s", item->string, 24 - (int)strlen(item->string), item->value_getter(i));
@ -1162,6 +1198,13 @@ void run_gui(bool is_running)
y += 12;
}
}
if (i == current_selection) {
if (y > scroll + 144) {
scroll = y - 144;
goto rerender;
}
}
}
break;
case SHOWING_HELP:

View File

@ -100,6 +100,9 @@ typedef struct {
SGB_2,
SGB_MAX
} sgb_revision;
/* v0.13 */
uint8_t dmg_palette;
} configuration_t;
extern configuration_t configuration;

View File

@ -92,6 +92,26 @@ static const char *end_capturing_logs(bool show_popup, bool should_exit)
return captured_log;
}
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);
}
}
static void open_menu(void)
{
bool audio_playing = SDL_GetAudioDeviceStatus(device_id) == SDL_AUDIO_PLAYING;
@ -105,6 +125,7 @@ static void open_menu(void)
SDL_PauseAudioDevice(device_id, 0);
}
GB_set_color_correction_mode(&gb, configuration.color_correction_mode);
update_palette();
GB_set_highpass_filter_mode(&gb, configuration.highpass_mode);
}
@ -454,6 +475,7 @@ restart:
GB_set_rgb_encode_callback(&gb, rgb_encode);
GB_set_sample_rate(&gb, have_aspec.freq);
GB_set_color_correction_mode(&gb, configuration.color_correction_mode);
update_palette();
GB_set_highpass_filter_mode(&gb, configuration.highpass_mode);
GB_set_rewind_length(&gb, configuration.rewind_length);
GB_set_update_input_hint_callback(&gb, handle_events);