diff --git a/source/code/CppSdl/CppSdlCommon.cpp b/source/code/CppSdl/CppSdlCommon.cpp deleted file mode 100644 index bc8d942..0000000 --- a/source/code/CppSdl/CppSdlCommon.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * File: CppSdlCommon.cpp - * Author: poul - * - * Created on 7. december 2010, 20:12 - */ - -#include "CppSdlCommon.hpp" - - -namespace CppSdl { - -CppSdlCommon* CppSdlCommon::instance = NULL; - -CppSdlCommon* CppSdlCommon::getInstance() { - if(!instance) { - instance = new CppSdlCommon(); - return instance; - } else - return instance; -} -void CppSdlCommon::setTime(long time) { - this->time = time; -} -long CppSdlCommon::getTime() const { - return time; -} - -CppSdlCommon::CppSdlCommon() { -} - - -} //namespace \ No newline at end of file diff --git a/source/code/CppSdl/CppSdlCommon.hpp b/source/code/CppSdl/CppSdlCommon.hpp deleted file mode 100644 index b0c2f78..0000000 --- a/source/code/CppSdl/CppSdlCommon.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * File: CppSdlCommon.hpp - * Author: poul - * - * Created on 7. december 2010, 20:12 - */ - -#ifndef _CPPSDLCOMMON_HPP -#define _CPPSDLCOMMON_HPP - -namespace CppSdl { -class CppSdlCommon { -public: - CppSdlCommon* getInstance(); - void setTime(long time); - long getTime() const; -protected: - CppSdlCommon(); - -private: - static CppSdlCommon *instance; - long time; -}; -}//namespace - -#endif /* _CPPSDLCOMMON_HPP */ - diff --git a/source/code/CppSdl/CppSdlException.cpp b/source/code/CppSdl/CppSdlException.cpp deleted file mode 100644 index fbec9e6..0000000 --- a/source/code/CppSdl/CppSdlException.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * File: CppSdlException.cpp - * Author: poul - * - * Created on 7. november 2010, 13:19 - */ - -#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/CppSdl/CppSdlException.hpp b/source/code/CppSdl/CppSdlException.hpp deleted file mode 100644 index acc7715..0000000 --- a/source/code/CppSdl/CppSdlException.hpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * File: CppSdlException.hpp - * Author: poul - * - * Created on 7. november 2010, 13:19 - */ - -#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/CppSdl/CppSdlFrame.cpp b/source/code/CppSdl/CppSdlFrame.cpp deleted file mode 100644 index 6a0bd85..0000000 --- a/source/code/CppSdl/CppSdlFrame.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * File: CppSdlFrame.cpp - * Author: poul - * - * Created on 7. december 2010, 20:27 - */ - -#include "CppSdlFrame.hpp" - -namespace CppSdl { -CppSdlFrame::CppSdlFrame() { -} - -CppSdlFrame::CppSdlFrame(const CppSdlFrame& orig) { -} - -CppSdlFrame::~CppSdlFrame() { -} -void CppSdlFrame::SetMilliseconds(long _milliseconds) { - this->_milliseconds = _milliseconds; -} -long CppSdlFrame::GetMilliseconds() const { - return _milliseconds; -} -void CppSdlFrame::SetImage(CppSdlImageHolder _image) { - this->_image = _image; -} -CppSdlImageHolder CppSdlFrame::GetImage() const { - return _image; -} - -}//namespace diff --git a/source/code/CppSdl/CppSdlFrame.hpp b/source/code/CppSdl/CppSdlFrame.hpp deleted file mode 100644 index 0a5cf84..0000000 --- a/source/code/CppSdl/CppSdlFrame.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * File: CppSdlFrame.hpp - * Author: poul - * - * Created on 7. december 2010, 20:27 - */ - -#ifndef _CPPSDLFRAME_HPP -#define _CPPSDLFRAME_HPP - -#include "CppSdlImageHolder.hpp" - - -namespace CppSdl { -class CppSdlFrame { -public: - CppSdlFrame(); - CppSdlFrame(const CppSdlFrame& orig); - virtual ~CppSdlFrame(); - void SetMilliseconds(long _milliseconds); - long GetMilliseconds() const; - void SetImage(CppSdlImageHolder _image); - CppSdlImageHolder GetImage() const; -private: - CppSdlImageHolder _image; - long _milliseconds; -}; - -} // namespace - -#endif /* _CPPSDLFRAME_HPP */ - diff --git a/source/code/CppSdl/CppSdlImageHolder.cpp b/source/code/CppSdl/CppSdlImageHolder.cpp deleted file mode 100644 index 46b0916..0000000 --- a/source/code/CppSdl/CppSdlImageHolder.cpp +++ /dev/null @@ -1,187 +0,0 @@ -/* - * File: CppSdlImageHolder.cpp - * Author: poul - * - * Created on 29. september 2010, 19:46 - */ - -#include "CppSdlImageHolder.hpp" -#include "SDL_image.h" - -namespace CppSdl { - -CppSdlImageHolder::CppSdlImageHolder() { - data = NULL; - counter = new int; - (*counter)=1; -} - -CppSdlImageHolder::CppSdlImageHolder(std::string filename) { - data = IMG_Load(filename.c_str()); - if(!data) - { - //Here we should throw an exception - CppSdlException e(IMAGE,CPPSDL_ERROR_MISSINGFILE,"Could not read file \""+filename+"\""); - throw e; - } - 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) { - 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; - } - - 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; - } - - 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() -{ - Initialized(); - return data; -} - -Uint32 CppSdlImageHolder::GetWidth() -{ - if(IsNull()) - return 0; - return area.w; -} - -Uint32 CppSdlImageHolder::GetHeight() -{ - if(IsNull()) - return 0; - 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 - if(IsNull()) - return; - dest.x = x; - dest.y = y; - SDL_BlitSurface(data,&area, target,&dest); -} - -void CppSdlImageHolder::OptimizeForBlit(bool allowAlpha) { - static SDL_Surface *tmp; - Initialized(); - if(allowAlpha) - tmp = SDL_DisplayFormatAlpha(data); - else - tmp = SDL_DisplayFormat(data); - SDL_FreeSurface(data); - data = tmp; -} - -void CppSdlImageHolder::Initialized() { - if(data == NULL) - throw(CppSdlException(IMAGE,CPPSDL_ERROR_NULLPOINTER,"ImageHolder used uninitialized!")); -} - -bool CppSdlImageHolder::IsNull() { - if(data == NULL || counter == NULL) - return true; - else - return false; -} - -void CppSdlImageHolder::MakeNull() { - if(IsNull()) - return; - (*counter)--; - if(*counter == 0 && data != NULL) - { - SDL_FreeSurface(data); - data = NULL; - } -} - -} diff --git a/source/code/CppSdl/CppSdlImageHolder.hpp b/source/code/CppSdl/CppSdlImageHolder.hpp deleted file mode 100644 index ad42bfb..0000000 --- a/source/code/CppSdl/CppSdlImageHolder.hpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - * File: CppSdlImageHolder.hpp - * Author: poul - * - * Created on 29. september 2010, 19:46 - */ - -#ifndef _CPPSDLIMAGEHOLDER_HPP -#define _CPPSDLIMAGEHOLDER_HPP - -#include "SDL.h" -#include -#include "CppSdlException.hpp" - -namespace CppSdl { - -class CppSdlImageHolder { -public: - /** - * Creates a nulled ImageHolder (without data) - */ - CppSdlImageHolder(); - /** - * Creates an image from a file - * @param filename to be loaded - */ - CppSdlImageHolder(std::string filename); - /** - * Creates a copy. The new copy shares raw data with the original but is otherwise independent. - * @param orig - */ - CppSdlImageHolder(const CppSdlImageHolder& orig); - CppSdlImageHolder(char *rawdata, int datasize); - virtual ~CppSdlImageHolder(); - /** - * This gives access direct access to the internal data. - * Careful, the data might be shared between multiple ImageHolders, - * chaing one will change them all. - * @return A pointer - */ - SDL_Surface *GetRawDataInsecure(); - CppSdlImageHolder & operator=(const CppSdlImageHolder &rhs); - void Cut(Uint32 x,Uint32 y,Sint32 w,Sint32 h); - /** - * The width of the image - * 0 if the image is nulled - * @return The width of the image in pizels - */ - Uint32 GetWidth(); - /** - * The height of the image - * 0 if the image is nulled - * @return The height of the image - */ - Uint32 GetHeight(); - /** - * Draws an image to surface at a given position. - * If the image is nulled nothing is drawn - * @param target Destination surface - * @param x horizontal placement on destination surface - * @param y vertical placement on destination surface - */ - void PaintTo(SDL_Surface *target, int x, int y); - void OptimizeForBlit(bool allowAlpha = true); - /** - * Tests if there are data in the object - * @return true if where are data in the object - */ - bool IsNull(); - /** - * Removes the data in the object. - * Once all ImageHolders pointing to the same data has been freed the internal data will be freed too - */ - void MakeNull(); -private: - void Initialized(); //throws an exception if *data is null - int *counter; - SDL_Rect area; - SDL_Surface *data; -}; - -} -#endif /* _CPPSDLIMAGEHOLDER_HPP */ - diff --git a/source/code/CppSdl/CppSdlSprite.cpp b/source/code/CppSdl/CppSdlSprite.cpp deleted file mode 100644 index ea2888a..0000000 --- a/source/code/CppSdl/CppSdlSprite.cpp +++ /dev/null @@ -1,18 +0,0 @@ -/* - * File: CppSdlSprite.cpp - * Author: poul - * - * Created on 7. december 2010, 19:47 - */ - -#include "CppSdlSprite.hpp" - -CppSdlSprite::CppSdlSprite() { -} - -CppSdlSprite::CppSdlSprite(const CppSdlSprite& orig) { -} - -CppSdlSprite::~CppSdlSprite() { -} - diff --git a/source/code/CppSdl/CppSdlSprite.hpp b/source/code/CppSdl/CppSdlSprite.hpp deleted file mode 100644 index fd1b2ce..0000000 --- a/source/code/CppSdl/CppSdlSprite.hpp +++ /dev/null @@ -1,21 +0,0 @@ -/* - * File: CppSdlSprite.hpp - * Author: poul - * - * Created on 7. december 2010, 19:47 - */ - -#ifndef _CPPSDLSPRITE_HPP -#define _CPPSDLSPRITE_HPP - -class CppSdlSprite { -public: - CppSdlSprite(); - CppSdlSprite(const CppSdlSprite& orig); - virtual ~CppSdlSprite(); -private: - -}; - -#endif /* _CPPSDLSPRITE_HPP */ - diff --git a/source/code/CppSdl/CppSdlTile.cpp b/source/code/CppSdl/CppSdlTile.cpp deleted file mode 100644 index 5997230..0000000 --- a/source/code/CppSdl/CppSdlTile.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - * File: CppSdlTile.cpp - * Author: poul - * - * Created on 7. december 2010, 19:55 - */ - -#include - -#include "CppSdlTile.hpp" - -namespace CppSdl { -CppSdlTile::CppSdlTile() { - _collideMode = OFF; - _frames.clear(); -} - -CppSdlTile::CppSdlTile(const CppSdlTile& orig) { - _collideMode = orig->_collideMode; - _frames = orig->_frames; -} - -CppSdlTile::~CppSdlTile() { -} - -void CppSdlTile::setCollideMode(CollideMode _collideMode) { - this->_collideMode = _collideMode; -} - -CollideMode CppSdlTile::getCollideMode() const { - return _collideMode; -} - -void CppSdlTile::addFrame(CppSdlFrame frame) { - _frames.push_back(frame); -} - -void CppSdlTile::addFrame(CppSdlImageHolder image, long milliseconds) { - CppSdlFrame f; - f.SetImage(image); - f.SetMilliseconds(milliseconds); - addFrame(f); -} - -void CppSdlTile::clearFrames() { - _frames.clear(); -} - - -} //namespace \ No newline at end of file diff --git a/source/code/CppSdl/CppSdlTile.hpp b/source/code/CppSdl/CppSdlTile.hpp deleted file mode 100644 index 6d5d02c..0000000 --- a/source/code/CppSdl/CppSdlTile.hpp +++ /dev/null @@ -1,38 +0,0 @@ -/* - * File: CppSdlTile.hpp - * Author: poul - * - * Created on 7. december 2010, 19:55 - */ - -#ifndef _CPPSDLTILE_HPP -#define _CPPSDLTILE_HPP - -#include "CppSdlFrame.hpp" - - -namespace CppSdl { -enum CollideMode {OFF,BOX,PIXEL}; - -class CppSdlTile { -public: - CppSdlTile(); - CppSdlTile(const CppSdlTile& orig); - virtual ~CppSdlTile(); - - void addFrame(CppSdlImageHolder image, long milliseconds); - void addFrame(CppSdlFrame frame); - void clearFrames(); - - void draw(); - - void setCollideMode(CollideMode _collideMode); - CollideMode getCollideMode() const; -private: - CollideMode _collideMode; - std::vector _frames; -}; -} //namespace - -#endif /* _CPPSDLTILE_HPP */ - diff --git a/source/code/CppSdl/Makefile b/source/code/CppSdl/Makefile deleted file mode 100644 index 37f04b5..0000000 --- a/source/code/CppSdl/Makefile +++ /dev/null @@ -1,30 +0,0 @@ -#CppSdl/Makefile - -ifndef CPP -CPP=g++ -endif - -OUTDIR=../build - -ODIR=$(OUTDIR)/ - -BASE_CFLAGS=-c $(shell sdl-config --cflags) -BASE_LIBS=$(shell sdl-config --libs) -lSDL_image -lSDL_mixer - -DEBUG=1 - -#Compile with debug information or optimized. -ifeq ($(DEBUG),1) -BASE_CFLAGS += -g -DDEBUG=1 -else -BASE_CFLAGS += -O4 -endif - -total: $(ODIR)/CppSdlImageHolder.o $(ODIR)/CppSdlException.o - -$(ODIR)/CppSdlImageHolder.o: CppSdlImageHolder.hpp CppSdlImageHolder.cpp - $(CPP) $(BASE_CFLAGS) -o $(ODIR)/CppSdlImageHolder.o CppSdlImageHolder.cpp - - -$(ODIR)/CppSdlException.o: CppSdlException.hpp CppSdlException.cpp - $(CPP) $(BASE_CFLAGS) -o $(ODIR)/CppSdlException.o CppSdlException.cpp diff --git a/source/code/CppSdlException.cpp b/source/code/CppSdlException.cpp new file mode 100644 index 0000000..fbec9e6 --- /dev/null +++ b/source/code/CppSdlException.cpp @@ -0,0 +1,36 @@ +/* + * File: CppSdlException.cpp + * Author: poul + * + * Created on 7. november 2010, 13:19 + */ + +#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 new file mode 100644 index 0000000..acc7715 --- /dev/null +++ b/source/code/CppSdlException.hpp @@ -0,0 +1,46 @@ +/* + * File: CppSdlException.hpp + * Author: poul + * + * Created on 7. november 2010, 13:19 + */ + +#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 new file mode 100644 index 0000000..46b0916 --- /dev/null +++ b/source/code/CppSdlImageHolder.cpp @@ -0,0 +1,187 @@ +/* + * File: CppSdlImageHolder.cpp + * Author: poul + * + * Created on 29. september 2010, 19:46 + */ + +#include "CppSdlImageHolder.hpp" +#include "SDL_image.h" + +namespace CppSdl { + +CppSdlImageHolder::CppSdlImageHolder() { + data = NULL; + counter = new int; + (*counter)=1; +} + +CppSdlImageHolder::CppSdlImageHolder(std::string filename) { + data = IMG_Load(filename.c_str()); + if(!data) + { + //Here we should throw an exception + CppSdlException e(IMAGE,CPPSDL_ERROR_MISSINGFILE,"Could not read file \""+filename+"\""); + throw e; + } + 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) { + 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; + } + + 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; + } + + 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() +{ + Initialized(); + return data; +} + +Uint32 CppSdlImageHolder::GetWidth() +{ + if(IsNull()) + return 0; + return area.w; +} + +Uint32 CppSdlImageHolder::GetHeight() +{ + if(IsNull()) + return 0; + 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 + if(IsNull()) + return; + dest.x = x; + dest.y = y; + SDL_BlitSurface(data,&area, target,&dest); +} + +void CppSdlImageHolder::OptimizeForBlit(bool allowAlpha) { + static SDL_Surface *tmp; + Initialized(); + if(allowAlpha) + tmp = SDL_DisplayFormatAlpha(data); + else + tmp = SDL_DisplayFormat(data); + SDL_FreeSurface(data); + data = tmp; +} + +void CppSdlImageHolder::Initialized() { + if(data == NULL) + throw(CppSdlException(IMAGE,CPPSDL_ERROR_NULLPOINTER,"ImageHolder used uninitialized!")); +} + +bool CppSdlImageHolder::IsNull() { + if(data == NULL || counter == NULL) + return true; + else + return false; +} + +void CppSdlImageHolder::MakeNull() { + if(IsNull()) + return; + (*counter)--; + if(*counter == 0 && data != NULL) + { + SDL_FreeSurface(data); + data = NULL; + } +} + +} diff --git a/source/code/CppSdlImageHolder.hpp b/source/code/CppSdlImageHolder.hpp new file mode 100644 index 0000000..ad42bfb --- /dev/null +++ b/source/code/CppSdlImageHolder.hpp @@ -0,0 +1,84 @@ +/* + * File: CppSdlImageHolder.hpp + * Author: poul + * + * Created on 29. september 2010, 19:46 + */ + +#ifndef _CPPSDLIMAGEHOLDER_HPP +#define _CPPSDLIMAGEHOLDER_HPP + +#include "SDL.h" +#include +#include "CppSdlException.hpp" + +namespace CppSdl { + +class CppSdlImageHolder { +public: + /** + * Creates a nulled ImageHolder (without data) + */ + CppSdlImageHolder(); + /** + * Creates an image from a file + * @param filename to be loaded + */ + CppSdlImageHolder(std::string filename); + /** + * Creates a copy. The new copy shares raw data with the original but is otherwise independent. + * @param orig + */ + CppSdlImageHolder(const CppSdlImageHolder& orig); + CppSdlImageHolder(char *rawdata, int datasize); + virtual ~CppSdlImageHolder(); + /** + * This gives access direct access to the internal data. + * Careful, the data might be shared between multiple ImageHolders, + * chaing one will change them all. + * @return A pointer + */ + SDL_Surface *GetRawDataInsecure(); + CppSdlImageHolder & operator=(const CppSdlImageHolder &rhs); + void Cut(Uint32 x,Uint32 y,Sint32 w,Sint32 h); + /** + * The width of the image + * 0 if the image is nulled + * @return The width of the image in pizels + */ + Uint32 GetWidth(); + /** + * The height of the image + * 0 if the image is nulled + * @return The height of the image + */ + Uint32 GetHeight(); + /** + * Draws an image to surface at a given position. + * If the image is nulled nothing is drawn + * @param target Destination surface + * @param x horizontal placement on destination surface + * @param y vertical placement on destination surface + */ + void PaintTo(SDL_Surface *target, int x, int y); + void OptimizeForBlit(bool allowAlpha = true); + /** + * Tests if there are data in the object + * @return true if where are data in the object + */ + bool IsNull(); + /** + * Removes the data in the object. + * Once all ImageHolders pointing to the same data has been freed the internal data will be freed too + */ + void MakeNull(); +private: + void Initialized(); //throws an exception if *data is null + int *counter; + SDL_Rect area; + SDL_Surface *data; +}; + +} +#endif /* _CPPSDLIMAGEHOLDER_HPP */ + diff --git a/source/code/MenuSystem.cc b/source/code/MenuSystem.cc index 2787732..44947ad 100644 --- a/source/code/MenuSystem.cc +++ b/source/code/MenuSystem.cc @@ -22,7 +22,7 @@ Copyright (C) 2008 Poul Sander #include "MenuSystem.h" #include "common.h" -#include "CppSdl/CppSdlImageHolder.hpp" +#include "CppSdlImageHolder.hpp" extern CppSdl::CppSdlImageHolder mouse; extern SDL_Surface *backgroundImage; diff --git a/source/code/MenuSystem.h b/source/code/MenuSystem.h index 3e8813d..8d8fb58 100644 --- a/source/code/MenuSystem.h +++ b/source/code/MenuSystem.h @@ -28,7 +28,7 @@ Copyright (C) 2008 Poul Sander #include "SDL.h" #include #include "Libs/NFont.h" -#include "CppSdl/CppSdlImageHolder.hpp" +#include "CppSdlImageHolder.hpp" using namespace std; diff --git a/source/code/block.make b/source/code/block.make index 6104040..22b4618 100644 --- a/source/code/block.make +++ b/source/code/block.make @@ -6,9 +6,9 @@ ifndef CC CC=gcc endif -ifndef CPP +#ifndef CPP CPP=g++ -endif +#endif BASE_CFLAGS=-c $(shell sdl-config --cflags) @@ -43,8 +43,7 @@ endif BASE_LIBS += -lphysfs -$(BINARY): $(BUILDDIR)/main.o $(BUILDDIR)/highscore.o $(BUILDDIR)/ReadKeyboard.o $(BUILDDIR)/joypad.o $(BUILDDIR)/listFiles.o $(BUILDDIR)/replay.o $(BUILDDIR)/common.o $(BUILDDIR)/stats.o $(BUILDDIR)/nfont.o $(BUILDDIR)/MenuSystem.o $(BUILDDIR)/menudef.o - @make -C CppSdl +$(BINARY): $(BUILDDIR)/main.o $(BUILDDIR)/highscore.o $(BUILDDIR)/ReadKeyboard.o $(BUILDDIR)/joypad.o $(BUILDDIR)/listFiles.o $(BUILDDIR)/replay.o $(BUILDDIR)/common.o $(BUILDDIR)/stats.o $(BUILDDIR)/CppSdlException.o $(BUILDDIR)/CppSdlImageHolder.o $(BUILDDIR)/nfont.o $(BUILDDIR)/MenuSystem.o $(BUILDDIR)/menudef.o $(CPP) -O -o $(BINARY) $(BUILDDIR)/main.o $(BUILDDIR)/highscore.o $(BUILDDIR)/ReadKeyboard.o $(BUILDDIR)/joypad.o $(BUILDDIR)/listFiles.o $(BUILDDIR)/replay.o $(BUILDDIR)/common.o $(BUILDDIR)/stats.o $(BUILDDIR)/CppSdlException.o $(BUILDDIR)/CppSdlImageHolder.o $(BUILDDIR)/nfont.o $(BUILDDIR)/MenuSystem.o $(BUILDDIR)/menudef.o $(BASE_LIBS) #-lphysfs @@ -91,6 +90,12 @@ $(BUILDDIR)/MenuSystem.o: MenuSystem.cc MenuSystem.h #$(BUILDDIR)/ttfont.o: ttfont.h ttfont.cc # $(CPP) $(BASE_CFLAGS) ttfont.cc -o $(BUILDDIR)/ttfont.o +$(BUILDDIR)/CppSdlImageHolder.o: CppSdlImageHolder.hpp CppSdlImageHolder.cpp + $(CPP) $(BASE_CFLAGS) -o $(BUILDDIR)/CppSdlImageHolder.o CppSdlImageHolder.cpp + +$(BUILDDIR)/CppSdlException.o: CppSdlException.hpp CppSdlException.cpp + $(CPP) $(BASE_CFLAGS) -o $(BUILDDIR)/CppSdlException.o CppSdlException.cpp + run: $(BINARY) diff --git a/source/code/global.hpp b/source/code/global.hpp index f9bb64a..f7bbd93 100644 --- a/source/code/global.hpp +++ b/source/code/global.hpp @@ -8,7 +8,7 @@ #ifndef _GLOBAL_HPP #define _GLOBAL_HPP -#include "CppSdl/CppSdlImageHolder.hpp" +#include "CppSdlImageHolder.hpp" #include "Libs/NFont.h" void MainMenu(); diff --git a/source/code/main.cpp b/source/code/main.cpp index 64bada2..039db65 100644 --- a/source/code/main.cpp +++ b/source/code/main.cpp @@ -66,7 +66,7 @@ Copyright (C) 2008 Poul Sander #include #include #include -#include "CppSdl/CppSdlImageHolder.hpp" +#include "CppSdlImageHolder.hpp" #include "MenuSystem.h" //if SHAREDIR is not used we look in current directory @@ -4671,4 +4671,4 @@ void runGame(int gametype) { mouse.PaintTo(screen,mousex,mousey); SDL_Flip(screen); } //game loop -} \ No newline at end of file +}