//TODO SEARCH MEMORY LEAKS FAST!!!! #include "imagedata.h" #include "assert.h" #include "init.h" /*########################################################################################################################################################## IMAGE LOAD FUNCTIONS ############################################################################################################################################################*/ SDL_Surface *load_image( std::string *filename){ //images saved to surfaces SDL_Surface* tempimage = NULL; SDL_Surface* returnimage = NULL; std::string fn= *filename; //load the image from a file tempimage= IMG_Load( fn.c_str() ); //if an image was loaded optimize it and delete the temporary if(tempimage != NULL ) { returnimage = SDL_DisplayFormatAlpha( tempimage ); SDL_FreeSurface( tempimage ); } return returnimage; } SDL_Surface *load_image_transparent( std::string filename){ //images saved to surfaces SDL_Surface* tempimage = NULL; SDL_Surface* returnimage = NULL; //load the image from a file tempimage= IMG_Load( filename.c_str() ); //if an image was loaded optimize it and delete the temporary if(tempimage != NULL ) { SDL_SetAlpha(tempimage,SDL_SRCALPHA,128); returnimage=SDL_DisplayFormatAlpha( tempimage );; SDL_FreeSurface( tempimage ); } return returnimage; } /*########################################################################################################################################################## SOLIDITY DATA DATATYPE (should work) ############################################################################################################################################################*/ solidity_data::solidity_data(){ this->solidity=new bool[this->size]; for(int i=0;isize;i++)this->solidity[i]=false; } solidity_data::solidity_data(bool s1,bool s2,bool s3,bool s4,bool s5,bool s6,bool s7, bool s8, bool s9, bool s10, bool s11, bool s12, bool s13,bool s14,bool s15, bool s16){ this->solidity=new bool[this->size]; this->solidity[0]=s1; this->solidity[1]=s2; this->solidity[2]=s3; this->solidity[3]=s4; this->solidity[4]=s5; this->solidity[5]=s6; this->solidity[6]=s7; this->solidity[7]=s8; this->solidity[8]=s9; this->solidity[9]=s10; this->solidity[10]=s11; this->solidity[11]=s12; this->solidity[12]=s13; this->solidity[13]=s14; this->solidity[14]=s15; this->solidity[15]=s16; } solidity_data::~solidity_data(){ } void solidity_data::set_data_i(int i, bool new_state){ assert(i>=0&&isize&&"trying to set solidity out of range !!!solidity_data::set_data_ij exception!!!"); this->solidity[i]=new_state; } void solidity_data::write_solidity_data(std::ofstream *outfile){ assert(outfile!=NULL&&"tried to save solidity to a NULLPointer Badboy!!!"); assert(outfile->is_open()&&"trying to write to a non-open filestream!!! solidity_data exception !!!"); for(int i=0;isize;i++){ *outfile<solidity[i]<=0&&isize&&"trying to get solidity out of range !!!solidity_data::get_datapoint exception!!!"); return this->solidity[i]; } int solidity_data::GetSize(){ return this->size; } solidity_data* load_solidity_data(std::ifstream* infile){ assert(infile!=NULL&&"tried to read solidity from a NULLPointer Badboy!!!"); assert(infile->is_open()&&"trying to read from a non-open filestream!!! write_g_sprite_data exception !!!"); solidity_data* return_solidity_data=new solidity_data; for(int i=0;iGetSize();i++){ bool var; *infile>>var; return_solidity_data->set_data_i(i,var); } return return_solidity_data; } void solidity_data::delete_anything(){ // delete this->solidity; this->solidity=NULL; } /*########################################################################################################################################################## ANYTHING FOR THE TILE DATATYPE ############################################################################################################################################################*/ tile::tile(){ this->image=NULL; } tile::tile(std::string *filename){ assert(filename!=NULL&&"tried to make tile from a NULLPointer Badboy!!!"); this->image=new SDL_Surface; this->image=load_image(filename); assert(this->image->w==80&&this->image->h==80&&"wrong imagesize"); //force that only 80x80pix become tiles } tile::~tile(){ //dereferences everything(objects are not to be deleted because they will be used in a higher instance(sprite)) this->image=NULL; } void tile::draw_tile(int x, int y,SDL_Surface *screen){ assert(screen!=NULL&&"SOMEONE tries to draw to a non-existant screen !!!draw tile exception!!!"); SDL_Rect xycoord; xycoord.x=x; xycoord.y=y; assert(this->image!=NULL&&"error tried to draw a tile from a nullpointer !!! draw_tile exception!!!"); SDL_BlitSurface( this->image, NULL, screen, &xycoord ); } void tile::delete_anything(){ assert(this->image!=NULL&& "error tried to free a SDL_Surface Nullpointer!!!tile delete anything exception!!!"); SDL_FreeSurface(this->image); //free the surface this->image=NULL; //set pointer to Null to avoid trouble } /*########################################################################################################################################################## ANYTHING FOR THE SPRITE DATATYPE ############################################################################################################################################################*/ sprite::sprite(){ //start everything off with NULL-Pointers to avoid trouble this->animstate=NULL; this->frames=NULL; this->speed=NULL; this->state=NULL; this->tiles=NULL; } sprite::sprite(int _frames,int _speed,std::string* t){ //create all data for the sprite this->frames=new int; *this->frames=_frames; this->speed=new int; *this->speed=_speed; this->state=new int; *this->state=0; this->animstate=new int; *this->animstate=0; this->tiles=new tile[*this->frames]; for(int i=0;i<*this->frames;i++){ assert((t+i)!=NULL&&"trying to make a sprite with a string NULL pointer !!!sprite constructor exception!!!"); this->tiles[i]=tile(t+i); } } void sprite::delete_anything(){ assert(this->speed!=NULL&&this->state!=NULL&&this->animstate!=NULL&&"error one of the int-variables in sprite delete anything is a NULLpointer fix that!!!"); //clean up all pointers delete this->speed; delete this->state; delete this->animstate; //clean up any data from the tiles for(int i=0;i<*this->frames;i++){ assert((this->tiles+i)!=NULL&&"error tried to delete a NUllpointer to a tile !!!sprite delete anything exception !!!"); (this->tiles+i)->delete_anything(); } //finish cleaning up all pointers delete[] this->tiles; assert(this->frames!=NULL&&"error one of the int-variables in sprite delete anything is a NULLpointer fix that!!!"); delete this->frames; //set all pointers to 0 to avoid trouble this->speed=NULL; this->state=NULL; this->tiles=NULL; this->animstate=NULL; this->frames=NULL; } sprite::~sprite(){ //dereferences everything(objects are not to be deleted because they will be used in an higher instance(s_sprite)) this->speed=NULL; this->state=NULL; this->tiles=NULL; this->animstate=NULL; this->frames=NULL; } void sprite::draw_sprite_anyspeed(int x, int y,int _speed,SDL_Surface *screen){ assert(this->animstate!=NULL&&this->frames!=NULL&&this->state!=NULL&&"error frames, state or animstate are nullpointers!!! draw_sprite exception!!!"); assert((this->tiles+*this->animstate)!=NULL&&"Error the current tile of the a sprite you try to draw is s NULLpointer !!! draw sprite exception!!!"); assert(*this->animstate<*this->frames&&"Error got an animation state that is bigger than allowed frames!!!draw_sprite exception !!!"); (this->tiles+*this->animstate)->draw_tile(x,y,screen); *this->state=(*this->state+1)%_speed; if(*state==1){ //if you reach the point where the sprite is supposed to be updated update it but make sure you wont go beyond the frame boundary and for 1 frame only dont update ;-) *this->animstate=(*this->animstate+1)%*this->frames; } } void sprite::draw_sprite(int x, int y,SDL_Surface *screen){ draw_sprite_anyspeed( x, y,*this->speed,screen); } std::string* load_spritedata(std::ifstream * infile, int *_frames, int *_speed){ assert(infile!=NULL&&"tried to take data from a NULLPointer!!!load_spritedata exception !!!"); assert(infile->is_open()&&"tried to take data from a non open filestream!!!load_spritedata exception!!!"); *infile>>*_frames; *infile>>*_speed; std::string *pics=new std::string[*_frames]; for(int i=0;i<*_frames;i++){ *infile>>pics[i]; } return pics; } void sprite::create_sprite_from_data(int _frames, int _speed, std::string* pngs){ this->frames=new int; *this->frames=_frames; this->speed=new int; *this->speed=_speed; this->state=new int; *this->state=0; this->animstate=new int; *this->animstate=0; this->tiles=new tile[*this->frames]; for(int i=0;i<*this->frames;i++){ assert(i<(int)pngs->size()); //make sure you wont go beyond the array bounds this->tiles[i]=tile(pngs+i); } } void sprite::load_from_spritefile(std::string filename){ //get all the data stored in the spritefile int _frames; int _speed; std::ifstream *infile=new std::ifstream; infile->open(filename); std::string *pics=load_spritedata(infile, &_frames,&_speed); infile->close(); delete infile; create_sprite_from_data(_frames,_speed, pics); delete[] pics; pics=NULL; infile=NULL; } void write_sprite_data(int _frames, int _speed,std::string *t,std::ofstream *outfile){ assert(outfile!=NULL&&"tried to write to a file nullpointer !!!write spritedata exception !!!"); assert(outfile->is_open()&&"seems you tried to write to a file that is not open!!!write sprite data exception !!!"); *outfile<<_frames<open(filename); write_sprite_data(_frames,_speed,t,outfile); outfile->close(); delete outfile; outfile=NULL; } /*########################################################################################################################################################## ANYTHING FOR THE SHADOW SPRITE DATATYPE ############################################################################################################################################################*/ s_sprite::s_sprite(){ //initialize everything as NULL-Pointers to avoid trouble this->has_shadow=NULL; this->shadow=NULL; this->thesprite=NULL; } s_sprite::s_sprite(int _frames,int _speed,std::string* t1,std::string*t2){ assert(t1!=NULL&&t2!=NULL&&"trying to make a s_sprite with at least 1 Nullpointer string !!!s_sprite constructor exception!!!"); this->thesprite=new sprite; this->shadow=new sprite; this->has_shadow=new bool; this->thesprite=&sprite(_frames,_speed, t1); *this->shadow=sprite(_frames,_speed,t2); *this->has_shadow=true; } s_sprite::s_sprite(int _frames,int _speed,std::string* t1){ assert(t1!=NULL&&"trying to make a s_sprite with 1 Nullpointer string !!!s_sprite constructor(without shadow) exception!!!"); this->thesprite=new sprite; this->has_shadow=new bool; *this->thesprite=sprite(_frames,_speed, t1); *this->has_shadow=false; } s_sprite::~s_sprite(){ //dereference everything(do not delete it yet data will be used in higher instance g_sprite) this->has_shadow=NULL; this->thesprite=NULL; this->shadow=NULL; } void s_sprite::draw_s_sprite_anyspeed(int x, int y,int x_shadow_offset, int y_shadow_offset,int speed,SDL_Surface *screen){ if(*this->has_shadow){ assert(*this->has_shadow&&"there was supposed to be a shadow"); assert(this->shadow!=NULL&&"shadow is a NULLpointer !!! draw s_sprite exception!!!"); this->shadow->draw_sprite_anyspeed(x+x_shadow_offset,y+y_shadow_offset,speed,screen); } assert(this->thesprite!=NULL&&"sprite in a NULLpointer !!! draw s_sprite exception!!!"); this->thesprite->draw_sprite_anyspeed(x, y,speed,screen); } void s_sprite::draw_s_sprite(int x, int y,int x_shadow_offset, int y_shadow_offset,SDL_Surface *screen){ assert(this->has_shadow!=NULL&&"has_shadow is a Nullpointer !!!draw_s_sprite exception !!!"); if(*this->has_shadow){ assert(*this->has_shadow&&"there was supposed to be a shadow"); this->shadow->draw_sprite(x+x_shadow_offset,y+y_shadow_offset,screen); } this->thesprite->draw_sprite(x, y,screen); } void s_sprite::delete_anything(){ //if there is a shadow remove all data associated with it if(*this->has_shadow){ assert(this->shadow!=NULL&&"there was supposed to be a shadow to delete !!! s_sprite delete anything exception!!!"); this->shadow->delete_anything(); delete this->shadow; this->shadow=NULL; this->has_shadow=NULL; } //now get rid of all other data assert(this->thesprite!=NULL&&"there was supposed to be a sprite to delete !!! s_sprite delete anything exception!!! "); this->thesprite->delete_anything(); delete this->thesprite; delete this->has_shadow; //set all pointers to null to avoid trouble this->thesprite=NULL; } void s_sprite::create_s_sprite_from_data(int _frames, int _speed, std::string* _sprite,std::string* _shadow){ this->has_shadow=new bool; this->thesprite=new sprite; this->shadow=new sprite; *this->has_shadow=true; *this->thesprite=sprite(_frames,_speed,_sprite); *this->shadow=sprite(_frames,_speed,_shadow); } void s_sprite::create_s_sprite_from_data(int _frames, int _speed, std::string* _sprite){ this->has_shadow=new bool; this->thesprite=new sprite; *this->has_shadow=false; *this->thesprite=sprite(_frames,_speed,_sprite); } void s_sprite::load_from_file(std::string filename){ std::ifstream* infile=new std::ifstream; infile->open(filename); s_sprite* stest=new s_sprite; stest=load_s_sprite_from_ifstream(infile); infile->close(); delete infile; infile=NULL; *this=*stest; delete stest; stest=NULL; } void write_s_sprite_data(int _frames, int _speed,std::string *spritepicfilenames,std::string* shadowpicfilenames,std::ofstream *outfile){ assert(outfile!=NULL&&"using a Nullpointer filestream to write to !!! write_s_sprite_data exception!!!"); assert(outfile->is_open()&&"trying to write to a non-open filestream!!! write_s_sprite_data exception !!!"); *outfile<is_open()&&"trying to write to a non-open filestream!!! write_s_sprite_data exception !!!"); *outfile<open(filename); //open the file the data is written to write_s_sprite_data(_frames,_speed,spritepicfilenames,shadowpicfilenames,outfile); outfile->close(); delete outfile; outfile=NULL; } void create_s_sprite_datafile(int _frames, int _speed,std::string* spritepicfilenames,std::string filename){ std::ofstream *outfile=new std::ofstream; outfile->open(filename); //open the file the data is written to write_s_sprite_data(_frames,_speed,spritepicfilenames,outfile); outfile->close(); delete outfile; outfile=NULL; } s_sprite* load_s_sprite_from_ifstream(std::ifstream* infile){ s_sprite* return_s_sprite=new s_sprite; int _frames; int _speed; bool haz_shadow; assert(infile!=NULL&&"trying to use a NULL-Pointer as ifstream !!!oad_s_sprite_from_ifstream exception !!!"); assert(infile->is_open()&&"error trying to read from a closed ifstream !!load_s_sprite_from_ifstream exception !!!"); *infile>>haz_shadow; *infile>>_frames; *infile>>_speed; std::string *picssprite=new std::string[_frames]; for(int i=0;i<_frames;i++){ *infile>>picssprite[i]; } std::string *picsshadow=new std::string[_frames]; if(haz_shadow){ for(int i=0;i<_frames;i++){ *infile>>picsshadow[i]; } } if(haz_shadow){ assert(return_s_sprite!=NULL&&"return_s_sprite is a Null-Pointer !!!load_s_sprite_from_file exception !!!"); return_s_sprite->create_s_sprite_from_data( _frames,_speed,picssprite,picsshadow); delete[] picsshadow; picsshadow=NULL; }else{ assert(return_s_sprite!=NULL&&"return_s_sprite is a Null-Pointer !!!load_s_sprite_from_file exception !!!"); return_s_sprite->create_s_sprite_from_data( _frames,_speed,picssprite); } delete[] picssprite; return return_s_sprite; } /*########################################################################################################################################################## ANYTHING FOR THE GAME SPRITE DATATYPE ############################################################################################################################################################*/ g_sprite::g_sprite(){ this->ssprite=NULL; this->Colltype=NULL; } g_sprite::g_sprite(solidity_data _solidity,int coll_type,int _frames,int _speed,std::string* t1,std::string*t2){ this->ssprite=new s_sprite; *this->ssprite=s_sprite(_frames,_speed,t1,t2); this->Colltype=new int; *this->Colltype=coll_type; } g_sprite::g_sprite(int coll_type,int _frames,int _speed,std::string* t1){ this->ssprite=new s_sprite; *this->ssprite=s_sprite(_frames,_speed,t1); this->Colltype=new int; *this->Colltype=coll_type; } g_sprite::~g_sprite(){ //dereference everything(do not delete it yet data will be used in higher instance scr_sprite) //this->ssprite=NULL; this->Colltype=NULL; } //draw functions void g_sprite::draw_g_sprite_anyspeed(int x, int y,int x_shadow_offset, int y_shadow_offset,int speed,SDL_Surface *screen){ assert(this->ssprite!=NULL&&"trying to draw a s_sprite NULLPOINTER !!!draw g_sprite exception!!!"); this->ssprite->draw_s_sprite_anyspeed(x,y,x_shadow_offset,y_shadow_offset,speed,screen); } void g_sprite::draw_g_sprite(int x, int y,int x_shadow_offset, int y_shadow_offset,SDL_Surface *screen){ assert(this->ssprite!=NULL&&"your ssprite pointer is invalid exception in gsprite draw function"); this->ssprite->draw_s_sprite(x,y,x_shadow_offset,y_shadow_offset,screen); } void g_sprite::delete_anything(){ //delete all data associated with the g_sprite assert(this->ssprite!=NULL&&"trying to delete a ssprite NULL-POinter !!! g_sprite delete anything exception!!!"); assert(this->Colltype!=NULL&&"trying to delete a colltype NULL-Pointer !!! g_sprite delete anything exception!!!"); this->ssprite->delete_anything(); delete this->ssprite; delete this->Colltype; //set pointers to Null to avoid trouble this->ssprite=NULL; this->Colltype=NULL; } void g_sprite::create_g_sprite_from_data(int coll_data,int _frames, int _speed, std::string* _sprite,std::string* _shadow){ assert(_sprite!=NULL&&"trying to read sprite strings from NULL.-Pointer!!! g_sprite::create_g_sprite_from_data exception !!!"); assert(_shadow!=NULL&&"trying to read sprite shadow from NULL.-Pointer!!! g_sprite::create_g_sprite_from_data exception !!!"); this->ssprite=new s_sprite; this->Colltype=new int; *this->ssprite=s_sprite(_frames,_speed,_sprite,_shadow); *this->Colltype=coll_data; } void g_sprite::create_g_sprite_from_data(int coll_data,int _frames, int _speed, std::string* _sprite){ assert(_sprite!=NULL&&"trying to read sprite strings from NULL.-Pointer!!! g_sprite::create_g_sprite_from_data exception !!!"); this->ssprite=new s_sprite; this->Colltype=new int; *this->ssprite=s_sprite(_frames,_speed,_sprite); *this->Colltype=coll_data; } void g_sprite::load_from_file(std::string filename){ std::ifstream* infile=new std::ifstream; infile->open(filename); g_sprite* gtest=new g_sprite; gtest=load_g_sprite_from_ifstream(infile); infile->close(); delete infile; infile=NULL; *this=*gtest; delete gtest; } void write_g_sprite_data(int coll_type,int _frames, int _speed,std::string *spritepicfilenames,std::string* shadowpicfilenames,solidity_data solid,std::ofstream *outfile){ assert(outfile!=NULL&&"using a Nullpointer filestream to write to !!! write_g_sprite_data exception!!!"); assert(outfile->is_open()&&"trying to write to a non-open filestream!!! write_g_sprite_data exception !!!"); write_s_sprite_data(_frames,_speed,spritepicfilenames, shadowpicfilenames,outfile); //writes s_sprite *outfile<is_open()&&"trying to write to a non-open filestream!!! write_g_sprite_data exception !!!"); write_s_sprite_data(_frames,_speed,spritepicfilenames,outfile); //writes s_sprite *outfile<open(filename); //open the file the data is written to write_g_sprite_data(coll_type,_frames,_speed,spritepicfilenames,shadowpicfilenames,solid,outfile); outfile->close(); delete outfile; outfile=NULL; } void create_g_sprite_datafile(int coll_type,int _frames, int _speed,std::string* spritepicfilenames,solidity_data solid,std::string filename){ //create datafile with no shadows std::ofstream *outfile=new std::ofstream; outfile->open(filename); //open the file the data is written to write_g_sprite_data(coll_type,_frames,_speed,spritepicfilenames,solid,outfile); outfile->close(); delete outfile; outfile=NULL; } g_sprite* load_g_sprite_from_ifstream(std::ifstream *infile){ g_sprite* return_g_sprite=new g_sprite; int _frames; int _speed; int _coll_data; bool haz_shadow; solidity_data *solid=new solidity_data; assert(infile!=NULL&&"trying to use a NULL-Pointer as ifstream !!!oad_s_sprite_from_ifstream exception !!!"); assert(infile->is_open()&&"error trying to read from a closed ifstream !!load_s_sprite_from_ifstream exception !!!"); *infile>>haz_shadow; *infile>>_frames; *infile>>_speed; std::string *picssprite=new std::string[_frames]; for(int i=0;i<_frames;i++){ *infile>>picssprite[i]; } std::string *picsshadow=new std::string[_frames]; if(haz_shadow){ for(int i=0;i<_frames;i++){ *infile>>picsshadow[i]; } } *infile>>_coll_data; bool temp=false; for(int i=0;iGetSize();i++){ //create solidity data *infile>>temp; solid->set_data_i(i,temp); } if(haz_shadow){ assert(return_g_sprite!=NULL&&"return_s_sprite is a Null-Pointer !!!load_s_sprite_from_file exception !!!"); return_g_sprite->create_g_sprite_from_data( _coll_data,_frames, _speed, picssprite,picsshadow); delete[] picsshadow; }else{ assert(return_g_sprite!=NULL&&"return_s_sprite is a Null-Pointer !!!load_s_sprite_from_file exception !!!"); return_g_sprite->create_g_sprite_from_data( _coll_data,_frames, _speed, picssprite); } delete[] picssprite; delete solid; return return_g_sprite; } /*########################################################################################################################################################## ANYTHING FOR THE SCREEN SPRITE DATATYPE ############################################################################################################################################################*/ scr_sprite::scr_sprite(){ this->gsprite=NULL; this->xpos=NULL; this->ypos=NULL; } scr_sprite::scr_sprite(g_sprite* the_g_sprite ,int x_pos,int y_pos){ assert(the_g_sprite!=NULL&&"trying to build screen sprite from a NULL Pointer g_sprite!!! scr_sprite constructor exception!!!"); this->gsprite=new g_sprite; this->gsprite=the_g_sprite; this->xpos=new int; *this->xpos=x_pos; this->ypos=new int; *this->ypos=y_pos; } scr_sprite::scr_sprite(int x_pos, int y_pos,std::string g_sprite_file){ this->gsprite=new g_sprite; this->gsprite->load_from_file(g_sprite_file); this->xpos=new int; *this->xpos=x_pos; this->ypos=new int; *this->ypos=y_pos; } scr_sprite::~scr_sprite(){ this->gsprite=NULL; this->xpos=NULL; this->ypos=NULL; } void scr_sprite::draw_scr_sprite_anyspeed(int x_shadow_offset, int y_shadow_offset,int speed,SDL_Surface *screen){ assert(this->gsprite!=NULL&&this->xpos!=NULL&&this->ypos!=NULL&&screen!=0&&"seems either your gamesprite pointer,your screen, your scrsprite xpos or your scrsprite ypos is pointing to NULL exception in gsrpite draw function"); this->gsprite->draw_g_sprite_anyspeed(*this->xpos,*this->ypos,x_shadow_offset,y_shadow_offset,speed,screen); } void scr_sprite::draw_scr_sprite(int x_shadow_offset, int y_shadow_offset,SDL_Surface *screen){ assert(this->gsprite!=NULL&&this->xpos!=NULL&&this->ypos!=NULL&&"seems either your gamesprite pointer,your screen, your scrsprite xpos or your scrsprite ypos is pointing to NULL exception in gsrpite draw function"); this->gsprite->draw_g_sprite(*this->xpos,*this->ypos,x_shadow_offset,y_shadow_offset,screen); } void scr_sprite::draw_scr_sprite_anyspeed_w_offset(int xoffset,int yoffset,int x_shadow_offset, int y_shadow_offset,int speed,SDL_Surface *screen){ assert(this->gsprite!=NULL&&this->xpos!=NULL&&this->ypos!=NULL&&"seems either your gamesprite pointer,your screen, your scrsprite xpos or your scrsprite ypos is pointing to NULL exception in gsrpite draw function"); this->gsprite->draw_g_sprite_anyspeed(*this->xpos+xoffset,*this->ypos+yoffset,x_shadow_offset,y_shadow_offset,speed,screen); } void scr_sprite::draw_scr_sprite_w_offset(int xoffset,int yoffset,int x_shadow_offset, int y_shadow_offset,SDL_Surface *screen){ assert(this->gsprite!=NULL&&this->xpos!=NULL&&this->ypos!=NULL&&"seems either your gamesprite pointer,your screen, your scrsprite xpos or your scrsprite ypos is pointing to NULL exception in gsrpite draw function"); this->gsprite->draw_g_sprite(*this->xpos+xoffset,*this->ypos+yoffset,x_shadow_offset,y_shadow_offset,screen); } void scr_sprite::set_new_gsprite(std::string g_sprite_file){ this->gsprite->delete_anything(); //delete things before setting new data delete this->gsprite; this->gsprite=new g_sprite; this->gsprite->load_from_file(g_sprite_file); } void scr_sprite::delete_anything(){ assert(this->gsprite!=NULL&&"trying to delete a gsprite NULLpointer !!! scr_sprite delete anything exception!!!"); delete this->xpos; delete this->ypos; delete this->gsprite; this->gsprite=NULL; this->xpos=NULL; this->ypos=NULL; } /*########################################################################################################################################################## ANYTHING FOR THE LAYER DATATYPE ############################################################################################################################################################*/ layer::layer(){ for(int i=0;i<16;i++){ for(int j=0;j<9;j++){ this->layersprites[i+j*16]= new scr_sprite; *this->layersprites[i+j*16]=scr_sprite(80*i, 80*j,"empty.g_sprite"); } } } layer::layer(std::string* g_sprites){ for(int i=0;i<16;i++){ for(int j=0;j<9;j++){ assert(&g_sprites[i+j*16]!=NULL&&"Trying to read from non existant data name !!!layer::layer(std::string* g_sprites) exception !!!"); *this->layersprites[i+j*16]=scr_sprite(80*i, 80*j,g_sprites[i+j*16]); } } } void layer::set_scr_sprite(int i, std::string a){ assert(this->layersprites[i]!=NULL&&"TRying to replace Non-existant screen sprite !!layer::set_scr_sprite exception!!!"); this->layersprites[i]->delete_anything(); this->layersprites[i]->set_new_gsprite(a); } void layer::load_new_layer(std::string * new_layer){ for(int i=0;i<144;i++){ assert(new_layer+i!=NULL&&"Trying to read string from a NULLPOINTER !!!layer::load_new_layer exception !!!"); this->set_scr_sprite(i,new_layer[i]); } } void layer::draw_layer_anyspeed(int x_shadow_offset, int y_shadow_offset,int speed,SDL_Surface *screen){ for(int i=0;i<144;i++){ assert(this->layersprites[i]!=NULL&&"TRying to draw Non-existant screen sprite !!layer::draw_layer_anyspeed exception!!!"); this->layersprites[i]->draw_scr_sprite_anyspeed(x_shadow_offset, y_shadow_offset, speed,screen); } } void layer::draw_layer(int x_shadow_offset, int y_shadow_offset,SDL_Surface *screen){ for(int i=0;i<144;i++){ assert(this->layersprites[i]!=NULL&&"TRying to draw Non-existant screen sprite !!layer::draw_layer exception!!!"); this->layersprites[i]->draw_scr_sprite( x_shadow_offset, y_shadow_offset,screen); } } //TODO FIX DATA ACCESS FOR EVERYTHING G_SPRITE UPWARD. LOOK INTO THE PROBLEM WITH GARBAGE HANDLING(WHEN GSPRITES GET PASSED TO SCRSPRITES VIA REFERENCE)