C++ 为什么每帧4像素的80 fps对我来说是滞后的?
因此,我正在开发arkanoid,而这项运动对我来说滞后了。C++ 为什么每帧4像素的80 fps对我来说是滞后的?,c++,frame-rate,C++,Frame Rate,因此,我正在开发arkanoid,而这项运动对我来说滞后了。 我不知道为什么它会滞后,因为我听说每秒60帧是眼罩。 有没有关于我应该做什么的建议,因为我不想以每秒100帧的速度编程游戏 因为如果我能以60 fps的速度编程,我处理计算的时间就更少了? 这是我的封顶框架代码 //Cap the frame rate if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) { SDL_Delay( (
我不知道为什么它会滞后,因为我听说每秒60帧是眼罩。
有没有关于我应该做什么的建议,因为我不想以每秒100帧的速度编程游戏
因为如果我能以60 fps的速度编程,我处理计算的时间就更少了?
这是我的封顶框架代码
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
//限制帧速率
if(fps.get_ticks()<1000/帧/秒)
{
SDL_延迟((1000/帧/秒)-fps.get_ticks());
}
如果你好奇的话,这是我的全部密码
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
/*This source code copyrighted by Lazy Foo' Productions (2004-2012)
and may not be redistributed without written permission.*/
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
//The screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 40;
//The dimensions of the dot
const int DOT_WIDTH = 20;
const int DOT_HEIGHT = 20;
//The surfaces
SDL_Surface *dot = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The dot that will move around on the screen
class Dot
{
private:
//The X and Y offsets of the dot
int x, y;
//The velocity of the dot
int xVel, yVel;
public:
//Initializes the variables
Dot();
//Takes key presses and adjusts the dot's velocity
void handle_input();
//Moves the dot
void move();
//Shows the dot on the screen
void show();
};
//The timer
class Timer
{
private:
//The clock time when the timer started
int startTicks;
//The ticks stored when the timer was paused
int pausedTicks;
//The timer status
bool paused;
bool started;
public:
//Initializes variables
Timer();
//The various clock actions
void start();
void stop();
void pause();
void unpause();
//Gets the timer's time
int get_ticks();
//Checks the status of the timer
bool is_started();
bool is_paused();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
SDL_FreeSurface( loadedImage );
//If the surface was optimized
if( optimizedImage != NULL )
{
//Color key surface
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//Return the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Move the Dot", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the dot image
dot = load_image( "dot.bmp" );
//If there was a problem in loading the dot
if( dot == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( dot );
//Quit SDL
SDL_Quit();
}
Dot::Dot()
{
//Initialize the offsets
x = 0;
y = 0;
//Initialize the velocity
xVel = 0;
yVel = 0;
}
void Dot::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;
case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;
case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
}
}
}
void Dot::move()
{
//Move the dot left or right
x += xVel;
//If the dot went too far to the left or right
if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) )
{
//move back
x -= xVel;
}
//Move the dot up or down
y += yVel;
//If the dot went too far up or down
if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) )
{
//move back
y -= yVel;
}
}
void Dot::show()
{
//Show the dot
apply_surface( x, y, dot, screen );
}
Timer::Timer()
{
//Initialize the variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//Start the timer
started = true;
//Unpause the timer
paused = false;
//Get the current clock time
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//Stop the timer
started = false;
//Unpause the timer
paused = false;
}
void Timer::pause()
{
//If the timer is running and isn't already paused
if( ( started == true ) && ( paused == false ) )
{
//Pause the timer
paused = true;
//Calculate the paused ticks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//If the timer is paused
if( paused == true )
{
//Unpause the timer
paused = false;
//Reset the starting ticks
startTicks = SDL_GetTicks() - pausedTicks;
//Reset the paused ticks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//If the timer is running
if( started == true )
{
//If the timer is paused
if( paused == true )
{
//Return the number of ticks when the timer was paused
return pausedTicks;
}
else
{
//Return the current time minus the start time
return SDL_GetTicks() - startTicks;
}
}
//If the timer isn't running
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//The dot that will be used
Dot myDot;
//The frame rate regulator
Timer fps;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//While the user hasn't quit
while( quit == false )
{
//Start the frame timer
fps.start();
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the dot
myDot.handle_input();
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Move the dot
myDot.move();
//Fill the screen white
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
//Show the dot on the screen
myDot.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
//Clean up
clean_up();
return 0;
}
/*此源代码受Lazy Foo’Productions版权所有(2004-2012)
未经书面许可,不得重新发布*/
//标题
#包括“SDL/SDL.h”
#包括“SDL/SDL_image.h”
#包括
//屏幕属性
屏幕宽度=640;
屏幕上的常数=480;
屏幕常数(BPP=32);
//帧速率
const int FRAMES_PER_SECOND=40;
//圆点的尺寸
const int DOT_WIDTH=20;
const int DOT_HEIGHT=20;
//表面
SDL_表面*点=零;
SDL_表面*屏幕=空;
//事件结构
SDL_事件;
//将在屏幕上移动的点
类点
{
私人:
//点的X和Y偏移
int x,y;
//点的速度
内特xVel,伊维尔;
公众:
//初始化变量
点();
//按下按键并调整圆点的速度
无效句柄_输入();
//移动圆点
无效移动();
//显示屏幕上的点
void show();
};
//计时器
班级计时器
{
私人:
//计时器启动时的时钟时间
int startTicks;
//计时器暂停时存储的滴答声
int暂停时间;
//计时器状态
布尔停顿了一下;
布尔开始;
公众:
//初始化变量
定时器();
//各种时钟动作
void start();
无效停止();
无效暂停();
无效取消暂停();
//获取计时器的时间
int get_ticks();
//检查计时器的状态
bool已启动();
布尔已暂停();
};
SDL_表面*加载_图像(标准::字符串文件名)
{
//加载的图像
SDL_表面*LoadeImage=NULL;
//将使用的优化曲面
SDL_曲面*optimizedImage=NULL;
//加载图像
LoadeImage=IMG_Load(filename.c_str());
//如果图像已加载
如果(LoadeImage!=NULL)
{
//创建优化的曲面
optimizedImage=SDL_显示格式(LoadeImage);
//释放旧表面
SDL_自由曲面(加载图像);
//如果曲面经过优化
如果(optimizedImage!=NULL)
{
//彩色键表面
SDL_SetColorKey(optimizedImage、SDL_SRCCOLORKEY、SDL_MapRGB(optimizedImage->format,0,0xFF,0xFF));
}
}
//返回优化的曲面
返回优化年龄;
}
void apply_surface(int x,int y,SDL_surface*源,SDL_surface*目标,SDL_Rect*剪辑=NULL)
{
//保留偏移量
垂直偏移量;
//得到补偿
偏移量x=x;
偏移量y=y;
//布利特
SDL_BlitSurface(源、剪辑、目标和偏移);
}
boolinit()
{
//初始化所有SDL子系统
if(SDL_Init(SDL_Init_EVERYTHING)=-1)
{
返回false;
}
//设置屏幕
屏幕=SDL_设置视频模式(屏幕宽度、屏幕高度、屏幕BPP、SDL_表面);
//如果设置屏幕时出错
如果(屏幕==NULL)
{
返回false;
}
//设置窗口标题
SDL_WM_SetCaption(“移动点”,空);
//如果一切都好的话
返回true;
}
bool load_文件()
{
//加载点图像
点=加载_图像(“dot.bmp”);
//如果加载点时出现问题
如果(点==NULL)
{
返回false;
}
//如果一切顺利的话
返回true;
}
无效清除()
{
//释放表面
SDL_自由曲面(dot);
//退出SDL
SDL_退出();
}
点::点()
{
//初始化偏移量
x=0;
y=0;
//初始化速度
xVel=0;
yVel=0;
}
void Dot::handle_input()
{
//如果按了一个键
if(event.type==SDL\u KEYDOWN)
{
//调整速度
开关(event.key.keysym.sym)
{
案例SDLK向上:yVel-=点高度/2;中断;
案例SDLK\u DOWN:yVel+=点高度/2;中断;
案例SDLK_左:xVel-=点宽度/2;中断;
案例SDLK_RIGHT:xVel+=DOT_WIDTH/2;中断;
}
}
//如果钥匙被释放了
else if(event.type==SDL\u KEYUP)
{
//调整速度
开关(event.key.keysym.sym)
{
案例SDLK向上:yVel+=点高度/2;中断;
案例SDLK\u DOWN:yVel-=点高度/2;中断;
案例SDLK_左:xVel+=点宽度/2;中断;
案例SDLK_RIGHT:xVel-=DOT_WIDTH/2;中断;
}
}
}
void Dot::move()
{
//将点向左或向右移动
x+=xVel;
//如果圆点向左或向右偏得太远
如果((x<0)| |(x+网点宽度>屏幕宽度))
{
//退后
x-=xVel;
}
//上下移动圆点
y+=yVel;
//如果圆点向上或向下过远
如果((y<0)| |(y+点高度>屏幕高度))
{
//退后
y-=yVel;
}
}
void Dot::show()
{
//露点
应用于表面(x、y、圆点、屏幕);
}
计时器::计时器()
{
//初始化变量
startTicks=0;
pausedTicks=0;
暂停=错误;
开始=错误;
}
void Timer::start()
{
//启动计时器
开始=真;
//解除计时器的暂停
暂停=错误;
//获取当前时钟时间
startTicks=SDL_GetTicks();
}
void Timer::stop()
{
//停止计时
开始=错误;
//解除计时器的暂停
暂停=错误;
}
无效计时器::暂停()
{
//如果计时器正在运行且不是
unsigned int timestamp = SDL_GetTicks(); // on startup or right before entering your main loop
// when you need the time passed:
unsigned int time_now = SDL_GetTicks(); // get current time
unsigned int time_passed = time_now - timestamp; // get time difference/time passed
timestamp = time_now; // essentially reset the timer
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cstdio>
__int64 frequency = 0;
__int64 last_time = 0;
double lag = 0;
double stat_lag = 0;
unsigned int render_steps = 0;
unsigned int logic_steps = 0;
const unsigned char target_updates_per_second = 100;
const unsigned char target_frames_per_second = 60;
// reset or initialize the timer
void reset_timer(__int64 *timer) {
QueryPerformanceFrequency((PLARGE_INTEGER)&frequency);
QueryPerformanceCounter((PLARGE_INTEGER)timer);
}
// get the time since last query and reset the timer for next iteration
double get_delta(__int64 *timer) {
__int64 temp; // stores the new timestamp
__int64 delta; // stores the difference
QueryPerformanceCounter((PLARGE_INTEGER)&temp);
delta = temp - *timer;
*timer = temp;
return (double)delta / frequency;
}
int main(int argc, char *argv[]) {
reset_timer(&last_time);
const unsigned int logic_frame_time = 1000 / target_updates_per_second;
const unsigned int render_frame_time = 1000 / target_frames_per_second;
while (1) {
double delta = 1000 * get_delta(&last_time); // get the time that passed since last iteration (multiplied with 1000 for milliseconds)
lag += delta; // here we add the time passed to the lag counter/timer
stat_lag += delta; // here we do the same for the fps/ups counter
for (char runs = 0; runs < 10 && lag > logic_frame_time; ++runs, lag -= logic_frame_time) {
++logic_steps; // here game logic would happen
Sleep(0); // increase this number to simulate more load during logic steps
}
// lag = lag % logic_frame_time;
// i made it the manual way to keep the double value, which is more precise here
while (lag > logic_frame_time)
lag -= logic_frame_time;
if (stat_lag > 1000) { // if more than 1 second passed
printf("updates per second: %u\nframes per second: %u\n\n", logic_steps, render_steps);
// reset the stats
logic_steps = 0;
render_steps = 0;
// stat_lag = stat_lag % 1000;
// as above, doing it the manual way for double
while (stat_lag > 1000)
stat_lag -= 1000;
}
++render_steps; // here rendering would happen
// here, vsync would slow down processing, lowering the number of frames per second!
//Sleep(...);
Sleep(1); // avoid 0 milliseconds passing; this also causes rendering to be limited to 1000 fps!
}
}