Page 3 of 6

Re: Managing a consistent framerate

Posted: Wed Apr 27, 2011 9:22 am
by BlackBulletIV
Doh! Why didn't I think of that one! :P Thanks.

Re: Managing a consistent framerate

Posted: Wed Apr 27, 2011 11:02 am
by Taehl
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.
Taehl wrote:A very simple way
I was trying to show the most minimal form. He may not be using width and height, after all.

Re: Managing a consistent framerate

Posted: Wed Apr 27, 2011 9:31 pm
by BlackBulletIV
Oh rightio then :).

Re: Managing a consistent framerate

Posted: Thu Apr 28, 2011 2:40 am
by Ertain
Oh, I'm using the width and height of the screen.

Re: Managing a consistent framerate

Posted: Wed May 04, 2011 1:56 pm
by Lafolie
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
I dread to think how many times I'm -not- doing this is UnLöve.... D:

Re: Managing a consistent framerate

Posted: Wed May 04, 2011 8:29 pm
by BlackBulletIV
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.

Re: Managing a consistent framerate

Posted: Wed May 04, 2011 9:22 pm
by Lafolie
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.
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.

Re: Managing a consistent framerate

Posted: Wed May 04, 2011 9:45 pm
by slime
The performance increase is marginal when compared with the real bottlenecks a typical LÖVE game has. :P

I do that too (store graphics getWidth/getHeight in global variables) but mainly for convenience. xres is a lot faster to type than love.graphics.getWidth(). :)

Re: Managing a consistent framerate

Posted: Wed May 04, 2011 10:52 pm
by BlackBulletIV
Lafolie wrote:
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.
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.
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).

Re: Managing a consistent framerate

Posted: Wed May 04, 2011 11:08 pm
by Lafolie
Of course! It is a lot quicker to type.

The performance increase isn't the real issue though. What this is really about is writing optimised code. Whilst the purpose of optimised code is pretty much for performance, writing such code is just good practice and I believe that it is good to get into that mindset. Write the best code you can. A good example of this philosophy would be using that lua strict module. There's no real need to use it, but it helps you write optimised, clean code which inevitably has several benefits other than performance increase.

Optimised code = good code. In most cases anyway.

:D

BlackBulletIV posted while I was typing....
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).
Oh, I see. That's cool but it's even quicker to type out a var name using a global outside of love.graphics.

And, I meant doing this:

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