-
Notifications
You must be signed in to change notification settings - Fork 0
/
textures.py
123 lines (94 loc) · 4.08 KB
/
textures.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pygame
import os
from settings import *
import time
class Texture:
window = pygame.display.set_mode((1, 1))
os.chdir(os.getcwd()+"/images")
supportedFormats = ".png"
path = os.getcwd()
path += "/"
native = {} # Keeps a copy of all the unscaled images
scaled = {} # Keeps track of all the scaled images, so it only loads them once
data = {} # Keeps track of all spritesheets tile width and height and animations fps
tick = time.time()
animationSpeed = 10
def __init__(self, resolution, name, position=0, add=True):
self.name = name
if self.name in COLORS:
self.color = True
else:
self.color = False
self.resolution = resolution
self.s_resolution = str(resolution)
self.position = position
if add:
self.image = self.__add()
def __call__(self):
return self.image
def rescale(self, resolution):
self.resolution = resolution
self.s_resolution = str(resolution)
self.image = self.__add()
def rename(self, name, position=0):
self.name = name
if self.name in COLORS:
self.color = True
else:
self.color = False
self.image = self.__add()
def __add(self):
# If the resolution doesn't exist add it to dict
if self.s_resolution not in self.scaled:
self.scaled[self.s_resolution] = {}
# If the name doesn't exist add an empty list
if self.name not in self.scaled[self.s_resolution]:
self.scaled[self.s_resolution][self.name] = [None]
# If the position you're looking for is outside existing ones, extend the list to fit it
while len(self.scaled[self.s_resolution][self.name]) < self.position+1:
self.scaled[self.s_resolution][self.name].append(None)
# If the texture doesn't exist in the scaled dict, we make it
if self.scaled[self.s_resolution][self.name][self.position] is None:
self.__addScaled(self.name, self.resolution, self.position, self.color)
return self.scaled[self.s_resolution][self.name][self.position]
def bulk(self, folder="images"):
# Loads everything in the textures folder
for filename in os.listdir(self.path+folder):
self.load(filename)
def load(self, filename):
if self.supportedFormats in filename:
# Sheet format <name_tilesize_sheet.png>
if "_sheet" in filename: self.loadSheet("s_", filename)
# Animation format <name_tilesize_anim.png>
if "_anim" in filename: self.loadSheet("a_", filename)
# Transparent image format <name_alpha.png>
elif "_alpha" in filename:
img = pygame.image.load(self.path + filename).convert_alpha()
self.__addNative(img, filename)
else:
img = pygame.image.load(self.path + filename).convert()
self.__addNative(img, filename)
def loadSheet(self, form, filename):
img = pygame.image.load(self.path + filename).convert_alpha()
name, sx, sy, useless = self.__getInfo(filename)
sx, sy = int(sx), int(sy)
w, h = img.get_rect()[2] // sx, img.get_rect()[3] // sy
for y in range(h):
for x in range(w):
image = img.subsurface((pygame.Rect(x * sx, y * sy, sx, sy)))
self.__addNative(image, form + name)
self.data[form + name] = [w, h, sx, sy]
def __addNative(self, img, filename):
try:
self.native[filename].append(img)
except KeyError:
self.native[filename] = [img]
def __addScaled(self, filename, resolution, position, color):
if color:
surface = pygame.Surface(resolution)
surface.fill(COLORS[filename])
self.scaled[str(resolution)][filename] = [surface]
else:
self.scaled[str(resolution)][filename][position] = pygame.transform.scale(self.native[filename][position], resolution)
def __getInfo(self, filename):
return filename.split("_")