The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL2 Animation loading
paul188


Joined: 22 Dec 2016
Posts: 1
Hey guys i just invented a little code snippet and wanted to ask if it works in practice.
Every answer is very appreciated Wink.

Player.cpp:

void Player::LoadAnimation(char*animationFrames[], SDL_Renderer *renderer, int count, int index) {
for (int i = 0; i < count; i++) {
SDL_Surface *surf_temp = LoadSurfaceFromImage(animationFrames[i],"PNG");
SDL_Texture *tex_temp = SDL_CreateTextureFromSurface(renderer, surf_temp);
AnimationFrames.push_back(tex_temp);
}
PlayerAnimations.at(index) = AnimationFrames;
AnimationFrames.clear();
}

Player.h:

class Player{
public:
Player(SDL_Rect *bounds);//bounds either in SDL_Rect* or in ints
Player(int x, int y, int w, int h);
void Player::LoadAnimation(char*animationFrames[], SDL_Renderer *renderer, int count, int index);//parameters: animationFrames: names of the files of one animation
//renderer: the renderer object for the launcher, count: how many frames the animation has, index: the index of the animation in the array
private:
std::array <std::vector<SDL_Texture*>,10>PlayerAnimations;
std::vector<SDL_Texture*>AnimationFrames;