| 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"cxx . . $ `<file>/demo/sokol/sokol_gfx.h`/ |
| 26 | |
| 27 | // #include "chipmunk/chipmunk_private.h" |
| 28 | // #include "ChipmunkDemo.h" |
| 29 | #include "ChipmunkDemoTextSupport.h"cxx . . $ `<file>/demo/ChipmunkDemoTextSupport.h`/ |
| 30 | |
| 31 | #include "VeraMoBd.ttf_sdf.h"cxx . . $ `<file>/demo/VeraMoBd.ttf_sdf.h`/ |
| 32 | |
| 33 | //#define Scale 3.0f |
| 34 | #define Scalecxx . . $ `demo/ChipmunkDemoTextSupport.c:34:9`! 0.70f |
| 35 | #define LineHeightcxx . . $ `demo/ChipmunkDemoTextSupport.c:35:9`! (18.0f*Scalecxx . . $ `demo/ChipmunkDemoTextSupport.c:34:9`!) |
| 36 | |
| 37 | // char -> glyph indexes generated by the lonesock tool. |
| 38 | static int glyph_indexescxx . . $ glyph_indexes.[256]; |
| 39 | |
| 40 | cpTransformcxx . . $ cpTransform# ChipmunkDemoTextMatrixcxx . . $ ChipmunkDemoTextMatrix.; |
| 41 | |
| 42 | #define GLSL33cxx . . $ `demo/ChipmunkDemoTextSupport.c:42:9`!(x) "#version 330\n" #x |
| 43 | |
| 44 | static sg_bindingscxx . . $ sg_bindings# bindingscxx . . $ bindings.; |
| 45 | static sg_pipelinecxx . . $ sg_pipeline# pipelinecxx . . $ pipeline.; |
| 46 | |
| 47 | typedef structcxx . . $ $anonymous_type_bbaa99136c730a2_0# {float xcxx . . $ $anonymous_type_bbaa99136c730a2_0#x., ycxx . . $ $anonymous_type_bbaa99136c730a2_0#y.;} float2cxx . . $ float2#; |
| 48 | typedef structcxx . . $ $anonymous_type_bbaa99136c730a2_1# {uint8_tcxx . . $ uint8_t# rcxx . . $ $anonymous_type_bbaa99136c730a2_1#r., gcxx . . $ $anonymous_type_bbaa99136c730a2_1#g., bcxx . . $ $anonymous_type_bbaa99136c730a2_1#b., acxx . . $ $anonymous_type_bbaa99136c730a2_1#a.;} RGBA8cxx . . $ RGBA8#; |
| 49 | typedef structcxx . . $ $anonymous_type_bbaa99136c730a2_2# {float2cxx . . $ float2# poscxx . . $ $anonymous_type_bbaa99136c730a2_2#pos.; float2cxx . . $ float2# uvcxx . . $ $anonymous_type_bbaa99136c730a2_2#uv.; RGBA8cxx . . $ RGBA8# colorcxx . . $ $anonymous_type_bbaa99136c730a2_2#color.;} Vertexcxx . . $ Vertex#; |
| 50 | typedef uint16_tcxx . . $ uint16_t# Indexcxx . . $ Index#; |
| 51 | |
| 52 | typedef structcxx . . $ $anonymous_type_bbaa99136c730a2_3# { |
| 53 | float U_vp_matrixcxx . . $ $anonymous_type_bbaa99136c730a2_3#U_vp_matrix.[16]; |
| 54 | } Uniformscxx . . $ Uniforms#; |
| 55 | |
| 56 | // Meh, just max out 16 bit index size. |
| 57 | #define VERTEX_MAXcxx . . $ `demo/ChipmunkDemoTextSupport.c:57:9`! (64*1024) |
| 58 | #define INDEX_MAXcxx . . $ `demo/ChipmunkDemoTextSupport.c:58:9`! (128*1024) |
| 59 | |
| 60 | static sg_buffercxx . . $ sg_buffer# VertexBuffercxx . . $ VertexBuffer., IndexBuffercxx . . $ IndexBuffer.; |
| 61 | static size_t VertexCountcxx . . $ VertexCount., IndexCountcxx . . $ IndexCount.; |
| 62 | |
| 63 | static Vertexcxx . . $ Vertex# Vertexescxx . . $ Vertexes.[VERTEX_MAXcxx . . $ `demo/ChipmunkDemoTextSupport.c:57:9`!]; |
| 64 | static uint16_tcxx . . $ uint16_t# Indexescxx . . $ Indexes.[INDEX_MAXcxx . . $ `demo/ChipmunkDemoTextSupport.c:58:9`!]; |
| 65 | |
| 66 | void |
| 67 | ChipmunkDemoTextInitcxx . . $ ChipmunkDemoTextInit(49f6e7a06ebc5aa8).(void) |
| 68 | { |
| 69 | VertexBuffercxx . . $ VertexBuffer. = sg_make_buffercxx . . $ sg_make_buffer(5efb77b03fd1b393).(&(sg_buffer_desccxx . . $ sg_buffer_desc#){ |
| 70 | .label = "ChipmunkDemoText Vertex Buffer", |
| 71 | .size = VERTEX_MAXcxx . . $ `demo/ChipmunkDemoTextSupport.c:57:9`!*sizeof(Vertexcxx . . $ Vertex#), |
| 72 | .type = SG_BUFFERTYPE_VERTEXBUFFERcxx . . $ SG_BUFFERTYPE_VERTEXBUFFER., |
| 73 | .usage = SG_USAGE_STREAMcxx . . $ SG_USAGE_STREAM., |
| 74 | }); |
| 75 | |
| 76 | IndexBuffercxx . . $ IndexBuffer. = sg_make_buffercxx . . $ sg_make_buffer(5efb77b03fd1b393).(&(sg_buffer_desccxx . . $ sg_buffer_desc#){ |
| 77 | .label = "ChipmunkDemoText Index Buffer", |
| 78 | .size = INDEX_MAXcxx . . $ `demo/ChipmunkDemoTextSupport.c:58:9`!*sizeof(Indexcxx . . $ Index#), |
| 79 | .type = SG_BUFFERTYPE_INDEXBUFFERcxx . . $ SG_BUFFERTYPE_INDEXBUFFER., |
| 80 | .usage = SG_USAGE_STREAMcxx . . $ SG_USAGE_STREAM., |
| 81 | }); |
| 82 | |
| 83 | sg_imagecxx . . $ sg_image# sdf_texture = sg_make_imagecxx . . $ sg_make_image(50a18bf9177d92a4).(&(sg_image_desccxx . . $ sg_image_desc#){ |
| 84 | .label = "ChipmunkDemoText SDF Atlas", |
| 85 | .width = sdf_tex_widthcxx . . $ sdf_tex_width., .height = sdf_tex_heightcxx . . $ sdf_tex_height., |
| 86 | .pixel_format = SG_PIXELFORMAT_L8cxx . . $ SG_PIXELFORMAT_L8., |
| 87 | .min_filter = SG_FILTER_LINEARcxx . . $ SG_FILTER_LINEAR., |
| 88 | .mag_filter = SG_FILTER_LINEARcxx . . $ SG_FILTER_LINEAR., |
| 89 | .wrap_u = SG_WRAP_CLAMP_TO_EDGEcxx . . $ SG_WRAP_CLAMP_TO_EDGE., |
| 90 | .wrap_v = SG_WRAP_CLAMP_TO_EDGEcxx . . $ SG_WRAP_CLAMP_TO_EDGE., |
| 91 | .content.subimage[0][0] = {.ptr = sdf_datacxx . . $ sdf_data., .size = sizeof(sdf_datacxx . . $ sdf_data.)}, |
| 92 | }); |
| 93 | |
| 94 | bindingscxx . . $ bindings. = (sg_bindingscxx . . $ sg_bindings#){ |
| 95 | .vertex_buffers[0] = VertexBuffercxx . . $ VertexBuffer., |
| 96 | .index_buffer = IndexBuffercxx . . $ IndexBuffer., |
| 97 | .fs_images[0] = sdf_texture, |
| 98 | }; |
| 99 | |
| 100 | sg_shadercxx . . $ sg_shader# shd = sg_make_shadercxx . . $ sg_make_shader(ab0d6bdcaa5aceeb).(&(sg_shader_desccxx . . $ sg_shader_desc#){ |
| 101 | .vs.uniform_blocks[0] = { |
| 102 | .size = sizeof(Uniformscxx . . $ Uniforms#), |
| 103 | .uniforms[0] = {.name = "U_vp_matrix", .type = SG_UNIFORMTYPE_MAT4cxx . . $ SG_UNIFORMTYPE_MAT4.}, |
| 104 | }, |
| 105 | .vs.source = GLSL33cxx . . $ `demo/ChipmunkDemoTextSupport.c:42:9`!( |
| 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_2Dcxx . . $ SG_IMAGETYPE_2D.}, |
| 124 | .fs.source = GLSL33cxx . . $ `demo/ChipmunkDemoTextSupport.c:42:9`!( |
| 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 | pipelinecxx . . $ pipeline. = sg_make_pipelinecxx . . $ sg_make_pipeline(6e5e446a35687b83).(&(sg_pipeline_desccxx . . $ sg_pipeline_desc#){ |
| 145 | .shader = shd, |
| 146 | .blend = { |
| 147 | .enabled = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!, |
| 148 | .src_factor_rgb = SG_BLENDFACTOR_ONEcxx . . $ SG_BLENDFACTOR_ONE., |
| 149 | .dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHAcxx . . $ SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA. |
| 150 | }, |
| 151 | .index_type = SG_INDEXTYPE_UINT16cxx . . $ SG_INDEXTYPE_UINT16., |
| 152 | .layout = { |
| 153 | .attrs = { |
| 154 | [0] = {.offset = offsetofcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:431:9`!(Vertex, pos), .format = SG_VERTEXFORMAT_FLOAT2cxx . . $ SG_VERTEXFORMAT_FLOAT2.}, |
| 155 | [1] = {.offset = offsetofcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:431:9`!(Vertex, uv), .format = SG_VERTEXFORMAT_FLOAT2cxx . . $ SG_VERTEXFORMAT_FLOAT2.}, |
| 156 | [2] = {.offset = offsetofcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:431:9`!(Vertex, color), .format = SG_VERTEXFORMAT_UBYTE4Ncxx . . $ SG_VERTEXFORMAT_UBYTE4N.}, |
| 157 | } |
| 158 | } |
| 159 | }); |
| 160 | |
| 161 | // Fill in the glyph index array. |
| 162 | for(int i=0; i<sdf_num_charscxx . . $ sdf_num_chars.; i++){ |
| 163 | int char_index = sdf_spacingcxx . . $ sdf_spacing.[i*8]; |
| 164 | glyph_indexescxx . . $ glyph_indexes.[char_index] = i; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static Vertexcxx . . $ Vertex# *push_vertexescxx . . $ push_vertexes(19a9b1b0b1bd3c57).(size_t vcount, const Indexcxx . . $ Index# *index_src, size_t icount){ |
| 169 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(VertexCount + vcount <= VERTEX_MAXcxx . . $ `demo/ChipmunkDemoTextSupport.c:57:9`! || IndexCount + icount <= INDEX_MAXcxx . . $ `demo/ChipmunkDemoTextSupport.c:58:9`!, "Geometry buffer full."); |
| 170 | |
| 171 | Vertexcxx . . $ Vertex# *vertex_dst = Vertexescxx . . $ Vertexes. + VertexCountcxx . . $ VertexCount.; |
| 172 | size_t base = VertexCountcxx . . $ VertexCount.; |
| 173 | VertexCountcxx . . $ VertexCount. += vcount; |
| 174 | |
| 175 | Indexcxx . . $ Index# *index_dst = Indexescxx . . $ Indexes. + IndexCountcxx . . $ IndexCount.; |
| 176 | for(size_t i = 0; i < icount; i++) index_dst[i] = index_src[i] + (Indexcxx . . $ Index#)base; |
| 177 | IndexCountcxx . . $ IndexCount. += icount; |
| 178 | |
| 179 | return vertex_dst; |
| 180 | } |
| 181 | |
| 182 | static float |
| 183 | PushCharcxx . . $ PushChar(862d6589e008f424).(int character, float x, float y) |
| 184 | { |
| 185 | int i = glyph_indexescxx . . $ glyph_indexes.[character]; |
| 186 | float w = (float)sdf_tex_widthcxx . . $ sdf_tex_width.; |
| 187 | float h = (float)sdf_tex_heightcxx . . $ sdf_tex_height.; |
| 188 | |
| 189 | float gw = (float)sdf_spacingcxx . . $ sdf_spacing.[i*8 + 3]; |
| 190 | float gh = (float)sdf_spacingcxx . . $ sdf_spacing.[i*8 + 4]; |
| 191 | |
| 192 | float txmin = sdf_spacingcxx . . $ sdf_spacing.[i*8 + 1]/w; |
| 193 | float tymin = sdf_spacingcxx . . $ sdf_spacing.[i*8 + 2]/h; |
| 194 | float txmax = txmin + gw/w; |
| 195 | float tymax = tymin + gh/h; |
| 196 | |
| 197 | float s = Scalecxx . . $ `demo/ChipmunkDemoTextSupport.c:34:9`!/scale_factorcxx . . $ scale_factor.; |
| 198 | float xmin = x + sdf_spacingcxx . . $ sdf_spacing.[i*8 + 5]/scale_factorcxx . . $ scale_factor.*Scalecxx . . $ `demo/ChipmunkDemoTextSupport.c:34:9`!; |
| 199 | float ymin = y + (sdf_spacingcxx . . $ sdf_spacing.[i*8 + 6]/scale_factorcxx . . $ scale_factor. - gh)*Scalecxx . . $ `demo/ChipmunkDemoTextSupport.c:34:9`!; |
| 200 | float xmax = xmin + gw*Scalecxx . . $ `demo/ChipmunkDemoTextSupport.c:34:9`!; |
| 201 | float ymax = ymin + gh*Scalecxx . . $ `demo/ChipmunkDemoTextSupport.c:34:9`!; |
| 202 | |
| 203 | RGBA8cxx . . $ RGBA8# color = {0xFD, 0xF6, 0xE3, 0xFF}; |
| 204 | Indexcxx . . $ Index# indexes[] = {0, 1, 2, 0, 2, 3}; |
| 205 | Vertexcxx . . $ Vertex# *vertexes = push_vertexescxx . . $ push_vertexes(19a9b1b0b1bd3c57).(4, indexes, 6); |
| 206 | vertexes[0] = (Vertexcxx . . $ Vertex#){{xmin, ymin}, {txmin, tymax}, color}; |
| 207 | vertexes[1] = (Vertexcxx . . $ Vertex#){{xmin, ymax}, {txmin, tymin}, color}; |
| 208 | vertexes[2] = (Vertexcxx . . $ Vertex#){{xmax, ymax}, {txmax, tymin}, color}; |
| 209 | vertexes[3] = (Vertexcxx . . $ Vertex#){{xmax, ymin}, {txmax, tymax}, color}; |
| 210 | |
| 211 | return sdf_spacingcxx . . $ sdf_spacing.[i*8 + 7]*s; |
| 212 | } |
| 213 | |
| 214 | #undef ChipmunkDemoTextDrawString |
| 215 | |
| 216 | void |
| 217 | ChipmunkDemoTextDrawStringcxx . . $ ChipmunkDemoTextDrawString(ba6331894cb75a04).(cpVectcxx . . $ cpVect# pos, char const *str) |
| 218 | { |
| 219 | float x = (float)pos.xcxx . . $ cpVect#x., y = (float)pos.ycxx . . $ cpVect#y.; |
| 220 | |
| 221 | for(size_t i=0, len=strlencxx . . $ strlen(751346f8406fd082).(str); i<len; i++){ |
| 222 | if(str[i] == '\n'){ |
| 223 | y -= LineHeightcxx . . $ `demo/ChipmunkDemoTextSupport.c:35:9`!; |
| 224 | x = (float)pos.xcxx . . $ cpVect#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)PushCharcxx . . $ PushChar(862d6589e008f424).(str[i], x, y); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void |
| 234 | ChipmunkDemoTextFlushRenderercxx . . $ ChipmunkDemoTextFlushRenderer(49f6e7a06ebc5aa8).(void) |
| 235 | { |
| 236 | cpTransformcxx . . $ cpTransform# t = ChipmunkDemoTextMatrixcxx . . $ ChipmunkDemoTextMatrix.; |
| 237 | Uniformscxx . . $ Uniforms# uniforms = { |
| 238 | .U_vp_matrix = {(float)t.acxx . . $ cpTransform#a. , (float)t.bcxx . . $ cpTransform#b. , 0.0f, 0.0f, (float)t.ccxx . . $ cpTransform#c. , (float)t.dcxx . . $ cpTransform#d. , 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, (float)t.txcxx . . $ cpTransform#tx., (float)t.tycxx . . $ cpTransform#ty., 0.0f, 1.0f}, |
| 239 | }; |
| 240 | |
| 241 | sg_update_buffercxx . . $ sg_update_buffer(5c59391e5d00dba1).(VertexBuffercxx . . $ VertexBuffer., Vertexescxx . . $ Vertexes., VertexCountcxx . . $ VertexCount.*sizeof(Vertexcxx . . $ Vertex#)); |
| 242 | sg_update_buffercxx . . $ sg_update_buffer(5c59391e5d00dba1).(IndexBuffercxx . . $ IndexBuffer., Indexescxx . . $ Indexes., IndexCountcxx . . $ IndexCount.*sizeof(Indexcxx . . $ Index#)); |
| 243 | |
| 244 | sg_apply_pipelinecxx . . $ sg_apply_pipeline(fa06e78c216ede9c).(pipelinecxx . . $ pipeline.); |
| 245 | sg_apply_bindingscxx . . $ sg_apply_bindings(5f6aac639b109626).(&bindingscxx . . $ bindings.); |
| 246 | sg_apply_uniformscxx . . $ sg_apply_uniforms(d4a2e1eb7e556ec4).(SG_SHADERSTAGE_VScxx . . $ SG_SHADERSTAGE_VS., 0, &uniforms, sizeof(Uniformscxx . . $ Uniforms#)); |
| 247 | sg_drawcxx . . $ sg_draw(b1146eedae1e1d0b).(0, IndexCountcxx . . $ IndexCount., 1); |
| 248 | |
| 249 | VertexCountcxx . . $ VertexCount. = 0; |
| 250 | IndexCountcxx . . $ IndexCount. = 0; |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | ChipmunkDemoTextClearRenderercxx . . $ ChipmunkDemoTextClearRenderer(49f6e7a06ebc5aa8).(void) |
| 255 | { |
| 256 | VertexCountcxx . . $ VertexCount. = IndexCountcxx . . $ IndexCount. = 0; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | static size_t PushedVertexCountcxx . . $ PushedVertexCount., PushedIndexCountcxx . . $ PushedIndexCount.; |
| 261 | |
| 262 | void |
| 263 | ChipmunkDemoTextPushRenderercxx . . $ ChipmunkDemoTextPushRenderer(49f6e7a06ebc5aa8).(void) |
| 264 | { |
| 265 | PushedVertexCountcxx . . $ PushedVertexCount. = VertexCountcxx . . $ VertexCount.; |
| 266 | PushedIndexCountcxx . . $ PushedIndexCount. = IndexCountcxx . . $ IndexCount.; |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | ChipmunkDemoTextPopRenderercxx . . $ ChipmunkDemoTextPopRenderer(49f6e7a06ebc5aa8).(void) |
| 271 | { |
| 272 | VertexCountcxx . . $ VertexCount. = PushedVertexCountcxx . . $ PushedVertexCount.; |
| 273 | IndexCountcxx . . $ IndexCount. = PushedIndexCountcxx . . $ PushedIndexCount.; |
| 274 | } |