-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_manager.py
62 lines (53 loc) · 1.72 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
58
59
60
61
62
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, scale_by=1):
"""
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()
path_key = path + str(scale_by)
if path_key in ImageManager.sounds:
return ImageManager.sounds[path_key]
sound = pygame.image.load(path).convert_alpha()
if scale_by != 1:
w = int(sound.get_width()*scale_by)
h = int(sound.get_height()*scale_by)
sound = pygame.transform.scale(sound, (w, h))
ImageManager.sounds[path_key] = sound
return sound
@staticmethod
def load_copy(path):
return ImageManager.load(path).copy()