Skip to content

Commit

Permalink
refactor: use unique_ptr in some GUI elements
Browse files Browse the repository at this point in the history
This could be expanded in more widgets, but I got crashes on exit when doing so.
Until I figure out why, I'm keeping it to only a few elements
  • Loading branch information
midwan committed Jan 12, 2025
1 parent 79487a2 commit e25d906
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/osdep/amiberry_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1985,15 +1985,15 @@ void apply_theme()
// Check if the font_name contains the full path to the file (e.g. in /usr/share/fonts)
if (my_existsfile2(gui_theme.font_name.c_str()))
{
gui_font = new gcn::SDLTrueTypeFont(gui_theme.font_name, gui_theme.font_size);
gui_font = std::make_unique<gcn::SDLTrueTypeFont>(gui_theme.font_name, gui_theme.font_size);
}
else
{
// If only a font name was given, try to open it from the data directory
std::string font = get_data_path();
font.append(gui_theme.font_name);
if (my_existsfile2(font.c_str()))
gui_font = new gcn::SDLTrueTypeFont(font, gui_theme.font_size);
gui_font = std::make_unique<gcn::SDLTrueTypeFont>(font, gui_theme.font_size);
else
{
// If the font file was not found in the data directory, fallback to a system font
Expand All @@ -2019,7 +2019,7 @@ void apply_theme()
write_log("An error occurred while trying to open the GUI font! Exception: %s\n", ex.what());
abort();
}
gcn::Widget::setGlobalFont(gui_font);
gcn::Widget::setGlobalFont(gui_font.get());
gcn::Widget::setWidgetsBaseColor(gui_base_color);
gcn::Widget::setWidgetsForegroundColor(gui_foreground_color);
gcn::Widget::setWidgetsBackgroundColor(gui_background_color);
Expand Down
18 changes: 5 additions & 13 deletions src/osdep/gui/ShowMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,19 @@ static void InitShowMessage(const std::string& message)

if (gui_graphics == nullptr)
{
gui_graphics = new gcn::SDLGraphics();
gui_graphics = std::make_unique<gcn::SDLGraphics>();
gui_graphics->setTarget(gui_screen);
}
if (gui_input == nullptr)
{
gui_input = new gcn::SDLInput();
gui_input = std::make_unique<gcn::SDLInput>();
}
if (uae_gui == nullptr)
{
halt_gui = true;
uae_gui = new gcn::Gui();
uae_gui->setGraphics(gui_graphics);
uae_gui->setInput(gui_input);
uae_gui = std::make_unique<gcn::Gui>();
uae_gui->setGraphics(gui_graphics.get());
uae_gui->setInput(gui_input.get());
}
if (gui_top == nullptr)
{
Expand Down Expand Up @@ -259,14 +259,6 @@ static void ExitShowMessage()

if (halt_gui)
{
delete uae_gui;
uae_gui = nullptr;
delete gui_input;
gui_input = nullptr;
delete gui_graphics;
gui_graphics = nullptr;
delete gui_font;
gui_font = nullptr;
delete gui_top;
gui_top = nullptr;

Expand Down
8 changes: 4 additions & 4 deletions src/osdep/gui/gui_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ extern bool gui_running;
extern gcn::Container* selectors;
extern gcn::ScrollArea* selectorsScrollArea;
extern ConfigCategory categories[];
extern gcn::Gui* uae_gui;
extern gcn::Container* gui_top;
extern std::unique_ptr<gcn::Gui> uae_gui;

// GUI Colors
extern amiberry_gui_theme gui_theme;
Expand All @@ -186,12 +186,12 @@ extern gcn::Color gui_selection_color;
extern gcn::Color gui_foreground_color;
extern gcn::Color gui_font_color;

extern gcn::SDLInput* gui_input;
extern std::unique_ptr<gcn::SDLInput> gui_input;
extern SDL_Surface* gui_screen;
extern SDL_Joystick* gui_joystick;

extern gcn::SDLGraphics* gui_graphics;
extern gcn::SDLTrueTypeFont* gui_font;
extern std::unique_ptr<gcn::SDLGraphics> gui_graphics;
extern std::unique_ptr<gcn::SDLTrueTypeFont> gui_font;
extern SDL_Texture* gui_texture;


Expand Down
47 changes: 17 additions & 30 deletions src/osdep/gui/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,18 @@ SDL_Rect gui_window_rect{0, 0, GUI_WIDTH, GUI_HEIGHT};
/*
* Gui SDL stuff we need
*/
gcn::SDLInput* gui_input;
gcn::SDLGraphics* gui_graphics;
gcn::SDLImageLoader* gui_imageLoader;
gcn::SDLTrueTypeFont* gui_font;
std::unique_ptr<gcn::SDLInput> gui_input;
std::unique_ptr<gcn::SDLGraphics> gui_graphics;
std::unique_ptr<gcn::SDLImageLoader> gui_imageLoader;
std::unique_ptr<gcn::SDLTrueTypeFont> gui_font;

/*
* Gui stuff we need
*/
gcn::Gui* uae_gui;
gcn::Container* gui_top;
gcn::Container* selectors;
gcn::ScrollArea* selectorsScrollArea;
std::unique_ptr<gcn::Gui> uae_gui;

// GUI Colors
gcn::Color gui_base_color;
Expand All @@ -163,8 +163,8 @@ gcn::Color gui_selection_color;
gcn::Color gui_foreground_color;
gcn::Color gui_font_color;

gcn::FocusHandler* focusHdl;
gcn::Widget* activeWidget;
std::unique_ptr<gcn::FocusHandler> focusHdl;
std::unique_ptr<gcn::Widget> activeWidget;

// Main buttons
gcn::Button* cmdQuit;
Expand Down Expand Up @@ -392,31 +392,24 @@ void amiberry_gui_init()
// Create helpers for GUI framework
//-------------------------------------------------

gui_imageLoader = new gcn::SDLImageLoader();
gui_imageLoader = std::make_unique<gcn::SDLImageLoader>();
gui_imageLoader->setRenderer(mon->gui_renderer);

// The ImageLoader in use is static and must be set to be
// able to load images
gcn::Image::setImageLoader(gui_imageLoader);
gui_graphics = new gcn::SDLGraphics();
gcn::Image::setImageLoader(gui_imageLoader.get());
gui_graphics = std::make_unique<gcn::SDLGraphics>();
// Set the target for the graphics object to be the screen.
// In other words, we will draw to the screen.
// Note, any surface will do, it doesn't have to be the screen.
gui_graphics->setTarget(gui_screen);
gui_input = new gcn::SDLInput();
gui_input = std::make_unique<gcn::SDLInput>();
}

void amiberry_gui_halt()
{
AmigaMonitor* mon = &AMonitors[0];

delete gui_imageLoader;
gui_imageLoader = nullptr;
delete gui_input;
gui_input = nullptr;
delete gui_graphics;
gui_graphics = nullptr;

if (gui_screen != nullptr)
{
SDL_FreeSurface(gui_screen);
Expand Down Expand Up @@ -715,9 +708,9 @@ void check_input()
//-------------------------------------------------
// Quit entire program via Q on keyboard
//-------------------------------------------------
focusHdl = gui_top->_getFocusHandler();
activeWidget = focusHdl->getFocused();
if (dynamic_cast<gcn::TextField*>(activeWidget) == nullptr)
focusHdl.reset(gui_top->_getFocusHandler());
activeWidget.reset(focusHdl->getFocused());
if (dynamic_cast<gcn::TextField*>(activeWidget.get()) == nullptr)
{
// ...but only if we are not in a Textfield...
uae_quit();
Expand Down Expand Up @@ -1023,9 +1016,9 @@ void gui_widgets_init()
//-------------------------------------------------
// Create GUI
//-------------------------------------------------
uae_gui = new gcn::Gui();
uae_gui->setGraphics(gui_graphics);
uae_gui->setInput(gui_input);
uae_gui = std::make_unique<gcn::Gui>();
uae_gui->setGraphics(gui_graphics.get());
uae_gui->setInput(gui_input.get());

//-------------------------------------------------
// Initialize fonts
Expand Down Expand Up @@ -1204,15 +1197,9 @@ void gui_widgets_halt()
delete cmdRestart;
delete cmdStart;
delete cmdHelp;

delete mainButtonActionListener;

delete gui_font;
gui_font = nullptr;
delete gui_top;
gui_top = nullptr;
delete uae_gui;
uae_gui = nullptr;
}

void refresh_all_panels()
Expand Down

0 comments on commit e25d906

Please sign in to comment.