문제

I am playing around with SDL, trying to make a simple fighting game like street fighter or such, but I don't understand how to make more than one animation at once on the screen without flickering. For some reason while both players are idle on the screen they don't flicker, but for other animations the second player flickers. The code looks something like this:

Class player1:

...

void setrects_idle(SDL_Rect* clip)  //loads the frames from a bmp image
{

    for(int i = 0; i < 10; i ++) {
        clip[i].x = 0 + i*224;                  
        clip[i].y = 0;
        clip[i].w = 224;
        clip[i].h = 226;
    }
}

void setrects_walkf(SDL_Rect* clip)
  {
    for(int i = 0; i < 11; i ++) {        
        clip[i].x = 0 + i*224;                
        clip[i].y = 0;
        clip[i].w = 224;
        clip[i].h = 226;
    }
  }

void player::idle(SDL_Surface* screen)
{   
 if (!other_action)
  {
    SDL_BlitSurface(player1_idle, &frames_idle[static_cast<int>(frame_idle)], screen, &offset);
    SDL_Flip(screen);
 if(frame_idle > 8)
    frame_idle = 0;
 else
    frame_idle ++;
  }
}

void player::walkf(SDL_Surface* screen)
{   
 other_action = true;
 SDL_BlitSurface(player1_walkf, &frames_walkf[static_cast<int>(frame_walkf)], screen, &offset);

 SDL_Flip(screen);

 if(frame_walkf > 9)
  frame_walkf = 0;
 else
  frame_walkf ++;
}

********************
Class player2:

void player2::idle(SDL_Surface* screen)
{   
 if (!other_action)
  {
    SDL_BlitSurface(player2_idle, &frames_idle[static_cast<int>(frame_idle)], screen, &offset);
    //SDL_Flip(screen); //with this commented, there is no flicker on both players idle
 if(frame_idle > 8)
    frame_idle = 0;
 else
    frame_idle ++;
  }
}

void player2::walkf(SDL_Surface* screen)
{   
 other_action = true;
 SDL_BlitSurface(player2_walkf, &frames_walkf[static_cast<int>(frame_walkf)], screen, &offset);

 SDL_Flip(screen); //if I comment this there is no animation for player2 at all. with it on, it flickers.

 if(frame_walkf > 9)
  frame_walkf = 0;
 else
  frame_walkf ++;
}

*****************************
SDL_Surface *screen;
screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);

In the main loop:

player1.idle(screen);
player2.idle(screen);
...
  case SDLK_d:
      player1.b[1] = 1;
      break;
  case SDLK_j:
      player2.b[1] = 1;
      break;
...
  if(player1.b[0])
    player1.walkb(screen);
  else
    player1.return_to_idle();

  if(player2.b[0])
    player2.walkb(screen);
  else
    player2.return_to_idle();
도움이 되었습니까?

해결책

You call several time SDL_Flip. Into your main loop, call it juste once.

  • Erase background
  • Draw ALL objects.
  • Flip Once

Do not flip in your function walkf

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top