🔒 Locked
sdl sprites
Advertisement
Original Post
I just went through LazyFoo's tutorial on sdl animation and i get sprites, the idea that you have each frame on a sheet and then load each image from that sprite sheet but i am confused on the setClips function. So this is taking the sprite sheet and dividing up the series of images into an array to be iterated through and then the picture at that index of the clips array is whats shown to the screen, show a bunch in sequence and its like animation. But i dont understand if these sprites are all on one sprite sheet which i assume is 1 picture file how does it know where 1 sprite image ends and the other begins in that same sprite sheet file, so the clipsRight[0] and those in the setClips function knows where to clip the sprite to only get 1 sprite from the sprite sheet? Thanks.
Here is a picture which hopefully might help understand what the code is attempting to do.
You are right that the sprite sheet is a single sheet, but is divided up into segments, or clips, so that only a portion of the sheet is drawn at each time frame. For example, in time frame 0, you would draw Frame 0 as above, and in the next time frame, you draw Frame 1, and so on...
This will give the impression of it animating.
The way that is determines which part of the spritesheet to draw is by the calculation shown in the code.
clipsLeft[ 0 ].x = 0; clipsLeft[ 0 ].y = FOO_HEIGHT; clipsLeft[ 0 ].w = FOO_WIDTH;clipsLeft[ 0 ].h = FOO_HEIGHT; clipsLeft[ 1 ].x = FOO_WIDTH; clipsLeft[ 1 ].y = FOO_HEIGHT; clipsLeft[ 1 ].w = FOO_WIDTH; clipsLeft[ 1 ].h = FOO_HEIGHT; clipsLeft[ 2 ].x = FOO_WIDTH * 2; clipsLeft[ 2 ].y = FOO_HEIGHT; clipsLeft[ 2 ].w = FOO_WIDTH; clipsLeft[ 2 ].h = FOO_HEIGHT; clipsLeft[ 3 ].x = FOO_WIDTH * 3; clipsLeft[ 3 ].y = FOO_HEIGHT; clipsLeft[ 3 ].w = FOO_WIDTH; clipsLeft[ 3 ].h = FOO_HEIGHT; As you can see, to draw the first frame (FRAME 0), it starts starts at location (0,h) at the top-left hand corner. Then specifying the width and height of the image, it is able to clip the image so that parts 1-3 are not included.
For the second frame (FRAME 1), it starts at the location (w,h), and does the same thing.
Hopefully this image makes things clearer!
ohhh i think i get it now. So each sprite on the sprite sheet is a set number of pixels high and wide which is what the constants FOO_HEIGHT and FOO_WIDTH are equal to so it only takes that much of the overall picture. FOO_WIDTH * 2 for clipsLeft[ 2 ].x moves over 2 pictures. So it starts at x = 0 on the image and goes until the width and takes the whole height, then it starts at width and takes the whole height then it starts at width * 2 and takes the whole height, width * 3 and takes the whole height, the whole height is always taken to get the whole person. So a sprite sheet that has an image of multiple actions, like running, attacking, etc, and they are stacked on top of each other you would have to manipulate the height as well to get the images that are on the 2nd row of the sprite sheet. As long as the height and width in pixels i assume, of each sprite is the same this process can be automated without too much difficulty.
1 more question. Then you just make the brackground of the sprite sheet 1 solid color and tell sdl to filter that color out so no background gets drawn to the screen along with the sprite. Do you filter that color out when the sprites are being read into clips or when the clips are being drawn to the screen?
I believe the filtering of the color occurs in his load_image function.
Alright so that makes sense. I have one more thing regarding sprites that im confused about with animation. So lets say you have a still sprite thats just part of the environment but its animated like a torch on a wall for example so the flame goes up and down, how do you keep that animation steady and at the same time still let the human controlled player or other cpu controlled players for that matter walk around the scene. Is putting the animation loop in its own thread the only way?
Not necessarily, when the game is running, the main game loop is still running so you will still see the animated torch burning in the background even if your character is standing still.
The moment some form of input is received ( e.g. pressing the right-button, clicking a mouse to move the character ), the character simply begins moving. If the character stops, the main game loop still continues, so the torch is still burning regardless.
Of course, you would have to define the sprite to be drawn independent from any form of input if you want that kind of behavior.
The moment some form of input is received ( e.g. pressing the right-button, clicking a mouse to move the character ), the character simply begins moving. If the character stops, the main game loop still continues, so the torch is still burning regardless.
Of course, you would have to define the sprite to be drawn independent from any form of input if you want that kind of behavior.
Oh ok so the best way to handle input for a character lets say is to put it in the that character's class and then it will just always handle input? That function would have to be called in order to handle input right? So how is that function called while the fire's animation function is still running?
You don't necessarily have to put the input parsing inside the player class, although it's probably helpful. For simplicity, I'm going to show it done outside of the class.
Notice, the fire's animation function isn't run continuously until the fire leaves the screen. It gets called once per frame.
/* GLOBAL */Uint8* KEYS = SDL_GetKeyState(NULL);.../* IN MAIN */// Check for inputif (KEYS[SDLK_RIGHT]) sprite.moveRight();if (KEYS[SDLK_LEFT]) sprite.moveLeft();/* Next, animate your torch. Notice how this is independent of any user input. It's going to happen whether a key is pressed or not. */torch.animate();// Blit images, flip the screen, etc etc.Notice, the fire's animation function isn't run continuously until the fire leaves the screen. It gets called once per frame.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement