Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.9KB

  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. class Flag:
  10. SWSURFACE = pygame.SWSURFACE
  11. HWSURFACE = pygame.HWSURFACE
  12. HWPALETTE = pygame.HWPALETTE
  13. DOUBLEBUF = pygame.DOUBLEBUF
  14. FULLSCREEN = pygame.FULLSCREEN
  15. OPENGL = pygame.OPENGL
  16. RESIZABLE = pygame.RESIZABLE
  17. NOFRAME = pygame.NOFRAME
  18. def isSet(flags, flag):
  19. return (flags & flag) == flag
  20. class _Display:
  21. def __init__(self, width=0, height=0):
  22. # 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.
  23. self._init = False
  24. self._resolution = (width, height)
  25. self._display_surface = None
  26. self._display_flags = Flag.RESIZABLE | Flag.HWSURFACE | Flag.DOUBLEBUF
  27. @property
  28. def surface(self):
  29. return self._display_surface
  30. @property
  31. def width(self):
  32. if self._display_surface is None:
  33. return 0
  34. return self._resolution[0]
  35. @property
  36. def height(self):
  37. if self._display_surface is None:
  38. return 0
  39. return self._resolution[1]
  40. @property
  41. def resolution(self):
  42. if self._display_surface is None:
  43. return (0,0)
  44. return self._display_surface.get_size()
  45. @property
  46. def flags(self):
  47. return self._display_flags
  48. @property
  49. def fullscreen(self):
  50. return Flag.isSet(self._display_flags, Flag.FULLSCREEN)
  51. @property
  52. def double_buffered(self):
  53. return Flag.isSet(self._display_flags, Flag.DOUBLEBUF)
  54. @property
  55. def resizable(self):
  56. return Flag.isSet(self._display_flags, Flag.RESIZABLE)
  57. @property
  58. def no_frame(self):
  59. return Flag.isSet(self._display_flags, Flag.NOFRAME)
  60. @property
  61. def opengl(self):
  62. return Flag.isSet(self._display_flags, Flag.OPENGL)
  63. def toggle_fullscreen(self):
  64. if self._isFlagSet(Flag.FULLSCREEN):
  65. self._display_flags ^= Flag.FULLSCREEN
  66. else:
  67. self._display_flags |= Flag.FULLSCREEN
  68. self.set_mode(self._resolution, flags)
  69. def set_mode(self, resolution, flags):
  70. if (self._init == False):
  71. self._init = True
  72. pygame.init()
  73. self._display_surface = pygame.display.set_mode(resolution, flags)
  74. self._display_flags = self._display_surface.get_flags()
  75. self._resolution = self._display_surface.get_size()
  76. return self
  77. def init(self):
  78. if self._init == False:
  79. self._init = True
  80. pygame.init()
  81. self.set_mode(self._resolution, self._display_flags)
  82. return self
  83. def close(self):
  84. pygame.quit()
  85. # Creating an instance of the _Display class. Really, this game engine is only going to use ONE display.
  86. Display=_Display()