Page 1 of 1

May I Please Have HELP on Cursor Customization

Posted: Wed Apr 20, 2022 12:54 am
by PixelHero
I am trying to do some Cursor Customization, may I please have help with this error that I am having? Here is the code:

Code: Select all

function love.load()
    
    -- mouse settings
    love.mouse.setVisible(false)
    love.mouse.setGrabbed(false)
    cursor = love.image.newImageData('cursor.png')
    MouseX = nil
    MouseY = nil

end

function love.update(dt)

    -- more mouse settings
    MouseX = love.mouse.getX()
    MouseY = love.mouse.getY()

end

function love.draw()

    -- mouse picture
    Cursor = love.mouse.newCursor(cursor, MouseX, MouseY)
    love.mouse.setCursor(Cursor)
    
end
If you can help, please HELP!!!

Re: May I Please Have HELP on Cursor Customization

Posted: Wed Apr 20, 2022 5:41 am
by gcmartijn
https://love2d.org/wiki/love.mouse.newCursor

Code: Select all

function love.load()
    
    -- mouse settings
    love.mouse.setVisible(false)
    love.mouse.setGrabbed(false)
    cursorImage = love.image.newImageData('cursor.png')
    cursor = love.mouse.newCursor(cursorImage, 0, 0)
    love.mouse.setCursor(cursor)
end

Re: May I Please Have HELP on Cursor Customization

Posted: Wed Apr 20, 2022 6:01 am
by MrFariator
I guess it is worth pointing out that whenever checking the wiki, heed the warning in a yellow box, if there is one. Because in your code you called love.mouse.newCursor in love.draw, it creates a new cursor object every time love.draw is called, which can cause a fair few issues in terms of memory usage.

As for your original issue, I can't immediately spot what your error might be. However, because you call love.mouse.setVisible, and set the visibility to false, you won't be able to see the new cursor you set. You want your cursor to be visible (true), not invisible (false).

Re: May I Please Have HELP on Cursor Customization

Posted: Wed Apr 20, 2022 6:08 am
by gcmartijn
Ah copy/past error, don't make the cursor invisible ;)

Re: May I Please Have HELP on Cursor Customization

Posted: Thu Apr 21, 2022 2:02 am
by PixelHero
I fixed it. If you want the finished code, here it is:

Code: Select all

function love.load()
    cursor = love.graphics.newImage("cursor.png")
    love.mouse.setVisible(false)
    love.mouse.setGrabbed(true)
end

function love.update(dt)
    function love.keypressed(key)
        if key == 'escape' then
            love.event.quit()
        end
    end
end

function love.draw()
    love.graphics.draw(cursor, love.mouse.getX() - cursor:getWidth() / 2, love.mouse.getY() - cursor:getHeight() / 2)
end
Also, thank you for devoting some time to this.

The quit code is important, because without that, you'd be stuck in that window, unless you know enough to press 'alt + f4'.

Re: May I Please Have HELP on Cursor Customization

Posted: Thu Apr 21, 2022 2:13 am
by MrFariator
There is an issue with that code, however. If you define love.keypressed inside the love.update, the keypressed callback will be defined every update tick, when this would suffice:

Code: Select all

function love.update(dt)
    -- update code
end

function love.keypressed(key)
    if key == 'escape' then
        love.event.quit()
    end
end
This defines the love.keypressed callback only once, and that's all that's needed in this scenario.

Re: May I Please Have HELP on Cursor Customization

Posted: Thu Apr 21, 2022 2:37 am
by PixelHero
MrFariator wrote: Thu Apr 21, 2022 2:13 am There is an issue with that code, however. If you define love.keypressed inside the love.update, the keypressed callback will be defined every update tick, when this would suffice:

Code: Select all

function love.update(dt)
    -- update code
end

function love.keypressed(key)
    if key == 'escape' then
        love.event.quit()
    end
end
This defines the love.keypressed callback only once, and that's all that's needed in this scenario.
I tried this before, it didn't work, but that might have been a problem with how I wrote it. I'll try it again. Also, could you please help with my actual problem? It's in this forum: viewtopic.php?t=92820

Re: May I Please Have HELP on Cursor Customization

Posted: Thu Apr 21, 2022 9:07 am
by ReFreezed
PixelHero wrote: Thu Apr 21, 2022 2:02 am

Code: Select all

function love.load()
	cursor = love.graphics.newImage("cursor.png")
	love.mouse.setVisible(false)
end
function love.draw()
	love.graphics.draw(cursor, love.mouse.getX() - cursor:getWidth() / 2, love.mouse.getY() - cursor:getHeight() / 2)
end
While this does work, it's not a hardware cursor and thus will have a higher latency and feel more sluggish (more info). It's easy to test:

Code: Select all

function love.load()
	cursorImage = love.graphics.newImage("cursor.png")
	cursor      = love.mouse.newCursor("cursor.png")
	love.mouse.setCursor(cursor)
end
function love.draw()
	love.graphics.setColor(1, 0, 1)
	love.graphics.draw(cursorImage, love.mouse.getPosition())
end
The software cursor will trail behind the hardware cursor.

Re: May I Please Have HELP on Cursor Customization

Posted: Sat Apr 23, 2022 6:07 pm
by PixelHero
ReFreezed wrote: Thu Apr 21, 2022 9:07 am
PixelHero wrote: Thu Apr 21, 2022 2:02 am

Code: Select all

function love.load()
	cursor = love.graphics.newImage("cursor.png")
	love.mouse.setVisible(false)
end
function love.draw()
	love.graphics.draw(cursor, love.mouse.getX() - cursor:getWidth() / 2, love.mouse.getY() - cursor:getHeight() / 2)
end
While this does work, it's not a hardware cursor and thus will have a higher latency and feel more sluggish (more info). It's easy to test:

Code: Select all

function love.load()
	cursorImage = love.graphics.newImage("cursor.png")
	cursor      = love.mouse.newCursor("cursor.png")
	love.mouse.setCursor(cursor)
end
function love.draw()
	love.graphics.setColor(1, 0, 1)
	love.graphics.draw(cursorImage, love.mouse.getPosition())
end
The software cursor will trail behind the hardware cursor.
Thank you for your input. I will take that into consideration.