Skip to content

Commit

Permalink
Merge pull request #123 from Jarod42/smart_pointer
Browse files Browse the repository at this point in the history
Use smart pointers
  • Loading branch information
Jarod42 authored Oct 16, 2024
2 parents 1d79d4c + 5715219 commit 36b9f9c
Show file tree
Hide file tree
Showing 21 changed files with 287 additions and 286 deletions.
3 changes: 0 additions & 3 deletions demo/ff/include/ffdemo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ class FFDemo : public gcn::ActionListener, public gcn::KeyListener
std::unique_ptr<gcn::Icon> mPerIcon;
std::unique_ptr<gcn::Icon> mOlofIcon;
std::unique_ptr<gcn::Icon> mTomasIcon;
std::unique_ptr<gcn::Image> mPerImage;
std::unique_ptr<gcn::Image> mOlofImage;
std::unique_ptr<gcn::Image> mTomasImage;
std::unique_ptr<gcn::Image> mSplashImage;
std::unique_ptr<gcn::Font> mFontWhite;
std::unique_ptr<gcn::Font> mFontCyan;
Expand Down
10 changes: 3 additions & 7 deletions demo/ff/src/ffdemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gcn::Icon>(mPerImage.get());
mOlofIcon = std::make_unique<gcn::Icon>(mOlofImage.get());
mTomasIcon = std::make_unique<gcn::Icon>(mTomasImage.get());
mPerIcon = std::make_unique<gcn::Icon>("images/finalman.png");
mOlofIcon = std::make_unique<gcn::Icon>("images/yakslem.png");
mTomasIcon = std::make_unique<gcn::Icon>("images/peak.png");

mPerInfo1 = std::make_unique<gcn::TextBox>("\n LV\n HP\n MP");
mPerInfo1->setFont(mFontCyan.get());
Expand Down
13 changes: 6 additions & 7 deletions examples/widgets_example.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gcn::Container>();
// 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<gcn::Label>("Label");

image.reset(gcn::Image::load("guisan.png"));
icon = std::make_unique<gcn::Icon>(image.get());
icon = std::make_unique<gcn::Icon>("guisan.png");

button = std::make_unique<gcn::Button>("Button");

Expand Down Expand Up @@ -125,8 +126,7 @@ namespace WidgetsExample
messageBox->setFrameSize(1);
messageBox->addActionListener(this);

guisanLogoImage.reset(gcn::Image::load("guisan-logo.png"));
guisanLogoIcon = std::make_unique<gcn::Icon>(guisanLogoImage.get());
guisanLogoIcon = std::make_unique<gcn::Icon>(guisanLogoImage);
window->add(guisanLogoIcon.get());
window->resizeToContent();

Expand Down Expand Up @@ -239,11 +239,10 @@ namespace WidgetsExample
std::unique_ptr<gcn::RadioButton> radioButton3;
std::unique_ptr<gcn::ToggleButton> toggleButton;
std::unique_ptr<gcn::Slider> slider; // A slider
std::unique_ptr<gcn::Image> image; // An image for the icon
std::unique_ptr<gcn::Window> window;
std::unique_ptr<gcn::MessageBox> messageBox;
std::unique_ptr<gcn::ProgressBar> progress;
std::unique_ptr<gcn::Image> guisanLogoImage;
std::shared_ptr<gcn::Image> guisanLogoImage;
std::unique_ptr<gcn::Icon> guisanLogoIcon;
std::unique_ptr<gcn::ScrollArea> nestedScrollArea;
std::unique_ptr<gcn::Container> nestedContainer;
Expand Down
34 changes: 22 additions & 12 deletions include/guisan/imagefont.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
#include "guisan/platform.hpp"
#include "guisan/rectangle.hpp"

#include <memory>

namespace gcn
{
class Color;
Expand Down Expand Up @@ -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> image, const std::string& glyphs);

/**
* Constructor. Takes an image file containing the font and
Expand All @@ -142,7 +156,7 @@ pqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); @endcode
/**
* Destructor.
*/
~ImageFont() override;
~ImageFont() override = default;

/**
* Draws a glyph.
Expand Down Expand Up @@ -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<Image> mImage;

/**
* Holds the glyphs areas in the image.
*/
Expand All @@ -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;
};
}

Expand Down
24 changes: 19 additions & 5 deletions include/guisan/widgets/dropdown.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
#include "guisan/selectionlistener.hpp"
#include "guisan/widget.hpp"

#include <memory>

namespace gcn
{
class ListBox;
Expand Down Expand Up @@ -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> scrollArea = nullptr,
std::shared_ptr<ListBox> listBox = nullptr);

/**
* Destructor.
Expand Down Expand Up @@ -269,8 +283,8 @@ namespace gcn
/**
* The scroll area used.
*/
ScrollArea* mScrollArea = nullptr;
ListBox* mListBox = nullptr;
std::shared_ptr<ScrollArea> mScrollArea;
std::shared_ptr<ListBox> mListBox;

/**
* The internal focus handler used to keep track of focus for the
Expand Down
36 changes: 22 additions & 14 deletions include/guisan/widgets/icon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#include "guisan/platform.hpp"
#include "guisan/widget.hpp"

#include <memory>

namespace gcn
{
/**
Expand All @@ -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<const Image> 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<const Image> image);

/**
* Gets the current image.
*
Expand All @@ -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<const Image> mImage;
};
}

Expand Down
34 changes: 21 additions & 13 deletions include/guisan/widgets/imagebutton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
#include "guisan/platform.hpp"
#include "guisan/widgets/button.hpp"

#include <memory>

namespace gcn
{
class Image;
Expand All @@ -80,33 +82,46 @@ 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<const Image> image);

/**
* Destructor.
*/
~ImageButton() override;
~ImageButton() override = default;

/**
* Adjusts the size of the image button to fit the image.
*/
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<const Image> image);

/**
* Gets current image.
*
Expand All @@ -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<const Image> mImage;
};
}
#endif
10 changes: 9 additions & 1 deletion include/guisan/widgets/imagetextbutton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const Image> image, const std::string& caption);

/**
* Destructor.
Expand Down
9 changes: 5 additions & 4 deletions include/guisan/widgets/tab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@
#ifndef GCN_TAB_HPP
#define GCN_TAB_HPP

#include <map>
#include <string>

#include "guisan/mouselistener.hpp"
#include "guisan/platform.hpp"
#include "guisan/widget.hpp"

#include <map>
#include <memory>
#include <string>

namespace gcn
{
class Label;
Expand Down Expand Up @@ -144,7 +145,7 @@ namespace gcn
/**
* Holds the label of the tab.
*/
Label* mLabel = nullptr;
std::unique_ptr<Label> mLabel;

/**
* Holds the tabbed area the tab is a part of.
Expand Down
Loading

0 comments on commit 36b9f9c

Please sign in to comment.