From a256fbb509af82c7b9c8a03224dfe225e0c123ca Mon Sep 17 00:00:00 2001 From: rom Date: Sat, 24 Apr 2021 13:21:39 +0200 Subject: text rendering lul --- .../builtin_res/fonts/fira_code/regular.png | Bin 27940 -> 44454 bytes .../builtin_res/fonts/fira_code/regular.xml | 190 ++++++++++----------- .../builtin_res/shaders/font/fragment.glsl | 5 +- txtgameengine/builtin_res/textures/uvs.jpg | Bin 0 -> 56976 bytes txtgameengine/fonts.py | 14 +- txtgameengine/platform.py | 13 +- txtgameengine/twod/textures.py | 17 +- 7 files changed, 131 insertions(+), 108 deletions(-) create mode 100644 txtgameengine/builtin_res/textures/uvs.jpg diff --git a/txtgameengine/builtin_res/fonts/fira_code/regular.png b/txtgameengine/builtin_res/fonts/fira_code/regular.png index 8fa2358..e604d8d 100644 Binary files a/txtgameengine/builtin_res/fonts/fira_code/regular.png and b/txtgameengine/builtin_res/fonts/fira_code/regular.png differ diff --git a/txtgameengine/builtin_res/fonts/fira_code/regular.xml b/txtgameengine/builtin_res/fonts/fira_code/regular.xml index 880f7d3..cd38624 100644 --- a/txtgameengine/builtin_res/fonts/fira_code/regular.xml +++ b/txtgameengine/builtin_res/fonts/fira_code/regular.xml @@ -1,98 +1,98 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/txtgameengine/builtin_res/shaders/font/fragment.glsl b/txtgameengine/builtin_res/shaders/font/fragment.glsl index d9d36fd..f670d57 100644 --- a/txtgameengine/builtin_res/shaders/font/fragment.glsl +++ b/txtgameengine/builtin_res/shaders/font/fragment.glsl @@ -2,11 +2,12 @@ in vec2 UV; -out vec3 color; +out vec4 color; uniform sampler2D textureSampler; void main() { - color = texture(textureSampler, UV).rgb; + vec4 fontPixel = texture(textureSampler, UV); + color = fontPixel; } diff --git a/txtgameengine/builtin_res/textures/uvs.jpg b/txtgameengine/builtin_res/textures/uvs.jpg new file mode 100644 index 0000000..42de8a1 Binary files /dev/null and b/txtgameengine/builtin_res/textures/uvs.jpg differ diff --git a/txtgameengine/fonts.py b/txtgameengine/fonts.py index afa9d87..445d9b1 100644 --- a/txtgameengine/fonts.py +++ b/txtgameengine/fonts.py @@ -4,7 +4,7 @@ from dataclasses import dataclass from typing import Optional import typing import xml.dom.minidom as minidom - +from PIL import Image from .shaders import FontShader if typing.TYPE_CHECKING: @@ -43,7 +43,8 @@ class BitmapFont(Font): code = char.attributes['code'].value width = char.attributes['width'].value x_render_offset, y_render_offset = char.attributes['offset'].value.split(' ') - x_texture_offset, y_texture_offset, x_texture_width, y_texture_width = char.attributes['rect'].value.split(' ') + x_texture_offset, y_texture_offset, x_texture_width, y_texture_width = char.attributes['rect'].value.split( + ' ') self.glyphs[code] = \ Glyph(self, int(x_texture_offset), int(y_texture_offset), int(x_texture_width), int(y_texture_width), int(x_render_offset), int(y_render_offset), int(width)) @@ -56,7 +57,8 @@ class BitmapFont(Font): @classmethod def fira_mono(cls, app: 'TxtGameApp'): from .app import builtin_resource_path - return cls.load(app, builtin_resource_path / 'fonts/fira_code/regular.png', + img = Image.open(builtin_resource_path / 'fonts/fira_code/regular.png') + return cls.load(app, img, builtin_resource_path / 'fonts/fira_code/regular.xml') @@ -94,8 +96,10 @@ class TextRenderer: tex_high_x = tex_low_x + glyph.x_texture_width tex_low_y = glyph.y_texture_offset tex_high_y = tex_low_y + glyph.y_texture_width - tex_low_x, tex_low_y = self.app.coords.from_pixels_to_screen(tex_low_x, tex_low_y) - tex_high_x, tex_high_y = self.app.coords.from_pixels_to_screen(tex_high_x, tex_high_y) + tex_low_x, tex_low_y = self.font.texture.uvs_from_pixels(tex_low_x, tex_low_y) + tex_high_x, tex_high_y = self.font.texture.uvs_from_pixels(tex_high_x, tex_high_y) + print(tex_low_x, tex_low_y) + print(tex_high_x, tex_high_y) render = self.app.render.setup_buffer([ low_x, low_y, high_x, low_y, diff --git a/txtgameengine/platform.py b/txtgameengine/platform.py index bcf3d10..4ab0b0a 100644 --- a/txtgameengine/platform.py +++ b/txtgameengine/platform.py @@ -23,6 +23,8 @@ class PlatformComponent: glfw.init() self.init_window() glfw.make_context_current(self.app.window) + glEnable(GL_BLEND) + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glViewport(0, 0, *self.app.size) @staticmethod @@ -61,8 +63,8 @@ class PlatformComponent: @staticmethod def init_callbacks(window, handler: CallbackHandler): glfw.set_key_callback(window, handler.get_keyboard_input_callback) - #glfw.set_mouse_button_callback(window, handler.get_mouse_click_callback()) - #glfw.set_cursor_pos_callback(window, handler.get_mouse_move_callback()) + # glfw.set_mouse_button_callback(window, handler.get_mouse_click_callback()) + # glfw.set_cursor_pos_callback(window, handler.get_mouse_move_callback()) def cleanup(self): glfw.destroy_window(self.app.window) @@ -113,6 +115,11 @@ class CoordinateComponent: return np.interp(x, self.pixel_x, self.screen_x), \ np.interp(y, self.pixel_y, self.screen_y) + @staticmethod + def from_pixels_to_uvs(size: typing.Tuple[int, int], x: int, y: int) -> typing.Tuple[float, float]: + return np.interp(x, [0, size[0]], [0, 1]), \ + np.interp(y, [0, size[1]], [0, 1]) + class ShaderComponent: def __init__(self, app: 'TxtGameApp'): @@ -184,7 +191,7 @@ class RenderComponent: glBindTexture(GL_TEXTURE_2D, tex_id) 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) + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data) return tex_id def free_texture(self, gl_texid: int): diff --git a/txtgameengine/twod/textures.py b/txtgameengine/twod/textures.py index 295af27..a17472e 100644 --- a/txtgameengine/twod/textures.py +++ b/txtgameengine/twod/textures.py @@ -11,17 +11,28 @@ if typing.TYPE_CHECKING: class Texture: - def __init__(self, app: 'TxtGameApp', path: os.PathLike): + def __init__(self, app: 'TxtGameApp', load: typing.Union[os.PathLike, Image.Image]): self.app = app - self.path = path + self.load = load self._bind_to_gl() def _bind_to_gl(self): - image = Image.open(self.path) + 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) -- cgit