| 1 | /* Copyright (c) 2007 Scott Lembcke |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | * of this software and associated documentation files (the "Software"), to deal |
| 5 | * in the Software without restriction, including without limitation the rights |
| 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | * copies of the Software, and to permit persons to whom the Software is |
| 8 | * furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | * SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | #include <stddef.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "sokol/sokol_gfx.h" |
| 26 | |
| 27 | // #include "chipmunk/chipmunk_private.h" |
| 28 | // #include "ChipmunkDemo.h" |
| 29 | #include "ChipmunkDemoTextSupport.h" |
| 30 | |
| 31 | #include "VeraMoBd.ttf_sdf.h" |
| 32 | |
| 33 | //#define Scale 3.0f |
| 34 | #define Scale 0.70f |
| 35 | #define LineHeight (18.0f*Scale) |
| 36 | |
| 37 | // char -> glyph indexes generated by the lonesock tool. |
| 38 | static int glyph_indexes[256]; |
| 39 | |
| 40 | cpTransform ChipmunkDemoTextMatrix; |
| 41 | |
| 42 | #define GLSL33(x) "#version 330\n" #x |
| 43 | |
| 44 | static sg_bindings bindings; |
| 45 | static sg_pipeline pipeline; |
| 46 | |
| 47 | typedef struct {float x, y;} float2; |
| 48 | typedef struct {uint8_t r, g, b, a;} RGBA8; |
| 49 | typedef struct {float2 pos; float2 uv; RGBA8 color;} Vertex; |
| 50 | typedef uint16_t Index; |
| 51 | |
| 52 | typedef struct { |
| 53 | float U_vp_matrix[16]; |
| 54 | } Uniforms; |
| 55 | |
| 56 | // Meh, just max out 16 bit index size. |
| 57 | #define VERTEX_MAX (64*1024) |
| 58 | #define INDEX_MAX (128*1024) |
| 59 | |
| 60 | static sg_buffer VertexBuffer, IndexBuffer; |
| 61 | static size_t VertexCount, IndexCount; |
| 62 | |
| 63 | static Vertex Vertexes[VERTEX_MAX]; |
| 64 | static uint16_t Indexes[INDEX_MAX]; |
| 65 | |
| 66 | void |
| 67 | ChipmunkDemoTextInit(void) |
| 68 | { |
| 69 | VertexBuffer = sg_make_buffer(&(sg_buffer_desc){ |
| 70 | .label = "ChipmunkDemoText Vertex Buffer", |
| 71 | .size = VERTEX_MAX*sizeof(Vertex), |
| 72 | .type = SG_BUFFERTYPE_VERTEXBUFFER, |
| 73 | .usage = SG_USAGE_STREAM, |
| 74 | }); |
| 75 | |
| 76 | IndexBuffer = sg_make_buffer(&(sg_buffer_desc){ |
| 77 | .label = "ChipmunkDemoText Index Buffer", |
| 78 | .size = INDEX_MAX*sizeof(Index), |
| 79 | .type = SG_BUFFERTYPE_INDEXBUFFER, |
| 80 | .usage = SG_USAGE_STREAM, |
| 81 | }); |
| 82 | |
| 83 | sg_image sdf_texture = sg_make_image(&(sg_image_desc){ |
| 84 | .label = "ChipmunkDemoText SDF Atlas", |
| 85 | .width = sdf_tex_width, .height = sdf_tex_height, |
| 86 | .pixel_format = SG_PIXELFORMAT_L8, |
| 87 | .min_filter = SG_FILTER_LINEAR, |
| 88 | .mag_filter = SG_FILTER_LINEAR, |
| 89 | .wrap_u = SG_WRAP_CLAMP_TO_EDGE, |
| 90 | .wrap_v = SG_WRAP_CLAMP_TO_EDGE, |
| 91 | .content.subimage[0][0] = {.ptr = sdf_data, .size = sizeof(sdf_data)}, |
| 92 | }); |
| 93 | |
| 94 | bindings = (sg_bindings){ |
| 95 | .vertex_buffers[0] = VertexBuffer, |
| 96 | .index_buffer = IndexBuffer, |
| 97 | .fs_images[0] = sdf_texture, |
| 98 | }; |
| 99 | |
| 100 | sg_shader shd = sg_make_shader(&(sg_shader_desc){ |
| 101 | .vs.uniform_blocks[0] = { |
| 102 | .size = sizeof(Uniforms), |
| 103 | .uniforms[0] = {.name = "U_vp_matrix", .type = SG_UNIFORMTYPE_MAT4}, |
| 104 | }, |
| 105 | .vs.source = GLSL33( |
| 106 | layout(location = 0) in vec2 IN_pos; |
| 107 | layout(location = 1) in vec2 IN_uv; |
| 108 | layout(location = 2) in vec4 IN_color; |
| 109 | |
| 110 | uniform mat4 U_vp_matrix; |
| 111 | |
| 112 | out struct { |
| 113 | vec2 uv; |
| 114 | vec4 color; |
| 115 | } FRAG; |
| 116 | |
| 117 | void main(){ |
| 118 | gl_Position = U_vp_matrix*vec4(IN_pos, 0, 1); |
| 119 | FRAG.uv = IN_uv; |
| 120 | FRAG.color = IN_color; |
| 121 | } |
| 122 | ), |
| 123 | .fs.images[0] = {.name = "U_texture", .type = SG_IMAGETYPE_2D}, |
| 124 | .fs.source = GLSL33( |
| 125 | in struct { |
| 126 | vec2 uv; |
| 127 | vec4 color; |
| 128 | } FRAG; |
| 129 | |
| 130 | uniform sampler2D U_texture; |
| 131 | out vec4 OUT_color; |
| 132 | |
| 133 | void main(){ |
| 134 | float sdf = texture(U_texture, FRAG.uv).r; |
| 135 | float fw = 0.5*fwidth(sdf); |
| 136 | float mask = smoothstep(0.5 - fw, 0.5 + fw, sdf); |
| 137 | |
| 138 | OUT_color = FRAG.color*mask; |
| 139 | OUT_color.a = 0.25*smoothstep(0.0, 0.5, sdf); |
| 140 | } |
| 141 | ), |
| 142 | }); |
| 143 | |
| 144 | pipeline = sg_make_pipeline(&(sg_pipeline_desc){ |
| 145 | .shader = shd, |
| 146 | .blend = { |
| 147 | .enabled = true, |
| 148 | .src_factor_rgb = SG_BLENDFACTOR_ONE, |
| 149 | .dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA |
| 150 | }, |
| 151 | .index_type = SG_INDEXTYPE_UINT16, |
| 152 | .layout = { |
| 153 | .attrs = { |
| 154 | [0] = {.offset = offsetof(Vertex, pos), .format = SG_VERTEXFORMAT_FLOAT2}, |
| 155 | [1] = {.offset = offsetof(Vertex, uv), .format = SG_VERTEXFORMAT_FLOAT2}, |
| 156 | [2] = {.offset = offsetof(Vertex, color), .format = SG_VERTEXFORMAT_UBYTE4N}, |
| 157 | } |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | // Fill in the glyph index array. |
| 162 | for(int i=0; i<sdf_num_chars; i++){ |
| 163 | int char_index = sdf_spacing[i*8]; |
| 164 | glyph_indexes[char_index] = i; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static Vertex *push_vertexes(size_t vcount, const Index *index_src, size_t icount){ |
| 169 | cpAssertHard(VertexCount + vcount <= VERTEX_MAX || IndexCount + icount <= INDEX_MAX, "Geometry buffer full."); |
| 170 | |
| 171 | Vertex *vertex_dst = Vertexes + VertexCount; |
| 172 | size_t base = VertexCount; |
| 173 | VertexCount += vcount; |
| 174 | |
| 175 | Index *index_dst = Indexes + IndexCount; |
| 176 | for(size_t i = 0; i < icount; i++) index_dst[i] = index_src[i] + (Index)base; |
| 177 | IndexCount += icount; |
| 178 | |
| 179 | return vertex_dst; |
| 180 | } |
| 181 | |
| 182 | static float |
| 183 | PushChar(int character, float x, float y) |
| 184 | { |
| 185 | int i = glyph_indexes[character]; |
| 186 | float w = (float)sdf_tex_width; |
| 187 | float h = (float)sdf_tex_height; |
| 188 | |
| 189 | float gw = (float)sdf_spacing[i*8 + 3]; |
| 190 | float gh = (float)sdf_spacing[i*8 + 4]; |
| 191 | |
| 192 | float txmin = sdf_spacing[i*8 + 1]/w; |
| 193 | float tymin = sdf_spacing[i*8 + 2]/h; |
| 194 | float txmax = txmin + gw/w; |
| 195 | float tymax = tymin + gh/h; |
| 196 | |
| 197 | float s = Scale/scale_factor; |
| 198 | float xmin = x + sdf_spacing[i*8 + 5]/scale_factor*Scale; |
| 199 | float ymin = y + (sdf_spacing[i*8 + 6]/scale_factor - gh)*Scale; |
| 200 | float xmax = xmin + gw*Scale; |
| 201 | float ymax = ymin + gh*Scale; |
| 202 | |
| 203 | RGBA8 color = {0xFD, 0xF6, 0xE3, 0xFF}; |
| 204 | Index indexes[] = {0, 1, 2, 0, 2, 3}; |
| 205 | Vertex *vertexes = push_vertexes(4, indexes, 6); |
| 206 | vertexes[0] = (Vertex){{xmin, ymin}, {txmin, tymax}, color}; |
| 207 | vertexes[1] = (Vertex){{xmin, ymax}, {txmin, tymin}, color}; |
| 208 | vertexes[2] = (Vertex){{xmax, ymax}, {txmax, tymin}, color}; |
| 209 | vertexes[3] = (Vertex){{xmax, ymin}, {txmax, tymax}, color}; |
| 210 | |
| 211 | return sdf_spacing[i*8 + 7]*s; |
| 212 | } |
| 213 | |
| 214 | #undef ChipmunkDemoTextDrawString |
| 215 | |
| 216 | void |
| 217 | ChipmunkDemoTextDrawString(cpVect pos, char const *str) |
| 218 | { |
| 219 | float x = (float)pos.x, y = (float)pos.y; |
| 220 | |
| 221 | for(size_t i=0, len=strlen(str); i<len; i++){ |
| 222 | if(str[i] == '\n'){ |
| 223 | y -= LineHeight; |
| 224 | x = (float)pos.x; |
| 225 | // } else if(str[i] == '*'){ // print out the last demo key |
| 226 | // glutBitmapCharacter(GLUT_BITMAP_HELVETICA_10, 'A' + demoCount - 1); |
| 227 | } else { |
| 228 | x += (float)PushChar(str[i], x, y); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void |
| 234 | ChipmunkDemoTextFlushRenderer(void) |
| 235 | { |
| 236 | cpTransform t = ChipmunkDemoTextMatrix; |
| 237 | Uniforms uniforms = { |
| 238 | .U_vp_matrix = {(float)t.a , (float)t.b , 0.0f, 0.0f, (float)t.c , (float)t.d , 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, (float)t.tx, (float)t.ty, 0.0f, 1.0f}, |
| 239 | }; |
| 240 | |
| 241 | sg_update_buffer(VertexBuffer, Vertexes, VertexCount*sizeof(Vertex)); |
| 242 | sg_update_buffer(IndexBuffer, Indexes, IndexCount*sizeof(Index)); |
| 243 | |
| 244 | sg_apply_pipeline(pipeline); |
| 245 | sg_apply_bindings(&bindings); |
| 246 | sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &uniforms, sizeof(Uniforms)); |
| 247 | sg_draw(0, IndexCount, 1); |
| 248 | |
| 249 | VertexCount = 0; |
| 250 | IndexCount = 0; |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | ChipmunkDemoTextClearRenderer(void) |
| 255 | { |
| 256 | VertexCount = IndexCount = 0; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | static size_t PushedVertexCount, PushedIndexCount; |
| 261 | |
| 262 | void |
| 263 | ChipmunkDemoTextPushRenderer(void) |
| 264 | { |
| 265 | PushedVertexCount = VertexCount; |
| 266 | PushedIndexCount = IndexCount; |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | ChipmunkDemoTextPopRenderer(void) |
| 271 | { |
| 272 | VertexCount = PushedVertexCount; |
| 273 | IndexCount = PushedIndexCount; |
| 274 | } |