Allegro帧率问题

时间:2015-02-22 19:51:02

标签: c++ allegro allegro5

所以我最近在Allegro遇到了一些问题。我不知道导致它的原因,但我会尽力详细解释下面的问题。

在我的主要功能中,我有类似的东西:

int main()
{
    Game *game = new Game();
    game->init(800, 600, 60); //width, height, framerate

    PlayState *playState = new PlayState(); //extends abstract class GameState
    game->setDefaultState(playState);

    game->startGame();

    gMemoryTracker.reportAllocations(cout);
    system("pause");
    return 0;
}

这样可以正常工作,并且在每帧结束时打印经过的时间显示每帧大约16.7ms。但是,由于我希望Game类最终充当一个成熟的GameState管理器(目前它只支持一个GameState),我希望Game类的init函数调用每个GameState自己的个人init函数。显然,这意味着在调用Game的初始化函数之前注册每个GameState,所以我将main函数更改为如下所示:

int main()
{
    Game *game = new Game();
    //this is where the call to init used to be

    PlayState *playState = new PlayState();
    game->setDefaultState(playState);

    game->init(800, 600, 60); //now this is called after we create a GameState and set it as default

    game->startGame();

    gMemoryTracker.reportAllocations(cout);
    system("pause");
    return 0;
}

问题是现在每帧渲染大约需要1100ms,即使Game类报告帧速率仍设置为60fps。为了尝试调试此问题,我对main进行了最后一次更改:

int main()
{
    Game *game = new Game();   

    PlayState *playState = new PlayState();
    game->init(800, 600, 60); //now init is being called after we create a new GameState, but before we set it as default
    game->setDefaultState(playState);

    game->startGame();

    gMemoryTracker.reportAllocations(cout);
    system("pause");
    return 0;
}

但是这导致了与以前相同的问题,每帧渲染大约需要1100ms。看来这个问题只发生在我创建一个新的GameState后调用init时,所以我查看了PlayState类的构造函数和Game类的init函数,看看我是否能找到会导致这种行为的东西,但是我仍然无法找到原因。您可以在下面找到上述两个功能:

bool Game::init(int width, int height, float frameRate)
{
    mFrameRate = frameRate;
    bool result = mpGraphics->init(width, height); //initialize the graphics system

    //this calls the init function for the default GameState. commented-out for debugging purposes
    //result = result && mpDefaultState->init();

    return result; //were all inits successful?
}



PlayState::PlayState() : GameState("play_state")
{
    //create required graphics buffers and fonts
    bgBuffer = new GraphicsBuffer(DISPLAY_SIZE.getX(), DISPLAY_SIZE.getY());
    woodsBuffer = new GraphicsBuffer(ASSET_PATH + "Woods.png");
    smurfBuffer = new GraphicsBuffer(ASSET_PATH + "smurf_sprites.png");
    font = new Font(ASSET_PATH + "cour.ttf", 12);

    smurfAnimation = new Animation(60, true); //create new animation for smurf walk cycle. args are animation framerate (must be <= game framerate) and loop animation

    //load sprites from spritesheet
    for (int y = 0; y < (int)SPRITE_SHEET_SIZE.getY() / (int)SPRITE_SIZE.getY(); y++)
    {
        for (int x = 0; x < (int)SPRITE_SHEET_SIZE.getX() / (int)SPRITE_SIZE.getX(); x++)
        {
            //there was already a class named Rectangle, so i had to get creative
            UnequalSquare uSquare(x * (int)SPRITE_SIZE.getX(), y * (int)SPRITE_SIZE.getY(), (int)SPRITE_SIZE.getX(), (int)SPRITE_SIZE.getY());
            smurfAnimation->addSprite(Sprite(*smurfBuffer, uSquare));
        }
    }
}

我还注意到的一件事是,如果我在PlayState的渲染功能中注释掉所有渲染调用,游戏将以适当的帧速率运行,但显然这不是一个可行的解决方案。我将在下面发布渲染功能。它目前效率不高(每帧都创建新的精灵对象),但这并不能解释为什么在创建PlayState实例之前调用Game.init()时它能完美运行。

void PlayState::render(GraphicsSystem* graphics, const Timer& frameTimer)
{
    // Create background
    Sprite bgSprite(*bgBuffer);
    graphics->fill(*bgBuffer, Color(255, 0, 0));

    // Create image
    Sprite woodsSprite(*woodsBuffer);
    Vector2D woodsLocation((DISPLAY_SIZE.getX() - (woodsSprite.getWidth() * WOODS_SCALE)) / 2.0, (DISPLAY_SIZE.getY() - (woodsSprite.getHeight() * WOODS_SCALE)) / 2.0);

    // Draw Text
    int fontWidth = font->getTextWidth(TEXT_STRING);
    int fontHeight = font->getFontHeight();

    for (int i = 0; i <= DISPLAY_SIZE.getY() / fontHeight; i++)
    {
        for (int j = 0; j <= DISPLAY_SIZE.getX() / fontWidth; j++)
        {
            graphics->drawText(*bgBuffer, Vector2D(j * fontWidth, i * fontHeight), TEXT_STRING, *font, Color());
        }
    }

    //update the animation
    smurfAnimation->update();

    // Draw sprites to backbuffer
    graphics->drawSpriteToBackbuffer(gZeroVector2D, bgSprite);
    graphics->drawSpriteToBackbuffer(woodsLocation, woodsSprite, WOODS_SCALE);
    graphics->drawSpriteToBackbuffer(gZeroVector2D, smurfAnimation->getCurrentSprite());
}

任何人都可以看到我可能遇到此问题的原因吗?我现在已经在这里工作了几个小时,我无法理解我的生活。

1 个答案:

答案 0 :(得分:0)

我发现了问题。从Allegro 5文档:

  

除非您设置ALLEGRO_MEMORY_BITMAP标志,否则将为当前显示创建位图。向另一个显示器进行布局可能会很慢。

我在创建显示之前尝试创建位图,从而导致性能不佳。设置ALLEGRO_MEMORY_BITMAP标志可以显着提高帧速率,但每帧仍需要大约100ms-200ms。我认为这只是Allegro的一个弱点,从现在开始我需要考虑它。

来源:https://www.allegro.cc/manual/5/al_create_bitmap