|
- import os
- import pygame
-
-
- class LoadError(Exception):
- pass
-
- def calculate_real_path(path):
- path = os.path.expandvars(os.path.expanduser(path))
- path = os.path.realpath(path)
- path = os.path.abspath(path)
- return os.path.normcase(os.path.normpath(path))
- def join_path(lpath, rpath):
- return os.path.normcase(os.path.normpath(os.path.join(lpath, rpath)))
-
- def file_exists(path):
- return os.path.isfile(path)
-
-
-
- def load_image(filename, params={}):
- if not os.path.isfile(filename):
- raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
- with open(filename) as f:
- try:
- i = pygame.image.load(f, filename)
- return i.convert_alpha()
- except pygame.error as e:
- raise LoadError("Pygame/SDL Exception: {}".format(e.message))
-
-
- def load_audio(filename, params={}):
- if not os.path.isfile(filename):
- raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
- try:
- if pygame.mixer.get_init() is not None:
- return pygame.mixer.Sound(filename)
- except pygame.error as e:
- raise LoadError("Pygame Exception: {}".format(e.message))
- raise LoadError("Audio subsystem not initialized before attempting to obtain resource.")
-
- def load_font(filename, params={}):
- if not os.path.isfile(filename):
- raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
- try:
- if pygame.font.get_init():
- size = 26
- if "size" in params:
- if isinstance(params["size"], int) and params["size"] > 0:
- size = params["size"]
- return pygame.font.Font(filename, size)
- except pygame.error as e:
- raise LoadError("Pygame Exception: {}".format(e.message))
- raise LoadError("Font subsystem not initialized before attempting to obtain resource.")
-
-
- def load_JSON(filename, params={}):
- if not os.path.isfile(filename):
- raise LoaderError("File '{}' is missing or not a file.".format(filename))
- data = None
- try:
- with open(filename) as f:
- data = json.load(f)
- return data
- except Exception as e:
- raise e
-
|