同一屏幕上的SDL多个动画闪烁

时间:2014-02-11 07:56:18

标签: visual-c++ sdl flicker

我正在玩SDL,尝试制作像街头霸王这样的简单格斗游戏,但我不明白如何在屏幕上一次制作多个动画而不会闪烁。出于某种原因,当两个玩家在屏幕上闲置时他们不会闪烁,但对于其他动画,第二个玩家会闪烁。代码看起来像这样:

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();

1 个答案:

答案 0 :(得分:2)

你多次调用SDL_Flip。 进入你的主循环,称之为juste一次。

  • 删除背景
  • 绘制所有对象。
  • 翻转一次

不要翻转你的函数walkf