From 156114359f6bb26a3cca198ac7a31bddc44381f7 Mon Sep 17 00:00:00 2001 From: rom Date: Sat, 24 Apr 2021 01:56:51 +0200 Subject: fonts --- txtgameengine/__main__.py | 2 +- txtgameengine/app.py | 2 +- .../builtin_res/fonts/fira_code/regular.png | Bin 0 -> 27940 bytes .../builtin_res/fonts/fira_code/regular.xml | 98 +++++++++++++++++++++ .../builtin_res/shaders/basic/fragment.glsl | 6 ++ .../builtin_res/shaders/basic/vertex.glsl | 7 ++ .../builtin_res/shaders/texture/fragment.glsl | 12 +++ .../builtin_res/shaders/texture/vertex.glsl | 12 +++ txtgameengine/builtin_shaders/basic/fragment.glsl | 6 -- txtgameengine/builtin_shaders/basic/vertex.glsl | 7 -- .../builtin_shaders/texture/fragment.glsl | 12 --- txtgameengine/builtin_shaders/texture/vertex.glsl | 12 --- txtgameengine/fonts.py | 51 ++++++++--- txtgameengine/shaders/integrated.py | 4 +- 14 files changed, 180 insertions(+), 51 deletions(-) create mode 100644 txtgameengine/builtin_res/fonts/fira_code/regular.png create mode 100644 txtgameengine/builtin_res/fonts/fira_code/regular.xml create mode 100644 txtgameengine/builtin_res/shaders/basic/fragment.glsl create mode 100644 txtgameengine/builtin_res/shaders/basic/vertex.glsl create mode 100644 txtgameengine/builtin_res/shaders/texture/fragment.glsl create mode 100644 txtgameengine/builtin_res/shaders/texture/vertex.glsl delete mode 100644 txtgameengine/builtin_shaders/basic/fragment.glsl delete mode 100644 txtgameengine/builtin_shaders/basic/vertex.glsl delete mode 100644 txtgameengine/builtin_shaders/texture/fragment.glsl delete mode 100644 txtgameengine/builtin_shaders/texture/vertex.glsl diff --git a/txtgameengine/__main__.py b/txtgameengine/__main__.py index b66d9fe..edfbd29 100644 --- a/txtgameengine/__main__.py +++ b/txtgameengine/__main__.py @@ -5,7 +5,7 @@ from pathlib import Path from .shaders import TextureShader from .twod.textures import Texture -shader_path = Path(__file__).parent / 'builtin_shaders' +shader_path = Path(__file__).parent / 'shaders' class TriangleScene(Scene): diff --git a/txtgameengine/app.py b/txtgameengine/app.py index e7121da..58b239f 100644 --- a/txtgameengine/app.py +++ b/txtgameengine/app.py @@ -4,7 +4,7 @@ from pathlib import Path from .platform import PlatformComponent, RenderComponent, ShaderComponent EPSILON = 1.e-10 -base_path = Path(__file__).parent +builtin_resource_path = Path(__file__).parent / 'builtin_res' class TxtGameApp: diff --git a/txtgameengine/builtin_res/fonts/fira_code/regular.png b/txtgameengine/builtin_res/fonts/fira_code/regular.png new file mode 100644 index 0000000..8fa2358 Binary files /dev/null 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 new file mode 100644 index 0000000..880f7d3 --- /dev/null +++ b/txtgameengine/builtin_res/fonts/fira_code/regular.xml @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/txtgameengine/builtin_res/shaders/basic/fragment.glsl b/txtgameengine/builtin_res/shaders/basic/fragment.glsl new file mode 100644 index 0000000..a9dfa6f --- /dev/null +++ b/txtgameengine/builtin_res/shaders/basic/fragment.glsl @@ -0,0 +1,6 @@ +#version 330 core + +out vec4 fragColor; +void main() { + fragColor = vec4(0, 0, 1, 1); +} \ No newline at end of file diff --git a/txtgameengine/builtin_res/shaders/basic/vertex.glsl b/txtgameengine/builtin_res/shaders/basic/vertex.glsl new file mode 100644 index 0000000..92f7c97 --- /dev/null +++ b/txtgameengine/builtin_res/shaders/basic/vertex.glsl @@ -0,0 +1,7 @@ +#version 330 core + +layout(location = 0) in vec2 vertexPosition_modelspace; + +void main() { + gl_Position = vec4(vertexPosition_modelspace, 0, 1); +} diff --git a/txtgameengine/builtin_res/shaders/texture/fragment.glsl b/txtgameengine/builtin_res/shaders/texture/fragment.glsl new file mode 100644 index 0000000..d9d36fd --- /dev/null +++ b/txtgameengine/builtin_res/shaders/texture/fragment.glsl @@ -0,0 +1,12 @@ +#version 330 core + +in vec2 UV; + +out vec3 color; + +uniform sampler2D textureSampler; + +void main() { + color = texture(textureSampler, UV).rgb; +} + diff --git a/txtgameengine/builtin_res/shaders/texture/vertex.glsl b/txtgameengine/builtin_res/shaders/texture/vertex.glsl new file mode 100644 index 0000000..dd93afb --- /dev/null +++ b/txtgameengine/builtin_res/shaders/texture/vertex.glsl @@ -0,0 +1,12 @@ +#version 330 core + +layout(location=0) in vec3 vertexPosition_modelspace; +layout(location=1) in vec2 vertexUV; + +out vec2 UV; + +void main() { + gl_Position = vec4(vertexPosition_modelspace, 1); + UV = vertexUV; +} + diff --git a/txtgameengine/builtin_shaders/basic/fragment.glsl b/txtgameengine/builtin_shaders/basic/fragment.glsl deleted file mode 100644 index a9dfa6f..0000000 --- a/txtgameengine/builtin_shaders/basic/fragment.glsl +++ /dev/null @@ -1,6 +0,0 @@ -#version 330 core - -out vec4 fragColor; -void main() { - fragColor = vec4(0, 0, 1, 1); -} \ No newline at end of file diff --git a/txtgameengine/builtin_shaders/basic/vertex.glsl b/txtgameengine/builtin_shaders/basic/vertex.glsl deleted file mode 100644 index 92f7c97..0000000 --- a/txtgameengine/builtin_shaders/basic/vertex.glsl +++ /dev/null @@ -1,7 +0,0 @@ -#version 330 core - -layout(location = 0) in vec2 vertexPosition_modelspace; - -void main() { - gl_Position = vec4(vertexPosition_modelspace, 0, 1); -} diff --git a/txtgameengine/builtin_shaders/texture/fragment.glsl b/txtgameengine/builtin_shaders/texture/fragment.glsl deleted file mode 100644 index d9d36fd..0000000 --- a/txtgameengine/builtin_shaders/texture/fragment.glsl +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 core - -in vec2 UV; - -out vec3 color; - -uniform sampler2D textureSampler; - -void main() { - color = texture(textureSampler, UV).rgb; -} - diff --git a/txtgameengine/builtin_shaders/texture/vertex.glsl b/txtgameengine/builtin_shaders/texture/vertex.glsl deleted file mode 100644 index dd93afb..0000000 --- a/txtgameengine/builtin_shaders/texture/vertex.glsl +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 core - -layout(location=0) in vec3 vertexPosition_modelspace; -layout(location=1) in vec2 vertexUV; - -out vec2 UV; - -void main() { - gl_Position = vec4(vertexPosition_modelspace, 1); - UV = vertexUV; -} - diff --git a/txtgameengine/fonts.py b/txtgameengine/fonts.py index 60660d6..fcbf8b5 100644 --- a/txtgameengine/fonts.py +++ b/txtgameengine/fonts.py @@ -1,15 +1,21 @@ +import os from abc import ABC from dataclasses import dataclass from typing import Optional +import typing +import xml.dom.minidom as minidom + +if typing.TYPE_CHECKING: + from .twod import Texture + from .app import TxtGameApp class Font(ABC): - def get_glyph(self, char: str) -> Optional['Glyph']: - raise NotImplementedError() + def __init__(self): + self.glyphs = dict() - @property - def font(self): - raise NotImplementedError() + def get_glyph(self, char: str) -> Optional['Glyph']: + return self.glyphs.get(char) @dataclass @@ -17,10 +23,35 @@ class Glyph: font: 'Font' x_texture_offset: int y_texture_offset: int - x_width: int - y_width: int + x_texture_width: int + y_texture_width: int + x_render_offset: int + y_render_offset: int + x_advance: int + + +class BitmapFont(Font): + def __init__(self, texture: Texture, xml_path: str): + self.texture = texture + dom = minidom.parse(xml_path) + for char in dom.getElementsByTagName('Char'): + 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'].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)) + + @classmethod + def load(cls, app: 'TxtGameApp', image_path: os.PathLike, xml_path: os.PathLike): + from .twod import Texture + return cls(Texture(app, image_path), xml_path) + +class TextRenderer: + def __init__(self, app: 'TxtGameApp'): + self.app = app -class MonospacedFont(Font): - def get_glyph(self, char: str): - pass + def use_font(self, font: Font): + self.font = font diff --git a/txtgameengine/shaders/integrated.py b/txtgameengine/shaders/integrated.py index dfa1033..d9e5ae5 100644 --- a/txtgameengine/shaders/integrated.py +++ b/txtgameengine/shaders/integrated.py @@ -1,7 +1,7 @@ from .shader import Shader -from ..app import base_path +from ..app import builtin_resource_path -shader_base_path = base_path / 'builtin_shaders' +shader_base_path = builtin_resource_path / 'shaders' class BasicShader(Shader): -- cgit