Browse Source

Parameters can now be sent to the associated loader using a param dictionary. Loaders should now accept a second argument that will contain a parameter dictionary. Added a font resource loader

master
Bryan Miller 6 years ago
parent
commit
9cf058c1df
2 changed files with 17 additions and 5 deletions
  1. +2
    -2
      game/gbe/resource.py
  2. +15
    -3
      game/gbe/resourceLoaders.py

+ 2
- 2
game/gbe/resource.py View File

d["instance"] = None d["instance"] = None
return self return self


def get(self, rtype, src):
def get(self, rtype, src, params={}):
global _RESOURCES global _RESOURCES
if rtype not in _RESOURCES: if rtype not in _RESOURCES:
raise ResourceError("Unknown resource type '{}'.".format(rtype)) raise ResourceError("Unknown resource type '{}'.".format(rtype))
loader = _RESOURCES[rtype]["loader"] loader = _RESOURCES[rtype]["loader"]
filename = join_path(_RESOURCES[rtype]["path"], src) filename = join_path(_RESOURCES[rtype]["path"], src)
try: try:
d["instance"] = loader(filename)
d["instance"] = loader(filename, params)
except Exception as e: except Exception as e:
_l.error(e.message) _l.error(e.message)
return None return None

+ 15
- 3
game/gbe/resourceLoaders.py View File







def load_image(filename):
def load_image(filename, params={}):
if not os.path.isfile(filename): if not os.path.isfile(filename):
raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename)) raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
with open(filename) as f: with open(filename) as f:
raise LoadError("Pygame/SDL Exception: {}".format(e.message)) raise LoadError("Pygame/SDL Exception: {}".format(e.message))




def load_audio(filename):
def load_audio(filename, params={}):
if not os.path.isfile(filename): if not os.path.isfile(filename):
raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename)) raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
try: try:
except pygame.error as e: except pygame.error as e:
raise LoadError("Pygame Exception: {}".format(e.message)) raise LoadError("Pygame Exception: {}".format(e.message))


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))




def load_JSON(filename):
def load_JSON(filename, params={}):
if not os.path.isfile(filename): if not os.path.isfile(filename):
raise LoaderError("File '{}' is missing or not a file.".format(filename)) raise LoaderError("File '{}' is missing or not a file.".format(filename))
data = None data = None

Loading…
Cancel
Save