@@ -54,6 +54,7 @@ static void push_recent_rom(std::string path);
5454static void show_status_message (void );
5555static void show_error_window (void );
5656static void set_style (void );
57+ static void load_custom_palette_from_settings (void );
5758static ImVec4 lerp (const ImVec4& a, const ImVec4& b, float t);
5859
5960bool gui_init (void )
@@ -109,13 +110,14 @@ bool gui_init(void)
109110 gui_audio_mute_cdrom = false ;
110111 gui_audio_mute_psg = false ;
111112 gui_audio_mute_adpcm = false ;
113+ gui_custom_palette_loaded = false ;
112114
113115 emu_audio_mute (!config_audio.enable );
114116 emu_audio_huc6280a (config_audio.huc6280a );
115117 emu_audio_psg_volume (config_audio.psg_volume );
116118 emu_audio_cdrom_volume (config_audio.cdrom_volume );
117119 emu_audio_adpcm_volume (config_audio.adpcm_volume );
118- emu_set_composite_palette (config_video.composite_palette );
120+ emu_set_palette (config_video.palette );
119121 emu_video_no_sprite_limit (config_video.sprite_limit );
120122 emu_set_overscan (config_debug.debug ? 0 : config_video.overscan );
121123 emu_set_scanline_start_end (
@@ -133,7 +135,7 @@ bool gui_init(void)
133135 emu_set_preload_cdrom (config_emulator.preload_cdrom );
134136 emu_set_backup_ram (config_emulator.backup_ram );
135137 emu_set_mb128_mode ((GG_MB128_Mode)config_emulator.mb128_mode );
136- emu_set_composite_palette (config_video.composite_palette );
138+ emu_set_palette (config_video.palette );
137139 emu_set_turbo_tap (config_input.turbo_tap );
138140 for (int i = 0 ; i < GG_MAX_GAMEPADS; i++)
139141 {
@@ -164,6 +166,8 @@ bool gui_init(void)
164166 if (strlen (gui_gameexpress_bios_path) > 0 )
165167 gui_load_bios (gui_gameexpress_bios_path, false );
166168
169+ load_custom_palette_from_settings ();
170+
167171 gui_hes_init ();
168172 gui_debug_init ();
169173 gui_init_menus ();
@@ -337,6 +341,75 @@ void gui_load_bios(const char* path, bool syscard)
337341 gui_action_reset ();
338342}
339343
344+ void gui_load_palette (const char * path)
345+ {
346+ using namespace std ;
347+ string fullpath (path);
348+ string filename;
349+
350+ size_t pos = fullpath.find_last_of (" /\\ " );
351+ if (pos != string::npos)
352+ filename = fullpath.substr (pos + 1 );
353+ else
354+ filename = fullpath;
355+
356+ ifstream file (path, ios::binary | ios::ate);
357+ if (!file.is_open ())
358+ {
359+ std::string message (" Error opening palette file:\n " );
360+ message += filename;
361+ gui_set_error_message (message.c_str ());
362+ return ;
363+ }
364+
365+ streamsize size = file.tellg ();
366+ if (size != 0x600 )
367+ {
368+ file.close ();
369+ std::string message (" Invalid palette file size:\n " );
370+ message += filename;
371+ message += " \n\n Palette files must be exactly 1536 bytes (0x600)." ;
372+ gui_set_error_message (message.c_str ());
373+ return ;
374+ }
375+
376+ file.seekg (0 , ios::beg);
377+ u8 palette_data[0x600 ];
378+ if (!file.read (reinterpret_cast <char *>(palette_data), 0x600 ))
379+ {
380+ file.close ();
381+ std::string message (" Error reading palette file:\n " );
382+ message += filename;
383+ gui_set_error_message (message.c_str ());
384+ return ;
385+ }
386+ file.close ();
387+
388+ emu_set_custom_palette (palette_data);
389+
390+ std::string dest_path = config_root_path;
391+ dest_path += " custom_palette.pal" ;
392+
393+ ofstream dest_file (dest_path, ios::binary);
394+ if (dest_file.is_open ())
395+ {
396+ dest_file.write (reinterpret_cast <const char *>(palette_data), 0x600 );
397+ dest_file.close ();
398+ }
399+ else
400+ {
401+ Log (" Warning: Could not save custom palette to %s" , dest_path.c_str ());
402+ }
403+
404+ config_video.palette = 2 ;
405+ emu_set_palette (config_video.palette );
406+ gui_custom_palette_loaded = true ;
407+
408+ std::string message (" Custom palette loaded: " );
409+ message += filename;
410+ gui_set_status_message (message.c_str (), 3000 );
411+ }
412+
340413void gui_load_rom (const char * path)
341414{
342415 using namespace std ;
@@ -730,6 +803,41 @@ static void set_style(void)
730803 style.Colors [ImGuiCol_TabDimmedSelectedOverline] = lerp (style.Colors [ImGuiCol_TabSelected], style.Colors [ImGuiCol_TitleBg], 0 .20f );
731804}
732805
806+ static void load_custom_palette_from_settings (void )
807+ {
808+ using namespace std ;
809+ string palette_path = config_root_path;
810+ palette_path += " custom_palette.pal" ;
811+
812+ ifstream file (palette_path, ios::binary | ios::ate);
813+ if (!file.is_open ())
814+ {
815+ return ;
816+ }
817+
818+ streamsize size = file.tellg ();
819+ if (size != 0x600 )
820+ {
821+ file.close ();
822+ Log (" Invalid custom palette file size: %s" , palette_path.c_str ());
823+ return ;
824+ }
825+
826+ file.seekg (0 , ios::beg);
827+ u8 palette_data[0x600 ];
828+ if (!file.read (reinterpret_cast <char *>(palette_data), 0x600 ))
829+ {
830+ file.close ();
831+ Log (" Error reading custom palette file: %s" , palette_path.c_str ());
832+ return ;
833+ }
834+ file.close ();
835+
836+ emu_set_custom_palette (palette_data);
837+ gui_custom_palette_loaded = true ;
838+ Log (" Custom palette loaded from: %s" , palette_path.c_str ());
839+ }
840+
733841static ImVec4 lerp (const ImVec4& a, const ImVec4& b, float t)
734842{
735843 return ImVec4 (a.x + (b.x - a.x ) * t, a.y + (b.y - a.y ) * t, a.z + (b.z - a.z ) * t, a.w + (b.w - a.w ) * t);
0 commit comments