C++ /SDL动画速度
所以我正在按照在线教程开发一款游戏。目前,我在系统中内置了一些 FPS,以及一个使用精灵片段的简单动画,如下所示:
if( frameCount > 12 )
frameCount = 0;
//hero frames
SDL_Rect clip[ 13 ];
clip[ 0 ].x = 0;
clip[ 0 ].y = 0;
clip[ 0 ].w = 44;
clip[ 0 ].h = 39;
clip[ 1 ].x = 51;
clip[ 1 ].y = 0;
clip[ 1 ].w = 44;
clip[ 1 ].h = 39;
clip[ 2 ].x = 102;
clip[ 2 ].y = 0;
clip[ 2 ].w = 44;
clip[ 2 ].h = 39;
...
...
SDL_BlitSurface( hero, &clip[ frameCount ], destination, &offset );
frameCount++;
现在这工作得很好,每次迭代 while 循环都会导致它播放动画中的下一帧(此动画顺便说一下,是字符类的一部分)。
我面临的问题是动画的速度。它发生在游戏当前的 FPS 下,即 60。我希望能够单独控制玩家动画的速度,这样我就可以将其减慢到合理的速度。
有人对我如何做到这一点有任何建议吗?
注:一共有13帧。
so I am working on a game, following a tutorial online. Currently I have some FPS built into the system, and a simple animation which uses pieces of a sprite like such:
if( frameCount > 12 )
frameCount = 0;
//hero frames
SDL_Rect clip[ 13 ];
clip[ 0 ].x = 0;
clip[ 0 ].y = 0;
clip[ 0 ].w = 44;
clip[ 0 ].h = 39;
clip[ 1 ].x = 51;
clip[ 1 ].y = 0;
clip[ 1 ].w = 44;
clip[ 1 ].h = 39;
clip[ 2 ].x = 102;
clip[ 2 ].y = 0;
clip[ 2 ].w = 44;
clip[ 2 ].h = 39;
...
...
SDL_BlitSurface( hero, &clip[ frameCount ], destination, &offset );
frameCount++;
Now this works just fine, and each iteration of the while loop will cause it to play the next frame in the animation (This animation is part of a character class by the way).
The problem I am facing is the speed of the animation. It takes place at the current FPS of the game, which is 60. I want to be able to control the speed of the player animation separately, so I can slow it down to a reasonable speed.
Does anyone have any suggestions on how I could go about doing this?
note: There are 13 frames all together.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已将刷新率 (60 fps) 与动画速率分开。在我看来,最好的解决方案是将动画速率与实时时钟联系起来。在 SDL 中,您可以使用 SDL_Ticks() 函数来执行此操作,您可以使用该函数以毫秒分辨率测量时间。
作为一个例子,考虑这个例子(不是工作代码,只是一个草图):
如果不清楚,
frameToDraw
变量是通过计算自动画开始播放以来经过的时间来计算的。将其乘以动画速率,即可得到动画速率下已经通过的绝对帧数。然后,您应用模运算符将该数字减少到动画长度的范围内,从而获得当时需要绘制的帧。如果您的刷新率比动画速率慢,您的精灵将跳过帧以跟上请求的动画速率。如果刷新率较快,则同一帧将被重复绘制,直到显示下一帧的时间到来。
我希望这有帮助。
You have separate your refresh rate (60 fps) from your animation rate. The best solution, in my opinion, is to tie your animation rate to the real time clock. In SDL, you can do this with the SDL_Ticks() function, which you can use to measure time in millisecond resolution.
As an example, consider this example (not working code, just a sketch):
In case it isn't clear, the
frameToDraw
variable is computed by calculating how much time passed since the animation started to play. You multiply that by the animation rate and you get how many absolute frames at the animation rate have passed. You then apply the modulo operator to reduce this number to the range of your animation length and that gives you the frame you need to draw at that time.If your refresh rate is slower than your animation rate your sprite will skip frames to keep up with the requested animation rate. If the refresh rate is faster then the same frame will be drawn repeatedly until the time to display the next frame comes.
I hope this helps.
米格尔写的东西很棒。您可以使用的另一种方法是使用计时器,将其设置为以特定频率触发,并在该频率下增加精灵索引。
请注意,您应该始终将渲染和逻辑分开。
What Miguel has written works great. Another method you can use is, to use a timer, set it to fire at a certain frequency and in that frequency, increment your sprite index.
Note that you should always make your rendering and logic separate.