diff --git a/source/code/CppSdl/CppSdlException.cpp b/source/code/CppSdl/CppSdlException.cpp new file mode 100644 index 0000000..f01c0db --- /dev/null +++ b/source/code/CppSdl/CppSdlException.cpp @@ -0,0 +1,30 @@ +/* + * File: CppSdlException.cpp + * Author: poul + * + * Created on 7. november 2010, 13:19 + */ + +#include "CppSdlException.hpp" + +namespace CppSdl { + +CppSdlException::CppSdlException() { + message = "An CppSdlException"; +} + +CppSdlException::CppSdlException(std::string msg) { + message = msg; +} + +CppSdlException::CppSdlException(const CppSdlException& orig) { +} + +//CppSdlException::~CppSdlException() { +//} + +const char* CppSdlException::what() const throw() { + return message.c_str(); +} + +} \ No newline at end of file diff --git a/source/code/CppSdl/CppSdlException.hpp b/source/code/CppSdl/CppSdlException.hpp new file mode 100644 index 0000000..7625a20 --- /dev/null +++ b/source/code/CppSdl/CppSdlException.hpp @@ -0,0 +1,29 @@ +/* + * File: CppSdlException.hpp + * Author: poul + * + * Created on 7. november 2010, 13:19 + */ + +#ifndef _CPPSDLEXCEPTION_HPP +#define _CPPSDLEXCEPTION_HPP + +#include + + +namespace CppSdl { +class CppSdlException : std::exception{ +public: + CppSdlException(); + CppSdlException(std::string msg); + CppSdlException(const CppSdlException& orig); + virtual ~CppSdlException() throw(); + virtual const char* what() const throw(); +private: + std::string message; +}; + + +} +#endif /* _CPPSDLEXCEPTION_HPP */ + diff --git a/source/code/CppSdl/CppSdlImageHolder.cpp b/source/code/CppSdl/CppSdlImageHolder.cpp new file mode 100644 index 0000000..387ec0d --- /dev/null +++ b/source/code/CppSdl/CppSdlImageHolder.cpp @@ -0,0 +1,111 @@ +/* + * File: CppSdlImageHolder.cpp + * Author: poul + * + * Created on 29. september 2010, 19:46 + */ + +#include "CppSdlImageHolder.hpp" +#include "SDL_image.h" + +namespace CppSdl { + +CppSdlImageHolder::CppSdlImageHolder(std::string filename) { + data = IMG_Load(filename.c_str()); + if(!data) + { + //Here we should throw an exception + CppSdlException e("Could not read file \""+filename+"\""); + throw e; + } + SDL_GetClipRect(data,&area); + + 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; + counter++; +} + +CppSdlImageHolder::CppSdlImageHolder(char* rawdata, int datasize) { + SDL_RWops *rw = SDL_RWFromMem (rawdata, datasize); + + //The above might fail an return null. + if(!rw) + { + CppSdlException e("Could not read raw data"); + throw e; + } + + SDL_Surface* data = IMG_Load_RW(rw,false); //the second argument tells the function to three RWops + + if(!data) + { + CppSdlException e("Could not read raw data"); + throw e; + } + + SDL_GetClipRect(data,&area); + counter = 1; +} + +CppSdlImageHolder::~CppSdlImageHolder() { + counter--; + if(counter == 0) + { + SDL_FreeSurface(data); + data = NULL; + } +} + +SDL_Surface* CppSdlImageHolder::GetRawDataInsecure() +{ + return data; +} + +Uint32 CppSdlImageHolder::GetWidth() +{ + return area.w; +} + +Uint32 CppSdlImageHolder::GetHeight() +{ + return area.h; +} + +void CppSdlImageHolder::Cut(Uint32 x, Uint32 y, Sint32 w = -1, Sint32 h = -1) +{ + 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; +} + +} \ No newline at end of file diff --git a/source/code/CppSdl/CppSdlImageHolder.hpp b/source/code/CppSdl/CppSdlImageHolder.hpp new file mode 100644 index 0000000..920114c --- /dev/null +++ b/source/code/CppSdl/CppSdlImageHolder.hpp @@ -0,0 +1,35 @@ +/* + * 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: + CppSdlImageHolder(std::string filename); + CppSdlImageHolder(const CppSdlImageHolder& orig); + CppSdlImageHolder(char *rawdata, int datasize); + virtual ~CppSdlImageHolder(); + SDL_Surface *GetRawDataInsecure(); + void Cut(Uint32 x,Uint32 y,Sint32 w,Sint32 h); + Uint32 GetWidth(); + Uint32 GetHeight(); +private: + Uint32 counter; + SDL_Rect area; + SDL_Surface *data; +}; + +} +#endif /* _CPPSDLIMAGEHOLDER_HPP */ + diff --git a/source/code/CppSdl/Makefile b/source/code/CppSdl/Makefile new file mode 100644 index 0000000..803910b --- /dev/null +++ b/source/code/CppSdl/Makefile @@ -0,0 +1,26 @@ +#CppSdl/Makefile + +CPP=g++ + +OUTDIR=../build + +ODIR=$(OUTDIR)/ + +BASE_CFLAGS=-c $(shell sdl-config --cflags) +BASE_LIBS=$(shell sdl-config --libs) -lSDL_image -lSDL_mixer + +#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/ReadKeyboard.cpp b/source/code/ReadKeyboard.cpp index e7178f8..816d783 100644 --- a/source/code/ReadKeyboard.cpp +++ b/source/code/ReadKeyboard.cpp @@ -17,9 +17,6 @@ Copyright (C) 2005 Poul Sander Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Poul Sander - R�veh�jvej 36, V. 1111 - 2800 Kgs. Lyngby - DENMARK blockattack@poulsander.com */ diff --git a/source/code/physfs_stream.hpp b/source/code/physfs_stream.hpp index d0bb3c0..0a6f232 100644 --- a/source/code/physfs_stream.hpp +++ b/source/code/physfs_stream.hpp @@ -24,9 +24,6 @@ physfs_stream.hpp Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Poul Sander - R�veh�jvej 36, V. 1111 - 2800 Kgs. Lyngby - DENMARK blockattack@poulsander.com */