Skip to content

Commit

Permalink
Use std::unique_ptr in examples and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Sep 20, 2024
1 parent 3aaeae1 commit fdd8286
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 684 deletions.
11 changes: 6 additions & 5 deletions demo/ff/include/ffcharacterchooser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,24 @@
#define __FFCHARACTERCHOOSER_HPP

#include <guisan.hpp>
#include <memory>

class FFCharacterChooser : public gcn::Widget, gcn::KeyListener
{
public:
FFCharacterChooser();
~FFCharacterChooser() override;
~FFCharacterChooser() override = default;
void draw(gcn::Graphics* graphics) override;
void keyPressed(gcn::KeyEvent& keyEvent) override;

int getSelected();
int getSelected() const;
void setSelected(int selected);
void setDistance(int distance);

private:
int mSelected;
int mDistance;
gcn::Image* mHand;
int mSelected = 0;
int mDistance = 76;
std::unique_ptr<gcn::Image> mHand;
};

#endif
20 changes: 6 additions & 14 deletions demo/ff/include/ffcontainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,12 @@ class FFContainer : public gcn::Container
void slideContentTo(int y);

private:
int mRealWidth;
int mRealHeight;
int mSlideTarget;
int mCurrentSlide;
int mTime;
bool mShow;

static int mInstances;
static gcn::Image *mCornerUL;
static gcn::Image *mCornerUR;
static gcn::Image *mCornerDL;
static gcn::Image *mCornerDR;
static gcn::Image *mHorizontal;
static gcn::Image *mVertical;
int mRealWidth = 0;
int mRealHeight = 0;
int mSlideTarget = 0;
int mCurrentSlide = 0;
int mTime = -1;
bool mShow = true;
};

#endif
146 changes: 71 additions & 75 deletions demo/ff/include/ffdemo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <SDL_mixer.h>
#include <guisan.hpp>
#include <guisan/sdl.hpp>
#include <memory>

#include "ffcharacterchooser.hpp"
#include "ffcontainer.hpp"
Expand All @@ -68,17 +69,12 @@ class FFDemo : public gcn::ActionListener, public gcn::KeyListener
private:
void input();
void initMain();
void cleanMain();
void initStatus();
void cleanStatus();
void initMagicSkills();
void cleanMagicSkills();
void initItems();
void cleanItems();
void initAbout();
void cleanAbout();

bool mRunning;
bool mRunning = true;

SDL_Surface* mScreen;
SDL_Window* mWindow;
Expand All @@ -88,75 +84,75 @@ class FFDemo : public gcn::ActionListener, public gcn::KeyListener
Mix_Chunk* mChooseSound;
Mix_Chunk* mEscapeSound;

gcn::SDLGraphics* mSDLGraphics;
gcn::SDLInput* mSDLInput;
gcn::SDLImageLoader* mSDLImageLoader;
gcn::Gui* mGui;

gcn::Container* mTop;
FFContainer* mMain;
FFContainer* mStatus;
FFContainer* mItems;
FFContainer* mMagicSkills;
FFContainer* mTime;
FFContainer* mGoldFootsteps;
FFContainer* mMenu;
FFContainer* mAbout;
FFContainer* mItemsInfo;

gcn::Icon* mPerIcon;
gcn::Icon* mOlofIcon;
gcn::Icon* mTomasIcon;
gcn::Image* mPerImage;
gcn::Image* mOlofImage;
gcn::Image* mTomasImage;
gcn::Image* mSplashImage;
gcn::Font* mFontWhite;
gcn::Font* mFontCyan;

FFListBox *mMenuList;

FFListBox *mMagicSkillsList;
FFScrollArea *mMagicSkillsScroll;

StringListModel *mPerSkills;
StringListModel *mPerMagic;
StringListModel *mOlofSkills;
StringListModel *mOlofMagic;
StringListModel *mTomasSkills;
StringListModel *mTomasMagic;

gcn::TextBox* mPerInfo1;
gcn::TextBox* mOlofInfo1;
gcn::TextBox* mTomasInfo1;
gcn::TextBox* mPerInfo2;
gcn::TextBox* mOlofInfo2;
gcn::TextBox* mTomasInfo2;
gcn::TextBox* mItemsInfoInfo;
gcn::TextBox* mOlofStatus1;
gcn::TextBox* mOlofStatus2;
gcn::TextBox* mPerStatus1;
gcn::TextBox* mPerStatus2;
gcn::TextBox* mTomasStatus1;
gcn::TextBox* mTomasStatus2;

gcn::TextBox* mGoldFootstepsInfo1;
gcn::TextBox* mGoldFootstepsInfo2;
gcn::Label* mTimeLabel1;
gcn::Label* mTimeLabel2;

gcn::Label* mNavigationLabel;

gcn::TextBox* mAboutInfo;
FFScrollArea *mAboutScrollArea;

FFListBox *mItemsList;
FFScrollArea *mItemsScrollArea;
StringListModel *mItemsListModel;
StringListModel *mItemsInfoListModel;
StringListModel *mMenuListModel;

FFCharacterChooser* mCharacterChooser;
std::unique_ptr<gcn::SDLGraphics> mSDLGraphics;
std::unique_ptr<gcn::SDLInput> mSDLInput;
std::unique_ptr<gcn::SDLImageLoader> mSDLImageLoader;
std::unique_ptr<gcn::Gui> mGui;

std::unique_ptr<gcn::Container> mTop;
std::unique_ptr<FFContainer> mMain;
std::unique_ptr<FFContainer> mStatus;
std::unique_ptr<FFContainer> mItems;
std::unique_ptr<FFContainer> mMagicSkills;
std::unique_ptr<FFContainer> mTime;
std::unique_ptr<FFContainer> mGoldFootsteps;
std::unique_ptr<FFContainer> mMenu;
std::unique_ptr<FFContainer> mAbout;
std::unique_ptr<FFContainer> mItemsInfo;

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;

std::unique_ptr<FFListBox> mMenuList;

std::unique_ptr<FFListBox> mMagicSkillsList;
std::unique_ptr<FFScrollArea> mMagicSkillsScroll;

std::unique_ptr<StringListModel> mPerSkills;
std::unique_ptr<StringListModel> mPerMagic;
std::unique_ptr<StringListModel> mOlofSkills;
std::unique_ptr<StringListModel> mOlofMagic;
std::unique_ptr<StringListModel> mTomasSkills;
std::unique_ptr<StringListModel> mTomasMagic;

std::unique_ptr<gcn::TextBox> mPerInfo1;
std::unique_ptr<gcn::TextBox> mOlofInfo1;
std::unique_ptr<gcn::TextBox> mTomasInfo1;
std::unique_ptr<gcn::TextBox> mPerInfo2;
std::unique_ptr<gcn::TextBox> mOlofInfo2;
std::unique_ptr<gcn::TextBox> mTomasInfo2;
std::unique_ptr<gcn::TextBox> mItemsInfoInfo;
std::unique_ptr<gcn::TextBox> mOlofStatus1;
std::unique_ptr<gcn::TextBox> mOlofStatus2;
std::unique_ptr<gcn::TextBox> mPerStatus1;
std::unique_ptr<gcn::TextBox> mPerStatus2;
std::unique_ptr<gcn::TextBox> mTomasStatus1;
std::unique_ptr<gcn::TextBox> mTomasStatus2;

std::unique_ptr<gcn::TextBox> mGoldFootstepsInfo1;
std::unique_ptr<gcn::TextBox> mGoldFootstepsInfo2;
std::unique_ptr<gcn::Label> mTimeLabel1;
std::unique_ptr<gcn::Label> mTimeLabel2;

std::unique_ptr<gcn::Label> mNavigationLabel;

std::unique_ptr<gcn::TextBox> mAboutInfo;
std::unique_ptr<FFScrollArea> mAboutScrollArea;

std::unique_ptr<FFListBox> mItemsList;
std::unique_ptr<FFScrollArea> mItemsScrollArea;
std::unique_ptr<StringListModel> mItemsListModel;
std::unique_ptr<StringListModel> mItemsInfoListModel;
std::unique_ptr<StringListModel> mMenuListModel;

std::unique_ptr<FFCharacterChooser> mCharacterChooser;
};

#endif
4 changes: 0 additions & 4 deletions demo/ff/include/fflistbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ class FFListBox : public gcn::ListBox
void draw(gcn::Graphics* graphics) override;

void setSelected(int i);

private:
static gcn::Image *mHand;
static int mInstances;
};

#endif
14 changes: 3 additions & 11 deletions demo/ff/src/ffcharacterchooser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,24 @@

#include "ffcharacterchooser.hpp"

FFCharacterChooser::FFCharacterChooser()
FFCharacterChooser::FFCharacterChooser() : mHand(gcn::Image::load("images/hand.png"))
{
setWidth(20);
setHeight(240);
mSelected = 0;
mDistance = 76;
mHand = gcn::Image::load("images/hand.png");
setFocusable(true);
addKeyListener(this);
setFrameSize(0);
}

FFCharacterChooser::~FFCharacterChooser()
{
delete mHand;
}

void FFCharacterChooser::draw(gcn::Graphics* graphics)
{
if (isFocused())
{
graphics->drawImage(mHand, 0, mDistance*mSelected);
graphics->drawImage(mHand.get(), 0, mDistance * mSelected);
}
}

int FFCharacterChooser::getSelected()
int FFCharacterChooser::getSelected() const
{
return mSelected;
}
Expand Down
75 changes: 38 additions & 37 deletions demo/ff/src/ffcontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,44 @@
*/

#include "ffcontainer.hpp"

#include <cmath>
#include <memory>

#ifndef _MSC_VER
#include <SDL2/SDL.h>
#else
#include <SDL.h>
#endif // !_MSC_

int FFContainer::mInstances = 0;
gcn::Image *FFContainer::mCornerUL = 0;
gcn::Image *FFContainer::mCornerUR = 0;
gcn::Image *FFContainer::mCornerDL = 0;
gcn::Image *FFContainer::mCornerDR = 0;
gcn::Image *FFContainer::mHorizontal = 0;
gcn::Image *FFContainer::mVertical = 0;
namespace
{
int mInstances = 0;
std::unique_ptr<gcn::Image> mCornerUL;
std::unique_ptr<gcn::Image> mCornerUR;
std::unique_ptr<gcn::Image> mCornerDL;
std::unique_ptr<gcn::Image> mCornerDR;
std::unique_ptr<gcn::Image> mHorizontal;
std::unique_ptr<gcn::Image> mVertical;
} // namespace

FFContainer::FFContainer()
{
if (mInstances == 0)
{
mCornerUL = gcn::Image::load("images/cornerul.png");
mCornerUR = gcn::Image::load("images/cornerur.png");
mCornerDL = gcn::Image::load("images/cornerdl.png");
mCornerDR = gcn::Image::load("images/cornerdr.png");
mHorizontal = gcn::Image::load("images/horizontal.png");
mVertical = gcn::Image::load("images/vertical.png");
mCornerUL.reset(gcn::Image::load("images/cornerul.png"));
mCornerUR.reset(gcn::Image::load("images/cornerur.png"));
mCornerDL.reset(gcn::Image::load("images/cornerdl.png"));
mCornerDR.reset(gcn::Image::load("images/cornerdr.png"));
mHorizontal.reset(gcn::Image::load("images/horizontal.png"));
mVertical.reset(gcn::Image::load("images/vertical.png"));
}

mInstances++;

mRealWidth = 0;
mRealHeight = 0;
mTime = -1;
mShow = true;
Container::setWidth(0);
Container::setHeight(0);
mSlideTarget = 0;
mCurrentSlide = 0;

setFrameSize(0);
}

Expand All @@ -88,12 +89,12 @@ FFContainer::~FFContainer()

if (mInstances == 0)
{
delete mCornerUL;
delete mCornerUR;
delete mCornerDL;
delete mCornerDR;
delete mHorizontal;
delete mVertical;
mCornerUL = nullptr;
mCornerUR = nullptr;
mCornerDL = nullptr;
mCornerDR = nullptr;
mHorizontal = nullptr;
mVertical = nullptr;
}
}

Expand All @@ -116,24 +117,24 @@ void FFContainer::draw(gcn::Graphics* graphics)

for (i = 5; i < getHeight()-10; i+=5)
{
graphics->drawImage(mVertical, 0, i);
graphics->drawImage(mVertical, getWidth()-4, i);
graphics->drawImage(mVertical.get(), 0, i);
graphics->drawImage(mVertical.get(), getWidth() - 4, i);
}
graphics->drawImage(mVertical, 0, 0, 0, i, 4, getHeight()-5-i);
graphics->drawImage(mVertical, 0, 0, getWidth()-4, i, 4, getHeight()-5-i);
graphics->drawImage(mVertical.get(), 0, 0, 0, i, 4, getHeight() - 5 - i);
graphics->drawImage(mVertical.get(), 0, 0, getWidth() - 4, i, 4, getHeight() - 5 - i);

for (i = 5; i < getWidth()-10; i+=5)
{
graphics->drawImage(mHorizontal, i, 0);
graphics->drawImage(mHorizontal, i, getHeight()-4);
graphics->drawImage(mHorizontal.get(), i, 0);
graphics->drawImage(mHorizontal.get(), i, getHeight() - 4);
}
graphics->drawImage(mHorizontal, 0, 0, i, 0, getWidth()-5-i, 4);
graphics->drawImage(mHorizontal, 0, 0, i, getHeight()-4, getWidth()-5-i, 4);
graphics->drawImage(mHorizontal.get(), 0, 0, i, 0, getWidth() - 5 - i, 4);
graphics->drawImage(mHorizontal.get(), 0, 0, i, getHeight() - 4, getWidth() - 5 - i, 4);

graphics->drawImage(mCornerUL, 0, 0);
graphics->drawImage(mCornerUR, getWidth()-5, 0);
graphics->drawImage(mCornerDL, 0, getHeight()-5);
graphics->drawImage(mCornerDR, getWidth()-5, getHeight()-5);
graphics->drawImage(mCornerUL.get(), 0, 0);
graphics->drawImage(mCornerUR.get(), getWidth() - 5, 0);
graphics->drawImage(mCornerDL.get(), 0, getHeight() - 5);
graphics->drawImage(mCornerDR.get(), getWidth() - 5, getHeight() - 5);
}

void FFContainer::logic()
Expand Down
Loading

0 comments on commit fdd8286

Please sign in to comment.