Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

133 lines
3.6KB

  1. '''
  2. Filename display.py
  3. Author: Bryan "ObsidianBlk" Miller
  4. Date Created: 8/1/2018
  5. Python Version: 3.7
  6. '''
  7. import pygame
  8. import weakref
  9. from .events import Events
  10. class Flag:
  11. SWSURFACE = pygame.SWSURFACE
  12. HWSURFACE = pygame.HWSURFACE
  13. HWPALETTE = pygame.HWPALETTE
  14. DOUBLEBUF = pygame.DOUBLEBUF
  15. FULLSCREEN = pygame.FULLSCREEN
  16. OPENGL = pygame.OPENGL
  17. RESIZABLE = pygame.RESIZABLE
  18. NOFRAME = pygame.NOFRAME
  19. def isSet(flags, flag):
  20. return (flags & flag) == flag
  21. class _Display:
  22. def __init__(self, width=0, height=0):
  23. # NOTE: In the newest version of pygame, setting resolution to 0,0 should set the resolution to that of the screen... so we'll just mimic that.
  24. self._init = False
  25. self._resolution = (width, height)
  26. self._display_surface = None
  27. self._display_flags = Flag.HWSURFACE | Flag.DOUBLEBUF
  28. @property
  29. def surface(self):
  30. return self._display_surface
  31. @property
  32. def width(self):
  33. if self._display_surface is None:
  34. return 0
  35. return self._resolution[0]
  36. @property
  37. def height(self):
  38. if self._display_surface is None:
  39. return 0
  40. return self._resolution[1]
  41. @property
  42. def resolution(self):
  43. if self._display_surface is None:
  44. return (0,0)
  45. return self._display_surface.get_size()
  46. @property
  47. def flags(self):
  48. return self._display_flags
  49. @property
  50. def fullscreen(self):
  51. return Flag.isSet(self._display_flags, Flag.FULLSCREEN)
  52. @property
  53. def double_buffered(self):
  54. return Flag.isSet(self._display_flags, Flag.DOUBLEBUF)
  55. @property
  56. def resizable(self):
  57. return Flag.isSet(self._display_flags, Flag.RESIZABLE)
  58. @property
  59. def no_frame(self):
  60. return Flag.isSet(self._display_flags, Flag.NOFRAME)
  61. @property
  62. def opengl(self):
  63. return Flag.isSet(self._display_flags, Flag.OPENGL)
  64. @property
  65. def caption(self):
  66. if pygame.display.get_init():
  67. return pygame.display.get_caption()
  68. @caption.setter
  69. def caption(self, caption):
  70. if pygame.display.get_init():
  71. pygame.display.set_caption(caption)
  72. def toggle_fullscreen(self):
  73. if self._isFlagSet(Flag.FULLSCREEN):
  74. self._display_flags ^= Flag.FULLSCREEN
  75. else:
  76. self._display_flags |= Flag.FULLSCREEN
  77. self.set_mode(self._resolution, flags)
  78. def watch_for_resize(self, enable):
  79. if enable == True:
  80. Events.listen("VIDEORESIZE", self._OnVideoResize)
  81. elif enable == False:
  82. Events.unlisten("VIDEORESIZE", self._OnVideoResize)
  83. def set_mode(self, resolution, flags):
  84. if (self._init == False):
  85. self._init = True
  86. pygame.init()
  87. self._display_surface = pygame.display.set_mode(resolution, flags)
  88. self._display_flags = self._display_surface.get_flags()
  89. self._resolution = self._display_surface.get_size()
  90. return self
  91. def flip(self):
  92. if self._init:
  93. pygame.display.flip()
  94. def init(self, width=0, height=0):
  95. if self._init == False:
  96. self._init = True
  97. pygame.init()
  98. self.set_mode((width, height), self._display_flags)
  99. return self
  100. def close(self):
  101. pygame.quit()
  102. def _OnVideoResize(self, event, data):
  103. self.set_mode(data["size"], self.flags)
  104. print("Resized to {}".format(self.resolution))
  105. # Creating an instance of the _Display class. Really, this game engine is only going to use ONE display.
  106. Display=_Display()