Cursor Movement

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.
Post Reply
Spade
Prole
Posts: 4
Joined: Wed Apr 06, 2011 1:39 pm

Cursor Movement

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Cursor Movement

Post 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.
Kurosuke needs beta testers
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Cursor Movement

Post by BlackBulletIV »

Or this:

Code: Select all

function love.draw()
  love.graphics.draw(cursor, love.mouse.getPosition())
end
User avatar
Boolsheet
Inner party member
Posts: 780
Joined: Wed Dec 29, 2010 4:57 am
Location: Switzerland

Re: Cursor Movement

Post 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)
Shallow indentations.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Cursor Movement

Post 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.
Spade
Prole
Posts: 4
Joined: Wed Apr 06, 2011 1:39 pm

Re: Cursor Movement

Post 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!
Post Reply

Who is online

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