git repos / blockattack-game

commit f3a1637d

sago007 · 2015-11-14 21:32
f3a1637d5a24c4b44564fd54911fbe2fccfc2e7c patch · browse files
parent 08f71f59ac8ea58a5ae8029f41162e6bb32cb759

Started removing unused code that should never have been in the game

Changed files

M source/code/CppSdlImageHolder.cpp before
M source/code/CppSdlImageHolder.hpp before
M source/code/MenuSystem.cpp before
M source/code/MenuSystem.h before
M source/code/global.hpp before
M source/code/main.cpp before
M source/code/mainVars.hpp before
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<CppSdl::CppSdlImageHolder> 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<CppSdl::CppSdlImageHolder> marked, shared_ptr<CppSdl::CppSdlImageHolder> 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: " <<xsize << " , " << ysize << endl;
}
@@ -115,9 +115,9 @@ void Button::drawTo(SDL_Surface **surface)
//cout << "Painting button: " << label << " at: " << x << "," << y << endl;
#endif
if (marked)
- gfx->_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 <vector>
#include "Libs/NFont.h"
#include "CppSdlImageHolder.hpp"
+#include <memory>
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<CppSdl::CppSdlImageHolder> _marked;
//Holds the graphic for a button that is not selected
- CppSdl::CppSdlImageHolder _unmarked;
+ std::shared_ptr<CppSdl::CppSdlImageHolder> _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<CppSdl::CppSdlImageHolder> marked, std::shared_ptr<CppSdl::CppSdlImageHolder> 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 <memory>
void MainMenu();
void ResetFullscreen();
-extern CppSdl::CppSdlImageHolder menuMarked;
-extern CppSdl::CppSdlImageHolder menuUnmarked;
+extern std::shared_ptr<CppSdl::CppSdlImageHolder> menuMarked;
+extern std::shared_ptr<CppSdl::CppSdlImageHolder> 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 <memory>
//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<CppSdl::CppSdlImageHolder> 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<CppSdl::CppSdlImageHolder> 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<CppSdl::CppSdlImageHolder> 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<CppSdl::CppSdlImageHolder> menuMarked;
+std::shared_ptr<CppSdl::CppSdlImageHolder> menuUnmarked;
+std::shared_ptr<CppSdl::CppSdlImageHolder> mouse;
static SDL_Surface *tmp; //a temporary surface to use DisplayFormat