diff options
| author | Andre Weissflog <floooh@gmail.com> | 2019-08-24 16:11:41 +0200 |
|---|---|---|
| committer | Andre Weissflog <floooh@gmail.com> | 2019-08-24 16:11:41 +0200 |
| commit | 7dfef9432284f151317f8da17fb2709ecf9f9059 (patch) | |
| tree | 53bbaf17e33bbb03d77f0fb7f566d0d86af57bbd /util | |
| parent | 3601a496890f6fe6df2c633ba22545c406ae1df4 (diff) | |
new sokol_fontstash.h util header (WIP)
Diffstat (limited to 'util')
| -rw-r--r-- | util/sokol_fontstash.h | 734 | ||||
| -rw-r--r-- | util/sokol_text.h | 201 |
2 files changed, 734 insertions, 201 deletions
diff --git a/util/sokol_fontstash.h b/util/sokol_fontstash.h new file mode 100644 index 00000000..fd262e4c --- /dev/null +++ b/util/sokol_fontstash.h @@ -0,0 +1,734 @@ +#pragma once +/* + sokol_fontstash.h -- renderer for https://github.com/memononen/fontstash + + Project URL: https://github.com/floooh/sokol + + Do this: + + #define SOKOL_FONTSTASH_IMPL + + before you include this file in *one* C or C++ file to create the + implementation. + + The following defines are used by the implementation to select the + platform-specific embedded shader code (these are the same defines as + used by sokol_gfx.h and sokol_app.h): + + SOKOL_GLCORE33 + SOKOL_GLES2 + SOKOL_GLES3 + SOKOL_D3D11 + SOKOL_METAL + + ...optionally provide the following macros to override defaults: + + SOKOL_ASSERT(c) - your own assert macro (default: assert(c)) + SOKOL_MALLOC(s) - your own malloc function (default: malloc(s)) + SOKOL_FREE(p) - your own free function (default: free(p)) + SOKOL_API_DECL - public function declaration prefix (default: extern) + SOKOL_API_IMPL - public function implementation prefix (default: -) + SOKOL_LOG(msg) - your own logging function (default: puts(msg)) + SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false)) + + Include the following headers before including sokol_fontstash.h: + + sokol_gfx.h + + Additionally include the following headers for including the sokol_fontstash.h + implementation: + + sokol_gl.h + + FIXME: documentation + + LICENSE + ======= + zlib/libpng license + + Copyright (c) 2018 Andre Weissflog + + This software is provided 'as-is', without any express or implied warranty. + In no event will the authors be held liable for any damages arising from the + use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software in a + product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not + be misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. +*/ +#define SOKOL_FONTSTASH_INCLUDED (1) +#include <stdint.h> +#include <stdlib.h> + +#if !defined(SOKOL_GFX_INCLUDED) +#error "Please include sokol_gfx.h before sokol_fontstash.h" +#endif + +#ifndef SOKOL_API_DECL +#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_IMPL) +#define SOKOL_API_DECL __declspec(dllexport) +#elif defined(_WIN32) && defined(SOKOL_DLL) +#define SOKOL_API_DECL __declspec(dllimport) +#else +#define SOKOL_API_DECL extern +#endif +#endif +#ifdef __cplusplus +extern "C" { +#endif + +SOKOL_API_DECL FONScontext* sfons_create(int width, int height, int flags); +SOKOL_API_DECL void sfons_destroy(FONScontext* ctx); +SOKOL_API_DECL void sfons_flush(FONScontext* ctx); +SOKOL_API_DECL uint32_t sfons_rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +/*-- IMPLEMENTATION ----------------------------------------------------------*/ +#ifdef SOKOL_FONTSTASH_IMPL +#define SOKOL_FONTSTASH_IMPL_INCLUDED (1) +#include <string.h> /* memset, memcpy */ + +#if !defined(SOKOL_GL_INCLUDED) +#error "Please include sokol_gl.h before sokol_fontstash.h" +#endif +#if !defined(FONS_H) +#error "Please include fontstash.h before sokol_fontstash.h" +#endif + +#ifndef SOKOL_API_IMPL + #define SOKOL_API_IMPL +#endif +#ifndef SOKOL_DEBUG + #ifndef NDEBUG + #define SOKOL_DEBUG (1) + #endif +#endif +#ifndef SOKOL_ASSERT + #include <assert.h> + #define SOKOL_ASSERT(c) assert(c) +#endif +#ifndef SOKOL_MALLOC + #include <stdlib.h> + #define SOKOL_MALLOC(s) malloc(s) + #define SOKOL_FREE(p) free(p) +#endif +#ifndef SOKOL_LOG + #ifdef SOKOL_DEBUG + #include <stdio.h> + #define SOKOL_LOG(s) { SOKOL_ASSERT(s); puts(s); } + #else + #define SOKOL_LOG(s) + #endif +#endif +#ifndef SOKOL_UNREACHABLE + #define SOKOL_UNREACHABLE SOKOL_ASSERT(false) +#endif + +#if defined(SOKOL_GLCORE33) +static const char* _sfons_vs_src = + "#version 330\n" + "uniform mat4 mvp;\n" + "uniform mat4 tm;\n" + "in vec4 position;\n" + "in vec2 texcoord0;\n" + "in vec4 color0;\n" + "out vec4 uv;\n" + "out vec4 color;\n" + "void main() {\n" + " gl_Position = mvp * position;\n" + " uv = tm * vec4(texcoord0, 0.0, 1.0);\n" + " color = color0;\n" + "}\n"; +static const char* _sfons_fs_src = + "#version 330\n" + "uniform sampler2D tex;\n" + "in vec4 uv;\n" + "in vec4 color;\n" + "out vec4 frag_color;\n" + "void main() {\n" + " frag_color = texture(tex, uv.xy) * color;\n" + "}\n"; +#elif defined(SOKOL_GLES2) || defined(SOKOL_GLES3) +static const char* _sfons_vs_src = + "uniform mat4 mvp;\n" + "uniform mat4 tm;\n" + "attribute vec4 position;\n" + "attribute vec2 texcoord0;\n" + "attribute vec4 color0;\n" + "varying vec4 uv;\n" + "varying vec4 color;\n" + "void main() {\n" + " gl_Position = mvp * position;\n" + " uv = tm * vec4(texcoord0, 0.0, 1.0);\n" + " color = color0;\n" + "}\n"; +static const char* _sfons_fs_src = + "precision mediump float;\n" + "uniform sampler2D tex;\n" + "varying vec4 uv;\n" + "varying vec4 color;\n" + "void main() {\n" + " gl_FragColor = texture2D(tex, uv.xy) * color;\n" + "}\n"; +#elif defined(SOKOL_METAL) +static const char* _sfons_vs_src = + "#include <metal_stdlib>\n" + "using namespace metal;\n" + "struct params_t {\n" + " float4x4 mvp;\n" + " float4x4 tm;\n" + "};\n" + "struct vs_in {\n" + " float4 pos [[attribute(0)]];\n" + " float2 uv [[attribute(1)]];\n" + " float4 color [[attribute(2)]];\n" + "};\n" + "struct vs_out {\n" + " float4 pos [[position]];\n" + " float4 uv;\n" + " float4 color;\n" + "};\n" + "vertex vs_out _main(vs_in in [[stage_in]], constant params_t& params [[buffer(0)]]) {\n" + " vs_out out;\n" + " out.pos = params.mvp * in.pos;\n" + " out.uv = params.tm * float4(in.uv, 0.0, 1.0);\n" + " out.color = in.color;\n" + " return out;\n" + "}\n"; +static const char* _sfons_fs_src = + "#include <metal_stdlib>\n" + "using namespace metal;\n" + "struct fs_in {\n" + " float4 uv;\n" + " float4 color;\n" + "};\n" + "fragment float4 _main(fs_in in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler smp [[sampler(0)]]) {\n" + " return float4(1.0, 1.0, 1.0, tex.sample(smp, in.uv.xy).r) * in.color;\n" + "}\n"; +#elif defined(SOKOL_D3D11) +/* + Shader blobs for D3D11, compiled with: + + fxc.exe /T vs_5_0 /Fh vs.h /Gec /O3 vs.hlsl + fxc.exe /T ps_5_0 /Fh fs.h /Gec /O3 fs.hlsl + + Vertex shader source: + + cbuffer params: register(b0) { + float4x4 mvp; + float4x4 tm; + }; + struct vs_in { + float4 pos: POSITION; + float2 uv: TEXCOORD0; + float4 color: COLOR0; + }; + struct vs_out { + float4 uv: TEXCOORD0; + float4 color: COLOR0; + float4 pos: SV_Position; + }; + vs_out main(vs_in inp) { + vs_out outp; + outp.pos = mul(mvp, inp.pos); + outp.uv = mul(tm, float4(inp.uv, 0.0, 1.0)); + outp.color = inp.color; + return outp; + }; + + Pixel shader source: + + Texture2D<float4> tex: register(t0); + sampler smp: register(s0); + float4 main(float4 uv: TEXCOORD0, float4 color: COLOR0): SV_Target0 { + return tex.Sample(smp, uv.xy) * color; + } +*/ +static const uint8_t _sfons_vs_bin[] = { + 68, 88, 66, 67, 239, 161, + 1, 229, 179, 68, 206, 40, + 34, 15, 57, 169, 103, 117, + 134, 191, 1, 0, 0, 0, + 120, 4, 0, 0, 5, 0, + 0, 0, 52, 0, 0, 0, + 104, 1, 0, 0, 216, 1, + 0, 0, 76, 2, 0, 0, + 220, 3, 0, 0, 82, 68, + 69, 70, 44, 1, 0, 0, + 1, 0, 0, 0, 100, 0, + 0, 0, 1, 0, 0, 0, + 60, 0, 0, 0, 0, 5, + 254, 255, 0, 145, 0, 0, + 3, 1, 0, 0, 82, 68, + 49, 49, 60, 0, 0, 0, + 24, 0, 0, 0, 32, 0, + 0, 0, 40, 0, 0, 0, + 36, 0, 0, 0, 12, 0, + 0, 0, 0, 0, 0, 0, + 92, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 1, 0, + 0, 0, 112, 97, 114, 97, + 109, 115, 0, 171, 92, 0, + 0, 0, 2, 0, 0, 0, + 124, 0, 0, 0, 128, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 204, 0, + 0, 0, 0, 0, 0, 0, + 64, 0, 0, 0, 2, 0, + 0, 0, 220, 0, 0, 0, + 0, 0, 0, 0, 255, 255, + 255, 255, 0, 0, 0, 0, + 255, 255, 255, 255, 0, 0, + 0, 0, 0, 1, 0, 0, + 64, 0, 0, 0, 64, 0, + 0, 0, 2, 0, 0, 0, + 220, 0, 0, 0, 0, 0, + 0, 0, 255, 255, 255, 255, + 0, 0, 0, 0, 255, 255, + 255, 255, 0, 0, 0, 0, + 109, 118, 112, 0, 102, 108, + 111, 97, 116, 52, 120, 52, + 0, 171, 171, 171, 3, 0, + 3, 0, 4, 0, 4, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 208, 0, 0, 0, 116, 109, + 0, 77, 105, 99, 114, 111, + 115, 111, 102, 116, 32, 40, + 82, 41, 32, 72, 76, 83, + 76, 32, 83, 104, 97, 100, + 101, 114, 32, 67, 111, 109, + 112, 105, 108, 101, 114, 32, + 49, 48, 46, 49, 0, 171, + 73, 83, 71, 78, 104, 0, + 0, 0, 3, 0, 0, 0, + 8, 0, 0, 0, 80, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 3, 0, + 0, 0, 0, 0, 0, 0, + 15, 15, 0, 0, 89, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 3, 0, + 0, 0, 1, 0, 0, 0, + 3, 3, 0, 0, 98, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 3, 0, + 0, 0, 2, 0, 0, 0, + 15, 15, 0, 0, 80, 79, + 83, 73, 84, 73, 79, 78, + 0, 84, 69, 88, 67, 79, + 79, 82, 68, 0, 67, 79, + 76, 79, 82, 0, 79, 83, + 71, 78, 108, 0, 0, 0, + 3, 0, 0, 0, 8, 0, + 0, 0, 80, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 3, 0, 0, 0, + 0, 0, 0, 0, 15, 0, + 0, 0, 89, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 3, 0, 0, 0, + 1, 0, 0, 0, 15, 0, + 0, 0, 95, 0, 0, 0, + 0, 0, 0, 0, 1, 0, + 0, 0, 3, 0, 0, 0, + 2, 0, 0, 0, 15, 0, + 0, 0, 84, 69, 88, 67, + 79, 79, 82, 68, 0, 67, + 79, 76, 79, 82, 0, 83, + 86, 95, 80, 111, 115, 105, + 116, 105, 111, 110, 0, 171, + 83, 72, 69, 88, 136, 1, + 0, 0, 80, 0, 1, 0, + 98, 0, 0, 0, 106, 8, + 0, 1, 89, 0, 0, 4, + 70, 142, 32, 0, 0, 0, + 0, 0, 8, 0, 0, 0, + 95, 0, 0, 3, 242, 16, + 16, 0, 0, 0, 0, 0, + 95, 0, 0, 3, 50, 16, + 16, 0, 1, 0, 0, 0, + 95, 0, 0, 3, 242, 16, + 16, 0, 2, 0, 0, 0, + 101, 0, 0, 3, 242, 32, + 16, 0, 0, 0, 0, 0, + 101, 0, 0, 3, 242, 32, + 16, 0, 1, 0, 0, 0, + 103, 0, 0, 4, 242, 32, + 16, 0, 2, 0, 0, 0, + 1, 0, 0, 0, 104, 0, + 0, 2, 1, 0, 0, 0, + 56, 0, 0, 8, 242, 0, + 16, 0, 0, 0, 0, 0, + 86, 21, 16, 0, 1, 0, + 0, 0, 70, 142, 32, 0, + 0, 0, 0, 0, 5, 0, + 0, 0, 50, 0, 0, 10, + 242, 0, 16, 0, 0, 0, + 0, 0, 70, 142, 32, 0, + 0, 0, 0, 0, 4, 0, + 0, 0, 6, 16, 16, 0, + 1, 0, 0, 0, 70, 14, + 16, 0, 0, 0, 0, 0, + 0, 0, 0, 8, 242, 32, + 16, 0, 0, 0, 0, 0, + 70, 14, 16, 0, 0, 0, + 0, 0, 70, 142, 32, 0, + 0, 0, 0, 0, 7, 0, + 0, 0, 54, 0, 0, 5, + 242, 32, 16, 0, 1, 0, + 0, 0, 70, 30, 16, 0, + 2, 0, 0, 0, 56, 0, + 0, 8, 242, 0, 16, 0, + 0, 0, 0, 0, 86, 21, + 16, 0, 0, 0, 0, 0, + 70, 142, 32, 0, 0, 0, + 0, 0, 1, 0, 0, 0, + 50, 0, 0, 10, 242, 0, + 16, 0, 0, 0, 0, 0, + 70, 142, 32, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 6, 16, 16, 0, 0, 0, + 0, 0, 70, 14, 16, 0, + 0, 0, 0, 0, 50, 0, + 0, 10, 242, 0, 16, 0, + 0, 0, 0, 0, 70, 142, + 32, 0, 0, 0, 0, 0, + 2, 0, 0, 0, 166, 26, + 16, 0, 0, 0, 0, 0, + 70, 14, 16, 0, 0, 0, + 0, 0, 50, 0, 0, 10, + 242, 32, 16, 0, 2, 0, + 0, 0, 70, 142, 32, 0, + 0, 0, 0, 0, 3, 0, + 0, 0, 246, 31, 16, 0, + 0, 0, 0, 0, 70, 14, + 16, 0, 0, 0, 0, 0, + 62, 0, 0, 1, 83, 84, + 65, 84, 148, 0, 0, 0, + 9, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, + 6, 0, 0, 0, 7, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0 +}; +static uint8_t _sfons_fs_bin[] = { + 68, 88, 66, 67, 145, 182, + 34, 101, 114, 183, 46, 3, + 176, 243, 147, 199, 109, 42, + 196, 114, 1, 0, 0, 0, + 176, 2, 0, 0, 5, 0, + 0, 0, 52, 0, 0, 0, + 232, 0, 0, 0, 56, 1, + 0, 0, 108, 1, 0, 0, + 20, 2, 0, 0, 82, 68, + 69, 70, 172, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 2, 0, 0, 0, + 60, 0, 0, 0, 0, 5, + 255, 255, 0, 145, 0, 0, + 132, 0, 0, 0, 82, 68, + 49, 49, 60, 0, 0, 0, + 24, 0, 0, 0, 32, 0, + 0, 0, 40, 0, 0, 0, + 36, 0, 0, 0, 12, 0, + 0, 0, 0, 0, 0, 0, + 124, 0, 0, 0, 3, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 1, 0, 0, 0, 1, 0, + 0, 0, 128, 0, 0, 0, + 2, 0, 0, 0, 5, 0, + 0, 0, 4, 0, 0, 0, + 255, 255, 255, 255, 0, 0, + 0, 0, 1, 0, 0, 0, + 13, 0, 0, 0, 115, 109, + 112, 0, 116, 101, 120, 0, + 77, 105, 99, 114, 111, 115, + 111, 102, 116, 32, 40, 82, + 41, 32, 72, 76, 83, 76, + 32, 83, 104, 97, 100, 101, + 114, 32, 67, 111, 109, 112, + 105, 108, 101, 114, 32, 49, + 48, 46, 49, 0, 73, 83, + 71, 78, 72, 0, 0, 0, + 2, 0, 0, 0, 8, 0, + 0, 0, 56, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 3, 0, 0, 0, + 0, 0, 0, 0, 15, 3, + 0, 0, 65, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 3, 0, 0, 0, + 1, 0, 0, 0, 15, 15, + 0, 0, 84, 69, 88, 67, + 79, 79, 82, 68, 0, 67, + 79, 76, 79, 82, 0, 171, + 79, 83, 71, 78, 44, 0, + 0, 0, 1, 0, 0, 0, + 8, 0, 0, 0, 32, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 3, 0, + 0, 0, 0, 0, 0, 0, + 15, 0, 0, 0, 83, 86, + 95, 84, 97, 114, 103, 101, + 116, 0, 171, 171, 83, 72, + 69, 88, 160, 0, 0, 0, + 80, 0, 0, 0, 40, 0, + 0, 0, 106, 8, 0, 1, + 90, 0, 0, 3, 0, 96, + 16, 0, 0, 0, 0, 0, + 88, 24, 0, 4, 0, 112, + 16, 0, 0, 0, 0, 0, + 85, 85, 0, 0, 98, 16, + 0, 3, 50, 16, 16, 0, + 0, 0, 0, 0, 98, 16, + 0, 3, 242, 16, 16, 0, + 1, 0, 0, 0, 101, 0, + 0, 3, 242, 32, 16, 0, + 0, 0, 0, 0, 104, 0, + 0, 2, 1, 0, 0, 0, + 69, 0, 0, 139, 194, 0, + 0, 128, 67, 85, 21, 0, + 242, 0, 16, 0, 0, 0, + 0, 0, 70, 16, 16, 0, + 0, 0, 0, 0, 70, 126, + 16, 0, 0, 0, 0, 0, + 0, 96, 16, 0, 0, 0, + 0, 0, 56, 0, 0, 7, + 242, 32, 16, 0, 0, 0, + 0, 0, 70, 14, 16, 0, + 0, 0, 0, 0, 70, 30, + 16, 0, 1, 0, 0, 0, + 62, 0, 0, 1, 83, 84, + 65, 84, 148, 0, 0, 0, + 3, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, + 3, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0 +}; +#elif defined(SOKOL_DUMMY_BACKEND) +static const char* _sfons_vs_src = ""; +static const char* _sfons_fs_src = ""; +#endif + +typedef struct _sfons_t { + sg_shader shd; + sgl_pipeline pip; + sg_image img; + int width, height; + bool img_dirty; +} _sfons_t; + +static int _sfons_render_create(void* user_ptr, int width, int height) { + SOKOL_ASSERT(user_ptr && (width > 8) && (height > 8)); + _sfons_t* sfons = (_sfons_t*) user_ptr; + + /* sokol-gl compatible shader which treats RED channel as alpha */ + if (sfons->shd.id == SG_INVALID_ID) { + sg_shader_desc shd_desc; + memset(&shd_desc, 0, sizeof(shd_desc)); + shd_desc.attrs[0].name = "position"; + shd_desc.attrs[1].name = "texcoord0"; + shd_desc.attrs[2].name = "color0"; + shd_desc.attrs[0].sem_name = "POSITION"; + shd_desc.attrs[1].sem_name = "TEXCOORD"; + shd_desc.attrs[2].sem_name = "COLOR"; + sg_shader_uniform_block_desc* ub = &shd_desc.vs.uniform_blocks[0]; + ub->size = 128; + ub->uniforms[0].name = "mvp"; + ub->uniforms[0].type = SG_UNIFORMTYPE_MAT4; + ub->uniforms[1].name = "tm"; + ub->uniforms[1].type = SG_UNIFORMTYPE_MAT4; + shd_desc.fs.images[0].name = "tex"; + shd_desc.fs.images[0].type = SG_IMAGETYPE_2D; + #if defined(SOKOL_D3D11) + shd_desc.vs.byte_code = _sfons_vs_bin; + shd_desc.vs.byte_code_size = sizeof(_sfons_vs_bin); + shd_desc.fs.byte_code = _sfons_fs_bin; + shd_desc.fs.byte_code_size = sizeof(_sfons_fs_bin); + #else + shd_desc.vs.source = _sfons_vs_src; + shd_desc.fs.source = _sfons_fs_src; + #endif + shd_desc.label = "sfons-shader"; + sfons->shd = sg_make_shader(&shd_desc); + } + + /* sokol-gl pipeline object */ + if (sfons->pip.id == SG_INVALID_ID) { + sg_pipeline_desc pip_desc; + memset(&pip_desc, 0, sizeof(pip_desc)); + pip_desc.shader = sfons->shd; + pip_desc.blend.enabled = true; + pip_desc.blend.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA; + pip_desc.blend.dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; + sfons->pip = sgl_make_pipeline(&pip_desc); + } + + /* create or re-create font atlas texture */ + if (sfons->img.id != SG_INVALID_ID) { + sg_destroy_image(sfons->img); + sfons->img.id = SG_INVALID_ID; + } + sfons->width = width; + sfons->height = height; + + SOKOL_ASSERT(sfons->img.id == SG_INVALID_ID); + sg_image_desc img_desc; + memset(&img_desc, 0, sizeof(img_desc)); + img_desc.width = sfons->width; + img_desc.height = sfons->height; + img_desc.min_filter = SG_FILTER_LINEAR; + img_desc.usage = SG_USAGE_DYNAMIC; + img_desc.pixel_format = SG_PIXELFORMAT_R8; + sfons->img = sg_make_image(&img_desc); + return 1; +} + +static int _sfons_render_resize(void* user_ptr, int width, int height) { + return _sfons_render_create(user_ptr, width, height); +} + +static void _sfons_render_update(void* user_ptr, int* rect, const unsigned char* data) { + SOKOL_ASSERT(user_ptr && rect && data); + _sfons_t* sfons = (_sfons_t*) user_ptr; + sfons->img_dirty = true; +} + +static void _sfons_render_draw(void* user_ptr, const float* verts, const float* tcoords, const unsigned int* colors, int nverts) { + SOKOL_ASSERT(user_ptr && verts && tcoords && colors && (nverts > 0)); + _sfons_t* sfons = (_sfons_t*) user_ptr; + sgl_enable_texture(); + sgl_texture(sfons->img); + sgl_push_pipeline(); + sgl_load_pipeline(sfons->pip); + sgl_begin_triangles(); + for (int i = 0; i < nverts; i++) { + sgl_v2f_t2f_c1i(verts[2*i+0], verts[2*i+1], tcoords[2*i+0], tcoords[2*i+1], colors[i]); + } + sgl_end(); + sgl_pop_pipeline(); + sgl_disable_texture(); +} + +static void _sfons_render_delete(void* user_ptr) { + SOKOL_ASSERT(user_ptr); + _sfons_t* sfons = (_sfons_t*) user_ptr; + if (sfons->img.id != SG_INVALID_ID) { + sg_destroy_image(sfons->img); + sfons->img.id = SG_INVALID_ID; + } + if (sfons->pip.id != SG_INVALID_ID) { + sgl_destroy_pipeline(sfons->pip); + sfons->pip.id = SG_INVALID_ID; + } + if (sfons->shd.id != SG_INVALID_ID) { + sg_destroy_shader(sfons->shd); + sfons->shd.id = SG_INVALID_ID; + } + SOKOL_FREE(sfons); +} + +SOKOL_API_IMPL FONScontext* sfons_create(int width, int height, int flags) { + SOKOL_ASSERT((width > 0) && (height > 0)); + FONSparams params; + _sfons_t* sfons = (_sfons_t*) SOKOL_MALLOC(sizeof(_sfons_t)); + memset(sfons, 0, sizeof(_sfons_t)); + memset(¶ms, 0, sizeof(params)); + params.width = width; + params.height = height; + params.flags = (unsigned char) flags; + params.renderCreate = _sfons_render_create; + params.renderResize = _sfons_render_resize; + params.renderUpdate = _sfons_render_update; + params.renderDraw = _sfons_render_draw; + params.renderDelete = _sfons_render_delete; + params.userPtr = sfons; + return fonsCreateInternal(¶ms); +} + +SOKOL_API_IMPL void sfons_destroy(FONScontext* ctx) { + SOKOL_ASSERT(ctx); + fonsDeleteInternal(ctx); +} + +SOKOL_API_IMPL void sfons_flush(FONScontext* ctx) { + SOKOL_ASSERT(ctx && ctx->params.userPtr); + _sfons_t* sfons = (_sfons_t*) ctx->params.userPtr; + if (sfons->img_dirty) { + sfons->img_dirty = false; + sg_image_content content; + memset(&content, 0, sizeof(content)); + content.subimage[0][0].ptr = ctx->texData; + content.subimage[0][0].size = sfons->width * sfons->height; + sg_update_image(sfons->img, &content); + } +} + +SOKOL_API_IMPL uint32_t sfons_rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { + return (r) | (g<<8) | (b<<16) | (a<<24); +} + +#endif /* SOKOL_FONTSTASH_IMPL */ diff --git a/util/sokol_text.h b/util/sokol_text.h deleted file mode 100644 index 393176e5..00000000 --- a/util/sokol_text.h +++ /dev/null @@ -1,201 +0,0 @@ -#pragma once -/* - sokol_text.h -- text render on top of fontstash.h, stb_truetype.h and sokol_gl.h - - WIP!!! - - Project URL: https://github.com/floooh/sokol - - Do this: - #define SOKOL_TEXT_IMPL - before you include this file in *one* C or C++ file to create the - implementation. - - ...optionally provide the following macros to override defaults: - - SOKOL_ASSERT(c) - your own assert macro (default: assert(c)) - SOKOL_MALLOC(s) - your own malloc function (default: malloc(s)) - SOKOL_FREE(p) - your own free function (default: free(p)) - SOKOL_API_DECL - public function declaration prefix (default: extern) - SOKOL_API_IMPL - public function implementation prefix (default: -) - SOKOL_LOG(msg) - your own logging function (default: puts(msg)) - SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false)) - - If sokol_text.h is compiled as a DLL, define the following before - including the declaration or implementation: - - SOKOL_DLL - - On Windows, SOKOL_DLL will define SOKOL_API_DECL as __declspec(dllexport) - or __declspec(dllimport) as needed. - - Include the following headers before including sokol_text.h: - - sokol_gfx.h - sokol_gl.h - - Additionally include the following headers before including the - sokol_text.h implementation: - - fontstash.h - stb_truetype.h - - The fontstash.h and matching stb_truetype.h headers can be found here: - - https://github.com/memononen/fontstash/tree/master/src - - - FIXME: documentation - - LICENSE - ======= - zlib/libpng license - - Copyright (c) 2018 Andre Weissflog - - This software is provided 'as-is', without any express or implied warranty. - In no event will the authors be held liable for any damages arising from the - use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software in a - product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not - be misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*/ -#define SOKOL_TEXT_INCLUDED (1) -#include <stdint.h> -#include <stdbool.h> - - -#if !defined(SOKOL_GFX_INCLUDED) -#error "Please include sokol_gfx.h before sokol_text.h" -#endif -#if !defined(SOKOL_GL_INCLUDED) -#error "Please include sokol_gl.h before sokol_text.h" -#endif - -#ifndef SOKOL_API_DECL -#if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_IMPL) -#define SOKOL_API_DECL __declspec(dllexport) -#elif defined(_WIN32) && defined(SOKOL_DLL) -#define SOKOL_API_DECL __declspec(dllimport) -#else -#define SOKOL_API_DECL extern -#endif -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* a sokol_text.h font handle */ -typedef struct stx_font_t { uint32_t id; } stx_font_t; - -/* setup parameters */ -typedef stx_desc_t { - int max_fonts; /* max number of fonts that will be added, default is 16 */ -} stx_desc_t; - -typedef enum stx_align_t { - STX_ALIGN_LEFT = (1<<0), - STX_ALIGN_CENTER = (1<<1), - STX_ALIGN_RIGHT = (1<<2), - STX_ALIGN_TOP = (1<<3), - STX_ALIGN_MIDDLE = (1<<4), - STX_ALIGN_BOTTOM = (1<<5), - STX_ALIGN_BASELINE = (1<<6), -} stx_align_t; - -/* font setup parameters */ -typedef stx_font_desc_t { - const char* name; - struct { - const void* ptr; /* pointer to TTF font data in memory */ - int size; /* size of TTF font data in number of bytes */ - } ttf_data; - int atlas_texture_width; /* width of atlas texture (default: 1024) */ - int atlas_texture_height; /* height of atlas texture (default: 1024) */ -} stx_font_desc_t; - -/* text bounding rectangle */ -typedef stx_bounds_t { - struct { float x, y; } min; - struct { float x, y; } max; - float width, height; -} stx_bounds_t; - -SOKOL_API_DECL void stx_setup(const stx_desc_t* desc); -SOKOL_API_DECL void stx_shutdown(void); -SOKOL_API_DECL stx_font_t stx_make_font(const stx_font_desc_t* desc); -SOKOL_API_DECL stx_destroy_font(stx_font_t font); - -SOKOL_API_DECL void stx_push_state(void); -SOKOL_API_DECL void stx_pop_state(void); -SOKOL_API_DECL void stx_clear_state(void); - -SOKOL_API_DECL void stx_font(stx_font_t font); -SOKOL_API_DECL void stx_size(float size); -SOKOL_API_DECL void stx_color_u32(uint32_t rgba); -SOKOL_API_DECL void stx_color_rgbaf(float r, float g, float b, float a); -SOKOL_API_DECL void stx_color_rgba8(uint8_t r, uint8_t g, uint8_t b, uint8_t a); -SOKOL_API_DECL void stx_spacing(float spacing); -SOKOL_API_DECL void stx_blur(float blur); -SOKOL_API_DECL void stx_align(stx_align_t align); - -SOKOL_API_DECL void stx_draw_string(float x, float y, const char* str); -SOKOL_API_DECL void stx_draw_slice(float x, float y, const char* str, const char* end); -SOKOL_API_DECL stx_bounds_t stx_bounds_string(float x, float y, const char* str); -SOKOL_API_DECL stx_bounds_t stx_bounds_slice(float x, float y, const char* str, const char* end); - -SOKOL_API_DECL stx_font_t stx_alloc_font(void); -SOKOL_API_DECL void stx_init_font(stx_font_t font, const stx_font_desc* desc); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -/*-- IMPLEMENTATION ----------------------------------------------------------*/ -#ifdef SOKOL_TEXT_IMPL -#define SOKOL_TEXT_IMPL_INCLUDED (1) - -#ifndef SOKOL_API_IMPL - #define SOKOL_API_IMPL -#endif -#ifndef SOKOL_DEBUG - #ifndef NDEBUG - #define SOKOL_DEBUG (1) - #endif -#endif -#ifndef SOKOL_ASSERT - #include <assert.h> - #define SOKOL_ASSERT(c) assert(c) -#endif -#ifndef SOKOL_MALLOC - #include <stdlib.h> - #define SOKOL_MALLOC(s) malloc(s) - #define SOKOL_FREE(p) free(p) -#endif -#ifndef SOKOL_LOG - #ifdef SOKOL_DEBUG - #include <stdio.h> - #define SOKOL_LOG(s) { SOKOL_ASSERT(s); puts(s); } - #else - #define SOKOL_LOG(s) - #endif -#endif -#ifndef SOKOL_UNREACHABLE - #define SOKOL_UNREACHABLE SOKOL_ASSERT(false) -#endif - - -#endif /* SOKOL_TEXT_IMPL */ |