Managing a consistent framerate
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Managing a consistent framerate
Ah ok. Doesn't OpenGL do some optimisations like not drawing what's not on the screen?
Re: Managing a consistent framerate
For your example, how would one change the value of "timer" every second? Are you referring to love.timer?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)
And now, more comments.
How can I use one image for representing many enemies on the screen?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.
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.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Managing a consistent framerate
A "timer" is really a variable whose value you constantly update to reflect time passed:Ertain wrote:For your example, how would one change the value of "timer" every second? Are you referring to love.timer?
Code: Select all
timer = 0
function love.update(dt)
timer = timer + dt
end
Use the same image to draw each enemy.Ertain wrote:How can I use one image for representing many enemies on the screen?
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
Yes.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?
(PS: do not use the code samples in this post as such, they are only examples.
Help us help you: attach a .love.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Managing a consistent framerate
Absolutely. A very simple way of doing this would be something like: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?
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
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Managing a consistent framerate
And then if you are drawing it (eg, seconds elapsed/remaining) you would do:Robin wrote:A "timer" is really a variable whose value you constantly update to reflect time passed:Ertain wrote:For your example, how would one change the value of "timer" every second? Are you referring to love.timer?Code: Select all
timer = 0 function love.update(dt) timer = timer + dt end
Code: Select all
if timer-previous_timer>=1 then
previous_timer=timer
renderNewTimerImage(timer)
end
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Managing a consistent framerate
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 courseBlackBulletIV wrote:Ah ok. Doesn't OpenGL do some optimisations like not drawing what's not on the screen?
When I write def I mean function.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Managing a consistent framerate
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.Taehl wrote:Absolutely. A very simple way of doing this would be something like:Code: Select all
...
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
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.kikito wrote: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 courseBlackBulletIV wrote:Ah ok. Doesn't OpenGL do some optimisations like not drawing what's not on the screen?
EDIT: Hey congratulations on 1000 posts kikito!
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Managing a consistent framerate
Why, thank you! This is not what I planned for my 1000th post. At least I hope it was useful.BlackBulletIV wrote:EDIT: Hey congratulations on 1000 posts kikito!
When I write def I mean function.
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Managing a consistent framerate
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.
Re: Managing a consistent framerate
You don't want do access global variables and make a call to get*() on every iteration. So the modified example would be: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
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
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 7 guests