Skip to content

Commit

Permalink
module initialization done... for now
Browse files Browse the repository at this point in the history
  • Loading branch information
Rizziepit committed Mar 10, 2014
1 parent f0c5500 commit db75580
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
24 changes: 20 additions & 4 deletions pygame/base.py
Expand Up @@ -13,6 +13,7 @@

# TODO: not sure whether it should be True or False
HAVE_NEWBUF = False

_sdl_was_init = False
_quit_functions = []

Expand Down Expand Up @@ -46,7 +47,18 @@ def init():
""" init() -> (numpass, numfail)
initialize all imported pygame modules
"""
# TODO: CheckSDLVersions()
# check that the SDL version is supported
# TODO: preserve backwards compatibility in mixer, RWops, etc
major, minor, patch = get_sdl_version()
try:
assert major == 1
assert minor == 2
assert patch >= 9
except AssertionError:
raise RuntimeError("Current version of SDL is %i.%i.%i. Only SDL "
"versions >= 1.2.9, < 2.0.0 are supported." %
(major, minor, patch))

global _sdl_was_init
if not platform.system().startswith('Windows') and _with_thread:
_sdl_was_init = sdl.SDL_Init(sdl.SDL_INIT_TIMER |
Expand All @@ -64,8 +76,11 @@ def init():
success += 1
else:
fail += 1
# pygame inspects sys.modules but checks for
# __PYGAMEinit__ attr instead of init

# pygame inspects sys.modules and finds all __PYGAMEinit__ functions.
# We look for autoinit and only consider submodules of pygame.
# pygame normally initializes 6 modules.
# We are at 4 modules: cdrom and joystick are missing
modules = [v for k, v in sys.modules.iteritems() if k.startswith('pygame.')
and v is not None and v != sys.modules[__name__]]
for module in modules:
Expand All @@ -83,9 +98,10 @@ def quit():
""" quit() -> None
uninitialize all pygame modules
"""
# TODO: this is a pale shadow of the madness inside pygame.base.quit()
# quit in reverse order of initialization
for quit_func in reversed(_quit_functions):
quit_func()
video_autoquit()

global _sdl_was_init
if _sdl_was_init:
Expand Down
31 changes: 21 additions & 10 deletions pygame/font.py
Expand Up @@ -4,6 +4,7 @@

from pygame._sdl import sdl, ffi
from pygame._error import SDLError
from pygame.base import register_quit
from pygame.color import Color
from pygame.pkgdata import getResource
from pygame.surface import Surface
Expand Down Expand Up @@ -33,15 +34,28 @@ def utf_8_needs_UCS_4(text):
return False


def autoinit():
global _font_initialised
if not _font_initialised:
register_quit(autoquit)
if sdl.TTF_Init():
return False
_font_initialised = 1
return bool(_font_initialised)


def autoquit():
global _font_initialised
if _font_initialised:
_font_initialised = 0
sdl.TTF_Quit()


def init():
"""pygame.font.init(): return None
initialize the font module"""
global _font_initialised
if _font_initialised == 0:
res = sdl.TTF_Init()
if res == -1:
raise SDLError.from_sdl_error()
_font_initialised += 1
if not autoinit():
raise SDLError.from_sdl_error()


def get_init():
Expand All @@ -54,10 +68,7 @@ def get_init():
def quit():
"""pygame.font.quit(): return None
uninitialize the font module"""
global _font_initialised
if _font_initialised:
sdl.TTF_Quit()
_font_initialised = 0
autoquit()


def check_font():
Expand Down

0 comments on commit db75580

Please sign in to comment.