2013-03-12 06:13:07 -04:00
|
|
|
from os import environ
|
2019-07-07 02:12:22 -04:00
|
|
|
from sys import maxsize, platform
|
2013-03-12 06:13:07 -04:00
|
|
|
|
2022-10-25 15:25:48 -04:00
|
|
|
import pygame
|
2013-03-03 08:25:41 -05:00
|
|
|
from pygame import display, image, mouse
|
2012-08-31 13:56:53 -04:00
|
|
|
from pygame.locals import *
|
2012-07-05 04:21:49 -04:00
|
|
|
|
2019-04-12 03:08:07 -04:00
|
|
|
from .GameChild import *
|
2012-07-05 04:21:49 -04:00
|
|
|
|
|
|
|
class Display(GameChild):
|
|
|
|
|
|
|
|
def __init__(self, game):
|
|
|
|
GameChild.__init__(self, game)
|
2022-11-01 17:52:27 -04:00
|
|
|
print(f"available fullscreen modes: {pygame.display.list_modes()}")
|
2013-04-14 10:46:43 -04:00
|
|
|
self.delegate = self.get_delegate()
|
|
|
|
self.load_configuration()
|
2013-03-12 06:13:07 -04:00
|
|
|
self.align_window()
|
2013-03-19 06:11:32 -04:00
|
|
|
self.init_screen()
|
2012-07-05 04:21:49 -04:00
|
|
|
self.set_caption()
|
|
|
|
self.set_icon()
|
2013-03-03 08:25:41 -05:00
|
|
|
self.set_mouse_visibility()
|
2013-03-07 08:01:08 -05:00
|
|
|
self.subscribe(self.toggle_fullscreen)
|
2012-07-05 04:21:49 -04:00
|
|
|
|
2013-04-14 10:46:43 -04:00
|
|
|
def load_configuration(self):
|
|
|
|
config = self.get_configuration("display")
|
|
|
|
self.centered = config["centered"]
|
2013-04-16 07:21:23 -04:00
|
|
|
self.fullscreen_enabled = config["fullscreen"]
|
2013-04-14 10:46:43 -04:00
|
|
|
self.caption = config["caption"]
|
2013-04-16 07:21:23 -04:00
|
|
|
self.windowed_flag = config["windowed-flag"]
|
2013-04-27 07:21:52 -04:00
|
|
|
self.icon_path = self.get_resource("display", "icon-path")
|
2013-04-14 10:46:43 -04:00
|
|
|
self.mouse_visibility = self.get_configuration("mouse", "visible")
|
|
|
|
|
2013-03-19 06:11:32 -04:00
|
|
|
def align_window(self):
|
2013-04-14 10:46:43 -04:00
|
|
|
if self.centered:
|
2013-03-19 06:11:32 -04:00
|
|
|
environ["SDL_VIDEO_CENTERED"] = "1"
|
|
|
|
|
|
|
|
def init_screen(self):
|
2018-11-18 23:07:01 -05:00
|
|
|
full = False
|
2013-04-16 07:21:23 -04:00
|
|
|
if self.fullscreen_requested():
|
2018-11-18 23:07:01 -05:00
|
|
|
full = True
|
2022-10-25 15:25:48 -04:00
|
|
|
|
|
|
|
# Added pygame.SCALED to avoid fullscreen bug
|
|
|
|
# <https://www.pygame.org/docs/ref/display.html#pygame.display.toggle_fullscreen>
|
2022-11-01 17:52:27 -04:00
|
|
|
try:
|
|
|
|
self.set_screen(pygame.SCALED, fs=full)
|
|
|
|
except AttributeError:
|
|
|
|
self.set_screen(fs=full)
|
2013-03-19 06:11:32 -04:00
|
|
|
|
2013-04-16 07:21:23 -04:00
|
|
|
def fullscreen_requested(self):
|
2022-10-25 15:25:48 -04:00
|
|
|
return not self.check_command_line(self.windowed_flag) and self.fullscreen_enabled
|
2013-04-16 07:21:23 -04:00
|
|
|
|
2018-11-18 23:07:01 -05:00
|
|
|
def set_screen(self, flags=0x0, dimensions=None, fs=False):
|
2014-03-06 22:37:27 -05:00
|
|
|
self.dimensions_changed = dimensions is not None
|
2013-10-12 11:41:40 -04:00
|
|
|
if dimensions is None:
|
|
|
|
if display.get_surface():
|
|
|
|
dimensions = display.get_surface().get_size()
|
|
|
|
else:
|
|
|
|
dimensions = self.get_configuration("display", "dimensions")
|
2018-11-18 23:07:01 -05:00
|
|
|
if fs:
|
|
|
|
self.screen = display.set_mode(dimensions, flags | -0x80000000)
|
|
|
|
else:
|
|
|
|
self.screen = display.set_mode(dimensions, flags)
|
2014-03-06 22:37:27 -05:00
|
|
|
if self.dimensions_changed:
|
|
|
|
interpolator = self.get_game().interpolator
|
|
|
|
if interpolator.gui_enabled:
|
|
|
|
interpolator.gui.rearrange()
|
2012-07-05 04:21:49 -04:00
|
|
|
|
|
|
|
def set_caption(self):
|
2021-09-25 17:31:28 -04:00
|
|
|
display.set_caption(self.caption)
|
2012-07-05 04:21:49 -04:00
|
|
|
|
|
|
|
def set_icon(self):
|
2013-04-27 07:21:52 -04:00
|
|
|
if self.icon_path:
|
|
|
|
display.set_icon(image.load(self.icon_path).convert_alpha())
|
2012-07-05 04:21:49 -04:00
|
|
|
|
2013-03-03 08:25:41 -05:00
|
|
|
def set_mouse_visibility(self, visibility=None):
|
|
|
|
if visibility is None:
|
2013-04-14 10:46:43 -04:00
|
|
|
visibility = self.mouse_visibility
|
2014-01-14 11:01:28 -05:00
|
|
|
return mouse.set_visible(visibility)
|
2013-03-03 08:25:41 -05:00
|
|
|
|
2012-07-05 04:21:49 -04:00
|
|
|
def get_screen(self):
|
|
|
|
return self.screen
|
|
|
|
|
|
|
|
def get_size(self):
|
|
|
|
return self.screen.get_size()
|
2012-08-31 13:56:53 -04:00
|
|
|
|
|
|
|
def toggle_fullscreen(self, event):
|
2013-04-14 10:46:43 -04:00
|
|
|
if self.delegate.compare(event, "toggle-fullscreen"):
|
2022-10-25 15:25:48 -04:00
|
|
|
pygame.display.toggle_fullscreen()
|