Search code examples
python-2.7pygamecoordinatesfullscreen

Strange pygame coordinates while in fullscreen


I have written an app using pygame on a raspberry pi with touchscreen display. It works totally fine in windowed mode but it acts completely strange as soon as I switch to fullscreen mode. None of my touches (clicks) return a rational x and y, and they are usually returned as 799 and 479 (the downright corner of screen) after a couple of clicks. What can I do to fix the problem?

The fullscreen resolution for my screen is 800x480, I have to mention I do not have this problem while working on a desktop computer, and it only occurs when using the touchscreen input. Here is the related part of the code:

import pygame, time, math, os
from pygame.locals import *
from sys import exit

pygame.init()        
pygame.mouse.set_visible(0)
pygame.mouse.set_pos(0, 0)
screen = pygame.display.set_mode((800, 480))

It works fine with this but when I switch to fullscreen:

screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

it will detect every click in the ending pixel of the screen. The strange thing is, when I place objects (e.g.buttons) on the main screen, they are being placed correctly at the right coordinates, but I cannot click on them since the returned coordinates from the touchscreen input are not correct.


Solution

  • What an interesting problem! I just tested some of this code using my touchscreen on my Windows laptop (for once!) and it runs into some strange issues as well, so you are not alone! It seems to me, at least, that only whenever I hide my mouse cursor using pygame.mouse.set_visible(False) in combination with the pygame.FULLSCREEN flag do I get issues with my pygame.MOUSEBUTTONDOWN events returning incorrect mouse positions. Making the cursor visible fixes this, but is obviously annoying for touchscreens. The solution (really a hack) I came up with is to use a borderless window that simulates a fullscreen mode instead by being positioned at the top-left corner of the screen. Here is the code; let me know if it works for you:

    import pygame, time, math, sys, os
    
    os.environ['SDL_VIDEO_WINDOW_POS'] = "{0},{1}".format(0, 0)
    pygame.init()        
    screen = pygame.display.set_mode((0, 0), pygame.NOFRAME)
    pygame.mouse.set_visible(False)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                print(event.pos)
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
    

    That being said, I am curious to know why it does not work with the pygame.FULLSCREEN flag. If anyone can offer some insight on this for either pygame or SDL1.2, that would be much appreciated...