__init__.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. # coding: ascii
  2. # pygame - Python Game Library
  3. # Copyright (C) 2000-2001 Pete Shinners
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Library General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public
  16. # License along with this library; if not, write to the Free
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. #
  19. # Pete Shinners
  20. # pete@shinners.org
  21. """Pygame is a set of Python modules designed for writing games.
  22. It is written on top of the excellent SDL library. This allows you
  23. to create fully featured games and multimedia programs in the python
  24. language. The package is highly portable, with games running on
  25. Windows, MacOS, OS X, BeOS, FreeBSD, IRIX, and Linux."""
  26. import sys
  27. import os
  28. # Choose Windows display driver
  29. if os.name == 'nt':
  30. #pypy does not find the dlls, so we add package folder to PATH.
  31. pygame_dir = os.path.split(__file__)[0]
  32. os.environ['PATH'] = os.environ['PATH'] + ';' + pygame_dir
  33. # Respect existing SDL_VIDEODRIVER setting if it has been set
  34. if 'SDL_VIDEODRIVER' not in os.environ:
  35. # If the Windows version is 95/98/ME and DirectX 5 or greater is
  36. # installed, then use the directx driver rather than the default
  37. # windib driver.
  38. # http://docs.python.org/lib/module-sys.html
  39. # 0 (VER_PLATFORM_WIN32s) Win32s on Windows 3.1
  40. # 1 (VER_PLATFORM_WIN32_WINDOWS) Windows 95/98/ME
  41. # 2 (VER_PLATFORM_WIN32_NT) Windows NT/2000/XP
  42. # 3 (VER_PLATFORM_WIN32_CE) Windows CE
  43. if sys.getwindowsversion()[0] == 1:
  44. import _winreg
  45. try:
  46. # Get DirectX version from registry
  47. key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE,
  48. 'SOFTWARE\\Microsoft\\DirectX')
  49. dx_version_string = _winreg.QueryValueEx(key, 'Version')
  50. key.Close()
  51. # Set video driver to directx if DirectX 5 or better is
  52. # installed.
  53. # To interpret DirectX version numbers, see this page:
  54. # http://en.wikipedia.org/wiki/DirectX#Releases
  55. minor_dx_version = int(dx_version_string.split('.')[1])
  56. if minor_dx_version >= 5:
  57. os.environ['SDL_VIDEODRIVER'] = 'directx'
  58. # Clean up namespace
  59. del key, dx_version_string, minor_dx_version
  60. except:
  61. pass
  62. # Clean up namespace
  63. del _winreg
  64. # when running under X11, always set the SDL window WM_CLASS to make the
  65. # window managers correctly match the pygame window.
  66. elif 'DISPLAY' in os.environ and 'SDL_VIDEO_X11_WMCLASS' not in os.environ:
  67. os.environ['SDL_VIDEO_X11_WMCLASS'] = os.path.basename(sys.argv[0])
  68. class MissingModule:
  69. _NOT_IMPLEMENTED_ = True
  70. def __init__(self, name, urgent=0):
  71. self.name = name
  72. exc_type, exc_msg = sys.exc_info()[:2]
  73. self.info = str(exc_msg)
  74. self.reason = "%s: %s" % (exc_type.__name__, self.info)
  75. self.urgent = urgent
  76. if urgent:
  77. self.warn()
  78. def __getattr__(self, var):
  79. if not self.urgent:
  80. self.warn()
  81. self.urgent = 1
  82. missing_msg = "%s module not available (%s)" % (self.name, self.reason)
  83. raise NotImplementedError(missing_msg)
  84. def __nonzero__(self):
  85. return 0
  86. def warn(self):
  87. msg_type = 'import' if self.urgent else 'use'
  88. message = '%s %s: %s\n(%s)' % (msg_type, self.name, self.info, self.reason)
  89. try:
  90. import warnings
  91. level = 4 if self.urgent else 3
  92. warnings.warn(message, RuntimeWarning, level)
  93. except ImportError:
  94. print (message)
  95. # we need to import like this, each at a time. the cleanest way to import
  96. # our modules is with the import command (not the __import__ function)
  97. # first, the "required" modules
  98. from pygame.base import *
  99. from pygame.constants import *
  100. from pygame.version import *
  101. from pygame.rect import Rect
  102. from pygame.compat import PY_MAJOR_VERSION
  103. from pygame.rwobject import encode_string, encode_file_path
  104. import pygame.surflock
  105. import pygame.color
  106. Color = color.Color
  107. import pygame.bufferproxy
  108. BufferProxy = bufferproxy.BufferProxy
  109. import pygame.math
  110. Vector2 = pygame.math.Vector2
  111. Vector3 = pygame.math.Vector3
  112. __version__ = ver
  113. # next, the "standard" modules
  114. # we still allow them to be missing for stripped down pygame distributions
  115. if get_sdl_version() < (2, 0, 0):
  116. # cdrom only available for SDL 1.2.X
  117. try:
  118. import pygame.cdrom
  119. except (ImportError, IOError):
  120. cdrom = MissingModule("cdrom", urgent=1)
  121. try:
  122. import pygame.cursors
  123. except (ImportError, IOError):
  124. cursors = MissingModule("cursors", urgent=1)
  125. try:
  126. import pygame.display
  127. except (ImportError, IOError):
  128. display = MissingModule("display", urgent=1)
  129. try:
  130. import pygame.draw
  131. except (ImportError, IOError):
  132. draw = MissingModule("draw", urgent=1)
  133. try:
  134. import pygame.event
  135. except (ImportError, IOError):
  136. event = MissingModule("event", urgent=1)
  137. try:
  138. import pygame.image
  139. except (ImportError, IOError):
  140. image = MissingModule("image", urgent=1)
  141. try:
  142. import pygame.joystick
  143. except (ImportError, IOError):
  144. joystick = MissingModule("joystick", urgent=1)
  145. try:
  146. import pygame.key
  147. except (ImportError, IOError):
  148. key = MissingModule("key", urgent=1)
  149. try:
  150. import pygame.mouse
  151. except (ImportError, IOError):
  152. mouse = MissingModule("mouse", urgent=1)
  153. try:
  154. import pygame.sprite
  155. except (ImportError, IOError):
  156. sprite = MissingModule("sprite", urgent=1)
  157. try:
  158. import pygame.threads
  159. except (ImportError, IOError):
  160. threads = MissingModule("threads", urgent=1)
  161. try:
  162. import pygame.pixelcopy
  163. except (ImportError, IOError):
  164. pixelcopy = MissingModule("pixelcopy", urgent=1)
  165. def warn_unwanted_files():
  166. """warn about unneeded old files"""
  167. # a temporary hack to warn about camera.so and camera.pyd.
  168. install_path = os.path.split(pygame.base.__file__)[0]
  169. extension_ext = os.path.splitext(pygame.base.__file__)[1]
  170. # here are the .so/.pyd files we need to ask to remove.
  171. ext_to_remove = ["camera"]
  172. # here are the .py/.pyo/.pyc files we need to ask to remove.
  173. py_to_remove = ["color"]
  174. # Don't warn on Symbian. The color.py is used as a wrapper.
  175. if os.name == "e32":
  176. py_to_remove = []
  177. # See if any of the files are there.
  178. extension_files = ["%s%s" % (x, extension_ext) for x in ext_to_remove]
  179. py_files = ["%s%s" % (x, py_ext)
  180. for py_ext in [".py", ".pyc", ".pyo"]
  181. for x in py_to_remove]
  182. files = py_files + extension_files
  183. unwanted_files = []
  184. for f in files:
  185. unwanted_files.append(os.path.join(install_path, f))
  186. ask_remove = []
  187. for f in unwanted_files:
  188. if os.path.exists(f):
  189. ask_remove.append(f)
  190. if ask_remove:
  191. message = "Detected old file(s). Please remove the old files:\n"
  192. for f in ask_remove:
  193. message += "%s " % f
  194. message += "\nLeaving them there might break pygame. Cheers!\n\n"
  195. try:
  196. import warnings
  197. level = 4
  198. warnings.warn(message, RuntimeWarning, level)
  199. except ImportError:
  200. print (message)
  201. # disable, because we hopefully don't need it.
  202. # warn_unwanted_files()
  203. try:
  204. from pygame.surface import *
  205. except (ImportError, IOError):
  206. Surface = lambda: Missing_Function
  207. try:
  208. import pygame.mask
  209. from pygame.mask import Mask
  210. except (ImportError, IOError):
  211. Mask = lambda: Missing_Function
  212. try:
  213. from pygame.pixelarray import *
  214. except (ImportError, IOError):
  215. PixelArray = lambda: Missing_Function
  216. try:
  217. from pygame.overlay import *
  218. except (ImportError, IOError):
  219. Overlay = lambda: Missing_Function
  220. try:
  221. import pygame.time
  222. except (ImportError, IOError):
  223. time = MissingModule("time", urgent=1)
  224. try:
  225. import pygame.transform
  226. except (ImportError, IOError):
  227. transform = MissingModule("transform", urgent=1)
  228. # lastly, the "optional" pygame modules
  229. if 'PYGAME_FREETYPE' in os.environ:
  230. try:
  231. import pygame.ftfont as font
  232. sys.modules['pygame.font'] = font
  233. except (ImportError, IOError):
  234. pass
  235. try:
  236. import pygame.font
  237. import pygame.sysfont
  238. pygame.font.SysFont = pygame.sysfont.SysFont
  239. pygame.font.get_fonts = pygame.sysfont.get_fonts
  240. pygame.font.match_font = pygame.sysfont.match_font
  241. except (ImportError, IOError):
  242. font = MissingModule("font", urgent=0)
  243. # try and load pygame.mixer_music before mixer, for py2app...
  244. try:
  245. import pygame.mixer_music
  246. #del pygame.mixer_music
  247. #print ("NOTE2: failed importing pygame.mixer_music in lib/__init__.py")
  248. except (ImportError, IOError):
  249. pass
  250. try:
  251. import pygame.mixer
  252. except (ImportError, IOError):
  253. mixer = MissingModule("mixer", urgent=0)
  254. try:
  255. import pygame.movie
  256. except (ImportError, IOError):
  257. movie = MissingModule("movie", urgent=0)
  258. # try:
  259. # import pygame.movieext
  260. # except (ImportError,IOError):
  261. # movieext=MissingModule("movieext", urgent=0)
  262. try:
  263. import pygame.scrap
  264. except (ImportError, IOError):
  265. scrap = MissingModule("scrap", urgent=0)
  266. try:
  267. import pygame.surfarray
  268. except (ImportError, IOError):
  269. surfarray = MissingModule("surfarray", urgent=0)
  270. try:
  271. import pygame.sndarray
  272. except (ImportError, IOError):
  273. sndarray = MissingModule("sndarray", urgent=0)
  274. try:
  275. import pygame.fastevent
  276. except (ImportError, IOError):
  277. fastevent = MissingModule("fastevent", urgent=0)
  278. # there's also a couple "internal" modules not needed
  279. # by users, but putting them here helps "dependency finder"
  280. # programs get everything they need (like py2exe)
  281. try:
  282. import pygame.imageext
  283. del pygame.imageext
  284. except (ImportError, IOError):
  285. pass
  286. def packager_imports():
  287. """some additional imports that py2app/py2exe will want to see"""
  288. import atexit
  289. import numpy
  290. import OpenGL.GL
  291. import pygame.macosx
  292. import pygame.bufferproxy
  293. import pygame.colordict
  294. import pygame._view
  295. # make Rects pickleable
  296. if PY_MAJOR_VERSION >= 3:
  297. import copyreg as copy_reg
  298. else:
  299. import copy_reg
  300. def __rect_constructor(x, y, w, h):
  301. return Rect(x, y, w, h)
  302. def __rect_reduce(r):
  303. assert type(r) == Rect
  304. return __rect_constructor, (r.x, r.y, r.w, r.h)
  305. copy_reg.pickle(Rect, __rect_reduce, __rect_constructor)
  306. # make Colors pickleable
  307. def __color_constructor(r, g, b, a):
  308. return Color(r, g, b, a)
  309. def __color_reduce(c):
  310. assert type(c) == Color
  311. return __color_constructor, (c.r, c.g, c.b, c.a)
  312. copy_reg.pickle(Color, __color_reduce, __color_constructor)
  313. # Thanks for supporting pygame. Without support now, there won't be pygame later.
  314. if 'PYGAME_HIDE_SUPPORT_PROMPT' not in os.environ:
  315. print('pygame %s' % ver)
  316. print('Hello from the pygame community. https://www.pygame.org/contribute.html')
  317. # cleanup namespace
  318. del pygame, os, sys, surflock, MissingModule, copy_reg, PY_MAJOR_VERSION