From d49aa08b45155c4740b1205bc1150885993187a5 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sat, 12 Oct 2019 00:00:00 +0000 Subject: [PATCH] [update] Implemented rendering font text. --- .bin/.gitignore | 3 +++ src/sj/Font.d | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 .bin/.gitignore diff --git a/.bin/.gitignore b/.bin/.gitignore new file mode 100644 index 0000000..dbe7ada --- /dev/null +++ b/.bin/.gitignore @@ -0,0 +1,3 @@ +* +!/.gitignore +!/fonts diff --git a/src/sj/Font.d b/src/sj/Font.d index ac644f0..a1909d1 100644 --- a/src/sj/Font.d +++ b/src/sj/Font.d @@ -1,7 +1,8 @@ /// License: MIT module sj.Font; -import std.exception, +import std.conv, + std.exception, std.format, std.string; @@ -22,8 +23,48 @@ class Font { } /// - Texture2DRef Render(string text, size_t px) { - assert(false); + Texture2DRef CreateTexture( + vec2i sz, string str, vec4 color, size_t px) { + auto text = sfText_create().enforce; + scope(exit) sfText_destroy(text); + + sfText_setFont(text, font_); + sfText_setString(text, str.toStringz); + sfText_setCharacterSize(text, px.to!uint); + sfText_setFillColor(text, sfBlack); // TODO: change color + + auto buf = sfRenderTexture_create(sz.x, sz.y, false).enforce; + scope(exit) sfRenderTexture_destroy(buf); + sfRenderTexture_drawText(buf, text, null); + + auto sftex = sfRenderTexture_getTexture(buf).enforce; + + auto tex = Texture2D.Create(); + Texture2DAllocator allocator; + with (allocator) { + internalFormat = GL_RGBA8; + size = sz; + format = GL_RED; + type = GL_UNSIGNED_BYTE; + Allocate(tex); + } + + gl.CopyImageSubData( + sfTexture_getNativeHandle(sftex), + GL_TEXTURE_2D, + 0, + 0, + 0, + 0, + tex.id, + GL_TEXTURE_2D, + 0, + 0, + 0, + 0, + sz.x, sz.y, 1 + ); + return tex; } private: