- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我最近在使用 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.7 毫秒。然而,由于我希望 Game 类最终充当一个成熟的 GameState 管理器(目前它只支持一个 GameState),我希望 Game 类的 init 函数调用每个 GameState 自己的单独 init 函数。显然,这意味着在调用 Game 的 init 函数之前注册每个 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;
}
问题是现在每帧渲染大约需要 1100 毫秒,即使 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;
}
但这会导致与以前相同的问题,每帧渲染大约需要 1100 毫秒。似乎这个问题只发生在我创建一个新的 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 的渲染函数中注释掉所有渲染调用,游戏会以正确的帧速率运行,但显然这不是一个可行的解决方案。我将在下面发布渲染功能。目前效率不是很高(每帧都创建新的 sprite 对象),但这并不能解释为什么在创建 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());
}
谁能看出我遇到此问题的任何原因?我已经研究了几个小时了,但我终究无法弄明白。
最佳答案
我发现了问题。来自 Allegro 5 文档:
Unless you set the ALLEGRO_MEMORY_BITMAP flag, the bitmap is created for the current display. Blitting to another display may be slow.
我试图在创建显示之前创建位图,因此导致性能不佳。设置 ALLEGRO_MEMORY_BITMAP 标志可显着提高帧速率,但每帧仍需要大约 100ms-200ms。我想这只是 Allegro 的一个弱点,我需要从现在开始考虑。
关于c++ - 快板帧率问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28662469/
我使用 QBFC v13 和 Delphi XE6。 目标是从我的应用程序创建 Estimate 并将其参数设置到 QuickBooks 中。 我从 QBFC13 导入类型库并将其添加到我的项目中。
我有一个数据库,其中有很多格式不正确的地址。在这个地址中,我只有一个街道地址或带有城市名称的街道。在我的应用程序中,用户选择一些类别,然后我想在 map 上显示例如 50,100,300 等地址。(不
在大多数软件项目中,缺陷源自需求、设计、编码和缺陷更正。根据我的经验,大多数缺陷都源于编码阶段。 我有兴趣了解软件开发人员使用哪些实用方法来降低缺陷注入(inject)率。 我已经看到以下方法的使用取
我正在使用实时 API 中的标签订阅构建一个应用程序,并且有一个与容量规划相关的问题。我们可能有大量用户同时发布到订阅的主题标签,因此问题是 API 实际发布到我们的订阅处理端点的频率是多少?例如,如
尝试使用 NULLIF 或 IFNULL 函数,但仍收到被零除的消息。 SELECT client_id ,COUNT(distinct CASE WHEN status = 'failed' THE
我目前正在开发一个使用 Django-rest-framework 制作的 API。我必须根据每个用户组设置限制率。 我们目前使用默认配置的 memcached 作为缓存后端,即按站点缓存。 在使用
我认为有时在神经网络(特别是一般对抗网络)训练期间改变丢失率可能是一个好主意,从高丢失率开始,然后线性地将丢失率降低到零。您认为这有意义吗?如果是的话,是否有可能在 tensorflow 中实现这一点
我有一个 Windows Server 2008,Plesk 运行着两个网站。有时服务器运行缓慢,并且有一个 named.exe 进程使 CPU 峰值达到 100%。它持续很短的时间,过一会儿它又来了
我正在使用 scikit-learn 随机森林分类器,我想通过将成功投票所需的树数量从大于 50% 增加到 75% 来降低 FP 率,在阅读文档后我不这样做确定如何做到这一点。有没有人有什么建议。 (
当我连续按下按键事件(字母 k)时,为什么以下按键事件不会减慢 3000 密耳?如果我按住手指,计数会迅速增加,因为 mcount 上没有 setTimeout。这是为什么?每次计数之间应该有延迟,但
我是一名优秀的程序员,十分优秀!