SDL_Rect srcRect = new SDL_Rect( [TileX] * [TileWidth], [TileY] * [TileHeight], [TileWidth], [TileHeight] );SDL_Rect dstRect = new SDL_Rect( [x], [y], [TileWidth], [TileHeight] );SDL_BlitSurface( [Surface] , srcRect, [Screen], dstRect);
Then just after each frame when you're application is running, increment the TileX (or TileY) and you'll have a steady animation using one surface for each sprite animation frame.
Animating a Sprite in SDL
Forgive me for writing in C#. It'll give you an opportunity to actually learn this stuff...
Okay I get the idea but I just don't know if I'm doing the checking if one frame has passed part. I'm going to post my UpdateFrames function along with my DrawScene,DrawIMG functions and while loop.
Here's my while loop
Notes: My sprite sheet is 297x46 making it a 11 frame animation with each frame being 27x46. I also have to call my DrawSprite function before the loop or else it won't load the image while it's running.
void DrawIMG(SDL_Surface *img, int x, int y){ SDL_Rect dest; dest.x = x; dest.y = y; SDL_Rect srcrect; srcrect.x = CURRENT_FRAME*spritew; srcrect.y = 0; srcrect.w = spritew; srcrect.h = spriteh; SDL_BlitSurface(sprite, &srcrect, screen, &dest);}void DrawSprite(){ DrawIMG(sprite, xpos, ypos); }void AdvanceFrame(){ if(CURRENT_FRAME > 10) { CURRENT_FRAME = 0; } else { CURRENT_FRAME++; } } void DrawScene(){ DrawSprite(); AdvanceFrame(); }
Here's my while loop
while(running) { while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { running = false; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { running = false; } } } DrawScene(); }
Notes: My sprite sheet is 297x46 making it a 11 frame animation with each frame being 27x46. I also have to call my DrawSprite function before the loop or else it won't load the image while it's running.
Are you loading the image properly? That's a common mistake when dealing with this [smile]. Otherwise, I don't see anything wrong with it. It's good that you understand what I'm talking about!
Quote:Original post by rpgman15
@WillF
I tried to compile your code but when it exucutes it just flashes the window and closes itself
Hmmm ... what OS are you using? I've only tested it on Linux.
I am running it on Windows 2000. I load the image like this...
void InitIMG()
{
sprite = SDL_LoadBMP("mysprite.bmp");
}
void InitIMG()
{
sprite = SDL_LoadBMP("mysprite.bmp");
}
Okay I'm going to post whole source code and a link to the sprite sheet for you guys to compile and see what I'm doing wrong.
You can get the sprite sheet here...
http://www.boomspeed.com/rpgman12/A_WALK_D.bmp
#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>SDL_Surface *sprite;SDL_Surface *screen; int CURRENT_FRAME = 0;int xpos = 300;int ypos = 400;int spritew = 27;int spriteh = 47;void InitImages(){ sprite = SDL_LoadBMP("Gfx/A_WALK_D.bmp"); }void DrawIMG(SDL_Surface *img, int x, int y){ SDL_Rect dest; dest.x = x; dest.y = y; SDL_Rect srcrect; srcrect.x = CURRENT_FRAME*spritew; srcrect.y = 0; srcrect.w = spritew; srcrect.h = spriteh; SDL_BlitSurface(sprite, &srcrect, screen, &dest);}void DrawSprite(){ DrawIMG(sprite, xpos, ypos); }void AdvanceFrame(){ if(CURRENT_FRAME > 10) { CURRENT_FRAME = 0; } else { CURRENT_FRAME++; } } void DrawScene(){ DrawSprite(); AdvanceFrame(); }int main(int argc, char *argv[]){ Uint8* keys; InitImages(); if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0 ) { printf("Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); SDL_WM_SetCaption("DarkSword","darksword"); screen=SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF); if ( screen == NULL ) { printf("Unable to set 800x600 video: %s\n", SDL_GetError()); exit(1); } DrawSprite(); bool running = true; SDL_Event event; while(running) { while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { running = false; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { running = false; } } } DrawScene(); } return 0;}
You can get the sprite sheet here...
http://www.boomspeed.com/rpgman12/A_WALK_D.bmp
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement