Fullscreen window, hide/show mouse cursor position

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
luminosity
Prole
Posts: 34
Joined: Fri Sep 24, 2010 5:46 am

Fullscreen window, hide/show mouse cursor position

Post by luminosity »

Hey guys,

So I've been testing out fullscreen mode in my game recently, and while I use the default pointer everything works fine. However hiding and showing the mouse cursor in fullscreen mode seems to alter the mouse's position. I can work around this, I think, but it's really quite annoying and unexpected. In windowed mode this never happens. I'm wondering if this is something wrong in SDL and if something in Love can be done to stop it?

Thanks guys.

Example code:

Move the mouse around to see its position getting changed as cursor visibility is changed, if you set the fullscreen flag to false this doesn't happen. Clicking exits.

Code: Select all

local fullscreen = true
function love.load()
	love.graphics.setMode(1680, 1050, fullscreen, false, nil)
end

local count = 0
local cursor = false

function love.update(dt)
	count = count + dt
	if count > 1.5 then
		count = 0
		love.mouse.setVisible(cursor)
		cursor = not cursor
	end
end

function love.draw()
	love.graphics.rectangle('fill', love.mouse.getX(), love.mouse.getY(), 10, 10)
end

function love.mousepressed()
	love.event.push("q")
end
luminosity
Prole
Posts: 34
Joined: Fri Sep 24, 2010 5:46 am

Re: Fullscreen window, hide/show mouse cursor position

Post by luminosity »

So I've been investigating further, this is a mockup of what ends up happening in my game, if the rectangle were the hex grid of my game:

Code: Select all

local fullscreen = true
function love.load()
	love.graphics.setMode(1680, 1050, fullscreen, false, nil)
	rectWidth = love.graphics.getWidth() / 2
	rectHeight = love.graphics.getHeight() / 2
	rectLeft = math.floor(love.graphics.getWidth()/4 + 0.5)
	rectTop = math.floor(love.graphics.getHeight()/4 + 0.5)
end

function inRect(x, y)
	if x < rectLeft or x > rectLeft + rectWidth or
	   y < rectTop or y > rectTop + rectHeight
	then
	 return false
	else
	 return true
	end
end

local prevX, prevY = love.mouse.getPosition()
function love.update(dt)
	local x, y = love.mouse.getPosition()
	if inRect(prevX, prevY) and not inRect(x, y) then
		love.mouse.setVisible(true)
	elseif inRect(x, y) and not inRect(prevX, prevY) then
		love.mouse.setVisible(false)
	end
	prevX, prevY = x, y
end

function love.draw()
	love.graphics.setColor(100, 100, 100)
	love.graphics.rectangle('fill', rectLeft, rectTop, rectWidth, rectHeight)
	love.graphics.setColor(255, 255, 255)
	love.graphics.rectangle('fill', love.mouse.getX(), love.mouse.getY(), 10, 10)
end

function love.mousepressed()
	love.event.push("q")
end
Try to move the mouse out, the mouseout code detects the pointer should change, that resets the pointer to the center, so you never mouse out.

Searching around for SDL bugs I found this thread:

http://forums.libsdl.org/viewtopic.php? ... dcbdca7601

which recommends using the code

Code: Select all

void MyShowCursor( int show )
{
int x, y;

SDL_GetMouseState( &x, &y );
SDL_ShowCursor( show ? SDL_ENABLE : SDL_DISABLE );
SDL_WarpMouse( x, y );
} 
So I made the closest equivalent I could to that in love.

Code: Select all

local fullscreen = true
function love.load()
	love.graphics.setMode(1680, 1050, fullscreen, false, nil)
	rectWidth = love.graphics.getWidth() / 2
	rectHeight = love.graphics.getHeight() / 2
	rectLeft = math.floor(love.graphics.getWidth()/4 + 0.5)
	rectTop = math.floor(love.graphics.getHeight()/4 + 0.5)
end

function inRect(x, y)
	if x < rectLeft or x > rectLeft + rectWidth or
	   y < rectTop or y > rectTop + rectHeight
	then
	 return false
	else
	 return true
	end
end

local prevX, prevY = love.mouse.getPosition()
function love.update(dt)
	local x, y = love.mouse.getPosition()
	if inRect(prevX, prevY) and not inRect(x, y) then
		love.mouse.setVisible(true)
		love.mouse.setPosition(x, y)
	elseif inRect(x, y) and not inRect(prevX, prevY) then
		love.mouse.setVisible(false)
		love.mouse.setPosition(x, y)
	end
	prevX, prevY = x, y
end

function love.draw()
	love.graphics.setColor(100, 100, 100)
	love.graphics.rectangle('fill', rectLeft, rectTop, rectWidth, rectHeight)
	love.graphics.setColor(255, 255, 255)
	love.graphics.rectangle('fill', love.mouse.getX(), love.mouse.getY(), 10, 10)
end

function love.mousepressed()
	love.event.push("q")
end
And now instead of being unable to get the mouse out of the rectangle, I can no longer get it in.
Post Reply

Who is online

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