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
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 »

Ah ok. Doesn't OpenGL do some optimisations like not drawing what's not on the screen?
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 »

miko wrote: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)
For your example, how would one change the value of "timer" every second? Are you referring to love.timer? :?

And now, more comments.
robin wrote: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.
How can I use one image for representing many enemies on the screen?

As for enemies on the screen, should I check for when they leave the screen, and stop drawing them when they leave said screen?
Booted, suited, and ready to get executed.
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:For your example, how would one change the value of "timer" every second? Are you referring to love.timer? :?
A "timer" is really a variable whose value you constantly update to reflect time passed:

Code: Select all

timer = 0
function love.update(dt)
 timer = timer + dt
end
Ertain wrote:How can I use one image for representing many enemies on the screen?
Use the same image to draw each enemy.

Code: Select all

img_enemy = love.graphics.newImage(...)

function love.draw()
   for i,enemy in ipairs(enemies) do
      love.graphics.draw(img_enemy, enemy.x, enemy.y)
   end
end
Ertain wrote:As for enemies on the screen, should I check for when they leave the screen, and stop drawing them when they leave said screen?
Yes.

(PS: do not use the code samples in this post as such, they are only examples.
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 »

Ertain wrote:As for enemies on the screen, should I check for when they leave the screen, and stop drawing them when they leave said screen?
Absolutely. A very simple way of doing this would be something like:

Code: Select all

love.draw()
	for i,enemy in ipairs(enemies) do
		-- Draw the enemy only if it's inside the window
		if enemy.x > 0 and enemy.x < 800 and enemy.y > 0 and enemy.y < 600 then
			love.graphics.draw(img_enemy, enemy.x, enemy.y)
		end
	end
end
This assumes you don't have a moving camera. You'd need a little extra math to take into account the camera's position, in that case.
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
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:For your example, how would one change the value of "timer" every second? Are you referring to love.timer? :?
A "timer" is really a variable whose value you constantly update to reflect time passed:

Code: Select all

timer = 0
function love.update(dt)
 timer = timer + dt
end
And then if you are drawing it (eg, seconds elapsed/remaining) you would do:

Code: Select all

if timer-previous_timer>=1 then
  previous_timer=timer
  renderNewTimerImage(timer)
end
Anyways, it was just an example. The main point is to avoid unnecessary work if you can.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Managing a consistent framerate

Post by kikito »

BlackBulletIV wrote:Ah ok. Doesn't OpenGL do some optimisations like not drawing what's not on the screen?
It seems to me that the actual bottleneck here might be Lua, not OpenGL. Even if the graphics architecture (be it OpenGL or the GPU itself) "optimizes out" all the non-visible draw functions, the CPU will still have to interpret all those Lua function calls, transform them into C, etc. If you remove the unnecesary Lua calls, this would be an improvement - the test to see what falls into the screen and what doesn't has to be more efficient that just drawing everything, of course :)
When I write def I mean function.
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:Absolutely. A very simple way of doing this would be something like:

Code: Select all

...
Don't forget the width and height! It's also best to use the getWidth/Height functions (or some variables), to make your code more abstract, as in, less reliant on outside conditions.

Code: Select all

function love.draw()
  for i, enemy in ipairs(enemies) do
    if enemy.x > -enemy.width and enemy.x < love.graphics.getWidth()
       and enemy.y > -enemy.height and enemy.y < love.graphics.getHeight()   
    then
      love.graphics.draw(img_enemy, enemy.x, enemy.y)
    end
  end
end
kikito wrote:
BlackBulletIV wrote:Ah ok. Doesn't OpenGL do some optimisations like not drawing what's not on the screen?
It seems to me that the actual bottleneck here might be Lua, not OpenGL. Even if the graphics architecture (be it OpenGL or the GPU itself) "optimizes out" all the non-visible draw functions, the CPU will still have to interpret all those Lua function calls, transform them into C, etc. If you remove the unnecesary Lua calls, this would be an improvement - the test to see what falls into the screen and what doesn't has to be more efficient that just drawing everything, of course :)
Ah ok, that's probably it. Because when I was reading about the OpenGL pipeline, I remember it saying something about OpenGL clearing out non-on-screen data.

EDIT: Hey congratulations on 1000 posts kikito!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Managing a consistent framerate

Post by kikito »

BlackBulletIV wrote:EDIT: Hey congratulations on 1000 posts kikito!
Why, thank you! :ultraglee: This is not what I planned for my 1000th post. At least I hope it was useful.
When I write def I mean function.
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 »

No worries :). Was it your thousandth post? I saw 1001 when I wrote that. It probably was, if you were going from the forums in top-to-bottom order.
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 »

BlackBulletIV wrote:Don't forget the width and height! It's also best to use the getWidth/Height functions (or some variables), to make your code more abstract, as in, less reliant on outside conditions.

Code: Select all

function love.draw()
  for i, enemy in ipairs(enemies) do
    if enemy.x > -enemy.width and enemy.x < love.graphics.getWidth()
       and enemy.y > -enemy.height and enemy.y < love.graphics.getHeight()   
    then
      love.graphics.draw(img_enemy, enemy.x, enemy.y)
    end
  end
end
You don't want do access global variables and make a call to get*() on every iteration. So the modified example would be:

Code: Select all

function love.draw()
  local width, height=love.graphics.getWidth(), love.graphics.getHeight()
  local draw=love.graphics.draw
  for i, enemy in ipairs(enemies) do
    if enemy.x > -enemy.width and enemy.x < width
       and enemy.y > -enemy.height and enemy.y < height   
    then
      draw(img_enemy, enemy.x, enemy.y)
    end
  end
end
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 7 guests