diff options
-rw-r--r-- | txtgameengine/builtin_res/fonts/fira_code/regular.png | bin | 27940 -> 44454 bytes | |||
-rw-r--r-- | txtgameengine/builtin_res/fonts/fira_code/regular.xml | 190 | ||||
-rw-r--r-- | txtgameengine/builtin_res/shaders/font/fragment.glsl | 5 | ||||
-rw-r--r-- | txtgameengine/builtin_res/textures/uvs.jpg | bin | 0 -> 56976 bytes | |||
-rw-r--r-- | txtgameengine/fonts.py | 14 | ||||
-rw-r--r-- | txtgameengine/platform.py | 13 | ||||
-rw-r--r-- | txtgameengine/twod/textures.py | 17 |
7 files changed, 131 insertions, 108 deletions
diff --git a/txtgameengine/builtin_res/fonts/fira_code/regular.png b/txtgameengine/builtin_res/fonts/fira_code/regular.png Binary files differindex 8fa2358..e604d8d 100644 --- a/txtgameengine/builtin_res/fonts/fira_code/regular.png +++ b/txtgameengine/builtin_res/fonts/fira_code/regular.png 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 @@ <?xml version="1.0" encoding="utf-8"?> <Font size="65" family="Fira Code" height="142" style="Regular"> - <Char width="142" offset="0 107" rect="1 108 0 0" code=" "/> - <Char width="142" offset="28 25" rect="2 26 16 83" code="!"/> - <Char width="142" offset="20 19" rect="19 20 31 31" code="""/> - <Char width="142" offset="6 25" rect="51 26 58 82" code="#"/> - <Char width="142" offset="7 6" rect="110 7 57 119" code="$"/> - <Char width="142" offset="2 23" rect="168 24 67 87" code="%"/> - <Char width="142" offset="6 23" rect="236 24 62 85" code="&"/> - <Char width="142" offset="30 19" rect="299 20 11 31" code="'"/> - <Char width="142" offset="15 6" rect="311 7 37 118" code="("/> - <Char width="142" offset="20 6" rect="349 7 37 118" code=")"/> - <Char width="142" offset="5 38" rect="387 39 61 58" code="*"/> - <Char width="142" offset="8 43" rect="449 44 56 55" code="+"/> - <Char width="142" offset="24 87" rect="506 88 21 41" code=","/> - <Char width="142" offset="12 66" rect="528 67 48 8" code="-"/> - <Char width="142" offset="25 87" rect="577 88 21 21" code="."/> - <Char width="142" offset="6 10" rect="599 11 59 110" code="/"/> - <Char width="142" offset="9 23" rect="659 24 54 85" code="0"/> - <Char width="142" offset="11 25" rect="714 26 50 82" code="1"/> - <Char width="142" offset="6 24" rect="765 25 53 83" code="2"/> - <Char width="142" offset="6 23" rect="819 24 54 85" code="3"/> - <Char width="142" offset="9 23" rect="874 24 54 84" code="4"/> - <Char width="142" offset="9 25" rect="929 26 52 83" code="5"/> - <Char width="142" offset="9 23" rect="982 24 53 85" code="6"/> - <Char width="142" offset="12 25" rect="1036 26 48 83" code="7"/> - <Char width="142" offset="8 23" rect="1085 24 56 85" code="8"/> - <Char width="142" offset="8 24" rect="1142 25 53 86" code="9"/> - <Char width="142" offset="26 46" rect="1196 47 19 62" code=":"/> - <Char width="142" offset="24 46" rect="1216 47 21 82" code=";"/> - <Char width="142" offset="10 36" rect="1238 37 51 68" code="<"/> - <Char width="142" offset="12 55" rect="1290 56 46 31" code="="/> - <Char width="142" offset="10 36" rect="1337 37 51 68" code=">"/> - <Char width="142" offset="10 23" rect="1389 24 49 85" code="?"/> - <Char width="142" offset="0 24" rect="1439 25 69 94" code="@"/> - <Char width="142" offset="2 25" rect="1509 26 67 82" code="A"/> - <Char width="142" offset="11 25" rect="1577 26 53 82" code="B"/> - <Char width="142" offset="8 24" rect="1631 25 61 84" code="C"/> - <Char width="142" offset="10 25" rect="1693 26 56 82" code="D"/> - <Char width="142" offset="15 25" rect="1750 26 48 82" code="E"/> - <Char width="142" offset="16 25" rect="1799 26 48 82" code="F"/> - <Char width="142" offset="5 24" rect="1848 25 58 84" code="G"/> - <Char width="142" offset="10 25" rect="1907 26 51 82" code="H"/> - <Char width="142" offset="12 25" rect="1959 26 47 82" code="I"/> - <Char width="142" offset="8 25" rect="2007 26 50 83" code="J"/> - <Char width="142" offset="12 25" rect="2058 26 58 82" code="K"/> - <Char width="142" offset="16 25" rect="2117 26 49 82" code="L"/> - <Char width="142" offset="6 25" rect="2167 26 59 82" code="M"/> - <Char width="142" offset="10 25" rect="2227 26 52 82" code="N"/> - <Char width="142" offset="6 24" rect="2280 25 60 84" code="O"/> - <Char width="142" offset="14 26" rect="2341 27 52 82" code="P"/> - <Char width="142" offset="5 24" rect="2394 25 65 104" code="Q"/> - <Char width="142" offset="12 25" rect="2460 26 57 82" code="R"/> - <Char width="142" offset="5 24" rect="2518 25 59 84" code="S"/> - <Char width="142" offset="4 25" rect="2578 26 62 82" code="T"/> - <Char width="142" offset="9 24" rect="2641 25 54 84" code="U"/> - <Char width="142" offset="2 25" rect="2696 26 67 82" code="V"/> - <Char width="142" offset="0 25" rect="2764 26 71 82" code="W"/> - <Char width="142" offset="3 25" rect="2836 26 65 82" code="X"/> - <Char width="142" offset="2 25" rect="2902 26 67 82" code="Y"/> - <Char width="142" offset="8 25" rect="2970 26 55 82" code="Z"/> - <Char width="142" offset="22 10" rect="3026 11 30 111" code="["/> - <Char width="142" offset="6 10" rect="3057 11 59 110" code="\"/> - <Char width="142" offset="19 10" rect="3117 11 30 111" code="]"/> - <Char width="142" offset="8 7" rect="3148 8 55 37" code="^"/> - <Char width="142" offset="6 120" rect="3204 121 59 9" code="_"/> - <Char width="142" offset="21 13" rect="3264 14 29 20" code="`"/> - <Char width="142" offset="8 43" rect="3294 44 54 65" code="a"/> - <Char width="142" offset="12 17" rect="3349 18 51 91" code="b"/> - <Char width="142" offset="11 43" rect="3401 44 51 65" code="c"/> - <Char width="142" offset="8 17" rect="3453 18 51 91" code="d"/> - <Char width="142" offset="9 43" rect="3505 44 53 65" code="e"/> - <Char width="142" offset="11 18" rect="3559 19 55 89" code="f"/> - <Char width="142" offset="8 36" rect="3615 37 58 96" code="g"/> - <Char width="142" offset="12 18" rect="3674 19 47 89" code="h"/> - <Char width="142" offset="14 15" rect="3722 16 47 92" code="i"/> - <Char width="142" offset="10 15" rect="3770 16 42 118" code="j"/> - <Char width="142" offset="13 18" rect="3813 19 55 89" code="k"/> - <Char width="142" offset="7 19" rect="3869 20 53 89" code="l"/> - <Char width="142" offset="6 43" rect="3923 44 59 64" code="m"/> - <Char width="142" offset="12 43" rect="3983 44 47 64" code="n"/> - <Char width="142" offset="9 43" rect="4031 44 54 65" code="o"/> - <Char width="142" offset="12 43" rect="4086 44 51 89" code="p"/> - <Char width="142" offset="9 43" rect="4138 44 50 89" code="q"/> - <Char width="142" offset="12 43" rect="4189 44 50 64" code="r"/> - <Char width="142" offset="8 43" rect="4240 44 51 65" code="s"/> - <Char width="142" offset="9 28" rect="4292 29 53 80" code="t"/> - <Char width="142" offset="12 44" rect="4346 45 47 64" code="u"/> - <Char width="142" offset="7 44" rect="4394 45 57 63" code="v"/> - <Char width="142" offset="2 44" rect="4452 45 68 63" code="w"/> - <Char width="142" offset="6 45" rect="4521 46 59 62" code="x"/> - <Char width="142" offset="7 44" rect="4581 45 57 89" code="y"/> - <Char width="142" offset="12 45" rect="4639 46 48 62" code="z"/> - <Char width="142" offset="13 7" rect="4688 8 40 116" code="{"/> - <Char width="142" offset="31 0" rect="4729 1 9 143" code="|"/> - <Char width="142" offset="18 7" rect="4739 8 40 116" code="}"/> - <Char width="142" offset="5 60" rect="4780 61 61 20" code="~"/> + <Char width="142" offset="0 107" rect="1 102 0 0" code=" "/> + <Char width="142" offset="28 25" rect="2 20 16 83" code="!"/> + <Char width="142" offset="20 19" rect="19 14 31 31" code="""/> + <Char width="142" offset="6 25" rect="51 20 58 82" code="#"/> + <Char width="142" offset="7 6" rect="110 1 57 119" code="$"/> + <Char width="142" offset="2 23" rect="168 18 67 87" code="%"/> + <Char width="142" offset="6 23" rect="236 18 62 85" code="&"/> + <Char width="142" offset="30 19" rect="299 14 11 31" code="'"/> + <Char width="142" offset="15 6" rect="311 1 37 118" code="("/> + <Char width="142" offset="20 6" rect="349 1 37 118" code=")"/> + <Char width="142" offset="5 38" rect="387 33 61 58" code="*"/> + <Char width="142" offset="8 43" rect="449 38 56 55" code="+"/> + <Char width="142" offset="24 87" rect="506 82 21 41" code=","/> + <Char width="142" offset="12 66" rect="528 61 48 8" code="-"/> + <Char width="142" offset="25 87" rect="577 82 21 21" code="."/> + <Char width="142" offset="6 10" rect="599 5 59 110" code="/"/> + <Char width="142" offset="9 23" rect="659 18 54 85" code="0"/> + <Char width="142" offset="11 25" rect="714 20 50 82" code="1"/> + <Char width="142" offset="6 24" rect="765 19 53 83" code="2"/> + <Char width="142" offset="6 23" rect="819 18 54 85" code="3"/> + <Char width="142" offset="9 23" rect="874 18 54 84" code="4"/> + <Char width="142" offset="9 25" rect="929 20 52 83" code="5"/> + <Char width="142" offset="9 23" rect="1 124 53 85" code="6"/> + <Char width="142" offset="12 25" rect="55 126 48 83" code="7"/> + <Char width="142" offset="8 23" rect="104 124 56 85" code="8"/> + <Char width="142" offset="8 24" rect="161 125 53 86" code="9"/> + <Char width="142" offset="26 46" rect="215 147 19 62" code=":"/> + <Char width="142" offset="24 46" rect="235 147 21 82" code=";"/> + <Char width="142" offset="10 36" rect="257 137 51 68" code="<"/> + <Char width="142" offset="12 55" rect="309 156 46 31" code="="/> + <Char width="142" offset="10 36" rect="356 137 51 68" code=">"/> + <Char width="142" offset="10 23" rect="408 124 49 85" code="?"/> + <Char width="142" offset="0 24" rect="458 125 69 94" code="@"/> + <Char width="142" offset="2 25" rect="528 126 67 82" code="A"/> + <Char width="142" offset="11 25" rect="596 126 53 82" code="B"/> + <Char width="142" offset="8 24" rect="650 125 61 84" code="C"/> + <Char width="142" offset="10 25" rect="712 126 56 82" code="D"/> + <Char width="142" offset="15 25" rect="769 126 48 82" code="E"/> + <Char width="142" offset="16 25" rect="818 126 48 82" code="F"/> + <Char width="142" offset="5 24" rect="867 125 58 84" code="G"/> + <Char width="142" offset="10 25" rect="926 126 51 82" code="H"/> + <Char width="142" offset="12 25" rect="1 231 47 82" code="I"/> + <Char width="142" offset="8 25" rect="49 231 50 83" code="J"/> + <Char width="142" offset="12 25" rect="100 231 58 82" code="K"/> + <Char width="142" offset="16 25" rect="159 231 49 82" code="L"/> + <Char width="142" offset="6 25" rect="209 231 59 82" code="M"/> + <Char width="142" offset="10 25" rect="269 231 52 82" code="N"/> + <Char width="142" offset="6 24" rect="322 230 60 84" code="O"/> + <Char width="142" offset="14 26" rect="383 232 52 82" code="P"/> + <Char width="142" offset="5 24" rect="436 230 65 104" code="Q"/> + <Char width="142" offset="12 25" rect="502 231 57 82" code="R"/> + <Char width="142" offset="5 24" rect="560 230 59 84" code="S"/> + <Char width="142" offset="4 25" rect="620 231 62 82" code="T"/> + <Char width="142" offset="9 24" rect="683 230 54 84" code="U"/> + <Char width="142" offset="2 25" rect="738 231 67 82" code="V"/> + <Char width="142" offset="0 25" rect="806 231 71 82" code="W"/> + <Char width="142" offset="3 25" rect="878 231 65 82" code="X"/> + <Char width="142" offset="2 25" rect="944 231 67 82" code="Y"/> + <Char width="142" offset="8 25" rect="1 353 55 82" code="Z"/> + <Char width="142" offset="22 10" rect="57 338 30 111" code="["/> + <Char width="142" offset="6 10" rect="88 338 59 110" code="\"/> + <Char width="142" offset="19 10" rect="148 338 30 111" code="]"/> + <Char width="142" offset="8 7" rect="179 335 55 37" code="^"/> + <Char width="142" offset="6 120" rect="235 448 59 9" code="_"/> + <Char width="142" offset="21 13" rect="295 341 29 20" code="`"/> + <Char width="142" offset="8 43" rect="325 371 54 65" code="a"/> + <Char width="142" offset="12 17" rect="380 345 51 91" code="b"/> + <Char width="142" offset="11 43" rect="432 371 51 65" code="c"/> + <Char width="142" offset="8 17" rect="484 345 51 91" code="d"/> + <Char width="142" offset="9 43" rect="536 371 53 65" code="e"/> + <Char width="142" offset="11 18" rect="590 346 55 89" code="f"/> + <Char width="142" offset="8 36" rect="646 364 58 96" code="g"/> + <Char width="142" offset="12 18" rect="705 346 47 89" code="h"/> + <Char width="142" offset="14 15" rect="753 343 47 92" code="i"/> + <Char width="142" offset="10 15" rect="801 343 42 118" code="j"/> + <Char width="142" offset="13 18" rect="844 346 55 89" code="k"/> + <Char width="142" offset="7 19" rect="900 347 53 89" code="l"/> + <Char width="142" offset="6 43" rect="954 371 59 64" code="m"/> + <Char width="142" offset="12 43" rect="1 505 47 64" code="n"/> + <Char width="142" offset="9 43" rect="49 505 54 65" code="o"/> + <Char width="142" offset="12 43" rect="104 505 51 89" code="p"/> + <Char width="142" offset="9 43" rect="156 505 50 89" code="q"/> + <Char width="142" offset="12 43" rect="207 505 50 64" code="r"/> + <Char width="142" offset="8 43" rect="258 505 51 65" code="s"/> + <Char width="142" offset="9 28" rect="310 490 53 80" code="t"/> + <Char width="142" offset="12 44" rect="364 506 47 64" code="u"/> + <Char width="142" offset="7 44" rect="412 506 57 63" code="v"/> + <Char width="142" offset="2 44" rect="470 506 68 63" code="w"/> + <Char width="142" offset="6 45" rect="539 507 59 62" code="x"/> + <Char width="142" offset="7 44" rect="599 506 57 89" code="y"/> + <Char width="142" offset="12 45" rect="657 507 48 62" code="z"/> + <Char width="142" offset="13 7" rect="706 469 40 116" code="{"/> + <Char width="142" offset="31 0" rect="747 462 9 143" code="|"/> + <Char width="142" offset="18 7" rect="757 469 40 116" code="}"/> + <Char width="142" offset="5 60" rect="798 522 61 20" code="~"/> </Font> 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 Binary files differnew file mode 100644 index 0000000..42de8a1 --- /dev/null +++ b/txtgameengine/builtin_res/textures/uvs.jpg 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) |