-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimage_manager.py
57 lines (48 loc) · 1.48 KB
/
image_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import pygame
class ImageManager:
"""
Static class to handle loading of pygame surfaces to improve performance
"""
initialized = False
sounds = None
@staticmethod
def init():
ImageManager.initialized = True
ImageManager.sounds = {}
@staticmethod
def check_initialized():
if not ImageManager.initialized:
raise Exception("Must call ImageHandler.init() before any other methods.")
@staticmethod
def clear(path):
"""
Forgets one thing.
:param path: The path of the file to remove from memory
:return:
"""
ImageManager.check_initialized()
if path in ImageManager.sounds:
del ImageManager.sounds[path]
@staticmethod
def clear_all():
"""
Forgets everything
"""
ImageManager.check_initialized()
ImageManager.sounds = {}
@staticmethod
def load(path):
"""
Loads a surface from file or from cache
:param path: The path of the image
:return: The surface. This is likely the same reference others are using, so don't be destructive.
"""
ImageManager.check_initialized()
if path in ImageManager.sounds:
return ImageManager.sounds[path]
sound = pygame.image.load(path).convert_alpha()
ImageManager.sounds[path] = sound
return sound
@staticmethod
def load_copy(path):
return ImageManager.load(path).copy()