summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Bernard <public.jbernard@web.de>2021-04-24 01:45:27 +0200
committerJonas Bernard <public.jbernard@web.de>2021-04-24 01:45:27 +0200
commitd4f6d3919e992ed9bb8d43833c694e431c9735c9 (patch)
tree39691ee36a7039dc3f0381013ef7b7bce9069b65
parentfd73d0ad2c3b54850baf3c467954c92bd40ed010 (diff)
downloadtxtgameengine-d4f6d3919e992ed9bb8d43833c694e431c9735c9.tar.gz
txtgameengine-d4f6d3919e992ed9bb8d43833c694e431c9735c9.tar.bz2
txtgameengine-d4f6d3919e992ed9bb8d43833c694e431c9735c9.zip
Texture
-rw-r--r--txtgameengine/__main__.py4
-rw-r--r--txtgameengine/platform.py10
-rw-r--r--txtgameengine/res/textures/test_image.png (renamed from test_image.png)bin3133555 -> 3133555 bytes
-rw-r--r--txtgameengine/scenes.py1
-rw-r--r--txtgameengine/twod/textures.py20
5 files changed, 26 insertions, 9 deletions
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/test_image.png b/txtgameengine/res/textures/test_image.png
index 6264a7a..6264a7a 100644
--- a/test_image.png
+++ b/txtgameengine/res/textures/test_image.png
Binary files 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