I have a bit of a problem passing a SDL_Surface* to a class constructor, and then use it for rendering it. The class in question is similar to the following:
class Entity{
public:
Entity(int posX, int posY, int width, int height,SDL_Surface* surf);
SDL_Surface* getSurface();
private:
int x,y;
int w,h;
SDL_Surface* entSurface;
}
Entity::Entity(int posX, int posY, int width, int height,SDL_Surface* surf){
x=posX;
y=posY;
w=width;
h=height;
entSurface=surf;
}
SDL_Surface* Entity::getSurface(){
return entSurface;
}
and I use it like this
SDL_Surface* surface=new SDL_Surface();
surface=loadBMP("path/to/img.bmp");
Entity someEntity=Entity(0,0,10,10,surface);
Program compiles and runs no problem but the following DOESN'T render the entity:
while(Running){
//render(SDL_Surface* destination,SDL_Surface* src,int x,int y);
//render() is a function that blits the surface
render(theWindow,someEntity.getSurface(),0,0);
}
But this renders just fine
while(Running){
render(theWindow,surface,0,0);
}
So what I'm wondering is why my pointer isn't passed to the class like I want it to. I'm very new to the concept of pointers but I don't understand why the address cant be passed like this.
Any help is appreciated!
EDIT: Requested by comments to show loadBMP
SDL_Surface* loadBMP(char* File) {
SDL_Surface* Surf_Temp = NULL;
SDL_Surface* Surf_Return = NULL;
if((Surf_Temp = SDL_LoadBMP(File)) == NULL) {
return NULL;
}
Surf_Return = SDL_DisplayFormat(Surf_Temp);
SDL_FreeSurface(Surf_Temp);
return Surf_Return;
}
EDIT 2: Made it more clear what wasn't working
I found the problem. My problem was that my surfaces was initialized AFTER I initialized the entities, which meant the pointer was still NULL. Swapped it around and it worked!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.