summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--txtgameengine/__main__.py6
-rw-r--r--txtgameengine/app.py7
-rw-r--r--txtgameengine/builtin_res/textures/test_image.png (renamed from txtgameengine/res/textures/test_image.png)bin3133555 -> 3133555 bytes
-rw-r--r--txtgameengine/platform.py39
-rw-r--r--txtgameengine/twod/textures.py2
5 files changed, 40 insertions, 14 deletions
diff --git a/txtgameengine/__main__.py b/txtgameengine/__main__.py
index edfbd29..a56571e 100644
--- a/txtgameengine/__main__.py
+++ b/txtgameengine/__main__.py
@@ -3,7 +3,7 @@ import numpy as np
from .scenes import SceneTxtGameApp, Scene
from pathlib import Path
from .shaders import TextureShader
-from .twod.textures import Texture
+from .twod.textures import Texture, TEXTURE_FOLDER
shader_path = Path(__file__).parent / 'shaders'
@@ -47,7 +47,7 @@ class EvilTriangleScene(TriangleScene):
class TextureScene(Scene):
def on_enter(self):
self.texture_shaders = TextureShader(self.app)
- self.texture = Texture(self.app, 'test_image.png')
+ self.texture = Texture(self.app, TEXTURE_FOLDER / 'test_image.png')
self.triangle = self.app.render.setup_buffer(
np.array([
-1.0, 1.0,
@@ -62,6 +62,8 @@ class TextureScene(Scene):
], np.float32))
def update(self, delta: float):
+ print(self.app.coords.from_pixels_to_screen(0, 0))
+ print(self.app.coords.from_screen_to_pixels(0, 0))
with self.texture_shaders:
self.app.render.textured_triangle(self.texture_shaders.textureSampler, self.texture, self.triangle,
self.uvs)
diff --git a/txtgameengine/app.py b/txtgameengine/app.py
index 58b239f..8206ef4 100644
--- a/txtgameengine/app.py
+++ b/txtgameengine/app.py
@@ -1,16 +1,16 @@
-import time
from pathlib import Path
-from .platform import PlatformComponent, RenderComponent, ShaderComponent
-
EPSILON = 1.e-10
builtin_resource_path = Path(__file__).parent / 'builtin_res'
+from .platform import PlatformComponent, RenderComponent, ShaderComponent, CoordinateComponent
+
class TxtGameApp:
PLATFORM_CLASS = PlatformComponent
RENDER_CLASS = RenderComponent
SHADER_CLASS = ShaderComponent
+ COORDINATE_CLASS = CoordinateComponent
def __init__(self, size: (int, int), name: str):
self.size = size
@@ -19,6 +19,7 @@ class TxtGameApp:
self.platform = self.PLATFORM_CLASS(self)
self.render = self.RENDER_CLASS(self)
self.shaders = self.SHADER_CLASS(self)
+ self.coords = self.COORDINATE_CLASS(self)
self.should_exit = False
def init(self):
diff --git a/txtgameengine/res/textures/test_image.png b/txtgameengine/builtin_res/textures/test_image.png
index 6264a7a..6264a7a 100644
--- a/txtgameengine/res/textures/test_image.png
+++ b/txtgameengine/builtin_res/textures/test_image.png
Binary files differ
diff --git a/txtgameengine/platform.py b/txtgameengine/platform.py
index de539d2..c04607e 100644
--- a/txtgameengine/platform.py
+++ b/txtgameengine/platform.py
@@ -17,12 +17,11 @@ class PlatformError(Exception):
class PlatformComponent:
def __init__(self, app: 'TxtGameApp'):
self.app = app
- self.window = None
def init(self):
glfw.init()
self.init_window()
- glfw.make_context_current(self.window)
+ glfw.make_context_current(self.app.window)
glViewport(0, 0, *self.app.size)
@staticmethod
@@ -41,29 +40,29 @@ class PlatformComponent:
glfw.window_hint(glfw.OPENGL_DEBUG_CONTEXT, glfw.TRUE)
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
- self.window = glfw.create_window(
+ self.app.window = glfw.create_window(
*self.app.size, self.app.name, None, None)
- if not self.window:
+ if not self.app.window:
raise PlatformError("Failed to initialize glfw window")
@property
def should_close(self) -> bool:
- return glfw.window_should_close(self.window)
+ return glfw.window_should_close(self.app.window)
@should_close.setter
def should_close(self, val: bool):
- glfw.set_window_should_close(val)
+ glfw.set_window_should_close(val, self.app.window)
@staticmethod
def poll_events():
glfw.poll_events()
def cleanup(self):
- glfw.destroy_window(self.window)
+ glfw.destroy_window(self.app.window)
glfw.terminate()
def swap_buffers(self):
- glfw.swap_buffers(self.window)
+ glfw.swap_buffers(self.app.window)
@staticmethod
def set_clear_color(r, g, b, a):
@@ -84,6 +83,30 @@ class PlatformComponent:
depth_buffer and GL_DEPTH_BUFFER_BIT or 0))
+class CoordinateComponent:
+
+ def __init__(self, app: 'TxtGameApp'):
+ self.screen_x = [-1, 1]
+ self.screen_y = [1, -1]
+ self.app = app
+
+ @property
+ def pixel_x(self):
+ return [0, self.app.size[0]]
+
+ @property
+ def pixel_y(self):
+ return [0, self.app.size[1]]
+
+ def from_screen_to_pixels(self, x: float, y: float) -> (int, int):
+ return int(np.interp(x, self.screen_x, self.pixel_x)), \
+ int(np.interp(y, self.screen_y, self.pixel_y))
+
+ def from_pixels_to_screen(self, x: int, y: int) -> (float, float):
+ return np.interp(x, self.pixel_x, self.screen_x), \
+ np.interp(y, self.pixel_y, self.screen_y)
+
+
class ShaderComponent:
def __init__(self, app: 'TxtGameApp'):
self.app = app
diff --git a/txtgameengine/twod/textures.py b/txtgameengine/twod/textures.py
index 378227d..295af27 100644
--- a/txtgameengine/twod/textures.py
+++ b/txtgameengine/twod/textures.py
@@ -17,7 +17,7 @@ class Texture:
self._bind_to_gl()
def _bind_to_gl(self):
- image = Image.open(os.path.join(TEXTURE_FOLDER, self.path))
+ image = Image.open(self.path)
self.width, self.height = image.size
imagedata = np.array(list(image.getdata()), np.uint8)
self.gl_texid = self.app.render.setup_texture(self.width, self.height, imagedata)