title: SDL How to animate a Sprite when key is pressed
body: <p>I'm going to get straight to the point:
  I have a sprite sheet that has 8 movement sprites (<a href="http://i.imgur.com/hLpo2Qn.png" rel="nofollow">http://i.imgur.com/hLpo2Qn.png</a> cant post images :\ ), so it creates the illusion that im walking)
  My problem is that when i press and hold a key i want the sprite to kinda have an animated walk. For example when i press the up arrow the first two sprites play in a loop until i let go of the key. All I've been able to do so far is to get only one sprite showing for each direction, so it kinda gives an unrealistic effect.
  Code:</p>
  
  <pre><code>#include &lt;SDL/SDL.h&gt;
  
  int main(int argc, char* argv[])
  {
  SDL_Init(SDL_INIT_EVERYTHING);
  SDL_Surface* screen, *image;
  screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  bool running = true;
  const int FPS = 30;
  Uint32 start;
  bool direction[4] = {0, 0, 0, 0};
  SDL_Rect pos;
      pos.x = 0;
      pos.y = 0;
      SDL_Rect sprite;
      sprite.x = 160;
      sprite.y = 0;
      sprite.w = 32;
      sprite.h = 32;;
  Uint32 color = SDL_MapRGB(screen -&gt; format, 0xff, 0xff, 0xff);
  image = SDL_DisplayFormat(SDL_LoadBMP("sprite.bmp"));
  SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(screen -&gt; format, 255, 0, 255));
  while (running == true)
  {
      start = SDL_GetTicks();
      SDL_Event event;
      while(SDL_PollEvent(&amp;event))
      {
          switch(event.type)
          {
          case SDL_QUIT:
              running = false;
              break;
          case SDL_KEYDOWN:
              switch(event.key.keysym.sym)
              {
              case SDLK_UP:
                  direction[0] = 1;
                  sprite.x = 0;
                  break;
              case SDLK_LEFT:
                  direction[1] = 1;
                  sprite.x = 224;
                  break;
              case SDLK_DOWN:
                  direction[2] = 1;
                  sprite.x = 160;
                  break;
              case SDLK_RIGHT:
                  direction[3] = 1;
                  sprite.x = 96;
                  break;
              }
              break;
          case SDL_KEYUP:
              switch(event.key.keysym.sym)
              {
              case SDLK_UP:
                  direction[0] = 0;
                  sprite.x = 32;
                  break;
              case SDLK_LEFT:
                  direction[1] = 0;
                  break;
              case SDLK_DOWN:
                  direction[2] = 0;
                  break;
              case SDLK_RIGHT:
                  direction[3] = 0;
                  break;
              }
              break;
          }
      }
      if(direction[0])
          pos.y-- ;
      if(direction[1])
          pos.x--;
      if(direction[2])
          pos.y++;
      if(direction[3])
          pos.x++;
  
      SDL_FillRect(screen, &amp;screen -&gt; clip_rect, color);
      SDL_BlitSurface(image, &amp;sprite, screen, &amp;pos);
      SDL_Flip(screen);
  
          if(1000 / FPS &gt; SDL_GetTicks() - start)
          SDL_Delay(1000 / FPS - (SDL_GetTicks() - start));
  
  }
  SDL_FreeSurface(image);
  SDL_Quit();
  return 0;
  </code></pre>
  
  <p>}</p>