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
|
import os
import typing
from PIL import Image
import numpy as np
from ..app import builtin_resource_path
TEXTURE_FOLDER = builtin_resource_path / 'textures'
if typing.TYPE_CHECKING:
from ..app import TxtGameApp
class Texture:
def __init__(self, app: 'TxtGameApp', load: typing.Union[os.PathLike, Image.Image]):
self.app = app
self.load = load
self._bind_to_gl()
def _bind_to_gl(self):
if isinstance(self.load, Image.Image):
image = self.load
else:
image = Image.open(str(self.load))
self.width, self.height = image.size
image = image.convert('RGBA')
imagedata = np.array(list(image.getdata()), np.uint8)
self.gl_texid = self.app.render.setup_texture(self.width, self.height, imagedata)
image.close()
@property
def size(self):
return self.width, self.height
def uvs_from_pixels(self, x: int, y: int) -> typing.Tuple[float, float]:
return self.app.coords.from_pixels_to_uvs(self.size, x, y)
def free(self):
self.app.render.free_texture(self.gl_texid)
|