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
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:I am at my wits end. I can't figure out why, at the end of my little demo here, it completely freezes up. I don't know if it's all of the ships I'm drawing, or it's a combination of the drawing and the collision detection.

If you guys wish to sample my game, here it is.
It errors when you start the game, sorry. :(
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 »

Hmm, must be the framebuffer.
Booted, suited, and ready to get executed.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: Managing a consistent framerate

Post by Lafolie »

Yeah, it returns an error to do with love.graphics.setRenderTarget()
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
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 »

Robin wrote:
Ertain wrote:I am at my wits end. I can't figure out why, at the end of my little demo here, it completely freezes up. I don't know if it's all of the ships I'm drawing, or it's a combination of the drawing and the collision detection.
I think the reason here is that you keep moving your enemies inifinitely (so their y position becames a really large number, and drawing anything at such extreme locations can be demanding, and using such numbers for computations also). You should remove your enemies when they go off-screen both for drawing and collision detection.

This one line fixes the crashing for me (remember, this is a hacky workaround, not a real solution):

Code: Select all

    pincerAttack = function(changeInTime)
    if swarm.enemy[2].buzzard1.y>1000 then return end
I have some more comments here. First of all, you know that love.update() is executed every frame, don't you? Well, inside it you define a new love.keypressed function 60 (or so) times a second. What you should do instead, is either move you checks to once defined love.keypressed function or switch its current value between the earlier defined functions:

Code: Select all

if condition then love.keypressed=myFirstFn else love.keypressed=mySecondFn end
Also, you can shorten this construct:

Code: Select all

 if not menus.letsGetThePartyStarted then
...
 elseif menus.letsGetThePartyStarted then
...
end
into this:

Code: Select all

 if not menus.letsGetThePartyStarted then
...
 else
...
end
Why are using a framebuffer in group.draw_waves()? It should be used to "cache" your current drawing, but if you regenerate it every frame, you only make things worse.

In summary, this code could get lots of optimization (or, should I write, it could waste way less resources).
Robin wrote:It errors when you start the game, sorry. :(
Change line 28 in libs/background.lua from

Code: Select all

mr_framebuffer = pcall(love.graphics.newFramebuffer, gf.getWidth()+50, gf.getHeight()+50)
to

Code: Select all

  _status, mr_framebuffer = pcall(love.graphics.newFramebuffer, gf.getWidth()+50, gf.getHeight()+50)
pcall() returns a status code (boolean) as its first argument. It has nothing to do with framebuffer support.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
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 »

Thank you for the help, Miko.
Booted, suited, and ready to get executed.
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 »

miko wrote:Also, you can shorten this construct:

Code: Select all

 if not menus.letsGetThePartyStarted then
...
 elseif menus.letsGetThePartyStarted then
...
end
into this:

Code: Select all

 if not menus.letsGetThePartyStarted then
...
 else
...
end
Even shorter would be:

Code: Select all

if menus.letsGetThePartyStarted then
  <code that was in the else part>
else
  <code that was in the if part>
end
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 »

Yeah, maybe I should organize it like that. :-?
Booted, suited, and ready to get executed.
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 »

By the way, about that framebuffer issue: how shall I exactly draw to it? I know I first load it in love.load(), but do I first set the render target (in the draw() step) as the framebuffer, and then use a draw() function to set the images in the buffer?
Booted, suited, and ready to get executed.
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 »

To render to it, either use love.graphics.setRenderTarget or Framebuffer:renderTo.

Code: Select all

love.graphics.setRenderTarget(fb)
-- draw stuff
love.graphics.setRenderTarget() -- set back to screen
or

Code: Select all

fb:renderTo(function()
  -- draw stuff
end)
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:By the way, about that framebuffer issue: how shall I exactly draw to it? I know I first load it in love.load(), but do I first set the render target (in the draw() step) as the framebuffer, and then use a draw() function to set the images in the buffer?
There is lots of examples in this forum, check them out! For example, my code here: https://github.com/miko/Love2d-samples/ ... e/main.lua
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests