diff --git a/SConstruct b/SConstruct index 5b55781..eebed40 100644 --- a/SConstruct +++ b/SConstruct @@ -7,6 +7,7 @@ opts.Add('sharedir', 'Directory to use to store data file', '$prefix/share/block opts.Add('bindir', 'Directory to use to store data file', '$prefix/bin') opts.Add('mandir', 'Directory to use to store data file', '$prefix/share/man') opts.Add('CXX', 'C++ compiler to use', os.environ['CXX']) +opts.Add('CXXFLAGS', 'C++ flags', "-std=c++11") # Copy Build Environment # diff --git a/source/code/CppSdlException.cpp b/source/code/CppSdlException.cpp deleted file mode 100644 index a33a892..0000000 --- a/source/code/CppSdlException.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* -=========================================================================== -blockattack - Block Attack - Rise of the Blocks -Copyright (C) 2005-2013 Poul Sander - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see http://www.gnu.org/licenses/ - -Source information and contacts persons can be found at -http://blockattack.sf.net -=========================================================================== -*/ - -#include "CppSdlException.hpp" - -namespace CppSdl -{ - -CppSdlException::CppSdlException(Subsystem subsystem,long errorNumber, std::string msg) -{ - _errorNumber = errorNumber; - _message = msg; - _subsystem = subsystem; -} - -CppSdlException::CppSdlException(const CppSdlException& orig) -{ -} - -CppSdlException::~CppSdlException() throw() -{ -} - -const char* CppSdlException::what() const throw() -{ - return _message.c_str(); -} - -long CppSdlException::GetErrorNumber() -{ - return _errorNumber; -} - -Subsystem CppSdlException::GetSubSystem() -{ - return _subsystem; -} - -} diff --git a/source/code/CppSdlException.hpp b/source/code/CppSdlException.hpp deleted file mode 100644 index 4782f39..0000000 --- a/source/code/CppSdlException.hpp +++ /dev/null @@ -1,64 +0,0 @@ -/* -=========================================================================== -blockattack - Block Attack - Rise of the Blocks -Copyright (C) 2005-2013 Poul Sander - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 2 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see http://www.gnu.org/licenses/ - -Source information and contacts persons can be found at -http://blockattack.sf.net -=========================================================================== -*/ - -#ifndef _CPPSDLEXCEPTION_HPP -#define _CPPSDLEXCEPTION_HPP - -#include - -#define CPPSDL_ERROR_DATA 1 -#define CPPSDL_ERROR_INVALID 2 -#define CPPSDL_ERROR_MISSINGFILE 3 -#define CPPSDL_ERROR_NULLPOINTER 4 - -enum Subsystem {OTHER,IMAGE,TILE,SPRITE,LAYER,SCREEN }; - - -namespace CppSdl -{ -class CppSdlException : std::exception -{ -public: - //CppSdlException(); - /** - * Constructor for making a fully CppSdl compliant expection - * @param subsystem Can be: OTHER,IMAGE,TILE,SPRITE,LAYER,SCREEN - * @param errorNumber 1=Data error, 2=Invalid, 3=Missingfile, 4=null pointer - * @param msg Human readable error message. - */ - CppSdlException(Subsystem subsystem, long errorNumber, std::string msg); - CppSdlException(const CppSdlException& orig); - virtual ~CppSdlException() throw(); - virtual const char* what() const throw(); - long GetErrorNumber(); - Subsystem GetSubSystem(); -private: - std::string _message; - long _errorNumber; - Subsystem _subsystem; -}; - - -} -#endif /* _CPPSDLEXCEPTION_HPP */ - diff --git a/source/code/CppSdlImageHolder.cpp b/source/code/CppSdlImageHolder.cpp index 6375248..be8fde0 100644 --- a/source/code/CppSdlImageHolder.cpp +++ b/source/code/CppSdlImageHolder.cpp @@ -23,6 +23,7 @@ http://blockattack.sf.net #include "CppSdlImageHolder.hpp" #include "SDL_image.h" +#include namespace CppSdl { @@ -30,8 +31,6 @@ namespace CppSdl CppSdlImageHolder::CppSdlImageHolder() { data = NULL; - counter = new int; - (*counter)=1; } CppSdlImageHolder::CppSdlImageHolder(std::string filename) @@ -40,41 +39,10 @@ CppSdlImageHolder::CppSdlImageHolder(std::string filename) if(!data) { //Here we should throw an exception - CppSdlException e(IMAGE,CPPSDL_ERROR_MISSINGFILE,"Could not read file \""+filename+"\""); - throw e; + throw std::runtime_error(std::string("Could not read file \""+filename+"\"")); } SDL_GetClipRect(data,&area); OptimizeForBlit(); - counter = new int; - *counter=1; -} - -CppSdlImageHolder::CppSdlImageHolder(const CppSdlImageHolder& orig) -{ - //Just take the data from the original. This is technically wrong but adds a little performance. - data = orig.data; - area = orig.area; - if(orig.counter) - { - counter = orig.counter; - (*counter)++; - } -} - -CppSdlImageHolder& CppSdlImageHolder::operator =(const CppSdlImageHolder& rhs) -{ - // Check for self-assignment! - if (this == &rhs) // Same object? - return *this; // Yes, so skip assignment, and just return *this. - MakeNull(); - data = rhs.data; - area = rhs.area; - if(rhs.counter) - { - counter = rhs.counter; - (*counter)++; - } - return *this; } CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize) @@ -82,36 +50,23 @@ CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize) SDL_RWops *rw = SDL_RWFromMem (rawdata, datasize); //The above might fail and return null. - if(!rw) - { - CppSdlException e(IMAGE,CPPSDL_ERROR_NULLPOINTER, "Could not read raw data"); - throw e; + if (!rw) { + throw std::runtime_error(std::string("Could not read raw data")); } data = IMG_Load_RW(rw,true); //the second argument tells the function to free RWops - if(!data) - { - CppSdlException e(IMAGE,CPPSDL_ERROR_DATA,"Could not convert raw data to image"); - throw e; + if (!data) { + throw std::runtime_error("Could not convert raw data to image"); } SDL_GetClipRect(data,&area); OptimizeForBlit(); - counter = new int; - *counter = 1; } CppSdlImageHolder::~CppSdlImageHolder() { - if(!counter) - return; //There are no counter, so already freed MakeNull(); - if(*counter == 0) - { - delete counter; - counter = NULL; - } } SDL_Surface* CppSdlImageHolder::GetRawDataInsecure() @@ -122,7 +77,7 @@ SDL_Surface* CppSdlImageHolder::GetRawDataInsecure() Uint32 CppSdlImageHolder::GetWidth() { - if(IsNull()) + if (IsNull()) return 0; return area.w; } @@ -134,39 +89,6 @@ Uint32 CppSdlImageHolder::GetHeight() return area.h; } -void CppSdlImageHolder::Cut(Uint32 x, Uint32 y, Sint32 w = -1, Sint32 h = -1) -{ - Initialized(); - if(w<0) - { - w = GetWidth() - x; - } - if(h<0) - { - h = GetHeight() - y; - } - if(x+w>GetWidth()) - { - //throw exception - } - if(y+h>GetHeight()) - { - //throw exception - } - if(x<0 || x>GetWidth()) - { - //throw exception - } - if(y<0 || y>GetHeight()) - { - //throw exception - } - area.x += x; - area.y += y; - area.w = w; - area.h = h; -} - void CppSdlImageHolder::PaintTo(SDL_Surface* target, int x, int y) { static SDL_Rect dest; //static for reuse @@ -191,28 +113,25 @@ void CppSdlImageHolder::OptimizeForBlit(bool allowAlpha) void CppSdlImageHolder::Initialized() { - if(data == NULL) - throw(CppSdlException(IMAGE,CPPSDL_ERROR_NULLPOINTER,"ImageHolder used uninitialized!")); + if(data == NULL) { + throw std::runtime_error("ImageHolder used uninitialized!"); + } } bool CppSdlImageHolder::IsNull() { - if(data == NULL || counter == NULL) + if(data == NULL ) { return true; - else - return false; + } + return false; } void CppSdlImageHolder::MakeNull() { if(IsNull()) return; - (*counter)--; - if(*counter == 0 && data != NULL) - { - SDL_FreeSurface(data); - data = NULL; - } + SDL_FreeSurface(data); + data = NULL; } } diff --git a/source/code/CppSdlImageHolder.hpp b/source/code/CppSdlImageHolder.hpp index bb94c7b..d6c5384 100644 --- a/source/code/CppSdlImageHolder.hpp +++ b/source/code/CppSdlImageHolder.hpp @@ -26,7 +26,6 @@ http://blockattack.sf.net #include "SDL.h" #include -#include "CppSdlException.hpp" namespace CppSdl { @@ -47,9 +46,9 @@ public: * Creates a copy. The new copy shares raw data with the original but is otherwise independent. * @param orig */ - CppSdlImageHolder(const CppSdlImageHolder& orig); + CppSdlImageHolder(const CppSdlImageHolder& orig) = delete; CppSdlImageHolder(char *rawdata, int datasize); - virtual ~CppSdlImageHolder(); + ~CppSdlImageHolder(); /** * This gives access direct access to the internal data. * Careful, the data might be shared between multiple ImageHolders, @@ -57,8 +56,7 @@ public: * @return A pointer */ SDL_Surface *GetRawDataInsecure(); - CppSdlImageHolder & operator=(const CppSdlImageHolder &rhs); - void Cut(Uint32 x,Uint32 y,Sint32 w,Sint32 h); + CppSdlImageHolder & operator=(const CppSdlImageHolder &rhs) = delete; /** * The width of the image * 0 if the image is nulled @@ -92,7 +90,6 @@ public: void MakeNull(); private: void Initialized(); //throws an exception if *data is null - int *counter; SDL_Rect area; SDL_Surface *data; }; diff --git a/source/code/Makefile b/source/code/Makefile index 442249c..14917aa 100644 --- a/source/code/Makefile +++ b/source/code/Makefile @@ -43,7 +43,7 @@ endif BASE_LIBS += -lphysfs -OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o common.o stats.o CppSdlException.o CppSdlImageHolder.o Libs/NFont.o MenuSystem.o menudef.o puzzlehandler.o +OFILES=main.o highscore.o ReadKeyboard.o joypad.o listFiles.o common.o stats.o CppSdlImageHolder.o Libs/NFont.o MenuSystem.o menudef.o puzzlehandler.o ifeq ($(CROSS),i686-pc-mingw32-) OFILES += winicon.res diff --git a/source/code/MenuSystem.cpp b/source/code/MenuSystem.cpp index 145ad56..86f8917 100644 --- a/source/code/MenuSystem.cpp +++ b/source/code/MenuSystem.cpp @@ -27,13 +27,15 @@ http://blockattack.sf.net #include "common.h" #include "CppSdlImageHolder.hpp" -extern CppSdl::CppSdlImageHolder mouse; +extern std::shared_ptr mouse; extern SDL_Surface *backgroundImage; extern bool highPriority; extern int verboseLevel; int mousex; int mousey; +using namespace std; + /*Draws a image from on a given Surface. Takes source image, destination surface and coordinates*/ inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y) { @@ -43,20 +45,14 @@ inline void DrawIMG(SDL_Surface *img, SDL_Surface *target, int x, int y) SDL_BlitSurface(img, NULL, target, &dest); } -/*CppSdl::CppSdlImageHolder ButtonGfx::_marked; -CppSdl::CppSdlImageHolder ButtonGfx::_unmarked; -int ButtonGfx::xsize; -int ButtonGfx::ysize; -NFont ButtonGfx::thefont;*/ - ButtonGfx standardButton; -void ButtonGfx::setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked) +void ButtonGfx::setSurfaces(shared_ptr marked, shared_ptr unmarked) { - ButtonGfx::_marked = marked; - ButtonGfx::_unmarked = unmarked; - xsize=(marked).GetWidth(); - ysize=(marked).GetHeight(); + this->marked = marked; + this->unmarked = unmarked; + xsize=(marked)->GetWidth(); + ysize=(marked)->GetHeight(); if(verboseLevel) cout << "Surfaces set, size: " <= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize) + if ( x >= this->x && y >= this->y && x<= this->x+gfx->xsize && y <= this->y + gfx->ysize) { return true; - else - return false; + } + return false; } void Button::doAction() { - if(action) + if (action) { action(this); - else - cerr << "Warning: button \"" << label << "\" has no action assigned!"; + return; + } + cerr << "Warning: button \"" << label << "\" has no action assigned!"; } void Button::drawTo(SDL_Surface **surface) @@ -114,13 +111,12 @@ void Button::drawTo(SDL_Surface **surface) #if DEBUG //cout << "Painting button: " << label << " at: " << x << "," << y << endl; #endif - if (marked) - gfx->_marked.PaintTo(*surface,x,y); - else - gfx->_unmarked.PaintTo(*surface,x,y); - //int stringx = x + (ButtonGfx::xsize)/2 - ButtonGfx::ttf->getTextWidth(label)/2; - //int stringy = y + (ButtonGfx::ysize)/2 - ButtonGfx::ttf->getTextHeight()/2; - //ButtonGfx::ttf->writeText(label,surface,stringx,stringy); + if (marked) { + gfx->marked->PaintTo(*surface,x,y); + } + else { + gfx->unmarked->PaintTo(*surface,x,y); + } gfx->thefont.setDest(*surface); gfx->thefont.drawCenter(x+gfx->xsize/2,y+gfx->ysize/2-gfx->thefont.getHeight(label.c_str())/2,label.c_str()); } @@ -149,11 +145,12 @@ void Menu::drawSelf() { DrawIMG(backgroundImage,screen,0,0); vector::iterator it; - for(it = buttons.begin(); it < buttons.end(); it++) + for (it = buttons.begin(); it < buttons.end(); it++) { (*it)->drawTo(&screen); + } exit.drawTo(&screen); standardButton.thefont.draw(50,50,title.c_str()); - mouse.PaintTo(screen,mousex,mousey); + mouse->PaintTo(screen,mousex,mousey); } void Menu::performClick(int x,int y) @@ -162,13 +159,16 @@ void Menu::performClick(int x,int y) for(it = buttons.begin(); it < buttons.end(); it++) { Button *b = (*it); - if(b->isClicked(x,y)) + if (b->isClicked(x,y)) { b->doAction(); - if(b->isPopOnRun()) + } + if (b->isPopOnRun()) { running = false; + } } - if(exit.isClicked(x,y)) + if (exit.isClicked(x,y)) { running = false; + } } void Menu::placeButtons() @@ -212,7 +212,7 @@ Menu::Menu(SDL_Surface **screen,bool submenu) exit.setLabel( _("Exit") ); } -Menu::Menu(SDL_Surface** screen, string title, bool submenu) +Menu::Menu(SDL_Surface** screen, const string& title, bool submenu) { this->screen = *screen; buttons = vector(0); diff --git a/source/code/MenuSystem.h b/source/code/MenuSystem.h index 3a3a4fd..b0afee4 100644 --- a/source/code/MenuSystem.h +++ b/source/code/MenuSystem.h @@ -30,23 +30,21 @@ http://blockattack.sf.net #include #include "Libs/NFont.h" #include "CppSdlImageHolder.hpp" - -using namespace std; +#include //The ButtonGfx object hold common media for all buttons, so we can reskin them by only changeing one pointer -class ButtonGfx +struct ButtonGfx { -public: //Holds the graphic for a button that is selected - CppSdl::CppSdlImageHolder _marked; + std::shared_ptr marked; //Holds the graphic for a button that is not selected - CppSdl::CppSdlImageHolder _unmarked; + std::shared_ptr unmarked; //The size of the buttons, so we don't have to ask w and h from the SDL Surfaces each time int xsize; int ysize; //A TTFont used for writing the label on the buttons NFont thefont; - void setSurfaces(CppSdl::CppSdlImageHolder marked,CppSdl::CppSdlImageHolder unmarked); + void setSurfaces(std::shared_ptr marked, std::shared_ptr unmarked); }; extern ButtonGfx standardButton; @@ -56,7 +54,7 @@ class Button { private: //The label. This is written on the button - string label; + std::string label; //Pointer to a callback function. void (*action)(Button *b); @@ -78,7 +76,7 @@ public: //Set the text to write on the button - void setLabel(string text); + void setLabel(const std::string& text); //Set the action to run void setAction(void (*action2run)(Button*)); @@ -98,13 +96,13 @@ public: class Menu { private: - vector buttons; //Vector holder the buttons + std::vector buttons; //Vector holder the buttons Button exit; //The exit button is special since it does not have a callback function bool isSubmenu; //True if the menu is a submenu int marked; //The index of the marked button (for keyboard up/down) bool running; //The menu is running. The menu will terminate then this is false SDL_Surface *screen; //Pointer to the screen to draw to - string title; + std::string title; // SDL_Surface *background; //Pointer to the background image void drawSelf(); //Private function to draw the screen @@ -115,7 +113,7 @@ public: //SubMenu is true by default Menu(SDL_Surface **screen,bool isSubmenu); Menu(SDL_Surface **screen); - Menu(SDL_Surface **screen, string title, bool isSubmenu); + Menu(SDL_Surface **screen, const std::string& title, bool isSubmenu); //Add a button to the menu void addButton(Button *b); @@ -127,13 +125,13 @@ public: class FileMenu { private: - string pm_path; - string pm_fileending; + std::string pm_path; + std::string pm_fileending; bool pm_hidden_files; public: - FileMenu(string path,string fileending,bool hidden_files = false); + FileMenu(const std::string& path, const std::string& fileending, bool hidden_files = false); - string getFile(SDL_Surface **screen); + std::string getFile(SDL_Surface **screen); }; #endif /* _MENUSYSTEM_H */ diff --git a/source/code/SConscript b/source/code/SConscript index 9b185fe..a81c4b6 100644 --- a/source/code/SConscript +++ b/source/code/SConscript @@ -15,8 +15,7 @@ common.cpp MenuSystem.cpp menudef.cpp puzzlehandler.cpp -CppSdlImageHolder.cpp -CppSdlException.cpp""") +CppSdlImageHolder.cpp""") # Modify Build Environment # env.ParseConfig('sdl-config --cflags --libs') diff --git a/source/code/global.hpp b/source/code/global.hpp index cbdd7db..d3bf790 100644 --- a/source/code/global.hpp +++ b/source/code/global.hpp @@ -26,12 +26,13 @@ http://blockattack.sf.net #include "CppSdlImageHolder.hpp" #include "Libs/NFont.h" +#include void MainMenu(); void ResetFullscreen(); -extern CppSdl::CppSdlImageHolder menuMarked; -extern CppSdl::CppSdlImageHolder menuUnmarked; +extern std::shared_ptr menuMarked; +extern std::shared_ptr menuUnmarked; extern NFont nf_scoreboard_font; extern bool MusicEnabled; //true if background music is enabled extern bool SoundEnabled; //true if sound effects is enabled diff --git a/source/code/main.cpp b/source/code/main.cpp index 96a07a8..5ae89aa 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -67,6 +67,7 @@ http://blockattack.sf.net #include "CppSdlImageHolder.hpp" #include "MenuSystem.h" #include "puzzlehandler.hpp" +#include //if SHAREDIR is not used we look in current directory #ifndef SHAREDIR @@ -147,7 +148,7 @@ SDL_Surface * IMG_Load2(const char* path) return surface; } -CppSdl::CppSdlImageHolder IMG_Load3(string path) +shared_ptr IMG_Load3(string path) { if (!PHYSFS_exists(path.c_str())) { @@ -176,7 +177,7 @@ CppSdl::CppSdlImageHolder IMG_Load3(string path) PHYSFS_close(myfile); - CppSdl::CppSdlImageHolder surface(m_data, m_size); + shared_ptr surface(new CppSdl::CppSdlImageHolder(m_data, m_size)); return surface; } @@ -560,8 +561,8 @@ static int InitImages() CONVERTA(iLoser); CONVERTA(iChainBack); CONVERTA(iGameOver); - mouse.OptimizeForBlit(true); - bNewGame.OptimizeForBlit(true); + mouse->OptimizeForBlit(true); + bNewGame->OptimizeForBlit(true); CONVERTA(stageBobble); CONVERTA(transCover); //Editor: @@ -717,8 +718,8 @@ void UnloadImages() SDL_FreeSurface(smiley[2]); SDL_FreeSurface(smiley[3]); SDL_FreeSurface(transCover); - mouse.MakeNull(); - bNewGame.MakeNull(); + mouse->MakeNull(); + bNewGame->MakeNull(); } //Function to convert numbers to string @@ -1785,7 +1786,7 @@ void OpenScoresDisplay() } //DrawIMG(mouse,screen,mousex,mousey); - mouse.PaintTo(screen,mousex,mousey); + mouse->PaintTo(screen,mousex,mousey); SDL_Flip(screen); //Update screen } @@ -1889,7 +1890,7 @@ bool OpenFileDialogbox(int x, int y, char *name) } //DrawIMG(mouse,screen,mousex,mousey); - mouse.PaintTo(screen,mousex,mousey); + mouse->PaintTo(screen,mousex,mousey); SDL_Flip(screen); //Update screen } return true; @@ -2377,7 +2378,7 @@ int PuzzleLevelSelect(int Type) } //DrawIMG(mouse,screen,mousex,mousey); - mouse.PaintTo(screen,mousex,mousey); + mouse->PaintTo(screen,mousex,mousey); SDL_Flip(screen); //draws it all to the screen } @@ -2523,7 +2524,7 @@ void startVsMenu() } //DrawIMG(mouse,screen,mousex,mousey); - mouse.PaintTo(screen,mousex,mousey); + mouse->PaintTo(screen,mousex,mousey); SDL_Flip(screen); //draws it all to the screen SDL_Delay(10); @@ -3847,7 +3848,7 @@ int runGame(int gametype, int level) oldMousex = mousex; oldMousey = mousey; //Draw the mouse: - mouse.PaintTo(screen,mousex,mousey); + mouse->PaintTo(screen,mousex,mousey); SDL_Flip(screen); } //game loop return 0; diff --git a/source/code/mainVars.hpp b/source/code/mainVars.hpp index b57b0d8..42e4b36 100644 --- a/source/code/mainVars.hpp +++ b/source/code/mainVars.hpp @@ -41,7 +41,7 @@ const char sharedir[] = SHAREDIR; static SDL_Surface *background; //Stores background SDL_Surface *backgroundImage; //Stores the background image static SDL_Surface *backBoard; //Stores the background to the board -static CppSdl::CppSdlImageHolder bNewGame; //The New Game botton +static std::shared_ptr bNewGame; //The New Game botton static SDL_Surface *bOptions; //The Options botton static SDL_Surface *bConfigure; //The configure button static SDL_Surface *bSelectPuzzle; //The Select Puzzle Button @@ -118,9 +118,9 @@ static SDL_Surface *bSkip; static SDL_Surface *bRetry; static SDL_Surface *bNext; -CppSdl::CppSdlImageHolder menuMarked; -CppSdl::CppSdlImageHolder menuUnmarked; -CppSdl::CppSdlImageHolder mouse; +std::shared_ptr menuMarked; +std::shared_ptr menuUnmarked; +std::shared_ptr mouse; static SDL_Surface *tmp; //a temporary surface to use DisplayFormat