如何定义在 SFML 游戏中使用多少 CPU?
How to define how much CPU to use in SFML game?
我制作了一款游戏,但我不知道这款游戏在其他设备上的运行方式是否相同。例如,如果计算机的 CPU 很高,玩家和敌人会移动得更快吗?如果是这样,是否有一种方法可以定义 SFML 中可用的 CPU 用法?玩家和敌人在我的程序中移动的方式是:
1-检查按键是否被按下
2-如果是:移动(x,y);或者有没有办法让 CPU 在 move 函数中做一些操作。
谢谢!
听起来您担心游戏的物理特性会受到游戏帧率的影响。你的直觉在为你服务!这是一个严重的问题,如果您想让您的游戏感觉专业,就需要解决这个问题。
根据 Glenn Fiedler 在他的 Gaffer on Games article 'Fix Your Timestep!'
[A game loop that handles time improperly can make] the behavior of your physics simulation [depend] on the delta time you pass in. The effect could be subtle as your game having a slightly different “feel” depending on framerate or it could be as extreme as your spring simulation exploding to infinity, fast moving objects tunneling through walls and the player falling through the floor!
从逻辑上讲,您必须将更新的依赖项与绘制框架的时间分离。一个简单的解决方案是:
- 选择可以安全处理的时间量(您的时间步长)
- 将每帧经过的时间添加到累积时间池中
- 处理以安全块形式传递的时间
在伪代码中:
time_pool = 0;
timestep = 0.01; //or whatever is safe for you!
old_time = get_current_time();
while (!closed) {
new_time = get_current_time();
time_pool += new_time - old_time;
old_time = new_time;
handle_input();
while (time_pool > timestep)
{
consume_time(timestep); //update your gamestate
time_pool -= timestep;
}
//note: leftover time is not lost, and will be left in time_pool
render();
}
值得注意的是,此方法有其自身的问题:未来的帧必须消耗调用 consume_time
产生的时间。如果调用 consume_time
花费的时间太长,产生的时间可能需要在下一帧进行两次调用——然后是四次——然后是八次——依此类推。如果使用此方法,则必须确保 consume_time
非常有效,即便如此,最好有一个应急计划。
为了更彻底的治疗,我建议您阅读链接的文章。
我制作了一款游戏,但我不知道这款游戏在其他设备上的运行方式是否相同。例如,如果计算机的 CPU 很高,玩家和敌人会移动得更快吗?如果是这样,是否有一种方法可以定义 SFML 中可用的 CPU 用法?玩家和敌人在我的程序中移动的方式是: 1-检查按键是否被按下 2-如果是:移动(x,y);或者有没有办法让 CPU 在 move 函数中做一些操作。 谢谢!
听起来您担心游戏的物理特性会受到游戏帧率的影响。你的直觉在为你服务!这是一个严重的问题,如果您想让您的游戏感觉专业,就需要解决这个问题。
根据 Glenn Fiedler 在他的 Gaffer on Games article 'Fix Your Timestep!'
[A game loop that handles time improperly can make] the behavior of your physics simulation [depend] on the delta time you pass in. The effect could be subtle as your game having a slightly different “feel” depending on framerate or it could be as extreme as your spring simulation exploding to infinity, fast moving objects tunneling through walls and the player falling through the floor!
从逻辑上讲,您必须将更新的依赖项与绘制框架的时间分离。一个简单的解决方案是:
- 选择可以安全处理的时间量(您的时间步长)
- 将每帧经过的时间添加到累积时间池中
- 处理以安全块形式传递的时间
在伪代码中:
time_pool = 0;
timestep = 0.01; //or whatever is safe for you!
old_time = get_current_time();
while (!closed) {
new_time = get_current_time();
time_pool += new_time - old_time;
old_time = new_time;
handle_input();
while (time_pool > timestep)
{
consume_time(timestep); //update your gamestate
time_pool -= timestep;
}
//note: leftover time is not lost, and will be left in time_pool
render();
}
值得注意的是,此方法有其自身的问题:未来的帧必须消耗调用 consume_time
产生的时间。如果调用 consume_time
花费的时间太长,产生的时间可能需要在下一帧进行两次调用——然后是四次——然后是八次——依此类推。如果使用此方法,则必须确保 consume_time
非常有效,即便如此,最好有一个应急计划。
为了更彻底的治疗,我建议您阅读链接的文章。