Page 1 of 1
C++ 'delete' keyword HELP NEEDED!
Posted: Tue Apr 26, 2011 9:14 am
by VoidElite
OK guys I'm quite new to using the delete keyword. Basically my game engine requires that the Sprite::hide() function free the 'bmp' surface(SDL) and delete the instance of the Sprite class. So that later a instance with the same name can be instantiated and hidden the same.
NOTE: this is for my show and hide system for sprites.
Here is the code for the Sprite class:
Code: Select all
#include "Content.h"
using namespace std;
#ifndef SPRITE
#define SPRITE
class Sprite{
public:
Sprite();
Sprite(string file2,int x2,int y2,bool visible2);
void show();
void hide();
SDL_Surface *bmp;
string file;
int x;
int y;
bool visible;
};
Sprite::Sprite(){
file="null.bmp";
bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
x=0;
y=0;
visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
file=file2;
bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
x=x2;
y=y2;
visible=visible2;
}
void show(){
SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void hide(){
SDL_FreeSurface(bmp);
delete this;
}
#endif
You should pay attention to the Sprite::hide() function:
Code: Select all
void hide(){
SDL_FreeSurface(bmp);
delete this;
}
The error says that bmp is not declared in that scope and that I'm using the 'this' keyword wrong(never really used that either).
Wait!
I have another few errors. Now that I've fixed that I'm getting 'screen' has not been declared in this scope but 'screen' is declared in 'Content.h' which is being included into 'Sprite.h'.
Anywhoo here's 'Content.h':
Code: Select all
#ifndef CONTENT
#define CONTENT
#include <iostream>
#include "SDL\SDL.h"
#include "windows.h"
#include "Environment.h"
#include "Sprite.h"
#include "Tile.h"
#include "Map.h"
#include "World.h"
#include "Object.h"
#include "Creature.h"
#include "Npc.h"
#include "Player.h"
using namespace std;
int mapIndex=0;
SDL_Surface *screen=NULL; //SDL's screen
Sprite *sprite=new Sprite("dude.bmp",10,10,true); //test sprite
Player *player=new Player(); //Main Player
World *world=new World(); //the world
#endif
Help once again would be greatly appreciated.
Re: C++ 'delete' keyword HELP NEEDED!
Posted: Tue Apr 26, 2011 9:28 am
by Rapid Cube
VoidElite wrote:OK guys I'm quite new to using the delete keyword. Basically my game engine requires that the Sprite::hide() function free the 'bmp' surface(SDL) and delete the instance of the Sprite class. So that later a instance with the same name can be instantiated and hidden the same.
NOTE: this is for my show and hide system for sprites.
Here is the code for the Sprite class:
Code: Select all
#include "Content.h"
using namespace std;
#ifndef SPRITE
#define SPRITE
class Sprite{
public:
Sprite();
Sprite(string file2,int x2,int y2,bool visible2);
void show();
void hide();
SDL_Surface *bmp;
string file;
int x;
int y;
bool visible;
};
Sprite::Sprite(){
file="null.bmp";
bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
x=0;
y=0;
visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
file=file2;
bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
x=x2;
y=y2;
visible=visible2;
}
void show(){
SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void hide(){
SDL_FreeSurface(bmp);
delete this;
}
#endif
You should pay attention to the Sprite::hide() function:
Code: Select all
void hide(){
SDL_FreeSurface(bmp);
delete this;
}
The error says that bmp is not declared in that scope and that I'm using the 'this' keyword wrong(never really used that either).
Help would be greatly appreciated.
you implemented the show and hide functions wrong
this is how you need to do it
Code: Select all
void Sprite::show()
{
SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void Sprite::hide()
{
SDL_FreeSurface(bmp);
delete this;
}
Re: C++ 'delete' keyword HELP NEEDED!
Posted: Tue Apr 26, 2011 9:35 am
by VoidElite
Rapid Cube wrote:VoidElite wrote:OK guys I'm quite new to using the delete keyword. Basically my game engine requires that the Sprite::hide() function free the 'bmp' surface(SDL) and delete the instance of the Sprite class. So that later a instance with the same name can be instantiated and hidden the same.
NOTE: this is for my show and hide system for sprites.
Here is the code for the Sprite class:
Code: Select all
#include "Content.h"
using namespace std;
#ifndef SPRITE
#define SPRITE
class Sprite{
public:
Sprite();
Sprite(string file2,int x2,int y2,bool visible2);
void show();
void hide();
SDL_Surface *bmp;
string file;
int x;
int y;
bool visible;
};
Sprite::Sprite(){
file="null.bmp";
bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
x=0;
y=0;
visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
file=file2;
bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
x=x2;
y=y2;
visible=visible2;
}
void show(){
SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void hide(){
SDL_FreeSurface(bmp);
delete this;
}
#endif
You should pay attention to the Sprite::hide() function:
Code: Select all
void hide(){
SDL_FreeSurface(bmp);
delete this;
}
The error says that bmp is not declared in that scope and that I'm using the 'this' keyword wrong(never really used that either).
Help would be greatly appreciated.
you implemented the show and hide functions wrong
this is how you need to do it
Code: Select all
void Sprite::show()
{
SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void Sprite::hide()
{
SDL_FreeSurface(bmp);
delete this;
}
My bad! Thanks.
Re: C++ 'delete' keyword HELP NEEDED!
Posted: Tue Apr 26, 2011 9:43 am
by VoidElite
Rapid Cube wrote:VoidElite wrote:OK guys I'm quite new to using the delete keyword. Basically my game engine requires that the Sprite::hide() function free the 'bmp' surface(SDL) and delete the instance of the Sprite class. So that later a instance with the same name can be instantiated and hidden the same.
NOTE: this is for my show and hide system for sprites.
Here is the code for the Sprite class:
Code: Select all
#include "Content.h"
using namespace std;
#ifndef SPRITE
#define SPRITE
class Sprite{
public:
Sprite();
Sprite(string file2,int x2,int y2,bool visible2);
void show();
void hide();
SDL_Surface *bmp;
string file;
int x;
int y;
bool visible;
};
Sprite::Sprite(){
file="null.bmp";
bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
x=0;
y=0;
visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
file=file2;
bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
x=x2;
y=y2;
visible=visible2;
}
void show(){
SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void hide(){
SDL_FreeSurface(bmp);
delete this;
}
#endif
You should pay attention to the Sprite::hide() function:
Code: Select all
void hide(){
SDL_FreeSurface(bmp);
delete this;
}
The error says that bmp is not declared in that scope and that I'm using the 'this' keyword wrong(never really used that either).
Help would be greatly appreciated.
you implemented the show and hide functions wrong
this is how you need to do it
Code: Select all
void Sprite::show()
{
SDL_BlitSurface(bmp,NULL,screen,new SDL_Rect(x,y,NULL,NULL)); //3)width 4)height
}
void Sprite::hide()
{
SDL_FreeSurface(bmp);
delete this;
}
Wait!
I have another few errors. Now that I've fixed that I'm getting 'screen' has not been declared in this scope but 'screen' is declared in 'Content.h' which is being included into 'Sprite.h'.
Anywhoo here's 'Content.h':
Code: Select all
#ifndef CONTENT
#define CONTENT
#include <iostream>
#include "SDL\SDL.h"
#include "windows.h"
#include "Environment.h"
#include "Sprite.h"
#include "Tile.h"
#include "Map.h"
#include "World.h"
#include "Object.h"
#include "Creature.h"
#include "Npc.h"
#include "Player.h"
using namespace std;
int mapIndex=0;
SDL_Surface *screen=NULL; //SDL's screen
Sprite *sprite=new Sprite("dude.bmp",10,10,true); //test sprite
Player *player=new Player(); //Main Player
World *world=new World(); //the world
#endif
Help once again would be greatly appreciated.
Re: C++ 'delete' keyword HELP NEEDED!
Posted: Thu Apr 28, 2011 1:16 am
by adikid89
Don't ever delete this. You're just asking for crashes...
Re: C++ 'delete' keyword HELP NEEDED!
Posted: Thu Apr 28, 2011 5:18 pm
by LeonBlade
Yeah... deleting this isn't really the safest thing to do
Create your draw function passing in a "draw to..." SDL_surface pointer that way you can just reference the pointer to draw to.
I think this is what you're asking...
I never liked how SDL did things this way having to blit to surfaces, that's why I use OpenGL now.
Re: C++ 'delete' keyword HELP NEEDED!
Posted: Thu Apr 28, 2011 7:29 pm
by BlobOfFailure
The second problem you had, the one you made a thread for, I answered in its thread yesterday, not sure if you saw it but it should help with that problem.