From fdd828641f9447014aff1a92274c644e7f471d11 Mon Sep 17 00:00:00 2001 From: Jarod42 Date: Fri, 20 Sep 2024 11:04:49 +0200 Subject: [PATCH] Use `std::unique_ptr` in examples and demo --- demo/ff/include/ffcharacterchooser.hpp | 11 +- demo/ff/include/ffcontainer.hpp | 20 +- demo/ff/include/ffdemo.hpp | 146 ++++--- demo/ff/include/fflistbox.hpp | 4 - demo/ff/src/ffcharacterchooser.cpp | 14 +- demo/ff/src/ffcontainer.cpp | 75 ++-- demo/ff/src/ffdemo.cpp | 518 ++++++++++--------------- demo/ff/src/fflistbox.cpp | 24 +- examples/action_example.hpp | 61 ++- examples/helloworld_example.hpp | 33 +- examples/rickroll_example.hpp | 88 ++--- examples/widgets_example.hpp | 186 ++++----- 12 files changed, 496 insertions(+), 684 deletions(-) diff --git a/demo/ff/include/ffcharacterchooser.hpp b/demo/ff/include/ffcharacterchooser.hpp index 2814401..44b9287 100644 --- a/demo/ff/include/ffcharacterchooser.hpp +++ b/demo/ff/include/ffcharacterchooser.hpp @@ -45,23 +45,24 @@ #define __FFCHARACTERCHOOSER_HPP #include +#include 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 mHand; }; #endif diff --git a/demo/ff/include/ffcontainer.hpp b/demo/ff/include/ffcontainer.hpp index 24fe040..6c67450 100644 --- a/demo/ff/include/ffcontainer.hpp +++ b/demo/ff/include/ffcontainer.hpp @@ -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 diff --git a/demo/ff/include/ffdemo.hpp b/demo/ff/include/ffdemo.hpp index 67992ac..a9fa572 100644 --- a/demo/ff/include/ffdemo.hpp +++ b/demo/ff/include/ffdemo.hpp @@ -48,6 +48,7 @@ #include #include #include +#include #include "ffcharacterchooser.hpp" #include "ffcontainer.hpp" @@ -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; @@ -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 mSDLGraphics; + std::unique_ptr mSDLInput; + std::unique_ptr mSDLImageLoader; + std::unique_ptr mGui; + + std::unique_ptr mTop; + std::unique_ptr mMain; + std::unique_ptr mStatus; + std::unique_ptr mItems; + std::unique_ptr mMagicSkills; + std::unique_ptr mTime; + std::unique_ptr mGoldFootsteps; + std::unique_ptr mMenu; + std::unique_ptr mAbout; + std::unique_ptr mItemsInfo; + + 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; + + std::unique_ptr mMenuList; + + std::unique_ptr mMagicSkillsList; + std::unique_ptr mMagicSkillsScroll; + + std::unique_ptr mPerSkills; + std::unique_ptr mPerMagic; + std::unique_ptr mOlofSkills; + std::unique_ptr mOlofMagic; + std::unique_ptr mTomasSkills; + std::unique_ptr mTomasMagic; + + std::unique_ptr mPerInfo1; + std::unique_ptr mOlofInfo1; + std::unique_ptr mTomasInfo1; + std::unique_ptr mPerInfo2; + std::unique_ptr mOlofInfo2; + std::unique_ptr mTomasInfo2; + std::unique_ptr mItemsInfoInfo; + std::unique_ptr mOlofStatus1; + std::unique_ptr mOlofStatus2; + std::unique_ptr mPerStatus1; + std::unique_ptr mPerStatus2; + std::unique_ptr mTomasStatus1; + std::unique_ptr mTomasStatus2; + + std::unique_ptr mGoldFootstepsInfo1; + std::unique_ptr mGoldFootstepsInfo2; + std::unique_ptr mTimeLabel1; + std::unique_ptr mTimeLabel2; + + std::unique_ptr mNavigationLabel; + + std::unique_ptr mAboutInfo; + std::unique_ptr mAboutScrollArea; + + std::unique_ptr mItemsList; + std::unique_ptr mItemsScrollArea; + std::unique_ptr mItemsListModel; + std::unique_ptr mItemsInfoListModel; + std::unique_ptr mMenuListModel; + + std::unique_ptr mCharacterChooser; }; #endif diff --git a/demo/ff/include/fflistbox.hpp b/demo/ff/include/fflistbox.hpp index 76f1329..567f3e3 100644 --- a/demo/ff/include/fflistbox.hpp +++ b/demo/ff/include/fflistbox.hpp @@ -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 diff --git a/demo/ff/src/ffcharacterchooser.cpp b/demo/ff/src/ffcharacterchooser.cpp index 58043eb..9a8cc1f 100644 --- a/demo/ff/src/ffcharacterchooser.cpp +++ b/demo/ff/src/ffcharacterchooser.cpp @@ -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; } diff --git a/demo/ff/src/ffcontainer.cpp b/demo/ff/src/ffcontainer.cpp index 3c1d715..486fa77 100644 --- a/demo/ff/src/ffcontainer.cpp +++ b/demo/ff/src/ffcontainer.cpp @@ -42,43 +42,44 @@ */ #include "ffcontainer.hpp" + #include +#include + #ifndef _MSC_VER #include #else #include #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 mCornerUL; + std::unique_ptr mCornerUR; + std::unique_ptr mCornerDL; + std::unique_ptr mCornerDR; + std::unique_ptr mHorizontal; + std::unique_ptr 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); } @@ -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; } } @@ -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() diff --git a/demo/ff/src/ffdemo.cpp b/demo/ff/src/ffdemo.cpp index 3ebae5f..6d9627e 100644 --- a/demo/ff/src/ffdemo.cpp +++ b/demo/ff/src/ffdemo.cpp @@ -48,8 +48,6 @@ FFDemo::FFDemo() { - mRunning = true; - /* * Here we initialize SDL as we would do with any SDL application. */ @@ -66,68 +64,71 @@ FFDemo::FFDemo() mChooseSound = Mix_LoadWAV("sound/sound1.wav"); mEscapeSound = Mix_LoadWAV("sound/sound2.wav"); - mSDLImageLoader = new gcn::SDLImageLoader(); - gcn::Image::setImageLoader(mSDLImageLoader); - mSDLGraphics = new gcn::SDLGraphics(); + mSDLImageLoader = std::make_unique(); + gcn::Image::setImageLoader(mSDLImageLoader.get()); + mSDLGraphics = std::make_unique(); mSDLGraphics->setTarget(mScreen); - mSDLInput = new gcn::SDLInput(); - - mSplashImage = gcn::Image::load("images/splash.png"); + mSDLInput = std::make_unique(); - mTop = new gcn::Container(); + mSplashImage.reset(gcn::Image::load("images/splash.png")); + + mTop = std::make_unique(); mTop->setBaseColor(gcn::Color(0x000000)); mTop->setDimension(gcn::Rectangle(0, 0, 320, 240)); - mGui = new gcn::Gui(); + mGui = std::make_unique(); mGui->setTabbingEnabled(false); - mGui->setGraphics(mSDLGraphics); - mGui->setInput(mSDLInput); - mGui->setTop(mTop); - mFontWhite = new gcn::ImageFont("images/rpgfont.png", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""); - mFontCyan = new gcn::ImageFont("images/rpgfont2.png", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""); - gcn::Widget::setGlobalFont(mFontWhite); - + mGui->setGraphics(mSDLGraphics.get()); + mGui->setInput(mSDLInput.get()); + mGui->setTop(mTop.get()); + mFontWhite = std::make_unique( + "images/rpgfont.png", + " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""); + mFontCyan = std::make_unique( + "images/rpgfont2.png", + " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""); + gcn::Widget::setGlobalFont(mFontWhite.get()); + initMain(); - mMenu = new FFContainer(); + mMenu = std::make_unique(); mMenu->setDimension(gcn::Rectangle(230, 0, 90, 130)); mMenu->setOpaque(false); - mTop->add(mMenu); - - mGoldFootsteps = new FFContainer(); + mTop->add(mMenu.get()); + + mGoldFootsteps = std::make_unique(); mGoldFootsteps->setDimension(gcn::Rectangle(210, 170, 110, 70)); mGoldFootsteps->setOpaque(false); mGoldFootsteps->setFrameSize(0); - mTop->add(mGoldFootsteps); + mTop->add(mGoldFootsteps.get()); - mTime = new FFContainer(); + mTime = std::make_unique(); mTime->setDimension(gcn::Rectangle(230, 130, 90, 40)); mTime->setOpaque(false); - mTop->add(mTime); - - mGoldFootstepsInfo1 = new gcn::TextBox("Steps\n\nGP"); - mGoldFootstepsInfo1->setFont(mFontCyan); + mTop->add(mTime.get()); + + mGoldFootstepsInfo1 = std::make_unique("Steps\n\nGP"); + mGoldFootstepsInfo1->setFont(mFontCyan.get()); mGoldFootstepsInfo1->setOpaque(false); mGoldFootstepsInfo1->setEditable(false); mGoldFootstepsInfo1->setFocusable(false); mGoldFootstepsInfo1->setFrameSize(0); - - mGoldFootstepsInfo2 = new gcn::TextBox("\n 9119092\n\n 1009213"); + + mGoldFootstepsInfo2 = std::make_unique("\n 9119092\n\n 1009213"); mGoldFootstepsInfo2->setOpaque(false); mGoldFootstepsInfo2->setEditable(false); mGoldFootstepsInfo2->setFocusable(false); mGoldFootstepsInfo2->setFrameSize(0); - mTimeLabel1 = new gcn::Label("Time"); - mTimeLabel1->setFont(mFontCyan); - mTimeLabel2 = new gcn::Label(); - mTime->add(mTimeLabel1, 5, 5); - mTime->add(mTimeLabel2, 22, 20); + mTimeLabel1 = std::make_unique("Time"); + mTimeLabel1->setFont(mFontCyan.get()); + mTimeLabel2 = std::make_unique(); + mTime->add(mTimeLabel1.get(), 5, 5); + mTime->add(mTimeLabel2.get(), 22, 20); - - mGoldFootsteps->add(mGoldFootstepsInfo2, 5, 0); - mGoldFootsteps->add(mGoldFootstepsInfo1, 5, 5); - - mMenuListModel = new StringListModel(); + mGoldFootsteps->add(mGoldFootstepsInfo2.get(), 5, 0); + mGoldFootsteps->add(mGoldFootstepsInfo1.get(), 5, 5); + + mMenuListModel = std::make_unique(); mMenuListModel->add("Items"); mMenuListModel->add("Status"); mMenuListModel->add("Skills"); @@ -135,12 +136,12 @@ FFDemo::FFDemo() mMenuListModel->add("About"); mMenuListModel->add(""); mMenuListModel->add("Quit"); - - mMenuList = new FFListBox(); + + mMenuList = std::make_unique(); mMenuList->setActionEventId("menu"); - mMenuList->addActionListener(this); - mMenuList->setListModel(mMenuListModel); - mMenu->add(mMenuList, 5, 5); + mMenuList->addActionListener(this); + mMenuList->setListModel(mMenuListModel.get()); + mMenu->add(mMenuList.get(), 5, 5); mMenuList->setSelected(0); mMenuList->requestFocus(); @@ -152,258 +153,202 @@ FFDemo::FFDemo() FFDemo::~FFDemo() { - cleanStatus(); - cleanAbout(); - cleanItems(); - cleanMagicSkills(); - cleanMain(); - - - delete mSplashImage; - - delete mTimeLabel1; - delete mTimeLabel2; - delete mTime; - - delete mGoldFootstepsInfo1; - delete mGoldFootstepsInfo2; - delete mGoldFootsteps; - - delete mMenuList; - delete mMenuListModel; - delete mMenu; - - delete mMain; - - delete mFontWhite; - delete mFontCyan; - delete mTop; - delete mGui; - - delete mSDLInput; - delete mSDLGraphics; - delete mSDLImageLoader; - Mix_FreeChunk(mChooseSound); Mix_FreeChunk(mEscapeSound); Mix_CloseAudio(); - + SDL_Quit(); } void FFDemo::initMain() { - mMain = new FFContainer(); + mMain = std::make_unique(); mMain->setDimension(gcn::Rectangle(0, 0, 320, 240)); - mTop->add(mMain); + mTop->add(mMain.get()); - mPerImage = gcn::Image::load("images/finalman.png"); - mOlofImage = gcn::Image::load("images/yakslem.png"); - mTomasImage = gcn::Image::load("images/peak.png"); + 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 = new gcn::Icon(mPerImage); - mOlofIcon = new gcn::Icon(mOlofImage); - mTomasIcon = new gcn::Icon(mTomasImage); + mPerIcon = std::make_unique(mPerImage.get()); + mOlofIcon = std::make_unique(mOlofImage.get()); + mTomasIcon = std::make_unique(mTomasImage.get()); - mPerInfo1 = new gcn::TextBox("\n LV\n HP\n MP"); - mPerInfo1->setFont(mFontCyan); + mPerInfo1 = std::make_unique("\n LV\n HP\n MP"); + mPerInfo1->setFont(mFontCyan.get()); mPerInfo1->setOpaque(false); mPerInfo1->setEditable(false); mPerInfo1->setFocusable(false); mPerInfo1->setFrameSize(0); - - mPerInfo2 = new gcn::TextBox("FINALMAN\n 13\n 12/ 336\n 33/ 40"); + + mPerInfo2 = std::make_unique("FINALMAN\n 13\n 12/ 336\n 33/ 40"); mPerInfo2->setOpaque(false); mPerInfo2->setEditable(false); mPerInfo2->setFocusable(false); mPerInfo2->setFrameSize(0); - - mOlofInfo1 = new gcn::TextBox("\n LV\n HP\n MP"); - mOlofInfo1->setFont(mFontCyan); + + mOlofInfo1 = std::make_unique("\n LV\n HP\n MP"); + mOlofInfo1->setFont(mFontCyan.get()); mOlofInfo1->setOpaque(false); mOlofInfo1->setEditable(false); mOlofInfo1->setFocusable(false); mOlofInfo1->setFrameSize(0); - - mOlofInfo2 = new gcn::TextBox("YAKSLEM\n 41\n 1304/2932\n 298/ 300"); + + mOlofInfo2 = std::make_unique("YAKSLEM\n 41\n 1304/2932\n 298/ 300"); mOlofInfo2->setOpaque(false); mOlofInfo2->setEditable(false); mOlofInfo2->setFocusable(false); mOlofInfo2->setFrameSize(0); - - mTomasInfo1 = new gcn::TextBox("\n LV\n HP\n MP"); - mTomasInfo1->setFont(mFontCyan); + + mTomasInfo1 = std::make_unique("\n LV\n HP\n MP"); + mTomasInfo1->setFont(mFontCyan.get()); mTomasInfo1->setOpaque(false); mTomasInfo1->setEditable(false); mTomasInfo1->setFocusable(false); mTomasInfo1->setFrameSize(0); - - mTomasInfo2 = new gcn::TextBox("PEAK\n 6\n 101/ 101\n 0/ 0"); + + mTomasInfo2 = std::make_unique("PEAK\n 6\n 101/ 101\n 0/ 0"); mTomasInfo2->setOpaque(false); mTomasInfo2->setEditable(false); mTomasInfo2->setFocusable(false); mTomasInfo2->setFrameSize(0); - + int offset = 6; - mMain->add(mPerIcon, 10, offset); - mMain->add(mPerInfo2, 60, offset); - mMain->add(mPerInfo1, 60, offset); + mMain->add(mPerIcon.get(), 10, offset); + mMain->add(mPerInfo2.get(), 60, offset); + mMain->add(mPerInfo1.get(), 60, offset); offset += 76; - mMain->add(mOlofIcon, 10, offset); - mMain->add(mOlofInfo2, 60, offset); - mMain->add(mOlofInfo1, 60, offset); + mMain->add(mOlofIcon.get(), 10, offset); + mMain->add(mOlofInfo2.get(), 60, offset); + mMain->add(mOlofInfo1.get(), 60, offset); offset += 76; - mMain->add(mTomasIcon, 10, offset); - mMain->add(mTomasInfo2, 60, offset); - mMain->add(mTomasInfo1, 60, offset); - - mCharacterChooser = new FFCharacterChooser(); + mMain->add(mTomasIcon.get(), 10, offset); + mMain->add(mTomasInfo2.get(), 60, offset); + mMain->add(mTomasInfo1.get(), 60, offset); + + mCharacterChooser = std::make_unique(); mCharacterChooser->setActionEventId("character"); mCharacterChooser->addActionListener(this); - mMain->add(mCharacterChooser, 5, 25); + mMain->add(mCharacterChooser.get(), 5, 25); - mNavigationLabel = new gcn::Label("STATUS "); + mNavigationLabel = std::make_unique("STATUS "); mNavigationLabel->setVisible(false); - mMain->add(mNavigationLabel, 230, 20); - - -} - -void FFDemo::cleanMain() -{ - delete mNavigationLabel; - delete mCharacterChooser; - - delete mPerInfo1; - delete mOlofInfo1; - delete mTomasInfo1; - - delete mPerInfo2; - delete mOlofInfo2; - delete mTomasInfo2; - - delete mPerIcon; - delete mOlofIcon; - delete mTomasIcon; - - delete mPerImage; - delete mOlofImage; - delete mTomasImage; + mMain->add(mNavigationLabel.get(), 230, 20); } void FFDemo::initStatus() { - mStatus = new FFContainer(); + mStatus = std::make_unique(); mStatus->setDimension(gcn::Rectangle(0, 80, 320, 160)); mStatus->setVisible(false); - mTop->add(mStatus); + mTop->add(mStatus.get()); - mPerStatus1 = new gcn::TextBox(" STR EXP\n" - " INT NEXT\n" - " DEF\n" - " MAGDEF\n"); - mPerStatus1->setFont(mFontCyan); + mPerStatus1 = std::make_unique(" STR EXP\n" + " INT NEXT\n" + " DEF\n" + " MAGDEF\n"); + mPerStatus1->setFont(mFontCyan.get()); mPerStatus1->setOpaque(false); mPerStatus1->setEditable(false); mPerStatus1->setFocusable(false); mPerStatus1->setVisible(false); mPerStatus1->setFrameSize(0); - - mPerStatus2 = new gcn::TextBox(" 32 12382\n" - " 56 13872\n" - " 12\n" - " 11\n\n" - " FINALMAN is immune against\n" - " poisinous attacks, thanks to his\n" - " face mask."); + + mPerStatus2 = std::make_unique(" 32 12382\n" + " 56 13872\n" + " 12\n" + " 11\n\n" + " FINALMAN is immune against\n" + " poisinous attacks, thanks to his\n" + " face mask."); mPerStatus2->setOpaque(false); mPerStatus2->setEditable(false); mPerStatus2->setFocusable(false); mPerStatus2->setVisible(false); mPerStatus2->setFrameSize(0); - - mOlofStatus1 = new gcn::TextBox(" STR EXP\n" - " INT NEXT\n" - " DEF\n" - " MAGDEF\n"); - mOlofStatus1->setFont(mFontCyan); + + mOlofStatus1 = std::make_unique(" STR EXP\n" + " INT NEXT\n" + " DEF\n" + " MAGDEF\n"); + mOlofStatus1->setFont(mFontCyan.get()); mOlofStatus1->setOpaque(false); mOlofStatus1->setEditable(false); mOlofStatus1->setFocusable(false); mOlofStatus1->setVisible(false); mOlofStatus1->setFrameSize(0); - - mOlofStatus2 = new gcn::TextBox(" 2 412382\n" - " 72 513872\n" - " 4\n" - " 34\n\n" - " YAKSLEM has one passion in life,\n" - " to annoy other people...\n" - " especially FINALMAN."); + + mOlofStatus2 = std::make_unique(" 2 412382\n" + " 72 513872\n" + " 4\n" + " 34\n\n" + " YAKSLEM has one passion in life,\n" + " to annoy other people...\n" + " especially FINALMAN."); mOlofStatus2->setOpaque(false); mOlofStatus2->setEditable(false); mOlofStatus2->setFocusable(false); - mOlofStatus2->setVisible(false); + mOlofStatus2->setVisible(false); mOlofStatus2->setFrameSize(0); - - mTomasStatus1 = new gcn::TextBox(" STR EXP\n" - " INT NEXT\n" - " DEF\n" - " MAGDEF\n"); - mTomasStatus1->setFont(mFontCyan); + + mTomasStatus1 = std::make_unique(" STR EXP\n" + " INT NEXT\n" + " DEF\n" + " MAGDEF\n"); + mTomasStatus1->setFont(mFontCyan.get()); mTomasStatus1->setOpaque(false); mTomasStatus1->setEditable(false); mTomasStatus1->setFocusable(false); mTomasStatus1->setVisible(false); mTomasStatus1->setFrameSize(0); - - mTomasStatus2 = new gcn::TextBox(" 1 412382\n" - " 3 513872\n" - " 9\n" - " 24\n\n" - " PEAK is very weak but so cute!\n" - " He has a tendency of answering\n" - " any question with \"KUPO!\""); + + mTomasStatus2 = std::make_unique(" 1 412382\n" + " 3 513872\n" + " 9\n" + " 24\n\n" + " PEAK is very weak but so cute!\n" + " He has a tendency of answering\n" + " any question with \"KUPO!\""); mTomasStatus2->setOpaque(false); mTomasStatus2->setEditable(false); mTomasStatus2->setFocusable(false); - mTomasStatus2->setVisible(false); + mTomasStatus2->setVisible(false); mTomasStatus2->setFrameSize(0); - - mStatus->add(mPerStatus2, 5, 10); - mStatus->add(mPerStatus1, 5, 10); - mStatus->add(mOlofStatus2, 5, 10); - mStatus->add(mOlofStatus1, 5, 10); - mStatus->add(mTomasStatus2, 5, 10); - mStatus->add(mTomasStatus1, 5, 10); + + mStatus->add(mPerStatus2.get(), 5, 10); + mStatus->add(mPerStatus1.get(), 5, 10); + mStatus->add(mOlofStatus2.get(), 5, 10); + mStatus->add(mOlofStatus1.get(), 5, 10); + mStatus->add(mTomasStatus2.get(), 5, 10); + mStatus->add(mTomasStatus1.get(), 5, 10); } void FFDemo::initMagicSkills() { - mMagicSkills = new FFContainer(); + mMagicSkills = std::make_unique(); mMagicSkills->setDimension(gcn::Rectangle(0, 80, 320, 160)); mMagicSkills->setVisible(false); - - mMagicSkillsScroll = new FFScrollArea(); + + mMagicSkillsScroll = std::make_unique(); mMagicSkillsScroll->setDimension(gcn::Rectangle(5, 5, 310, 150)); - mMagicSkillsList = new FFListBox(); + mMagicSkillsList = std::make_unique(); mMagicSkillsList->setWidth(300); - mMagicSkillsScroll->setContent(mMagicSkillsList); - mMagicSkills->add(mMagicSkillsScroll); - mTop->add(mMagicSkills); + mMagicSkillsScroll->setContent(mMagicSkillsList.get()); + mMagicSkills->add(mMagicSkillsScroll.get()); + mTop->add(mMagicSkills.get()); - mPerSkills = new StringListModel(); - mPerMagic = new StringListModel(); - mOlofSkills = new StringListModel(); - mOlofMagic = new StringListModel(); - mTomasSkills = new StringListModel(); - mTomasMagic = new StringListModel(); + mPerSkills = std::make_unique(); + mPerMagic = std::make_unique(); + mOlofSkills = std::make_unique(); + mOlofMagic = std::make_unique(); + mTomasSkills = std::make_unique(); + mTomasMagic = std::make_unique(); mPerSkills->add("Use"); mPerSkills->add("Steal"); - mPerSkills->add("Disassemble"); + mPerSkills->add("Disassemble"); mPerSkills->add("Tech-Talk"); mPerSkills->add("Double Compile"); @@ -418,57 +363,33 @@ void FFDemo::initMagicSkills() mOlofSkills->add("Evil Laughter"); mOlofSkills->add("Meta-circular Evaluation"); mOlofSkills->add("Lisp"); - mOlofSkills->add("Cursing PHP"); + mOlofSkills->add("Cursing PHP"); mOlofSkills->add("Paint"); mOlofSkills->add("Compose obscure music"); - + mOlofMagic->add("Ultima"); - mOlofMagic->add("Sonic Blast"); + mOlofMagic->add("Sonic Blast"); mTomasSkills->add("Precision Throw"); mTomasSkills->add("Jump"); - mTomasSkills->add("Dance"); + mTomasSkills->add("Dance"); mTomasSkills->add("Much talk and little factory"); mTomasSkills->add("Cheat"); mTomasSkills->add("Wear hotpants"); mTomasSkills->add("Programming Pong games"); mTomasSkills->add("Eat meat pie"); - + mTomasMagic->add("Slow"); mTomasMagic->add("Sleep"); mTomasMagic->add("Doom"); } -void FFDemo::cleanMagicSkills() -{ - delete mMagicSkills; - delete mMagicSkillsList; - delete mMagicSkillsScroll; - delete mPerSkills; - delete mPerMagic; - delete mOlofSkills; - delete mOlofMagic; - delete mTomasSkills; - delete mTomasMagic; -} - -void FFDemo::cleanStatus() -{ - delete mStatus; - delete mPerStatus1; - delete mPerStatus2; - delete mOlofStatus1; - delete mOlofStatus2; - delete mTomasStatus1; - delete mTomasStatus2; -} - void FFDemo::initItems() { - mItems = new FFContainer(); + mItems = std::make_unique(); - mItemsListModel = new StringListModel(); - mItemsInfoListModel = new StringListModel(); + mItemsListModel = std::make_unique(); + mItemsInfoListModel = std::make_unique(); mItemsListModel->add("23 x Potion"); mItemsInfoListModel->add("Restores 100 HP"); mItemsListModel->add("12 x Ether"); @@ -479,7 +400,7 @@ void FFDemo::initItems() mItemsInfoListModel->add("Kills a party member"); mItemsListModel->add(" 1 x Brass Key"); mItemsInfoListModel->add("No idea..."); - mItemsListModel->add(" 1 x Atma Weapon"); + mItemsListModel->add(" 1 x Atma Weapon"); mItemsInfoListModel->add("Grows with it's user"); mItemsListModel->add(" 1 x Converse Allstars"); mItemsInfoListModel->add("Yakslems red shoes"); @@ -499,52 +420,43 @@ void FFDemo::initItems() mItemsInfoListModel->add("We are waiting for Demo3"); mItemsListModel->add(" 2 x Joy Division LP"); mItemsInfoListModel->add("Unknown Pleasures and Closer"); - - mItemsInfo = new FFContainer; + + mItemsInfo = std::make_unique(); mItemsInfo->setDimension(gcn::Rectangle(0, 0, 320, 50)); mItemsInfo->setVisible(false); - - mItemsInfoInfo = new gcn::TextBox(); + + mItemsInfoInfo = std::make_unique(); mItemsInfoInfo->setOpaque(false); mItemsInfoInfo->setEditable(false); mItemsInfoInfo->setFocusable(false); mItemsInfoInfo->setDimension(gcn::Rectangle(5, 5, 310, 40)); mItemsInfoInfo->setFrameSize(0); - mItemsInfo->add(mItemsInfoInfo); - - mItemsList = new FFListBox(); - mItemsList->setActionEventId("items"); + mItemsInfo->add(mItemsInfoInfo.get()); + + mItemsList = std::make_unique(); + mItemsList->setActionEventId("items"); mItemsList->addKeyListener(this); mItemsList->setWidth(300); - mItemsList->setListModel(mItemsListModel); - mItemsScrollArea = new FFScrollArea(); - mItemsScrollArea->setContent(mItemsList); + mItemsList->setListModel(mItemsListModel.get()); + mItemsScrollArea = std::make_unique(); + mItemsScrollArea->setContent(mItemsList.get()); mItemsScrollArea->setDimension(gcn::Rectangle(5, 5, 310, 180)); - mItems = new FFContainer(); + mItems = std::make_unique(); mItems->setDimension(gcn::Rectangle(0, 50, 320, 190)); mItems->setVisible(false); - mItems->add(mItemsScrollArea); - mTop->add(mItems); - mTop->add(mItemsInfo); + mItems->add(mItemsScrollArea.get()); + mTop->add(mItems.get()); + mTop->add(mItemsInfo.get()); } -void FFDemo::cleanItems() -{ - delete mItems; - delete mItemsInfo; - delete mItemsInfoInfo; - delete mItemsList; - delete mItemsScrollArea; -} - void FFDemo::initAbout() { - mAbout = new FFContainer(); + mAbout = std::make_unique(); mAbout->setDimension(gcn::Rectangle(0, 0, 320, 240)); mAbout->setVisible(false); - mTop->add(mAbout); - - mAboutInfo = new gcn::TextBox(); + mTop->add(mAbout.get()); + + mAboutInfo = std::make_unique(); mAboutInfo->setOpaque(false); mAboutInfo->setEditable(false); mAboutInfo->setFocusable(false); @@ -579,28 +491,20 @@ void FFDemo::initAbout() " - Tomas Almgren (peak)\n" " font\n\n" ); - - mAboutScrollArea = new FFScrollArea(); - mAboutScrollArea->setContent(mAboutInfo); + + mAboutScrollArea = std::make_unique(); + mAboutScrollArea->setContent(mAboutInfo.get()); mAboutScrollArea->setFocusable(true); mAboutScrollArea->setDimension(gcn::Rectangle(5, 5, 310, 230)); mAboutScrollArea->setFrameSize(0); - mAbout->add(mAboutScrollArea); + mAbout->add(mAboutScrollArea.get()); mAbout->setFrameSize(0); } -void FFDemo::cleanAbout() -{ - delete mAboutInfo; - delete mAboutScrollArea; - delete mAbout; -} - - void FFDemo::run() { while(mRunning) - { + { input(); int sec = SDL_GetTicks() / 1000; @@ -615,7 +519,7 @@ void FFDemo::run() } else { - os << min << ":"; + os << min << ":"; } if (sec < 10) @@ -629,7 +533,7 @@ void FFDemo::run() mTimeLabel2->setCaption(os.str()); mTimeLabel2->adjustSize(); - + if (SDL_GetTicks() < 3000) { SDL_Rect src, dst; @@ -638,23 +542,23 @@ void FFDemo::run() src.h = dst.h = mSplashImage->getHeight(); dst.x = 10; dst.y = 50; - gcn::SDLImage* image = (gcn::SDLImage*) mSplashImage; + gcn::SDLImage* image = static_cast(mSplashImage.get()); SDL_BlitSurface(image->getSurface(), &src, mScreen, &dst); } - else + else { mGui->logic(); mGui->draw(); } - SDL_UpdateWindowSurface(mWindow); + SDL_UpdateWindowSurface(mWindow); SDL_Delay(10); - } + } } void FFDemo::action(const gcn::ActionEvent& actionEvent) { - if (actionEvent.getId() == "menu") + if (actionEvent.getId() == "menu") { switch (mMenuList->getSelected()) { @@ -677,11 +581,11 @@ void FFDemo::action(const gcn::ActionEvent& actionEvent) mAboutScrollArea->setVerticalScrollAmount(0); mAboutScrollArea->requestFocus(); break; - + case 6: mRunning = false; break; - + default: break; } @@ -719,16 +623,16 @@ void FFDemo::action(const gcn::ActionEvent& actionEvent) mNavigationLabel->setVisible(true); mNavigationLabel->setY(mCharacterChooser->getSelected()*76 + 30); - + switch(mMenuList->getSelected()) - { + { case 1: mNavigationLabel->setCaption("STATUS"); - + if (mCharacterChooser->getSelected() == 0) { mPerStatus1->setVisible(true); - mPerStatus2->setVisible(true); + mPerStatus2->setVisible(true); } else if (mCharacterChooser->getSelected() == 1) { @@ -745,18 +649,18 @@ void FFDemo::action(const gcn::ActionEvent& actionEvent) case 2: mNavigationLabel->setCaption("SKILLS"); - + if (mCharacterChooser->getSelected() == 0) { - mMagicSkillsList->setListModel(mPerSkills); + mMagicSkillsList->setListModel(mPerSkills.get()); } else if (mCharacterChooser->getSelected() == 1) { - mMagicSkillsList->setListModel(mOlofSkills); + mMagicSkillsList->setListModel(mOlofSkills.get()); } else if (mCharacterChooser->getSelected() == 2) { - mMagicSkillsList->setListModel(mTomasSkills); + mMagicSkillsList->setListModel(mTomasSkills.get()); } mMagicSkillsList->setSelected(0); mMagicSkills->setVisible(true); @@ -764,26 +668,24 @@ void FFDemo::action(const gcn::ActionEvent& actionEvent) break; case 3: - mNavigationLabel->setCaption("MAGIC"); - + mNavigationLabel->setCaption("MAGIC"); + if (mCharacterChooser->getSelected() == 0) { - mMagicSkillsList->setListModel(mPerMagic); + mMagicSkillsList->setListModel(mPerMagic.get()); } else if (mCharacterChooser->getSelected() == 1) { - mMagicSkillsList->setListModel(mOlofMagic); + mMagicSkillsList->setListModel(mOlofMagic.get()); } else if (mCharacterChooser->getSelected() == 2) { - mMagicSkillsList->setListModel(mTomasMagic); + mMagicSkillsList->setListModel(mTomasMagic.get()); } mMagicSkillsList->setSelected(0); mMagicSkills->setVisible(true); mMagicSkillsList->requestFocus(); break; - - } } } @@ -795,15 +697,15 @@ void FFDemo::input() if (mEvent.type == SDL_KEYDOWN) { if (mEvent.key.keysym.sym == SDLK_ESCAPE) - { + { Mix_PlayChannel(-1, mEscapeSound, 0); - + action(gcn::ActionEvent(NULL, "escape")); } else if (mEvent.key.keysym.sym == SDLK_RETURN || mEvent.key.keysym.sym == SDLK_UP || mEvent.key.keysym.sym == SDLK_DOWN) - { + { Mix_PlayChannel(-1, mChooseSound, 0); } else if (mEvent.key.keysym.sym == SDLK_f) @@ -812,7 +714,7 @@ void FFDemo::input() // Works with X11 only //SDL_WM_ToggleFullScreen(mScreen); } - mSDLInput->pushInput(mEvent); + mSDLInput->pushInput(mEvent); } else if (mEvent.type == SDL_KEYUP) { @@ -826,6 +728,6 @@ void FFDemo::input() } void FFDemo::keyPressed(gcn::KeyEvent& keyEvent) -{ - mItemsInfoInfo->setText(mItemsInfoListModel->getElementAt(mItemsList->getSelected())); +{ + mItemsInfoInfo->setText(mItemsInfoListModel->getElementAt(mItemsList->getSelected())); } diff --git a/demo/ff/src/fflistbox.cpp b/demo/ff/src/fflistbox.cpp index fdbe5ea..00131bb 100644 --- a/demo/ff/src/fflistbox.cpp +++ b/demo/ff/src/fflistbox.cpp @@ -41,23 +41,29 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "fflistbox.hpp" + #include +#include + #ifndef _MSC_VER -#include +# include #else -#include +# include #endif -#include "fflistbox.hpp" +namespace +{ + int mInstances = 0; + std::unique_ptr mHand; +} -int FFListBox::mInstances = 0; -gcn::Image *FFListBox::mHand = NULL; FFListBox::FFListBox() { if (mInstances == 0) { - mHand = gcn::Image::load("images/hand.png"); + mHand.reset(gcn::Image::load("images/hand.png")); } mInstances++; @@ -71,7 +77,7 @@ FFListBox::~FFListBox() if (mInstances == 0) { - delete mHand; + mHand = nullptr; } } @@ -101,11 +107,11 @@ void FFListBox::draw(gcn::Graphics* graphics) { if (isFocused()) { - graphics->drawImage(mHand, 0, y); + graphics->drawImage(mHand.get(), 0, y); } else if ((SDL_GetTicks() / 100) & 1) { - graphics->drawImage(mHand, 0, y); + graphics->drawImage(mHand.get(), 0, y); } } diff --git a/examples/action_example.hpp b/examples/action_example.hpp index b1efe66..df65058 100644 --- a/examples/action_example.hpp +++ b/examples/action_example.hpp @@ -1,4 +1,5 @@ #include +#include #include namespace ActionExample @@ -13,41 +14,39 @@ namespace ActionExample class MainContainer : public gcn::ActionListener { public: - MainContainer(gcn::Gui& gui, int width = 680, int height = 480) : - clickCountButton1(0), - clickCountButton2(0) + MainContainer(gcn::Gui& gui, int width = 680, int height = 480) { // Load the image font. - font = new gcn::ImageFont( + font = std::make_unique( "fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); // The global font is static and must be set. - gcn::Widget::setGlobalFont(font); + gcn::Widget::setGlobalFont(font.get()); - top = new 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); + gui.setTop(top.get()); // Create buttons - button1 = new gcn::Button("Button 1"); - button2 = new gcn::Button("Button 2"); + button1 = std::make_unique("Button 1"); + button2 = std::make_unique("Button 2"); // Set the buttons position button1->setPosition(120, 230); button2->setPosition(420, 230); // Add the buttons to the top container - top->add(button1); - top->add(button2); + top->add(button1.get()); + top->add(button2.get()); // Create labels - label1 = new gcn::Label("Button1 clicks 0"); - label2 = new gcn::Label("Button2 clicks 0"); + label1 = std::make_unique("Button1 clicks 0"); + label2 = std::make_unique("Button2 clicks 0"); // Set the labels position label1->setPosition(100, 200); label2->setPosition(400, 200); // Add the labels to the top container - top->add(label1); - top->add(label2); + top->add(label1.get()); + top->add(label2.get()); // Set the buttons action event id's. button1->setActionEventId("button1"); @@ -58,19 +57,7 @@ namespace ActionExample button2->addActionListener(this); } - ~MainContainer() override - { - delete font; - - /* - * Destroy Guichan stuff - */ - delete label1; - delete label2; - delete button1; - delete button2; - delete top; - } + ~MainContainer() override = default; // Implement the action function in ActionListener to receive actions // The eventId tells us which widget called the action function. @@ -81,7 +68,7 @@ namespace ActionExample // Here we use the widget pointer to check which widget the action // originated from. - if (actionEvent.getSource() == button1) + if (actionEvent.getSource() == button1.get()) { clickCountButton1++; os << "Button1 clicks " << clickCountButton1; @@ -103,19 +90,19 @@ namespace ActionExample } private: - gcn::ImageFont* font; // A font + std::unique_ptr font; // A font /* * All of the widgets */ - gcn::Container* top; // A top container - gcn::Button* button1; // A button for actionlistening - gcn::Button* button2; // Another button for actionlistening - gcn::Label* label1; // And a label for button1 click count display - gcn::Label* label2; // And another label for button2 click count display + std::unique_ptr top; // A top container + std::unique_ptr button1; // A button for actionlistening + std::unique_ptr button2; // Another button for actionlistening + std::unique_ptr label1; // And a label for button1 click count display + std::unique_ptr label2; // And another label for button2 click count display - int clickCountButton1; // Count clicks for button1 - int clickCountButton2; // Count clicks for button2 + int clickCountButton1 = 0; // Count clicks for button1 + int clickCountButton2 = 0; // Count clicks for button2 }; } // namespace ActionExample diff --git a/examples/helloworld_example.hpp b/examples/helloworld_example.hpp index 79fd4fc..4ed9e5c 100644 --- a/examples/helloworld_example.hpp +++ b/examples/helloworld_example.hpp @@ -1,4 +1,5 @@ #include +#include namespace HelloWorldExample { @@ -9,47 +10,35 @@ namespace HelloWorldExample MainContainer(gcn::Gui& gui, int width = 680, int height = 480) { // Load the image font. - font = new gcn::ImageFont( + font = std::make_unique( "fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); // The global font is static and must be set. - gcn::Widget::setGlobalFont(font); + gcn::Widget::setGlobalFont(font.get()); - top = new 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); + gui.setTop(top.get()); // Create a label with test hello world - label = new gcn::Label("Hello World"); + label = std::make_unique("Hello World"); // Set the labels position label->setPosition(280, 220); // Add the label to the top container - top->add(label); + top->add(label.get()); } - ~MainContainer() - { - /* - * Destroy font - */ - delete font; - - /* - * Widgets - */ - delete top; - delete label; - } + ~MainContainer() = default; private: - gcn::ImageFont* font; // A font + std::unique_ptr font; // A font /* * All of the widgets */ - gcn::Container* top; // A top container - gcn::Label* label; // A label + std::unique_ptr top; // A top container + std::unique_ptr label; // A label }; } // namespace HelloWorldExample diff --git a/examples/rickroll_example.hpp b/examples/rickroll_example.hpp index adbed9c..666473c 100644 --- a/examples/rickroll_example.hpp +++ b/examples/rickroll_example.hpp @@ -1,4 +1,5 @@ #include +#include namespace RickRollExample { @@ -12,41 +13,39 @@ namespace RickRollExample class MainContainer : public gcn::ActionListener { public: - MainContainer(gcn::Gui& gui, int width = 680, int height = 480) : - msgBox(NULL), - inputBox(NULL) + MainContainer(gcn::Gui& gui, int width = 680, int height = 480) { // Load the image font. - font = new gcn::ImageFont( + font = std::make_unique( "fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); // The global font is static and must be set. - gcn::Widget::setGlobalFont(font); + gcn::Widget::setGlobalFont(font.get()); - top = new 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); + gui.setTop(top.get()); // Create buttons - button1 = new gcn::Button("Click me"); - button2 = new gcn::Button("Enter your name"); + button1 = std::make_unique("Click me"); + button2 = std::make_unique("Enter your name"); // Set the buttons position button1->setPosition(120, 230); button2->setPosition(420, 230); // Add the buttons to the top container - top->add(button1); - top->add(button2); + top->add(button1.get()); + top->add(button2.get()); // Create labels - label1 = new gcn::Label("Get rick rolled"); - label2 = new gcn::Label("What is your name"); + label1 = std::make_unique("Get rick rolled"); + label2 = std::make_unique("What is your name"); // Set the labels position label1->setPosition(100, 200); label2->setPosition(400, 200); // Add the labels to the top container - top->add(label1); - top->add(label2); + top->add(label1.get()); + top->add(label2.get()); // Set the buttons action event id's. button1->setActionEventId("button1"); @@ -57,19 +56,7 @@ namespace RickRollExample button2->addActionListener(this); } - ~MainContainer() override - { - delete font; - - delete msgBox; - delete inputBox; - - delete label1; - delete button1; - delete label2; - delete button2; - delete top; - } + ~MainContainer() override = default; // Implement the action function in ActionListener to receive actions // The eventId tells us which widget called the action function. @@ -77,40 +64,31 @@ namespace RickRollExample { // Here we use the widget pointer to check which widget the action // originated from. - if (actionEvent.getSource() == msgBox) + if (actionEvent.getSource() == msgBox.get()) { msgBox->setVisible(false); label1->setCaption("You have been rick rolled"); // Adjust the label to fit the new caption label1->adjustSize(); - top->remove(msgBox); + top->remove(msgBox.get()); } - else if (actionEvent.getSource() == button1) + else if (actionEvent.getSource() == button1.get()) { - if (msgBox) - { - delete msgBox; - msgBox = NULL; - } - msgBox = new gcn::MessageBox("Rick Astley", "Never gonna give you up"); + msgBox = + std::make_unique("Rick Astley", "Never gonna give you up"); msgBox->setVisible(true); msgBox->addActionListener(this); - top->add(msgBox, 270, 180); + top->add(msgBox.get(), 270, 180); } - else if (actionEvent.getSource() == button2) + else if (actionEvent.getSource() == button2.get()) { - if (inputBox) - { - delete inputBox; - inputBox = NULL; - } - inputBox = new gcn::InputBox( + inputBox = std::make_unique( "Name request", "Enter your name below", "Here you go", "No way"); inputBox->setVisible(true); inputBox->addActionListener(this); - top->add(inputBox, 270, 180); + top->add(inputBox.get(), 270, 180); } - else if (actionEvent.getSource() == inputBox) + else if (actionEvent.getSource() == inputBox.get()) { inputBox->setVisible(false); if (inputBox->getClickedButton() == 1) @@ -125,18 +103,18 @@ namespace RickRollExample } private: - gcn::ImageFont* font; // A font + std::unique_ptr font; // A font /* * All of the widgets */ - gcn::Container* top; // A top container - gcn::Button* button1; // A button for actionlistening (triggers msgbox) - gcn::Label* label1; // And a label for button1 - gcn::Button* button2; // Button for InputBox - gcn::Label* label2; // Label for inputbox - gcn::MessageBox* msgBox; - gcn::InputBox* inputBox; + std::unique_ptr top; // A top container + std::unique_ptr button1; // A button for actionlistening (triggers msgbox) + std::unique_ptr label1; // And a label for button1 + std::unique_ptr button2; // Button for InputBox + std::unique_ptr label2; // Label for inputbox + std::unique_ptr msgBox; + std::unique_ptr inputBox; }; } // namespace ActionExample diff --git a/examples/widgets_example.hpp b/examples/widgets_example.hpp index cfb5db3..674b1a1 100644 --- a/examples/widgets_example.hpp +++ b/examples/widgets_example.hpp @@ -1,4 +1,5 @@ #include +#include namespace WidgetsExample { @@ -33,163 +34,134 @@ namespace WidgetsExample MainContainer(gcn::Gui& gui, int width, int height) { // Load the image font. - font = new gcn::ImageFont( + font = std::make_unique( "fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"); // The global font is static and must be set. - gcn::Widget::setGlobalFont(font); + gcn::Widget::setGlobalFont(font.get()); - top = new 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); + gui.setTop(top.get()); /* * Create all the widgets */ - label = new gcn::Label("Label"); + label = std::make_unique("Label"); - image = gcn::Image::load("guisan.png"); - icon = new gcn::Icon(image); + image.reset(gcn::Image::load("guisan.png")); + icon = std::make_unique(image.get()); - button = new gcn::Button("Button"); + button = std::make_unique("Button"); - textField = new gcn::TextField("Text field"); + textField = std::make_unique("Text field"); - textBox = new gcn::TextBox("Lorem ipsum dolor sit amet consectetur\n" - "adipiscing elit Integer vitae ultrices\n" - "eros Curabitur malesuada dolor imperdieat\n" - "ante facilisis ut convallis sem rutrum\n" - "Praesent consequat urna convallis leo\n" - "aliquam pellentesque Integer laoreet enim\n" - "vehicula libero blandit at pellentesque\n" - "ipsum vehicula Mauris id turpis hendrerit\n" - "tempor velit nec hendrerit nulla"); - textBoxScrollArea = new gcn::ScrollArea(textBox); + textBox = std::make_unique("Lorem ipsum dolor sit amet consectetur\n" + "adipiscing elit Integer vitae ultrices\n" + "eros Curabitur malesuada dolor imperdieat\n" + "ante facilisis ut convallis sem rutrum\n" + "Praesent consequat urna convallis leo\n" + "aliquam pellentesque Integer laoreet enim\n" + "vehicula libero blandit at pellentesque\n" + "ipsum vehicula Mauris id turpis hendrerit\n" + "tempor velit nec hendrerit nulla"); + textBoxScrollArea = std::make_unique(textBox.get()); textBoxScrollArea->setWidth(270); textBoxScrollArea->setHeight(100); textBoxScrollArea->setFrameSize(1); - listBox = new gcn::ListBox(&demoListModel); + listBox = std::make_unique(&demoListModel); listBox->setFrameSize(1); - dropDown = new gcn::DropDown(&demoListModel); + dropDown = std::make_unique(&demoListModel); - checkBox1 = new gcn::CheckBox("Checkbox 1"); - checkBox2 = new gcn::CheckBox("Checkbox 2"); + checkBox1 = std::make_unique("Checkbox 1"); + checkBox2 = std::make_unique("Checkbox 2"); - radioButton1 = new gcn::RadioButton("Radio Button 1", "radiogroup", true); - radioButton2 = new gcn::RadioButton("Radio Button 2", "radiogroup"); - radioButton3 = new gcn::RadioButton("Radio Button 3", "radiogroup"); + radioButton1 = std::make_unique("Radio Button 1", "radiogroup", true); + radioButton2 = std::make_unique("Radio Button 2", "radiogroup"); + radioButton3 = std::make_unique("Radio Button 3", "radiogroup"); - slider = new gcn::Slider(0, 10); + slider = std::make_unique(0, 10); slider->setSize(100, 10); - window = new gcn::Window("I am a window Drag me"); + window = std::make_unique("I am a window Drag me"); window->setBaseColor(gcn::Color(212, 255, 150, 190)); - progress = new gcn::ProgressBar(0, 100, 30); + progress = std::make_unique(0, 100, 30); progress->setCaption("Loading"); progress->setWidth(100); - guisanLogoImage = gcn::Image::load("guisan-logo.png"); - guisanLogoIcon = new gcn::Icon(guisanLogoImage); - window->add(guisanLogoIcon); + guisanLogoImage.reset(gcn::Image::load("guisan-logo.png")); + guisanLogoIcon = std::make_unique(guisanLogoImage.get()); + window->add(guisanLogoIcon.get()); window->resizeToContent(); - nestedSlider = new gcn::Slider(0, 10); + nestedSlider = std::make_unique(0, 10); nestedSlider->setSize(100, 10); - nestedContainer = new gcn::Container(); + nestedContainer = std::make_unique(); nestedContainer->setSize(400, 200); - nestedContainer->add(nestedSlider, 50, 70); + nestedContainer->add(nestedSlider.get(), 50, 70); - nestedScrollArea = new gcn::ScrollArea(nestedContainer); + nestedScrollArea = std::make_unique(nestedContainer.get()); nestedScrollArea->setSize(180, 90); nestedScrollArea->setFrameSize(1); /* * Add them to the top container */ - top->add(label, 290, 10); - top->add(icon, 10, 10); - top->add(button, 325, 10); - top->add(textField, 375, 10); - top->add(textBoxScrollArea, 290, 50); - top->add(listBox, 290, 200); - top->add(dropDown, 580, 10); - top->add(checkBox1, 580, 50); - top->add(checkBox2, 580, 70); - top->add(radioButton1, 580, 120); - top->add(radioButton2, 580, 140); - top->add(radioButton3, 580, 160); - top->add(slider, 580, 200); - top->add(window, 100, 350); - top->add(nestedScrollArea, 440, 350); - - top->add(progress, 580, 200); + top->add(label.get(), 290, 10); + top->add(icon.get(), 10, 10); + top->add(button.get(), 325, 10); + top->add(textField.get(), 375, 10); + top->add(textBoxScrollArea.get(), 290, 50); + top->add(listBox.get(), 290, 200); + top->add(dropDown.get(), 580, 10); + top->add(checkBox1.get(), 580, 50); + top->add(checkBox2.get(), 580, 70); + top->add(radioButton1.get(), 580, 120); + top->add(radioButton2.get(), 580, 140); + top->add(radioButton3.get(), 580, 160); + top->add(slider.get(), 580, 200); + top->add(window.get(), 100, 350); + top->add(nestedScrollArea.get(), 440, 350); + + top->add(progress.get(), 580, 200); } - ~MainContainer() - { - delete font; - - /* - * Widgets - */ - delete top; - delete label; - delete icon; - delete button; - delete textField; - delete textBox; - delete textBoxScrollArea; - delete listBox; - delete dropDown; - delete checkBox1; - delete checkBox2; - delete radioButton1; - delete radioButton2; - delete radioButton3; - delete slider; - delete window; - delete progress; - delete guisanLogoIcon; - delete guisanLogoImage; - delete nestedScrollArea; - delete nestedContainer; - delete nestedSlider; - } + ~MainContainer() = default; private: - gcn::ImageFont* font; // A font + std::unique_ptr font; // A font /* * All of the widgets */ - gcn::Container* top; // A top container + std::unique_ptr top; // A top container DemoListModel demoListModel; - gcn::Label* label; // A label - gcn::Icon* icon; // An icon (image) - gcn::Button* button; // A button - gcn::TextField* textField; // One-line text field - gcn::TextBox* textBox; // Multi-line text box - gcn::ScrollArea* textBoxScrollArea; // Scroll area for the text box - gcn::ListBox* listBox; // A list box - gcn::DropDown* dropDown; // Drop down - gcn::CheckBox* checkBox1; // Two checkboxes - gcn::CheckBox* checkBox2; - gcn::RadioButton* radioButton1; // Three radio buttons - gcn::RadioButton* radioButton2; - gcn::RadioButton* radioButton3; - gcn::Slider* slider; // A slider - gcn::Image* image; // An image for the icon - gcn::Window* window; - gcn::ProgressBar* progress; - gcn::Image* guisanLogoImage; - gcn::Icon* guisanLogoIcon; - gcn::ScrollArea* nestedScrollArea; - gcn::Container* nestedContainer; - gcn::Slider* nestedSlider; + std::unique_ptr label; // A label + std::unique_ptr icon; // An icon (image) + std::unique_ptr button; // A button + std::unique_ptr textField; // One-line text field + std::unique_ptr textBox; // Multi-line text box + std::unique_ptr textBoxScrollArea; // Scroll area for the text box + std::unique_ptr listBox; // A list box + std::unique_ptr dropDown; // Drop down + std::unique_ptr checkBox1; // Two checkboxes + std::unique_ptr checkBox2; + std::unique_ptr radioButton1; // Three radio buttons + std::unique_ptr radioButton2; + std::unique_ptr radioButton3; + std::unique_ptr slider; // A slider + std::unique_ptr image; // An image for the icon + std::unique_ptr window; + std::unique_ptr progress; + std::unique_ptr guisanLogoImage; + std::unique_ptr guisanLogoIcon; + std::unique_ptr nestedScrollArea; + std::unique_ptr nestedContainer; + std::unique_ptr nestedSlider; }; } // namespace WidgetsExample