User avatar
peppy.player
Posts: 461
Joined: Mon Mar 07, 2016 6:10 am

Pygame 2 & Multiple Monitors

Mon Sep 25, 2023 5:12 am

Hi,

Is it possible to use Raspberry Pi 4 and Pygame 2 with either two (HDMI, HDMI) or three (DSI, HDMI, HDMI) monitors?

Thanks!

User avatar
rpiMike
Posts: 2711
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Pygame 2 & Multiple Monitors

Mon Sep 25, 2023 6:17 am

I’ve done this a while ago with 2 screens. Just treat them as 1 huge graphics area.

User avatar
peppy.player
Posts: 461
Joined: Mon Mar 07, 2016 6:10 am

Re: Pygame 2 & Multiple Monitors

Sun Oct 01, 2023 2:52 pm

OK, so you draw on one part of the screen which will be shown on display 1 and another part will be shown on display 2. Just wondering if this approach will be possible using Pygame and Raspberry Pi 5 which has two DSI ports. The idea is to display the main audio player interface on HDMI display and use two other displays (e.g. DSI) to show left and right VU Meter channels.

User avatar
rpiMike
Posts: 2711
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Pygame 2 & Multiple Monitors

Sun Oct 01, 2023 3:49 pm

Hmm, either I was mistaken or it may have been a quirk of a previous OS or pygame.

pygame2 still only has one display surface per process I believe:
Pygame has a single display Surface that is either contained in a window or runs full screen.
https://www.pygame.org/docs/ref/display.html

The following code draws a line on screen 0 then closes that screen returning to the desktop, then draws a line on screen 1 then closes that screen returning to the desktop . Not very helpful!

python 3.9.2 pygame 2.5.1

Code: Select all

import pygame,os,time,sys

print('begin')

print('python:',sys.version)
print('pygame:',pygame.version.vernum )

pygame.init()

lcd1 = pygame.display.set_mode((0, 0), pygame.FULLSCREEN,display=0)
lcd1.fill(pygame.Color('black'))
pygame.draw.line(lcd1,pygame.Color('red'),(0,0),(1920,1080),1)

pygame.display.update()

time.sleep(2)

lcd2 = pygame.display.set_mode((0, 0), pygame.FULLSCREEN,display=1)
lcd2.fill(pygame.Color('black'))
pygame.draw.line(lcd2,pygame.Color('blue'),(0,0),(1920,1080),1)

pygame.display.update()

time.sleep(2)
pygame.quit()

print('end')

User avatar
rpiMike
Posts: 2711
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Pygame 2 & Multiple Monitors

Sun Oct 01, 2023 4:17 pm

The following using multiprocessing works but is hacky and you would have to communicate between processes.

Code: Select all

from multiprocessing import Process
import time
import pygame,os,time,sys

def a():
    
    pygame.init()

    lcd1 = pygame.display.set_mode((0, 0), pygame.FULLSCREEN,display=0)
    lcd1.fill(pygame.Color('black'))
    pygame.draw.line(lcd1,pygame.Color('red'),(0,0),(1920,1080),1)

    pygame.display.update()
    time.sleep(4)
    pygame.quit()
    
def b():
    pygame.init()
        
    lcd2 = pygame.display.set_mode((0, 0), pygame.FULLSCREEN,display=1)
    lcd2.fill(pygame.Color('black'))
    pygame.draw.line(lcd2,pygame.Color('blue'),(0,0),(1920,1080),1)

    pygame.display.update()
    time.sleep(4)
    pygame.quit()
    
if __name__ == '__main__':
    
    pa = Process(target=a)
    pa.start()
    
    pb = Process(target=b)
    pb.start()
    
    pa.join()
    pb.join()
    
    print('end')

User avatar
peppy.player
Posts: 461
Joined: Mon Mar 07, 2016 6:10 am

Re: Pygame 2 & Multiple Monitors

Sun Oct 01, 2023 7:18 pm

Thank you! That looks promising. Can I use Threads instead of Processes?
Another question is how those DSI outputs of Pi 5 can be handled in Pygame? The same way as HDMI? Probably nobody knows the answer until we put our hands on Pi 5 ;)

User avatar
rpiMike
Posts: 2711
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: Pygame 2 & Multiple Monitors

Sun Oct 01, 2023 7:48 pm

Don't think Threads will work, still the same process I guess. I'll try if I get time.

My Pi5 is pre-ordered :P

Return to “Graphics, sound and multimedia”