diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index ebc0eeb..a0d9151 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -5,7 +5,9 @@ jobs: Build-GuiSan: runs-on: ubuntu-latest steps: - - run: sudo apt install build-essential libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev libsdl2-mixer-dev scons + - run: | + sudo apt update + sudo apt install build-essential libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev libsdl2-mixer-dev scons - name: Check out repository code uses: actions/checkout@v4 - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." diff --git a/demo/ff/include/ffdemo.hpp b/demo/ff/include/ffdemo.hpp index a9fa572..52ed358 100644 --- a/demo/ff/include/ffdemo.hpp +++ b/demo/ff/include/ffdemo.hpp @@ -103,9 +103,6 @@ class FFDemo : public gcn::ActionListener, public gcn::KeyListener std::unique_ptr mPerIcon; std::unique_ptr mOlofIcon; std::unique_ptr mTomasIcon; - std::unique_ptr mPerImage; - std::unique_ptr mOlofImage; - std::unique_ptr mTomasImage; std::unique_ptr mSplashImage; std::unique_ptr mFontWhite; std::unique_ptr mFontCyan; diff --git a/demo/ff/src/ffdemo.cpp b/demo/ff/src/ffdemo.cpp index 2552ce9..de17533 100644 --- a/demo/ff/src/ffdemo.cpp +++ b/demo/ff/src/ffdemo.cpp @@ -166,13 +166,9 @@ void FFDemo::initMain() mMain->setDimension(gcn::Rectangle(0, 0, 320, 240)); mTop->add(mMain.get()); - mPerImage.reset(gcn::Image::load("images/finalman.png")); - mOlofImage.reset(gcn::Image::load("images/yakslem.png")); - mTomasImage.reset(gcn::Image::load("images/peak.png")); - - mPerIcon = std::make_unique(mPerImage.get()); - mOlofIcon = std::make_unique(mOlofImage.get()); - mTomasIcon = std::make_unique(mTomasImage.get()); + mPerIcon = std::make_unique("images/finalman.png"); + mOlofIcon = std::make_unique("images/yakslem.png"); + mTomasIcon = std::make_unique("images/peak.png"); mPerInfo1 = std::make_unique("\n LV\n HP\n MP"); mPerInfo1->setFont(mFontCyan.get()); diff --git a/examples/widgets_example.hpp b/examples/widgets_example.hpp index 79728f7..8746b62 100644 --- a/examples/widgets_example.hpp +++ b/examples/widgets_example.hpp @@ -41,18 +41,19 @@ namespace WidgetsExample // The global font is static and must be set. gcn::Widget::setGlobalFont(font.get()); - top = std::make_unique < gcn::Container>(); + top = std::make_unique(); // Set the dimension of the top container to match the screen. top->setDimension(gcn::Rectangle(0, 0, width, height)); // Set the top container gui.setTop(top.get()); + + guisanLogoImage.reset(gcn::Image::load("guisan-logo.png")); /* * Create all the widgets */ label = std::make_unique("Label"); - image.reset(gcn::Image::load("guisan.png")); - icon = std::make_unique(image.get()); + icon = std::make_unique("guisan.png"); button = std::make_unique("Button"); @@ -125,8 +126,7 @@ namespace WidgetsExample messageBox->setFrameSize(1); messageBox->addActionListener(this); - guisanLogoImage.reset(gcn::Image::load("guisan-logo.png")); - guisanLogoIcon = std::make_unique(guisanLogoImage.get()); + guisanLogoIcon = std::make_unique(guisanLogoImage); window->add(guisanLogoIcon.get()); window->resizeToContent(); @@ -239,11 +239,10 @@ namespace WidgetsExample std::unique_ptr radioButton3; std::unique_ptr toggleButton; std::unique_ptr slider; // A slider - std::unique_ptr image; // An image for the icon std::unique_ptr window; std::unique_ptr messageBox; std::unique_ptr progress; - std::unique_ptr guisanLogoImage; + std::shared_ptr guisanLogoImage; std::unique_ptr guisanLogoIcon; std::unique_ptr nestedScrollArea; std::unique_ptr nestedContainer; diff --git a/include/guisan/imagefont.hpp b/include/guisan/imagefont.hpp index c25e253..b501d43 100644 --- a/include/guisan/imagefont.hpp +++ b/include/guisan/imagefont.hpp @@ -63,6 +63,8 @@ #include "guisan/platform.hpp" #include "guisan/rectangle.hpp" +#include + namespace gcn { class Color; @@ -119,7 +121,19 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode * @throws Exception when glyph list is incorrect or the font image is * is missing. */ - ImageFont(Image* image, const std::string& glyphs); + [[deprecated]] ImageFont(Image* image, const std::string& glyphs); + + /** + * Constructor. Takes an image containing the font and + * a string containing the glyphs. The glyphs in the string should + * be in the same order as they appear in the font image. + * + * @param image The image with font glyphs. + * @param glyphs The glyphs found in the image. + * @throws Exception when glyph list is incorrect or the font image is + * is missing. + */ + ImageFont(std::shared_ptr image, const std::string& glyphs); /** * Constructor. Takes an image file containing the font and @@ -142,7 +156,7 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode /** * Destructor. */ - ~ImageFont() override; + ~ImageFont() override = default; /** * Draws a glyph. @@ -228,6 +242,12 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode */ Rectangle scanForGlyph(unsigned char glyph, int x, int y, const Color& separator) const; + protected: + /** + * Holds the image with the font data. + */ + std::shared_ptr mImage; + /** * Holds the glyphs areas in the image. */ @@ -247,16 +267,6 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode * Holds the row spacing of the image font. */ int mRowSpacing = 0; - - /** - * Holds the image with the font data. - */ - Image* mImage = nullptr; - - /** - * Holds the filename of the image with the font data. - */ - std::string mFilename; }; } diff --git a/include/guisan/widgets/dropdown.hpp b/include/guisan/widgets/dropdown.hpp index add22e3..8b1f5ee 100644 --- a/include/guisan/widgets/dropdown.hpp +++ b/include/guisan/widgets/dropdown.hpp @@ -66,6 +66,8 @@ #include "guisan/selectionlistener.hpp" #include "guisan/widget.hpp" +#include + namespace gcn { class ListBox; @@ -105,9 +107,21 @@ namespace gcn * @param listBox the listBox to use. * @see ListModel, ScrollArea, ListBox. */ - DropDown(ListModel *listModel = nullptr, - ScrollArea *scrollArea = nullptr, - ListBox *listBox = nullptr); + DropDown(ListModel* listModel, + ScrollArea* scrollArea, + ListBox* listBox = nullptr); + + /** + * Constructor. + * + * @param listModel the ListModel to use. + * @param scrollArea the ScrollArea to use. + * @param listBox the listBox to use. + * @see ListModel, ScrollArea, ListBox. + */ + DropDown(ListModel* listModel = nullptr, + std::shared_ptr scrollArea = nullptr, + std::shared_ptr listBox = nullptr); /** * Destructor. @@ -269,8 +283,8 @@ namespace gcn /** * The scroll area used. */ - ScrollArea* mScrollArea = nullptr; - ListBox* mListBox = nullptr; + std::shared_ptr mScrollArea; + std::shared_ptr mListBox; /** * The internal focus handler used to keep track of focus for the diff --git a/include/guisan/widgets/icon.hpp b/include/guisan/widgets/icon.hpp index ee12acb..d5f2889 100644 --- a/include/guisan/widgets/icon.hpp +++ b/include/guisan/widgets/icon.hpp @@ -61,6 +61,8 @@ #include "guisan/platform.hpp" #include "guisan/widget.hpp" +#include + namespace gcn { /** @@ -79,28 +81,41 @@ namespace gcn * * @param filename The filename of the image to display. */ - Icon(const std::string& filename); + explicit Icon(const std::string& filename); + + /** + * Constructor. + * + * @param image The image to display. + */ + explicit Icon(const Image* image); /** * Constructor. * * @param image The image to display. */ - Icon(const Image* image); + explicit Icon(std::shared_ptr image); /** * Destructor. */ - ~Icon() override; + ~Icon() override = default; /** - * Sets the image to display. Existing image is freed automatically - * if it was loaded internally. + * Sets the image to display. * - * @param image The image to display. + * @param image The image to display. */ void setImage(const Image* image); + /** + * Sets the image to display. + * + * @param image The image to display. + */ + void setImage(std::shared_ptr image); + /** * Gets the current image. * @@ -116,14 +131,7 @@ namespace gcn /** * The image to display. */ - const Image* mImage = nullptr; - - /** - * True if the image has been loaded internally, false otherwise. - * An image not loaded internally should not be deleted in the - * destructor. - */ - bool mInternalImage = false; + std::shared_ptr mImage; }; } diff --git a/include/guisan/widgets/imagebutton.hpp b/include/guisan/widgets/imagebutton.hpp index c476ea3..03f5ba4 100644 --- a/include/guisan/widgets/imagebutton.hpp +++ b/include/guisan/widgets/imagebutton.hpp @@ -60,6 +60,8 @@ #include "guisan/platform.hpp" #include "guisan/widgets/button.hpp" +#include + namespace gcn { class Image; @@ -80,19 +82,26 @@ namespace gcn * * @param filename The filename of the image to display. */ - ImageButton(const std::string& filename); + explicit ImageButton(const std::string& filename); + + /** + * Constructor. + * + * @param image The image to display. + */ + explicit ImageButton(const Image* image); /** * Constructor. * * @param image The image to display. */ - ImageButton(const Image* image); + explicit ImageButton(std::shared_ptr image); /** * Destructor. */ - ~ImageButton() override; + ~ImageButton() override = default; /** * Adjusts the size of the image button to fit the image. @@ -100,13 +109,19 @@ namespace gcn void adjustSize(); /** - * Sets the image to display. Existing Image is freed automatically, - * if it was loaded internally. + * Sets the image to display. * * @param image The image to display. */ void setImage(const Image* image); + /** + * Sets the image to display. + * + * @param image The image to display. + */ + void setImage(std::shared_ptr image); + /** * Gets current image. * @@ -119,14 +134,7 @@ namespace gcn void draw(gcn::Graphics* graphics) override; protected: - const Image* mImage = nullptr; - - /** - * True if the image has been loaded internally, false otherwise. - * An image not loaded internally should not be deleted in the - * destructor. - */ - bool mInternalImage = false; + std::shared_ptr mImage; }; } #endif diff --git a/include/guisan/widgets/imagetextbutton.hpp b/include/guisan/widgets/imagetextbutton.hpp index 96e9933..8d26c10 100644 --- a/include/guisan/widgets/imagetextbutton.hpp +++ b/include/guisan/widgets/imagetextbutton.hpp @@ -86,7 +86,15 @@ namespace gcn * @param image The image to display. * @param caption The text to display. */ - ImageTextButton(Image* image, const std::string& caption); + ImageTextButton(const Image* image, const std::string& caption); + + /** + * Constructor. + * + * @param image The image to display. + * @param caption The text to display. + */ + ImageTextButton(std::shared_ptr image, const std::string& caption); /** * Destructor. diff --git a/include/guisan/widgets/tab.hpp b/include/guisan/widgets/tab.hpp index 67649c7..a76b2ae 100644 --- a/include/guisan/widgets/tab.hpp +++ b/include/guisan/widgets/tab.hpp @@ -57,13 +57,14 @@ #ifndef GCN_TAB_HPP #define GCN_TAB_HPP -#include -#include - #include "guisan/mouselistener.hpp" #include "guisan/platform.hpp" #include "guisan/widget.hpp" +#include +#include +#include + namespace gcn { class Label; @@ -144,7 +145,7 @@ namespace gcn /** * Holds the label of the tab. */ - Label* mLabel = nullptr; + std::unique_ptr