SFML Framerate Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
GR33B
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Fri Nov 14, 2008 3:33 pm
Current Project: Project Crimson
Programming Language of Choice: C#
Location: Virginia
Contact:

SFML Framerate Help

Post by GR33B »

Hey all, I'm working on a tile engine, and have just migrated from XNA to SFML, because XNA hogs resources like a boss and I don't like the way it handles content loading.

Anyway, the switch was relatively simple, and gave me a huge boost in performance (XNA version was getting ~50fps on integrated graphics, SFML version is getting 100+ fps), so it's awesome so far. However, with this boost in framerate I encountered a problem: my game runs too fast, as in, the speed that things in-game happen is directly correlated to the framerate. Because at 50fps characters were walking, but at 100+ fps, characters are practically sprinting.

So how can I separate the framerate from the speed of gameplay, without limiting the framerate?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: SFML Framerate Help

Post by dandymcgee »

GR33B wrote:Hey all, I'm working on a tile engine, and have just migrated from XNA to SFML, because XNA hogs resources like a boss and I don't like the way it handles content loading.

Anyway, the switch was relatively simple, and gave me a huge boost in performance (XNA version was getting ~50fps on integrated graphics, SFML version is getting 100+ fps), so it's awesome so far. However, with this boost in framerate I encountered a problem: my game runs too fast, as in, the speed that things in-game happen is directly correlated to the framerate. Because at 50fps characters were walking, but at 100+ fps, characters are practically sprinting.

So how can I separate the framerate from the speed of gameplay, without limiting the framerate?
Base velocity on time, not frame. No idea how to get the current tick in SFML, but it shouldn't be too difficult.

Random link from Google, you'll probably want to search time-based movement for more info.
http://peterstuifzand.nl/gameprog_3.html
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SFML Framerate Help

Post by Ginto8 »

use sf::Window::SetFramerateLimit() or sf::Window::UseVerticalSync()
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: SFML Framerate Help

Post by X Abstract X »

User avatar
Trimmy
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 22
Joined: Thu Aug 05, 2010 1:16 am
Current Project: A Game
Favorite Gaming Platforms: PS3, PC ( Mac OS )
Programming Language of Choice: C++
Location: New Zealand
Contact:

Re: SFML Framerate Help

Post by Trimmy »

You should make things changed based on time, so as a simple example something like:

Code: Select all

player.pos.x += player.hspeed * timediff;
Where timediff is the time difference between the current frame and the last frame that way the player will move the same speed no matter what the frame-rate is.
My Programming Projects:http://www.youtube.com/user/TrimmyS
User avatar
GR33B
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Fri Nov 14, 2008 3:33 pm
Current Project: Project Crimson
Programming Language of Choice: C#
Location: Virginia
Contact:

Re: SFML Framerate Help

Post by GR33B »

Trimmy wrote:You should make things changed based on time, so as a simple example something like:

Code: Select all

player.pos.x += player.hspeed * timediff;
Where timediff is the time difference between the current frame and the last frame that way the player will move the same speed no matter what the frame-rate is.
Thanks, works like a charm now!
Post Reply