Re: Managing a consistent framerate
Posted: Wed Apr 27, 2011 9:22 am
Doh! Why didn't I think of that one! Thanks.
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.
I was trying to show the most minimal form. He may not be using width and height, after all.Taehl wrote:A very simple way
I dread to think how many times I'm -not- doing this is UnLöve.... D:miko wrote: 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
You mean you just store them in a global var? It's probably faster than storing them in a local value in each function that uses them in an iteration, even if it is calling a global. Perhaps you could still uses the locals, but pull the value from the globals instead of issuing love.graphics.get*() and it would be faster still? Hmmmm.BlackBulletIV wrote:I've personally taken the route of caching those values in love.graphics.width and love.graphics.height, and then of course adjusting them whenever something changes. This would be much faster and convenient, and it's certainly practical, because the width and height will hardly ever, or not at all, change.
No, not a global variable. A variable nested inside the table love.graphics. The only global there is love itself. I'm not quite sure what you're meaning by that last sentence, but if you made love.graphics.get*() just return a value stored in Lua, I certainly think it would be faster (no going up to the C level).Lafolie wrote:You mean you just store them in a global var? It's probably faster than storing them in a local value in each function that uses them in an iteration, even if it is calling a global. Perhaps you could still uses the locals, but pull the value from the globals instead of issuing love.graphics.get*() and it would be faster still? Hmmmm.BlackBulletIV wrote:I've personally taken the route of caching those values in love.graphics.width and love.graphics.height, and then of course adjusting them whenever something changes. This would be much faster and convenient, and it's certainly practical, because the width and height will hardly ever, or not at all, change.
Oh, I see. That's cool but it's even quicker to type out a var name using a global outside of love.graphics.No, not a global variable. A variable nested inside the table love.graphics. The only global there is love itself. I'm not quite sure what you're meaning by that last sentence, but if you made love.graphics.get*() just return a value stored in Lua, I certainly think it would be faster (no going up to the C level).
Code: Select all
width = love.graphics.getWidth()
height = love.graphics.getHeight()
function iterator()
local w = width
local h = height
for i = 1, 10 do
love.graphics.setColor(255, 255, 255, 10 * i)
love.graphics.rectangle("fill", 10 * i, 10 * i, w - 20 * i, h - 20 * i)
end
end
function love.draw()
iterator()
end