Page 1 of 1

Cursor Movement

Posted: Thu Apr 07, 2011 12:13 am
by Spade
Somewhat of a nub question. I just started messing around in lua and love today and it's going well so far, however I'm running into some issues.

I'm attempting to replace the mouse pointer with a crosshair-esque image. I have it working, however it seems to be laggy.

I'm essentially doing it like this:

Code: Select all

love.mouse.setVisible(false)

function love.update()
	cursor_x, cursor_y = love.mouse.getPosition()
end

function love.draw()
	love.graphics.draw(cursor, cursor_x, cursor_y)
end
Is this the best way to go about it? I looked around in the wiki and came up with this off the top of my head. How would I go about fixing the lag? I've messed around with delta time but I can't seem to get it to work.

Re: Cursor Movement

Posted: Thu Apr 07, 2011 12:28 am
by tentus
You could just do this:

Code: Select all

function love.draw()
   love.graphics.draw(cursor, love.mouse.getX(), love.mouse.getY())
end
I'm not sure how much it will affect performance, but it's certainly simpler codewise.

Re: Cursor Movement

Posted: Thu Apr 07, 2011 12:45 am
by BlackBulletIV
Or this:

Code: Select all

function love.draw()
  love.graphics.draw(cursor, love.mouse.getPosition())
end

Re: Cursor Movement

Posted: Thu Apr 07, 2011 3:28 am
by Boolsheet
Spade wrote:How would I go about fixing the lag?
The difference you see between the system cursor and your drawn cursor probably comes from vertical synchronization.
If vsync is enabled, the graphics card will not send the next frame until the monitor has fully drawn the last one. This means the framerate gets limited to the refresh rate of the monitor, which is usually 60 frames per second.

With 60 fps, one frame is displayed every ~16 ms. There is a chance that the execution stops for 16 ms, if the code is fast enough.

What it coud look like:

Code: Select all

   Time in seconds   called function
------------------------------------------
       0.0000        love.update
       0.0001        love.draw
       0.0171        love.present
       0.0172        love.update
       0.0173        love.draw
       0.0355        love.present
       0.0356        love.update
       0.0357        love.draw
       ...
The screen shows an image that was generated ~16 ms earlier in this case.

You can try to move the waiting before the update by writing your own love.run() and approximating the time it has to sleep for 60 fps, but this is inaccurate and can give problems once the game gets more complex.
Or you can disable vsync with love.graphics.setMode(). Remember that screen tearing and much higher frame rates are effects of disabling it. (LÖVE caps to 1000 fps by sleeping 1 ms in love.run)

Re: Cursor Movement

Posted: Thu Apr 07, 2011 8:34 am
by bartbes
Boolsheet wrote:(LÖVE caps to 1000 fps by sleeping 1 ms in love.run)
Actually, not really, the sleep function we use doesn't guarantee this accuracy (I believe 10ms accuracy is mentioned as common). In any case, it's more about telling the scheduler we don't need all cpu then anything else.

Re: Cursor Movement

Posted: Thu Apr 07, 2011 6:11 pm
by Spade
Boolsheet wrote: Or you can disable vsync with love.graphics.setMode(). Remember that screen tearing and much higher frame rates are effects of disabling it. (LÖVE caps to 1000 fps by sleeping 1 ms in love.run)
This worked, thanks!