diff --git a/source/code/CppSdlImageHolder.cpp b/source/code/CppSdlImageHolder.cpp index 6375248..f4b6353 100644 --- a/source/code/CppSdlImageHolder.cpp +++ b/source/code/CppSdlImageHolder.cpp @@ -30,8 +30,6 @@ namespace CppSdl CppSdlImageHolder::CppSdlImageHolder() { data = NULL; - counter = new int; - (*counter)=1; } CppSdlImageHolder::CppSdlImageHolder(std::string filename) @@ -45,36 +43,6 @@ CppSdlImageHolder::CppSdlImageHolder(std::string 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,25 @@ CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize) SDL_RWops *rw = SDL_RWFromMem (rawdata, datasize); //The above might fail and return null. - if(!rw) - { + if (!rw) { CppSdlException e(IMAGE,CPPSDL_ERROR_NULLPOINTER, "Could not read raw data"); throw e; } data = IMG_Load_RW(rw,true); //the second argument tells the function to free RWops - if(!data) - { + if (!data) { CppSdlException e(IMAGE,CPPSDL_ERROR_DATA,"Could not convert raw data to image"); throw e; } 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 +79,7 @@ SDL_Surface* CppSdlImageHolder::GetRawDataInsecure() Uint32 CppSdlImageHolder::GetWidth() { - if(IsNull()) + if (IsNull()) return 0; return area.w; } @@ -134,39 +91,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 @@ -197,22 +121,18 @@ void CppSdlImageHolder::Initialized() 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..b46f102 100644 --- a/source/code/CppSdlImageHolder.hpp +++ b/source/code/CppSdlImageHolder.hpp @@ -47,9 +47,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 +57,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 +91,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/MenuSystem.cpp b/source/code/MenuSystem.cpp index 145ad56..8992df4 100644 --- a/source/code/MenuSystem.cpp +++ b/source/code/MenuSystem.cpp @@ -27,7 +27,7 @@ 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; @@ -51,12 +51,12 @@ 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(); + xsize=(marked)->GetWidth(); + ysize=(marked)->GetHeight(); if(verboseLevel) cout << "Surfaces set, size: " <_marked.PaintTo(*surface,x,y); + gfx->_marked->PaintTo(*surface,x,y); else - gfx->_unmarked.PaintTo(*surface,x,y); + 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); @@ -153,7 +153,7 @@ void Menu::drawSelf() (*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) diff --git a/source/code/MenuSystem.h b/source/code/MenuSystem.h index 3a3a4fd..3857f29 100644 --- a/source/code/MenuSystem.h +++ b/source/code/MenuSystem.h @@ -30,6 +30,7 @@ http://blockattack.sf.net #include #include "Libs/NFont.h" #include "CppSdlImageHolder.hpp" +#include using namespace std; @@ -38,15 +39,15 @@ class 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; 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 0f2c5a0..c078f6a 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; } @@ -562,8 +563,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: @@ -719,8 +720,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 @@ -1787,7 +1788,7 @@ void OpenScoresDisplay() } //DrawIMG(mouse,screen,mousex,mousey); - mouse.PaintTo(screen,mousex,mousey); + mouse->PaintTo(screen,mousex,mousey); SDL_Flip(screen); //Update screen } @@ -1891,7 +1892,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; @@ -2379,7 +2380,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 } @@ -2525,7 +2526,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); @@ -3849,7 +3850,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