I have a class called SpriteCollection where I load the images with SDL. This class has an attribute called:
SDL_Surface* sprites[];
I think it’s right (though I’m not sure about that). In the same class I have a method:
void addNewSprite(SDL_Surface* sprite){
this->sprites[n_sprites+1] = new SDL_Surface;
this->sprites[n_sprites+1] = IMG_Load("spritepath.jpg");
this->n_sprites++;
}
And another to retrieve the SDL_Surface to draw on screen:
SDL_Surface getSprite(int sprite_index){
return this->sprites[sprite_index];
}
And to draw on the screen I’m using:
Draw(x_position, y_position, this->sprite->getSprite[0], screen);
I’m loading images normally; it’s all OK, but the IDE is returning an error about pointers and conversion between SDL_Surface and SDL_Surface *.
What am I doing wrong?
edit: the error message:
E:\cGame.cpp|71|error: cannot convert `SDL_Surface' to `SDL_Surface*' for argument `1' to `int SDL_UpperBlit(SDL_Surface*, SDL_Rect*, SDL_Surface*, SDL_Rect*)'|
In your
getSpritefunction which has a return type ofSDL_Surface, you’re trying to return anSDL_Surface*. Perhaps you meant:In addition, these lines are very suspect:
First you’re dynamically allocating a new
SDL_Surfaceand storing the pointer to it. Then you’re getting rid of that pointer by assigning the result of theIMG_Loadcall to it. Now you’ll never be able todeletethe surface because you’ve lost the pointer to it. You may think about encapsulating your sprite and using the RAII idiom to handle the allocation of theSDL_Surface.On top of all this, you’d probably be better off using a
std::vectorinstead of an array.