Legend of the Gold Box... A game written for the LOWREZJAM 2018 game jam
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.1KB

  1. import os
  2. import pygame
  3. class LoadError(Exception):
  4. pass
  5. def calculate_real_path(path):
  6. path = os.path.expandvars(os.path.expanduser(path))
  7. path = os.path.realpath(path)
  8. path = os.path.abspath(path)
  9. return os.path.normcase(os.path.normpath(path))
  10. def join_path(lpath, rpath):
  11. return os.path.normcase(os.path.normpath(os.path.join(lpath, rpath)))
  12. def file_exists(path):
  13. return os.path.isfile(path)
  14. def load_image(filename, params={}):
  15. if not os.path.isfile(filename):
  16. raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
  17. try:
  18. i = pygame.image.load(filename)
  19. return i.convert_alpha()
  20. except pygame.error as e:
  21. raise LoadError("Pygame/SDL Exception: {}".format(e))
  22. def load_audio(filename, params={}):
  23. if not os.path.isfile(filename):
  24. raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
  25. try:
  26. if pygame.mixer.get_init() is not None:
  27. return pygame.mixer.Sound(filename)
  28. except pygame.error as e:
  29. raise LoadError("Pygame Exception: {}".format(e))
  30. raise LoadError("Audio subsystem not initialized before attempting to obtain resource.")
  31. def load_font(filename, params={}):
  32. if not os.path.isfile(filename):
  33. raise LoadError("Failed to load '{}'. Path missing or invalid.".format(filename))
  34. try:
  35. if pygame.font.get_init():
  36. size = 26
  37. if "size" in params:
  38. if isinstance(params["size"], int) and params["size"] > 0:
  39. size = params["size"]
  40. return pygame.font.Font(filename, size)
  41. except pygame.error as e:
  42. raise LoadError("Pygame Exception: {}".format(e))
  43. raise LoadError("Font subsystem not initialized before attempting to obtain resource.")
  44. def load_JSON(filename, params={}):
  45. if not os.path.isfile(filename):
  46. raise LoaderError("File '{}' is missing or not a file.".format(filename))
  47. data = None
  48. try:
  49. with open(filename) as f:
  50. data = json.load(f)
  51. return data
  52. except Exception as e:
  53. raise e