Managing a consistent framerate

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Managing a consistent framerate

Post by Ertain »

Hello again, LÖVErs. I have a question about keeping the framerate consistent and making things run smooth. What are some methods for doing this? I have been trying to figure out how not to have so many pictures on the screen (so that it doesn't eat up CPU cycles). When one of the enemies in my little game is destroyed, for example, I just have them moved off the screen. I have been wondering about how to remove the enemy picture, data and all.

That would help to speed things up, but I'm sure there are better ways to make this more efficient, like we always try to do. ;)
Booted, suited, and ready to get executed.
User avatar
slime
Solid Snayke
Posts: 3131
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Managing a consistent framerate

Post by slime »

framebuffers or spritebatches are good ways to draw backgrounds while saving on framerate. As of LÖVE 0.7.1, love.graphics.print/love.graphics.printf are very inefficient and can cause large performance drops, but that will be improved in LÖVE 0.8.0.
If you're doing a lot of heavy lifting in love.update it can be a good idea to only update that portion of the update code every 30 or 60 frames.

There are always ways to improve performance, but beyond using the right tools for the job (framebuffers for example) don't spend too much time during early development optimizing, as you may have to rewrite large portions of your code later anyway.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Managing a consistent framerate

Post by Robin »

Ertain wrote:When one of the enemies in my little game is destroyed, for example, I just have them moved off the screen. I have been wondering about how to remove the enemy picture, data and all.
When you don't need a certain image, just don't draw it -- not even off screen. That helps.

If you are really sure you don't need the image any more, you can just clear all references to it, and Lua will collect it automatically.
Help us help you: attach a .love.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Managing a consistent framerate

Post by Taehl »

Yeah. Drawing off the screen is just as slow as drawing on the screen, so to have the best possible framerate, make your game only draw what's visible.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
slime
Solid Snayke
Posts: 3131
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Managing a consistent framerate

Post by slime »

Taehl wrote:Yeah. Drawing off the screen is just as slow as drawing on the screen, so to have the best possible framerate, make your game only draw what's visible.
Of course, sometimes it can be slower to check for whether an image is in the screen area or not than to just draw it without any checks. ;)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Managing a consistent framerate

Post by Robin »

slime wrote:Of course, sometimes it can be slower to check for whether an image is in the screen area or not than to just draw it without any checks. ;)
The absolute best way is to use a method that doesn't even have to bother checking for the location of an image. It scales much better. ;)
Help us help you: attach a .love.
User avatar
Ertain
Citizen
Posts: 55
Joined: Fri Nov 19, 2010 9:38 pm
Location: Texas, U.S.A.

Re: Managing a consistent framerate

Post by Ertain »

Thanks for the help, guys. I shall not use the in-game print statements and stop drawing the images. But the framebuffers will be a problem, because they don't work on all OSes. So I'm going to have to at least check for the framebuffers and stuff.
Booted, suited, and ready to get executed.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Managing a consistent framerate

Post by miko »

Ertain wrote:Hello again, LÖVErs. I have a question about keeping the framerate consistent and making things run smooth. What are some methods for doing this? I have been trying to figure out how not to have so many pictures on the screen (so that it doesn't eat up CPU cycles). When one of the enemies in my little game is destroyed, for example, I just have them moved off the screen. I have been wondering about how to remove the enemy picture, data and all.

That would help to speed things up, but I'm sure there are better ways to make this more efficient, like we always try to do. ;)
I would say those are standard problems. I do agree with what was said before, and I would add:
- don't do anything you don't need (calculations, drawings, etc)
- if you really have to do it, do it only once (move to love.load(), memoize, cache, etc)
- if it cannot be done once, do it only as frequent as it really is required (eg, change the value of timer every second, not every frame)
- move your busy code out of the main loop (move long-running operations to other threads, like love.thread)
- put more RAM (there is usually tradeoff between CPU and RAM usage)
- use clever algorithms (eg, do not recalculate the whole scene, only the part which really has changed, use clever sorting algorithms, use object partitioning for collision detection)
- check for efficient lua usage: avoid creating and destroying objects which have to be garbage-collected (so reuse your tables instead of recreating new ones)
- profile your code (find the slowest part and try to optimize it)
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Managing a consistent framerate

Post by BlackBulletIV »

Taehl wrote:Yeah. Drawing off the screen is just as slow as drawing on the screen, so to have the best possible framerate, make your game only draw what's visible.
At least when drawing rectangles, I found that to be quite the opposite. When a couple hundred rectangles were on-screen, there was a slow down in framerate to the 200s I think. Then it went back up to the high 400s when not so many were around. Again, when drawing rectangles, I found by doing checks on what to draw, I had a frame rate drop of about 40-50 when drawing about 15-20 rectangles. But, it might different for images.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Managing a consistent framerate

Post by Taehl »

I was talking about drawing images (and I assume the same would be true about framebuffers, quads, and spritebatches). I don't know about simple things like rectangles.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 47 guests