C++ 设置FPS时为什么要在SDL中启动计时器?
这是关于lazyfoo的SDL教程集的教程页面。在那里,他首先启动一个计时器,计算在刷新之前,每帧应该保持活动状态的时间。他使用以下方法来实现这一点C++ 设置FPS时为什么要在SDL中启动计时器?,c++,c,sdl,frame-rate,C++,C,Sdl,Frame Rate,这是关于lazyfoo的SDL教程集的教程页面。在那里,他首先启动一个计时器,计算在刷新之前,每帧应该保持活动状态的时间。他使用以下方法来实现这一点 if( ( cap == true ) && ( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) ) { //Sleep the remaining frame time SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_tic
if( ( cap == true ) && ( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) ) {
//Sleep the remaining frame time
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
if((cap==true)和&(fps.get_ticks()<1000/帧/秒)){
//休眠剩余的帧时间
SDL_延迟((1000/帧/秒)-fps.get_ticks());
}
虽然我发现fps.get_ticks()总是返回0(?),所以上面的不是不需要的(?),但我们不能完全忽略计时器,只延迟1000/fps
我试过下面两种方法,两种方法都给了我同样的东西。我错过了什么,为什么我们需要计时器
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <iostream>
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *msg = NULL;
const int FPS = 20;
void initialize(void){
if (SDL_Init(SDL_INIT_EVERYTHING) == -1 ){
std::cout<<"could not start sdl"<<std::endl;
}
screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
if (screen == NULL){
std::cout<<"could not make screen"<<std::endl;
}
}
void cleanUp(void){
SDL_Quit();
SDL_FreeSurface(background);
SDL_FreeSurface(msg);
}
void loadFiles(void){
background = IMG_Load("background.bmp");
msg = IMG_Load("msg.bmp");
if (background == NULL){
std::cout<<"could not load background"<<std::endl;
}
if (msg == NULL){
std::cout<<"could not load msg"<<std::endl;
}
}
void blitSurf(int x,int y,SDL_Surface *source,SDL_Surface *dest){
SDL_Rect dest_pos;
dest_pos.x = x;
dest_pos.y = y;
if (SDL_BlitSurface(source,NULL,dest,&dest_pos) == -1){
std::cout<<"could not blit surface"<<std::endl;
}
}
void update(void){
if (SDL_Flip(screen) == -1 ){
std::cout<<"could not update screen"<<std::endl;
}
}
int main(int argc,char *argv[]){
initialize();
loadFiles();
bool running = true;
bool cap = false;
int msg_pos_y = 0;
int start = 0;
int temp = 0;
SDL_Event event;
while (running == true){
start = SDL_GetTicks();
while (SDL_PollEvent(&event)){
if (event.type == SDL_KEYDOWN){
if (event.key.keysym.sym == SDLK_c){
if(cap == false){
cap = true;
std::cout<<"cap set to, true"<<std::endl;
}else{
cap = false;
std::cout<<"cap set to, false"<<std::endl;
}
}
}
if (event.type == SDL_QUIT){
running = false;
std::cout<<"Quit was pressed"<<std::endl;
}
}
blitSurf(0,0,background,screen);
if (msg_pos_y < 640){
blitSurf(200,msg_pos_y,msg,screen);
msg_pos_y++;
}else{
msg_pos_y = 0;
blitSurf(200,msg_pos_y,msg,screen);
}
update();
if ( (cap == true) && ( (SDL_GetTicks()-start) < (1000/FPS) ) ){
SDL_Delay( (1000/FPS) - (SDL_GetTicks()-start) );
}
/* this works as well ??
if ( cap == true ){
SDL_Delay(1000/FPS);
}
*/
}
cleanUp();
return 0;
}
#包括“SDL/SDL.h”
#包括“SDL/SDL_image.h”
#包括
SDL_表面*背景=空;
SDL_表面*屏幕=空;
SDL_表面*msg=NULL;
常数int FPS=20;
void初始化(void){
if(SDL_Init(SDL_Init_EVERYTHING)=-1){
std::cout我不是专家,但他所做的是:一旦你运行sdl应用程序的主循环完成(绘制blitting和所有这些东西),在你做这些事情的时候,时间可能已经过了一段时间,例如,如果绘制帧需要10毫秒,你必须等待(1000/FPS)-10毫秒,直到下一帧,否则您的帧将持续太长。假设您想要50 fps
1000毫秒/50=20毫秒延迟
但是渲染、计算物理、人工智能,无论你在做什么,都需要时间。假设我写的所有东西都需要10毫秒。你有1000毫秒/(20毫秒延迟+10毫秒其他一切)=33.3帧每秒。你需要从延迟中减去10毫秒。是的,我是这么想的,但是,两次调用SDL_GetTicks()一次循环迭代中的函数给出了相同的结果(?)。因此在if语句中,fps.get_ticks()
实际上只返回0(SDL_GetTicks()-startTicks;
),这就是为什么我感到困惑的原因。干杯,但我编写了一个调用SDL_GetTicks()的测试程序一次循环迭代两次,第一次在最顶端,第二次在最底端。它们都产生相同的结果,时间安排也一样。如果循环更复杂,有人工智能、物理等等,时间安排会不同吗?是的,如果你在它们之间做一些耗时的事情,时间安排会不同.