May I Please Have HELP on Cursor Customization

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

May I Please Have HELP on Cursor Customization

Post 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!!!
Dragons are great.
gcmartijn
Party member
Posts: 146
Joined: Sat Dec 28, 2019 6:35 pm

Re: May I Please Have HELP on Cursor Customization

Post 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
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: May I Please Have HELP on Cursor Customization

Post 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).
Last edited by MrFariator on Wed Apr 20, 2022 6:12 am, edited 1 time in total.
gcmartijn
Party member
Posts: 146
Joined: Sat Dec 28, 2019 6:35 pm

Re: May I Please Have HELP on Cursor Customization

Post by gcmartijn »

Ah copy/past error, don't make the cursor invisible ;)
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: May I Please Have HELP on Cursor Customization

Post 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'.
Dragons are great.
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: May I Please Have HELP on Cursor Customization

Post 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.
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: May I Please Have HELP on Cursor Customization

Post 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
Dragons are great.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: May I Please Have HELP on Cursor Customization

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: May I Please Have HELP on Cursor Customization

Post 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.
Dragons are great.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], lysandre and 4 guests