From d4f6d3919e992ed9bb8d43833c694e431c9735c9 Mon Sep 17 00:00:00 2001 From: Jonas Bernard Date: Sat, 24 Apr 2021 01:45:27 +0200 Subject: Texture --- test_image.png | Bin 3133555 -> 0 bytes txtgameengine/__main__.py | 4 ++-- txtgameengine/platform.py | 10 +++++----- txtgameengine/res/textures/test_image.png | Bin 0 -> 3133555 bytes txtgameengine/scenes.py | 1 + txtgameengine/twod/textures.py | 20 ++++++++++++++++++-- 6 files changed, 26 insertions(+), 9 deletions(-) delete mode 100644 test_image.png create mode 100644 txtgameengine/res/textures/test_image.png diff --git a/test_image.png b/test_image.png deleted file mode 100644 index 6264a7a..0000000 Binary files a/test_image.png and /dev/null differ diff --git a/txtgameengine/__main__.py b/txtgameengine/__main__.py index 9aeeec3..b66d9fe 100644 --- a/txtgameengine/__main__.py +++ b/txtgameengine/__main__.py @@ -1,9 +1,9 @@ import numpy as np -from PIL import Image from .scenes import SceneTxtGameApp, Scene from pathlib import Path from .shaders import TextureShader +from .twod.textures import Texture shader_path = Path(__file__).parent / 'builtin_shaders' @@ -47,7 +47,7 @@ class EvilTriangleScene(TriangleScene): class TextureScene(Scene): def on_enter(self): self.texture_shaders = TextureShader(self.app) - self.texture = self.app.render.setup_texture_from_pil(Image.open('test_image.png')) + self.texture = Texture(self.app, 'test_image.png') self.triangle = self.app.render.setup_buffer( np.array([ -1.0, 1.0, diff --git a/txtgameengine/platform.py b/txtgameengine/platform.py index f4b3b5b..de539d2 100644 --- a/txtgameengine/platform.py +++ b/txtgameengine/platform.py @@ -4,7 +4,6 @@ import typing import numpy as np from OpenGL.GL import * import OpenGL.GL.shaders as shaders -from PIL import Image from .twod import Texture if typing.TYPE_CHECKING: @@ -144,9 +143,6 @@ class RenderComponent: self.triangle(triangle) glDisableVertexAttribArray(1) - def setup_texture_from_pil(self, img: Image.Image): - return self.setup_texture(*img.size, np.array(list(img.getdata()), np.uint8)) - def setup_texture(self, width: int, height: int, data: np.ndarray): tex_id = glGenTextures(1) glPixelStorei(GL_UNPACK_ALIGNMENT, 4) @@ -154,4 +150,8 @@ class RenderComponent: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data) - return Texture(self.app, tex_id) + return tex_id + + def free_texture(self, gl_texid: int): + glBindTexture(gl_texid, 0) # unbind the texture + glDeleteTextures(1, gl_texid) # actually delete it diff --git a/txtgameengine/res/textures/test_image.png b/txtgameengine/res/textures/test_image.png new file mode 100644 index 0000000..6264a7a Binary files /dev/null and b/txtgameengine/res/textures/test_image.png differ diff --git a/txtgameengine/scenes.py b/txtgameengine/scenes.py index 58d7927..de5579c 100644 --- a/txtgameengine/scenes.py +++ b/txtgameengine/scenes.py @@ -31,6 +31,7 @@ class SceneTxtGameApp(TxtGameApp): print("Scene Stack:", ' > '.join(type(x).__name__ for x in self.scene_stack)) self.scene_stack[-1].update(delta) + class Scene: def __init__(self, app: 'SceneTxtGameApp'): self.app = app diff --git a/txtgameengine/twod/textures.py b/txtgameengine/twod/textures.py index 01dde52..0451e5c 100644 --- a/txtgameengine/twod/textures.py +++ b/txtgameengine/twod/textures.py @@ -1,11 +1,27 @@ import typing +from PIL import Image +import numpy as np + +TEXTURE_FOLDER = 'txtgameengine/res/textures/' if typing.TYPE_CHECKING: from ..app import TxtGameApp class Texture: - def __init__(self, app: 'TxtGameApp', gl_texid: int): + def __init__(self, app: 'TxtGameApp', path: str): self.app = app - self.gl_texid = gl_texid + self.path = path + self._load_from_disk() + self._bind_to_gl() + + def _load_from_disk(self): + self.image = Image.open(TEXTURE_FOLDER + self.path) + self.width, self.height = self.image.size + + def _bind_to_gl(self): + imagedata = np.array(list(self.image.getdata()), np.uint8) + self.gl_texid = self.app.render.setup_texture(self.width, self.height, imagedata) + def free(self): + self.app.render.free_texture(self.gl_texid) \ No newline at end of file -- cgit