我已经解决了有关此问题的其他问题,但似乎无法得到我正在寻找的答案。我当前的游戏以 60 fps 的速度运行,在调试模式下需要 9.5 毫秒/帧(在发布模式下为 3.8 毫秒/帧)。这是玩家、敌人和地图都在屏幕上闪烁,这似乎根本不会导致 fps 下降,但是当我对 1024x768 BG 图像进行 blit 时,它会下降到 27-30 FPS,现在渲染每一帧33 毫秒/帧。这是游戏循环---
while(!quit)
{
if(SDL_PollEvent(&event))
{
quit = Sys->isQuitGame(event);
}
fps->timerReset();
blit_surface(bg,Sys->screen,0,0);//HERE'S THE PROBLEM!
mapItem.RedrawingItems(Sys->screen, Sys->map);
movement = input->Keyboard(key, Sys->screen, player);
enemies.moveAllEnemies(Sys->map, player );
player->move(movement, Sys->map);
player->DrawPlayer(Sys->screen);
enemies.draw(Sys->screen);
SDL_Flip(Sys->screen);
SDL_FillRect(Sys->screen, NULL, SDL_MapRGB( Sys->screen->format, 0xFF, 0xFF, 0xFF ));
renderTime = calculatingFps->get_ticks();
frame++;
if(calculatingFps->get_ticks() > 1000)
{
renderTime -= totalDelay;
totalDelay = 0;
stringstream Afps;
Afps <<"fps: " <<frame/ (calculatingFps->get_ticks() / 1000.f)<<
" ---- RenderTimePerFrame: "<<(float)renderTime / (float)frame<<"/ms";
SDL_WM_SetCaption( Afps.str().c_str(), NULL);
Game::FPS = frame/ (calculatingFps->get_ticks() / 1000.f);
calculatingFps->timerReset();
frame = 0;
renderTime = 0;
}
if(frameRenderedTooFast(fps))
{
totalDelay += (remainingTime(fps));
SDL_Delay(remainingTime(fps));
}
}
所以那个img的一个片段设法将fps降低了一半,我一定错过了什么?顺便说一句,我不认为它是 SDL_DisplayFormat,我已经有了该代码,这是处理 img 加载的代码片段 ---
SDL_Surface* altSDL::load_image(std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( filename.c_str() );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
else
{
Failure* fail;
fail = Failure::getInstance();
fail->failLog(filename);
}
if( optimizedImage != NULL )
{
Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF );
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
}
return optimizedImage;
}
我想偶尔或每隔一段时间对其进行一次blit,但每一帧都有角色移动,所以等待移动然后重新blit是没有用的。我是否必须在单独的表面上使用 setVideoMode 作为背景或其他东西?谁能帮我?