summaryrefslogtreecommitdiff
path: root/src/render/gl_2d_font.cpp
blob: 329f0be217231fe9ea2f5454afc199bf2f11999d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "gl_2d_font.h"
#include "gl.h"

FT_Library libft = NULL;

THREAD_MUTEX font_mutex;

void freetype_init() {
  U32 err;

  if( !libft ) {
    err = FT_Init_FreeType( &libft );

    if( err )
      dlog( "gl_font_create() : FT_Init_FreeType() failed\n" );
  }
}

void gl_font_create_bitmap( GL_FONT* font ) {
  U32 char_width = (font->face->size->metrics.max_advance >> 6) + 1;
  U32 char_height = (font->face->size->metrics.height >> 6) + 1;
  U32 max_width = char_width * 16;
  U32 max_height = char_height * 16;

  if( !max_width || !max_height ) {
    dlog( "gl_font_create() : invalid font size\n" );
    return;
  }

  font->bitmap = (U32*)malloc( sizeof(U32) * max_width * max_height );
  memset( font->bitmap, 0, sizeof(U32) * max_width * max_height );
  font->bitmap_width = max_width;
  font->bitmap_height = max_height;
  font->char_width = char_width;
  font->char_height = char_height;

  U32 cur_x = 0, cur_y = 1;
  for( I32 c = 0; c < GL_FONT_MAX_GLYPHS; ++c ) {
    U32 err = FT_Load_Char( font->face, c, FT_LOAD_RENDER );
    if( !err ) {
      FT_Bitmap* bitmap = &font->face->glyph->bitmap;
      for( U32 y = 0; y < bitmap->rows; ++y ) {
        for( U32 x = 0; x < bitmap->width; ++x ) {
          U32 dest_x = x + cur_x;
          U32 dest_y = y + cur_y;

          U8 pixel = bitmap->buffer[x + bitmap->width * y];
          U32 out_pixel = 0x00FFFFFF;
          out_pixel |= ( pixel << 24 );
          font->bitmap[dest_x + max_width * dest_y] = out_pixel;
        }
      }

      font->glyphs[c].offset_x = (F32)cur_x;
      font->glyphs[c].offset_y = (F32)cur_y;
      font->glyphs[c].width = bitmap->width;
      font->glyphs[c].height = bitmap->rows;
      font->glyphs[c].advance_x = (F32)(font->face->glyph->metrics.horiAdvance >> 6);
      font->glyphs[c].advance_y = (F32)(char_height - (font->face->glyph->metrics.horiBearingY >> 6));
      font->glyphs[c].bearing = (F32)(font->face->glyph->metrics.horiBearingX >> 6);
    }
    else {
      dlog( "gl_font_create() : FT_Load_Char() failed (%s: %c) (%x)\n", font->name, c, err );
    }

    if( cur_x + char_width >= max_width ) {
      cur_x = 0;
      cur_y += char_height;
    } else {
      cur_x += char_width;
    }
  }
}

void gl_font_create_atlas( GL_DATA* gl, GL_FONT* font ) {
  if( !font->bitmap ) {
    dlog( "gl_font_create_atlas() : no bitmap\n" );
    return;
  }

  char texture_name[256];
  sprintf( texture_name, "%s_%d_atlas", font->name, font->size );

  font->atlas = gl_texture_from_bitmap( gl,
    texture_name,
    (U8*)font->bitmap,
    font->bitmap_width,
    font->bitmap_height
  );

  free( font->bitmap );
}

GL_FONT* gl_font_create( GL_DATA* gl, const char* path, I32 size ) {
  freetype_init();

  if( size <= 2 ) {
    dlog( "gl_font_create() : size too small.\n" );
    return 0;
  }

  char full_path[256];
  GL_FONT* font = (GL_FONT*)malloc( sizeof( GL_FONT ) );
  font->size = size;
  font->name = path;

  sprintf( full_path, "../assets/fonts/%s", path );
  U32 err = FT_New_Face( libft, full_path, 0, &font->face );

  if( err ) {
    dlog( "gl_font_create() : FT_New_Face() failed (%x)\n", err );
    free( font );
    return 0;
  }

  err = FT_Set_Pixel_Sizes( font->face, 0, size );
  if( err ) {
    dlog( "gl_font_create() : FT_Set_Char_Size() failed\n" );
    free( font );
    return 0;
  }

  gl_font_create_bitmap( font );
  FT_Done_Face( font->face );

  gl_font_create_atlas( gl, font );
  gl->fonts.push( font );
  return font;
}

void gl_font_destroy( GL_DATA* gl, GL_FONT* font ) {
  gl_texture_destroy( gl, font->atlas );
  I32 idx = gl->fonts.idx_of( font );
  if( idx != -1 )
    gl->fonts.erase( idx );

  free( font );
}


void gl_font_calc_vertices_uvs(
  GL_FONT* font,
  VEC2 origin,
  const char* text,
  F32 _scale,
  VERTEX* vertices,
  U16* indices,
  VEC2* coords,
  CLR clr
) {
  U32 len = (U32)strlen( text );

  F32 cur_x = origin.x;
  F32 cur_y = origin.y;

  for( U32 i = 0; i < len; ++i ) {
    VERTEX* v = &vertices[i * 6];
    U16* idx = &indices[i * 6];

    if( text[i] == '\n' ) {
      v[0] = v[1] = v[2] = v[3] = v[4] = v[5] = { { cur_x, cur_y }, {} };
      idx[0] = idx[1] = idx[2] = idx[3] = idx[4] = idx[5] = i * 6;

      cur_x = origin.x;
      cur_y += (F32)font->char_height * _scale;
      continue;
    }

    U32 c = (U8)text[i];
    FONT_GLYPH* glyph = &font->glyphs[c];
    F32 final_y = cur_y + glyph->advance_y * _scale;
    F32 final_x = cur_x + (F32)glyph->bearing * _scale;

    v[0].pos = { final_x, final_y };
    v[1].pos = { final_x + glyph->width * _scale, final_y };
    v[2].pos = { final_x, final_y + glyph->height * _scale };
    v[3].pos = { final_x + glyph->width * _scale, final_y + glyph->height * _scale };
    v[4].pos = { final_x, final_y + glyph->height * _scale };
    v[5].pos = { final_x + glyph->width * _scale, final_y };

    idx[0] = i * 6;
    idx[1] = i * 6 + 1;
    idx[2] = i * 6 + 2;
    idx[3] = i * 6 + 3;
    idx[4] = i * 6 + 4;
    idx[5] = i * 6 + 5;

    v[0].uv = { glyph->offset_x / (F32)font->bitmap_width, glyph->offset_y / font->bitmap_height };
    v[1].uv = { (glyph->offset_x + glyph->width) / (F32)font->bitmap_width, glyph->offset_y / font->bitmap_height };
    v[2].uv = { glyph->offset_x / (F32)font->bitmap_width, (glyph->offset_y + glyph->height) / font->bitmap_height };
    v[3].uv = { (glyph->offset_x + glyph->width) / (F32)font->bitmap_width, (glyph->offset_y + glyph->height) / font->bitmap_height };
    v[4].uv = { glyph->offset_x / (F32)font->bitmap_width, (glyph->offset_y + glyph->height) / font->bitmap_height };
    v[5].uv = { (glyph->offset_x + glyph->width) / (F32)font->bitmap_width, glyph->offset_y / font->bitmap_height };

    v[0].clr = v[1].clr = v[2].clr = v[3].clr = v[4].clr = v[5].clr = clr;
    v[0].sampler = v[1].sampler = v[2].sampler = v[3].sampler = v[4].sampler = v[5].sampler = 0;

    cur_x += glyph->advance_x * _scale;
  }
}

void gl_font_draw( GL_FONT* font, GL_SHADER_PROGRAM* shader, VEC2 origin, const char* text, CLR clr, F32 _scale ) {
  U32 len = strlen( text );
  VERTEX* vertices = (VERTEX*)malloc( sizeof(VERTEX) * 6 * len );
  U16* indices = (U16*)malloc( sizeof(U16) * 6 * len );
  VEC2* coords = (VEC2*)malloc( sizeof(VEC2) * 6 * len );

  gl_font_calc_vertices_uvs( font, origin, text, _scale, vertices, indices, coords, clr );
  glUseProgram( shader->id );
  glBindVertexArray( shader->gl->vao );

  glBindBuffer( GL_ARRAY_BUFFER, shader->gl->vbuffer );
  glBufferData( GL_ARRAY_BUFFER, sizeof(VERTEX) * 6 * len, vertices, GL_STATIC_DRAW );

  I32 position = glGetAttribLocation( shader->id, "in_pos" );
  I32 color = glGetAttribLocation( shader->id, "in_clr" );
  I32 texcoord = glGetAttribLocation( shader->id, "in_texcoord" );
  I32 sampler = glGetAttribLocation( shader->id, "in_sampler" );

  glEnableVertexAttribArray( position );
  glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->pos );

  glEnableVertexAttribArray( color );
  glVertexAttribPointer( color, 4, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->clr );

  glEnableVertexAttribArray( texcoord );
  glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->uv );

  glEnableVertexAttribArray( sampler );
  glVertexAttribPointer( sampler, 1, GL_UNSIGNED_BYTE, 1, sizeof(VERTEX), &( (VERTEX*)nullptr)->sampler );

  glActiveTexture( GL_TEXTURE0 );
  glBindTexture( GL_TEXTURE_2D, font->atlas->id );

  glBindBuffer( GL_ARRAY_BUFFER, 0 );

  glActiveTexture( GL_TEXTURE0 );

  glBindTexture( GL_TEXTURE_2D, font->atlas->id );

  glDrawElements( GL_TRIANGLES, len * 6, GL_UNSIGNED_SHORT, indices );
  glBindTexture( GL_TEXTURE_2D, 0 );
  
  free( vertices );
  free( indices );
  free( coords );

  glBindVertexArray( 0 );
}

void gl_font_textured(
  GL_FONT* font,
  GL_SHADER_PROGRAM* shader,
  VEC2 origin,
  const char* text,
  GL_TEX2D* tex,
  CLR clr,
  F32 _scale
) {
  U32 len = strlen( text );
  struct FONT_CUSTOM_VERTEX {
    VERTEX v;
    VEC2 uv2;
  };

  FONT_CUSTOM_VERTEX* custom_vertices = (FONT_CUSTOM_VERTEX*)malloc( sizeof(FONT_CUSTOM_VERTEX) * 6 * len );
  VERTEX* vertices = (VERTEX*)malloc( sizeof(VERTEX) * 6 * len );
  U16* indices = (U16*)malloc( sizeof(U16) * 6 * len );
  VEC2* coords = (VEC2*)malloc( sizeof(VEC2) * 6 * len );

  gl_font_calc_vertices_uvs( font, origin, text, _scale, vertices, indices, coords, clr );
  VEC2 dim = gl_font_dim( font, text, _scale );

  for( U32 i = 0; i < len * 6; ++i ) {
    custom_vertices[i].v = vertices[i];
    custom_vertices[i].uv2 = {
      (vertices[i].pos.x - origin.x) / dim.x,
      (vertices[i].pos.y - origin.y - vertices[0].pos.y) / dim.y
    };
  }

  glUseProgram( shader->id );
  glBindVertexArray( shader->gl->vao );

  glBindBuffer( GL_ARRAY_BUFFER, shader->gl->vbuffer );
  glBufferData( GL_ARRAY_BUFFER, sizeof(FONT_CUSTOM_VERTEX) * 6 * len, custom_vertices, GL_STATIC_DRAW ); 
  I32 position = glGetAttribLocation( shader->id, "in_pos" );
  glEnableVertexAttribArray( position );
  glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(FONT_CUSTOM_VERTEX), 0 );

  I32 texcoord = glGetAttribLocation( shader->id, "in_texcoord" );
  glEnableVertexAttribArray( texcoord );
  glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(FONT_CUSTOM_VERTEX), (void*)(sizeof(VEC2)) );

  I32 texcoord2 = glGetAttribLocation( shader->id, "in_texcoord2" );
  glEnableVertexAttribArray( texcoord2 );
  glVertexAttribPointer( texcoord2, 2, GL_FLOAT, 0, sizeof(FONT_CUSTOM_VERTEX), (void*)(sizeof(VERTEX)) );

  glBindBuffer( GL_ARRAY_BUFFER, 0 );

  I32 color = glGetUniformLocation( shader->id, "in_color" );
  glUniform4fv( color, 1, (F32*)&clr );

  glActiveTexture( GL_TEXTURE0 );
  glUniform1iv( glGetUniformLocation( shader->id, "in_sampler" ), 0, 0 );
  glBindTexture( GL_TEXTURE_2D, font->atlas->id );

  glDrawElements( GL_TRIANGLES, len * 6, GL_UNSIGNED_SHORT, indices );
  glBindTexture( GL_TEXTURE_2D, 0 );

  free( vertices );
  free( indices );
  free( coords );
  free( custom_vertices );

  glBindVertexArray( 0 );
}


VEC2 gl_font_dim( GL_FONT* font, const char* text, F32 _scale ) {
  U32 len = strlen( text );

  F32 cur_x = 0.f;
  F32 cur_y = font->char_height * _scale;
  F32 max_x = 0.f;

  for( U32 i = 0; i < len; ++i ) {
    if( text[i] == '\n' ) {
      cur_x = 0;
      cur_y += font->char_height * _scale;
      continue;
    }

    U32 c = (U8)text[i];
    FONT_GLYPH* glyph = &font->glyphs[c];
    cur_x += glyph->advance_x * _scale;
    if( cur_x > max_x )
      max_x = cur_x;
  }

  return { max_x, cur_y };
}

void gl_font_fit_into_box( GL_FONT* font, const char* source, char* out, F32 box_width, F32 box_height, F32 scale ) {
  U32 len = strlen( source );
  U32 last_space = 0;

  F32 cur_x = 0.f;
  for( U32 i = 0; i < len; ++i ) {
    if( source[i] == ' ' )
      last_space = i;

    if( source[i] == '\n' ) {
      cur_x = 0;
      continue;
    }

    FONT_GLYPH* glyph = &font->glyphs[(U8)source[i]];
    cur_x += glyph->advance_x * scale;

    out[i] = source[i];
    if( cur_x > box_width ) {
      if( last_space == 0 ) {
        out[i] = '\n';
        cur_x = 0;
      } else {
        out[last_space] = '\n';
        cur_x = 0;
        i = last_space;
        last_space = 0;
      }
    }

  }

  out[len] = '\0';
}