/* Copyright (c) 2007 Scott Lembcke * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include "sokol/sokol_gfx.h" // #include "chipmunk/chipmunk_private.h" // #include "ChipmunkDemo.h" #include "ChipmunkDemoTextSupport.h" #include "VeraMoBd.ttf_sdf.h" //#define Scale 3.0f #define Scale 0.70f #define LineHeight (18.0f*Scale) // char -> glyph indexes generated by the lonesock tool. static int glyph_indexes[256]; cpTransform ChipmunkDemoTextMatrix; #define GLSL33(x) "#version 330\n" #x static sg_bindings bindings; static sg_pipeline pipeline; typedef struct {float x, y;} float2; typedef struct {uint8_t r, g, b, a;} RGBA8; typedef struct {float2 pos; float2 uv; RGBA8 color;} Vertex; typedef uint16_t Index; typedef struct { float U_vp_matrix[16]; } Uniforms; // Meh, just max out 16 bit index size. #define VERTEX_MAX (64*1024) #define INDEX_MAX (128*1024) static sg_buffer VertexBuffer, IndexBuffer; static size_t VertexCount, IndexCount; static Vertex Vertexes[VERTEX_MAX]; static uint16_t Indexes[INDEX_MAX]; void ChipmunkDemoTextInit(void) { VertexBuffer = sg_make_buffer(&(sg_buffer_desc){ .label = "ChipmunkDemoText Vertex Buffer", .size = VERTEX_MAX*sizeof(Vertex), .type = SG_BUFFERTYPE_VERTEXBUFFER, .usage = SG_USAGE_STREAM, }); IndexBuffer = sg_make_buffer(&(sg_buffer_desc){ .label = "ChipmunkDemoText Index Buffer", .size = INDEX_MAX*sizeof(Index), .type = SG_BUFFERTYPE_INDEXBUFFER, .usage = SG_USAGE_STREAM, }); sg_image sdf_texture = sg_make_image(&(sg_image_desc){ .label = "ChipmunkDemoText SDF Atlas", .width = sdf_tex_width, .height = sdf_tex_height, .pixel_format = SG_PIXELFORMAT_L8, .min_filter = SG_FILTER_LINEAR, .mag_filter = SG_FILTER_LINEAR, .wrap_u = SG_WRAP_CLAMP_TO_EDGE, .wrap_v = SG_WRAP_CLAMP_TO_EDGE, .content.subimage[0][0] = {.ptr = sdf_data, .size = sizeof(sdf_data)}, }); bindings = (sg_bindings){ .vertex_buffers[0] = VertexBuffer, .index_buffer = IndexBuffer, .fs_images[0] = sdf_texture, }; sg_shader shd = sg_make_shader(&(sg_shader_desc){ .vs.uniform_blocks[0] = { .size = sizeof(Uniforms), .uniforms[0] = {.name = "U_vp_matrix", .type = SG_UNIFORMTYPE_MAT4}, }, .vs.source = GLSL33( layout(location = 0) in vec2 IN_pos; layout(location = 1) in vec2 IN_uv; layout(location = 2) in vec4 IN_color; uniform mat4 U_vp_matrix; out struct { vec2 uv; vec4 color; } FRAG; void main(){ gl_Position = U_vp_matrix*vec4(IN_pos, 0, 1); FRAG.uv = IN_uv; FRAG.color = IN_color; } ), .fs.images[0] = {.name = "U_texture", .type = SG_IMAGETYPE_2D}, .fs.source = GLSL33( in struct { vec2 uv; vec4 color; } FRAG; uniform sampler2D U_texture; out vec4 OUT_color; void main(){ float sdf = texture(U_texture, FRAG.uv).r; float fw = 0.5*fwidth(sdf); float mask = smoothstep(0.5 - fw, 0.5 + fw, sdf); OUT_color = FRAG.color*mask; OUT_color.a = 0.25*smoothstep(0.0, 0.5, sdf); } ), }); pipeline = sg_make_pipeline(&(sg_pipeline_desc){ .shader = shd, .blend = { .enabled = true, .src_factor_rgb = SG_BLENDFACTOR_ONE, .dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA }, .index_type = SG_INDEXTYPE_UINT16, .layout = { .attrs = { [0] = {.offset = offsetof(Vertex, pos), .format = SG_VERTEXFORMAT_FLOAT2}, [1] = {.offset = offsetof(Vertex, uv), .format = SG_VERTEXFORMAT_FLOAT2}, [2] = {.offset = offsetof(Vertex, color), .format = SG_VERTEXFORMAT_UBYTE4N}, } } }); // Fill in the glyph index array. for(int i=0; i