1# Copyright 2014 Tom Rothamel <tom@rothamel.us> 2# Copyright 2014 Patrick Dawson <pat@dw.is> 3# 4# This software is provided 'as-is', without any express or implied 5# warranty. In no event will the authors be held liable for any damages 6# arising from the use of this software. 7# 8# Permission is granted to anyone to use this software for any purpose, 9# including commercial applications, and to alter it and redistribute it 10# freely, subject to the following restrictions: 11# 12# 1. The origin of this software must not be misrepresented; you must not 13# claim that you wrote the original software. If you use this software 14# in a product, an acknowledgment in the product documentation would be 15# appreciated but is not required. 16# 2. Altered source versions must be plainly marked as such, and must not be 17# misrepresented as being the original software. 18# 3. This notice may not be removed or altered from any source distribution. 19 20from __future__ import division, print_function, absolute_import 21 22import sys 23import importlib 24 25 26class MissingModule(object): 27 28 def __init__(self, name, reason): 29 self.__name__ = name 30 self.reason = reason 31 32 def __getattr__(self, attr): 33 raise NotImplementedError(self.reason) 34 35 36def try_import(name): 37 full_name = "pygame_sdl2." + name 38 39 try: 40 module = importlib.import_module(full_name) 41 except (IOError, ImportError) as e: 42 module = MissingModule(full_name, "Could not import {}: {}".format(full_name, str(e))) 43 44 globals()[name] = module 45 sys.modules[full_name] = module 46 47 48# Lists of functions that are called on init and quit. 49init_functions = [ ] 50quit_functions = [ ] 51 52 53def register_init(fn): 54 init_functions.append(fn) 55 return fn 56 57 58def register_quit(fn): 59 quit_functions.append(fn) 60 return fn 61 62 63def init(): 64 numpass = 0 65 numfail = 0 66 67 for i in init_functions: 68 try: 69 i() 70 numpass += 1 71 except: 72 numfail += 1 73 74 return numpass, numfail 75 76 77def quit(): # @ReservedAssignment 78 for i in quit_functions: 79 try: 80 i() 81 except: 82 pass 83 84 85# Import core modules. 86from pygame_sdl2.error import * 87 88from pygame_sdl2.surface import Surface 89from pygame_sdl2.rect import Rect 90 91import pygame_sdl2.color 92import pygame_sdl2.display 93import pygame_sdl2.event 94import pygame_sdl2.key 95import pygame_sdl2.locals # @ReservedAssignment 96import pygame_sdl2.time 97import pygame_sdl2.version 98import pygame_sdl2.locals as constants 99 100# Import optional modules. 101try_import("controller") 102try_import("draw") 103try_import("font") 104try_import("image") 105try_import("joystick") 106try_import("mixer") 107try_import("mouse") 108try_import("power") 109try_import("transform") 110try_import("scrap") 111try_import("sprite") 112try_import("sysfont") 113 114# Optional imports should be included in this function, so they show up 115# in packaging tools (like py2exe). 116 117 118def _optional_imports(): 119 import pygame_sdl2.compat 120 import pygame_sdl2.controller 121 import pygame_sdl2.rwobject 122 import pygame_sdl2.gfxdraw 123 import pygame_sdl2.draw 124 import pygame_sdl2.font 125 import pygame_sdl2.image 126 import pygame_sdl2.joystick 127 import pygame_sdl2.mixer 128 import pygame_sdl2.mouse 129 import pygame_sdl2.power 130 import pygame_sdl2.transform 131 import pygame_sdl2.scrap 132 import pygame_sdl2.sprite 133 import pygame_sdl2.sysfont 134 135 136# Fill this module with locals. 137from pygame_sdl2.locals import * 138 139 140def import_as_pygame(): 141 """ 142 Imports pygame_sdl2 as pygame, so that running the 'import pygame.whatever' 143 statement will import pygame_sdl2.whatever instead. 144 """ 145 146 import os 147 import warnings 148 149 if "PYGAME_SDL2_USE_PYGAME" in os.environ: 150 return 151 152 if "pygame" in sys.modules: 153 warnings.warn("Pygame has already been imported, import_as_pygame may not work.", stacklevel=2) 154 155 for name, mod in list(sys.modules.items()): 156 name = name.split('.') 157 if name[0] != 'pygame_sdl2': 158 continue 159 160 name[0] = 'pygame' 161 name = ".".join(name) 162 163 sys.modules[name] = mod 164 165 sys.modules['pygame.constants'] = constants 166 167 168def get_sdl_byteorder(): 169 return BYTEORDER 170 171 172def get_sdl_version(): 173 return SDL_VERSION_TUPLE 174 175 176get_platform = pygame_sdl2.display.get_platform 177 178# We have new-style buffers, but not the pygame.newbuffer module. 179HAVE_NEWBUF = False 180