代码之家  ›  专栏  ›  技术社区  ›  Lucifer893

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

  •  0
  • Lucifer893  · 技术社区  · 11 年前

    我正在玩SDL,试图制作一个简单的格斗游戏,比如街头格斗之类的,但我不知道如何在屏幕上同时制作多个动画而不闪烁。由于某些原因,当两个玩家在屏幕上空闲时,它们不会闪烁,但对于其他动画,第二个玩家会闪烁。代码如下所示:

    类播放器1:

    ...

    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 回复  |  直到 11 年前
        1
  •  2
  •   Fvirtman    11 年前

    您可以多次调用SDL_Flip。 进入主循环,只需调用一次。

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

    不要在功能步行中翻转