Handmade Hero»Forums»Code
107 posts
printing text is cumbersome... any advice for a better way to do it?
Edited by C_Worm on Reason: Initial post
Hey, so i've implemented a PrintText-system that prints text.
PrintText_Direct(): Prints a whole sentence at once.
PrintText_Delayed(): Prints a sentence one character at a time.

However, the way i've implemented this system makes the process of printing text a bit cumbersome and it goes like this:
----------------------------------------------------------------
1. define str_info
2. define str_entity
3. define str ( the acutal text string )
4. define strlen(str)

5. setup str_info ( position, color, print-delay count ) ( in gamecode.cpp )
6. PrintText_ Direct/Delayed ( in gamecode.cpp )
7. Update Vertices pos/col/texcoords with UpdateVertexArray_pos/col/texcoord() ( in platform.cpp )
8. Draw text DrawElements() ( in platform.cpp )
----------------------------------------------------------------

I can't think of a smoother system right now, but do you have any ideas on how to make it smoother, like get down the number of states in process, or are there like better ways to do this?

thanks,

I'll append src for
platform.cpp
gamecode.h
gamecode.cpp
gameDefines.h
-------------------------------------



gameDefines.h
 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
#define str_i_mainmenu 0
#define str_i_newgame 1
#define str_i_options 2
#define str_i_intro_01 3
#define str_i_intro_02 4
#define str_i_intro_03 5

// TEXT ENTITY INDICES
#define str_e_mainmenu 10100
#define str_e_newgame 10200
#define str_e_options 10300
#define str_e_intro_01 10400
#define str_e_intro_02 10500
#define str_e_intro_03 10600

// MENU STRING
#define str_mainmenu "Main Menu"
#define str_newgame "New Game"
#define str_options "Options WITH MANUYA EFPOKPOK"

// TEXT STRINGS
#define str_intro_01 "... ... ..."
#define str_intro_02 "what.. the... heck.. mmh... ?.."
#define str_intro_03 "... .. ."

i32 len_mainmenu = strlen(str_mainmenu);
i32 len_newgame = strlen(str_newgame);
i32 len_options = strlen(str_options);

i32 len_intro_01 = strlen(str_intro_01);
i32 len_intro_02 = strlen(str_intro_02);
i32 len_intro_03 = strlen(str_intro_03);

struct StringInfo
{
    float x;
    float y;
    V4f_COL col;
    char text[100];
    char delayedText[100];
    i32 currCharIndex;
    i32 delayCount;
    i32 delayFinish;
    bool delayInitialized;
    float strQuad_x;
    float strQuad_y;
    float strQuad_size_x;
    float strQuad_size_y;
    bool strQuad_pos_initialized = false;
    bool strQuad_size_initialized = false;
};


gamecode.h
  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
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#define STB_TRUETYPE_IMPLEMENTATION
#include "stb_truetype.h"

#include "gameDefines.h"

#include "gamemath.h"

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdint.h>

typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

#include "gameText.h"

struct GameState
{
    bool running;
    bool initializedEntities;
    V4f_COL clearColor;
    i32 currentState;
    i32 winWidth;
    i32 winHeight;
};

struct Time

{
    double currTime, fps, ms_per_frame, s_per_frame, timer, timerCountDown;
    bool started;
    char textBuffer[5];
};
struct MouseCoord
{
    double x, y;
};

struct Input
{
    
    bool keyPressed[1024];
    bool isPressed[1024];
    MouseCoord mouseCoord;
};

struct TextureInfo_Image
{
    i32 w, h, nChannels;
    u32 texture;
    unsigned char *bitmap;
};


struct TextureInfo_Font
{
    float pixelHeight;
    i32 bmWidth;
    i32 bmHeight;
    i32 fontBuffer_size;
    i32 bitmap_size;
    u32 texture;
    unsigned char *fontBuffer;
    unsigned char *bitmap;
    stbtt_bakedchar *bakedChar;
    
    void Free()
    {
        free(fontBuffer);
        free(bitmap);
    }
};

struct Entity
{
    V3f pos;
    V3f size;
    V4f_COL col;
    V2f texCoord;
    V2f texDimension;
    
    void Init(float _x, float _y, float _w, float _h)
    {
        pos.x = _x;
        pos.y = _y;
        pos.z = 0.0f;
        
        size.x = _w;
        size.y = _h;
        size.z = 0.0f;
        
        texCoord.x = 0.0f;
        texCoord.y = 0.0f;
        
        texDimension.x = 1.0f;
        texDimension.y = 1.0f;
    }
    
    void SetSolidColor(float _r, float _g, float _b, float _a)
    {
        col.r = _r;
        col.g = _g;
        col.b = _b;
        col.a = _a;
    }
    
    void SetTextureCoordsToFullMap()
    {
        texCoord.x = 0.0f;
        texCoord.y = 0.0f;
        texDimension.x = 1.0f;
        texDimension.y = 1.0f;
    }
};

#define UPDATEGAMECODE(name) extern "C" __declspec(dllexport) void name(GameState *gameState, Input *input, Time *time, Entity *entity, Vertex *vertex, TextureInfo_Font *ti_font, StringInfo *str_i)
UPDATEGAMECODE(UpdateGameCode);
typedef void (*PFNUPDATEGAMECODE)(GameState*, Input*, Time*, Entity*, Vertex*, TextureInfo_Font*, StringInfo*);


void RenderMainMenu(Entity *e, TextureInfo_Font *tif, GameState *gameState, Input *input, StringInfo *str_i);
void RenderOptionsMenu(Entity *e, TextureInfo_Font *tif, GameState *gameState, Input *input, StringInfo *str_i);
void RenderNewGameMenu(Entity *e, TextureInfo_Font *tif, GameState *gameState, Input *input, StringInfo *str_i);


void InitEntities(Entity *entity, TextureInfo_Font *ti_font, GameState *gameState, StringInfo *str_i);
void InitStringInfo(StringInfo *str_i, GameState *gameState);
void SetEntitiesPosToNULL(Entity *e, u32 start, u32 end);

void StartTimer(Time *t);

bool Collision_Entity_MouseCursor(Input *input, Entity *e, u32 min, u32 max);

void PrintText_Direct(char *text, StringInfo *str_i, Entity *e, TextureInfo_Font *ti_font);
void PrintText_Delayed(char *text, StringInfo *str_i, Entity *e, TextureInfo_Font *ti_font);

void UpdateVertexArray_Pos(Vertex *v, Entity *e, i32 min, i32 max);
void UpdateVertexArray_Col(Vertex *v, Entity *e, i32 min, i32 max);
void UpdateVertexArray_texCoord(Vertex *v, Entity *e, i32 min, i32 max);
void UpdateVertices_pos(Entity *e, Vertex *v);
void UpdateVertices_col(Entity *e, Vertex *v);
void UpdateVertices_texCoord(Entity *e, Vertex *v);


gamecode.cpp

  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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
UPDATEGAMECODE(UpdateGameCode)
{
    
    InitStringInfo(str_i, gameState);
    
    if(!gameState->initializedEntities)
    {
        InitEntities(entity, &ti_font[times32], gameState, str_i);
        gameState->initializedEntities = true;
        
    }
    // Resest Initialization
    if(input->keyPressed['R'])
    {
        gameState->initializedEntities = false;
    }
    
    switch(gameState->currentState)
    {
        case MAINMENU:
        {
            gameState->clearColor = col4f(0.05f, 0.05f, 0.05f, 1.0f);
            RenderMainMenu(entity, ti_font, gameState, input, str_i);
            
            StartTimer(&time[1]);
            PrintText_Direct(time[1].textBuffer, &str_i[0], &entity[10], &ti_font[times32]); 
            
        }break;
        
        case NEWGAME:
        {
            SetEntitiesPosToNULL(entity, 10, 20);
            
            gameState->clearColor = col4f(0.2f, 0.0f, 0.1f, 1.0f);
            
            RenderNewGameMenu(entity, ti_font, gameState, input, str_i);
            
        }break;
        
        case OPTIONS:
        {
            gameState->clearColor = col4f(0.0f, 0.0f, 0.2f, 1.0f);
            RenderOptionsMenu(entity, ti_font, gameState, input, str_i);
            
        }break;
    }
    
    // Dont have to update col and texCoord every time, do it once in the init()
    
    
    for(int i = 0; i < 2; i++)
    {
        
        UpdateVertices_pos(&entity[i], &vertex[i * 4]);
        UpdateVertices_col(&entity[i], &vertex[i * 4]);
        UpdateVertices_texCoord(&entity[i], &vertex[i * 4]);
        
    }
    
    UpdateVertexArray_Pos(vertex, entity, str_e_mainmenu, str_e_options + len_options);
    UpdateVertexArray_Col(vertex, entity, str_e_mainmenu, str_e_options + len_options);
    UpdateVertexArray_texCoord(vertex, entity, str_e_mainmenu, str_e_options + len_options);
    
    UpdateVertexArray_Pos(vertex, entity, 10, 20);
    UpdateVertexArray_Col(vertex, entity, 10, 20);
    UpdateVertexArray_texCoord(vertex, entity, 10, 20);
    
    
    UpdateVertexArray_Pos(vertex, entity, str_e_intro_01, str_e_intro_01 + len_intro_01);
    UpdateVertexArray_Col(vertex, entity, str_e_intro_01, str_e_intro_01 + len_intro_01);
    UpdateVertexArray_texCoord(vertex, entity, str_e_intro_01, str_e_intro_01 + len_intro_01);
    
    UpdateVertexArray_Pos(vertex, entity, str_e_intro_02, str_e_intro_02 + len_intro_02);
    UpdateVertexArray_Col(vertex, entity, str_e_intro_02, str_e_intro_02 + len_intro_02);
    UpdateVertexArray_texCoord(vertex, entity, str_e_intro_02, str_e_intro_02 + len_intro_02);
    
    UpdateVertexArray_Pos(vertex, entity, str_e_intro_03, str_e_intro_03 + len_intro_03);
    UpdateVertexArray_Col(vertex, entity, str_e_intro_03, str_e_intro_03 + len_intro_03);
    UpdateVertexArray_texCoord(vertex, entity, str_e_intro_03, str_e_intro_03 + len_intro_03);
    
    
    
}

void RenderMainMenu(Entity *e, TextureInfo_Font *tif, GameState *gameState, Input *input, StringInfo *str_i)
{
    
    SetEntitiesPosToNULL(e, 0, 1);
    SetEntitiesPosToNULL(e, str_e_mainmenu, len_mainmenu);
    
    if(input->keyPressed[Esc])
    {
        
        if(!input->isPressed[Esc])
        {
            input->isPressed[Esc] = true;
            gameState->running = false;
        }
        
    }
    else
    {
        input->isPressed[Esc] = false;
    }
    
    PrintText_Direct(str_mainmenu, &str_i[str_i_mainmenu], &e[str_e_mainmenu], &tif[times128]);
    PrintText_Delayed(str_newgame, &str_i[str_i_newgame], &e[str_e_newgame], &tif[times32]);
    PrintText_Delayed(str_options, &str_i[str_i_options], &e[str_e_options], &tif[times32]);
    
    if(Collision_Entity_MouseCursor(input, e, str_e_newgame, str_e_newgame + len_newgame))
    {
        
        if(input->keyPressed[MOUSE_L])
        {
            gameState->currentState = NEWGAME;
            
        }
        printf("state: %d", gameState->currentState);
        for(i32 i = str_e_newgame; i < str_e_newgame +  len_newgame; i++)
        {
            e[i].SetSolidColor(1.0f, 0.0f, 0.0f, 1.0f);
        }
        
    }
    
    if(Collision_Entity_MouseCursor(input, e, str_e_options, str_e_options + len_options))
    {
        
        if(input->keyPressed[MOUSE_L])
        {
            gameState->currentState = OPTIONS;
            
        }
        printf("state: %d", gameState->currentState);
        for(i32 i = str_e_options; i < str_e_options +  len_options; i++)
        {
            e[i].SetSolidColor(1.0f, 0.0f, 0.0f, 1.0f);
        }
        
    }
    
}

void RenderNewGameMenu(Entity *e, TextureInfo_Font *tif, GameState *gameState, Input *input, StringInfo *str_i)
{
    str_i[str_i_options].currCharIndex = 0;
    str_i[str_i_newgame].currCharIndex = 0;
    
    SetEntitiesPosToNULL(e, 0, 1);
    SetEntitiesPosToNULL(e, str_e_mainmenu, str_e_options + len_options);
    
    if(input->keyPressed[Esc])
    {
        
        if(!input->isPressed[Esc])
        {
            input->isPressed[Esc] = true;
            gameState->currentState = MAINMENU;
        }
        
    }
    else
    {
        input->isPressed[Esc] = false;
    }
    
    
    PrintText_Delayed(str_intro_01, &str_i[str_i_intro_01], &e[str_e_intro_01], &tif[times32]);  
    
    if((str_i[str_i_intro_01].currCharIndex) == len_intro_01)
    {
        static i32 pause = 0;
        if(pause == 60)
        {
            PrintText_Delayed(str_intro_02, &str_i[str_i_intro_02], &e[str_e_intro_02], &tif[times32]);  
        }
        else if(pause < 61)
        {
            pause++;
        }
    }
    PrintText_Delayed(str_intro_03, &str_i[str_i_intro_03], &e[str_e_intro_03], &tif[times32]);  
    
}

void RenderOptionsMenu(Entity *e, TextureInfo_Font *tif, GameState *gameState, Input *input, StringInfo *str_i)
{
    str_i[NEWGAME].currCharIndex = 0;
    str_i[OPTIONS].currCharIndex = 0;
    
    if(input->keyPressed[Esc])
    {
        
        if(!input->isPressed[Esc])
        {
            input->isPressed[Esc] = true;
            gameState->currentState = MAINMENU;
        }
        
    }
    else
    {
        input->isPressed[Esc] = false;
    }
    
    SetEntitiesPosToNULL(e, 0, 1);
    SetEntitiesPosToNULL(e, str_e_mainmenu, str_e_options + len_options);
    
    e[0].Init(200.0f, 300.0f, 800.0f, 800.0f);
    e[0].SetSolidColor(1.0f, 1.0f, .0f, 1.0f);
    e[0].SetTextureCoordsToFullMap();
    
    e[1].Init(700.0f, 200.0f, 900.0f, 900.0f);
    e[1].SetSolidColor(1.0f, 1.0f, 1.0f, 1.0f);
    e[1].SetTextureCoordsToFullMap();
    
    PrintText_Direct("NEWGAME", &str_i[MAINMENU], &e[2], &tif[times32]);
    
}

void InitEntities(Entity *entity, TextureInfo_Font *ti_font, GameState *gameState, StringInfo *str_i)
{
    
}
void InitStringInfo(StringInfo *str_i, GameState *gameState)
{
    str_i[str_i_mainmenu].x = gameState->winWidth/2.0f - 150.0f;
    str_i[str_i_mainmenu].y = 150.0f;
    str_i[str_i_mainmenu].col = col4f(0.0f, 1.0f, 0.0f, 1.0f);;
    
    str_i[str_i_newgame].x = gameState->winWidth/2.0f - 150.0f;
    str_i[str_i_newgame].y = 190.0f;
    str_i[str_i_newgame].col = col4f(0.0f, 1.0f, 0.0f, 1.0f);;
    if(!str_i[str_i_newgame].delayInitialized)
    {
        str_i[str_i_newgame].delayInitialized = true;
        str_i[str_i_newgame].delayCount = 10;
        str_i[str_i_newgame].delayFinish = 10;
    }
    
    str_i[str_i_options].x = gameState->winWidth/2.0f - 150.0f;
    str_i[str_i_options].y = 230.0f;
    str_i[str_i_options].col = col4f(0.0f, 1.0f, 0.0f, 1.0f);;
    if(!str_i[str_i_options].delayInitialized)
    {
        str_i[str_i_options].delayInitialized = true;
        str_i[str_i_options].delayCount = 30;
        str_i[str_i_options].delayFinish = 30;
    }
    
    str_i[str_i_intro_01].x = gameState->winWidth/2.0f - 150.0f;
    str_i[str_i_intro_01].y = 230.0f;
    str_i[str_i_intro_01].col = col4f(0.0f, 1.0f, 0.0f, 1.0f);;
    if(!str_i[str_i_intro_01].delayInitialized)
    {
        str_i[str_i_intro_01].delayInitialized = true;
        str_i[str_i_intro_01].delayCount = 10;
        str_i[str_i_intro_01].delayFinish = 10;
    }
    
    str_i[str_i_intro_02].x = gameState->winWidth/2.0f - 150.0f;
    str_i[str_i_intro_02].y = 260.0f;
    str_i[str_i_intro_02].col = col4f(0.0f, 1.0f, 0.0f, 1.0f);;
    if(!str_i[str_i_intro_02].delayInitialized)
    {
        str_i[str_i_intro_02].delayInitialized = true;
        str_i[str_i_intro_02].delayCount = 10;
        str_i[str_i_intro_02].delayFinish = 10;
    }
    
    str_i[str_i_intro_03].x = gameState->winWidth/2.0f - 150.0f;
    str_i[str_i_intro_03].y = 290.0f;
    str_i[str_i_intro_03].col = col4f(0.0f, 1.0f, 0.0f, 1.0f);;
    if(!str_i[str_i_intro_03].delayInitialized)
    {
        str_i[str_i_intro_03].delayInitialized = true;
        str_i[str_i_intro_03].delayCount = 100;
        str_i[str_i_intro_03].delayFinish = 100;
    }
    
}

void SetEntitiesPosToNULL(Entity *e, u32 start, u32 end)
{
    for(u32 i = start; i <= end; i++)
    {
        e[i].pos.x = 0;
        e[i].pos.y = 0;
        e[i].size.x = 0;
        e[i].size.y = 0;
    }
}

void StartTimer(Time *t)
{
    if(!t->started)
    {
        t->started = true;
        t->currTime = t->timer;
    }
    if(t->currTime <= 0.0f)
    {
        t->currTime = t->timer;
    }
    
    sprintf(t->textBuffer, "%d", (int)t->currTime);
}

bool Collision_Entity_MouseCursor(Input *input, Entity *e, u32 min, u32 max)
{
    if (min > max)
    {
        printf("Collision_Entity_MouseCursor(): min > max\n");
        return false;
    }
    else
    {
        for(u32 i = min; i <= max; i++)
        {
            if(input->mouseCoord.x >= e[i].pos.x &&
               input->mouseCoord.x <= e[i].pos.x + e[i].size.x &&
               input->mouseCoord.y >= e[i].pos.y &&
               input->mouseCoord.y <= e[i].pos.y + e[i].size.y)
            {
                return true;
            }
        }
    }
    
    return false;
    
}


void PrintText_Direct(char *text, StringInfo *str_i, Entity *e, TextureInfo_Font *ti_font)
{
    
    stbtt_aligned_quad q = {};
    
    for(int i = 0; *text; i++, text++)
    {
        
        stbtt_GetBakedQuad(ti_font->bakedChar, ti_font->bmWidth, ti_font->bmHeight, *text - 32, &str_i->x, &str_i->y, &q, 1);
        if(!str_i->strQuad_pos_initialized)
        {
            str_i->strQuad_pos_initialized = true;
            str_i->strQuad_x = q.x0;
            str_i->strQuad_y = q.y0;
            str_i->strQuad_size_y = q.y1 - q.y0;
        }
        
        e[i].pos.x = q.x0;
        e[i].pos.y = q.y0;
        // subract x0 and y0 because it's added in UpdateVertices()
        e[i].size.x = q.x1 - q.x0;
        e[i].size.y = q.y1 - q.y0;
        
        e[i].texCoord.x = q.s0;
        e[i].texCoord.y = q.t0;
        // subract s0 and t0 because it's added in UpdateVertices()
        e[i].texDimension.x = q.s1 - q.s0;
        e[i].texDimension.y = q.t1 - q.t0;
        
        e[i].SetSolidColor(str_i->col.r, str_i->col.g, str_i->col.b, str_i->col.a);
    }
    
    if(!str_i->strQuad_size_initialized)
    {
        str_i->strQuad_size_initialized = true;
        str_i->strQuad_size_x = q.x1 - str_i->strQuad_x;
    }
    
}

void PrintText_Delayed(char *text, StringInfo *str_i, Entity *e, TextureInfo_Font *ti_font)
{
    if(str_i->delayCount == str_i->delayFinish)
    {
        
        
        if(str_i->currCharIndex < strlen(text))
        {
            
            str_i->delayedText[str_i->currCharIndex] = text[str_i->currCharIndex];
            
            for(int i = 0; i <= str_i->currCharIndex; i++)
            {
                stbtt_aligned_quad q = {};
                stbtt_GetBakedQuad(ti_font->bakedChar, ti_font->bmWidth, ti_font->bmHeight, str_i->delayedText[i] - 32, &str_i->x, &str_i->y, &q, 1);
                
                e[i].pos.x = q.x0;
                e[i].pos.y = q.y0;
                // subract x0 and y0 because it's added in UpdateVertices()
                e[i].size.x = q.x1 - q.x0;
                e[i].size.y = q.y1 - q.y0;
                
                e[i].texCoord.x = q.s0;
                e[i].texCoord.y = q.t0;
                // subract s0 and t0 because it's added in UpdateVertices()
                e[i].texDimension.x = q.s1 - q.s0;
                e[i].texDimension.y = q.t1 - q.t0;
                
                
            }
            
            str_i->delayCount = 0;
            str_i->currCharIndex++;
        }
        
    }
    else
    {
        str_i->delayCount++;
    }
    
    for(i32 i = 0; i < strlen(text); i++)
    {
        e[i].SetSolidColor(str_i->col.r, str_i->col.g, str_i->col.b, str_i->col.a);
    }
}


void UpdateVertexArray_Pos(Vertex *v, Entity *e, i32 min, i32 max)
{
    
    for(i32 i = min; i <= max; i++)
    {
        // &v[i * 4],  4 is for every 4th vertex in a quad
        UpdateVertices_pos(&e[i], &v[i * 4]);
    }
    
}

void UpdateVertexArray_Col(Vertex *v, Entity *e, i32 min, i32 max)
{
    
    for(i32 i = min; i <= max; i++)
    {
        // &v[i * 4],  4 is for every 4th vertex in a quad
        UpdateVertices_col(&e[i], &v[i * 4]);
    }
    
}

void UpdateVertexArray_texCoord(Vertex *v, Entity *e, i32 min, i32 max)
{
    
    for(i32 i = min; i <= max; i++)
    {
        // &v[i * 4],  4 is for every 4th vertex in a quad
        UpdateVertices_texCoord(&e[i], &v[i * 4]);
    }
    
}

void UpdateVertices_pos(Entity *e, Vertex *v)
{
    v[0].pos.x = e->pos.x;
    v[0].pos.y = e->pos.y;
    
    v[1].pos.x = e->pos.x;
    v[1].pos.y = e->pos.y + e->size.y;
    
    v[2].pos.x = e->pos.x + e->size.x;
    v[2].pos.y = e->pos.y;
    
    v[3].pos.x = e->pos.x + e->size.x;
    v[3].pos.y = e->pos.y + e->size.y;
}

void UpdateVertices_col(Entity *e, Vertex *v)
{
    v[0].col.r = e->col.r;
    v[0].col.g = e->col.g;
    v[0].col.b = e->col.b;
    v[0].col.a = e->col.a;
    
    v[1].col.r = e->col.r;
    v[1].col.g = e->col.g;
    v[1].col.b = e->col.b;
    v[1].col.a = e->col.a;
    
    v[2].col.r = e->col.r;
    v[2].col.g = e->col.g;
    v[2].col.b = e->col.b;
    v[2].col.a = e->col.a;
    
    v[3].col.r = e->col.r;
    v[3].col.g = e->col.g;
    v[3].col.b = e->col.b;
    v[3].col.a = e->col.a;
}

void UpdateVertices_texCoord(Entity *e, Vertex *v)
{
    v[0].texCoord.x = e->texCoord.x;
    v[0].texCoord.y = e->texCoord.y;
    
    v[1].texCoord.x = e->texCoord.x;
    v[1].texCoord.y = e->texCoord.y + e->texDimension.y;
    
    v[2].texCoord.x = e->texCoord.x + e->texDimension.x;
    v[2].texCoord.y = e->texCoord.y;
    
    v[3].texCoord.x = e->texCoord.x + e->texDimension.x;
    v[3].texCoord.y = e->texCoord.y + e->texDimension.y;
}


platform.h
1




platform.cpp

  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
#include "include\openGL.h"
#include "include\gamecode.h"
#include "include\platform.h"



int main()
{
    srand(time(0));
    
    GLFWwindow *hWindow = InitializeGLFWandGLAD(1920, 1080, "Project", windowed);
    
    GameState gameState = {};
    Input input = {};
    Time time[10] = {};
    WindowDimension winDim = {};
    Entity entity[MAX_ENTITIES] = {};
    Vertex vertices[MAX_ENTITIES * 4] = {};
    TextureInfo_Image ti_image[10] = {};
    TextureInfo_Font ti_font[10] = {};
    StringInfo str_i[100] = {};
    GameCodeDLL gameCode = {};
    LoadGameCode(&gameCode);
    
    gameState.running = true;
    gameState.initializedEntities = false;
    gameState.currentState = MAINMENU;
    
    
    // Load Shaderprograms
    u32 shaderProgram_Texture = LoadShaderProgram("shader\\VS.glsl", "shader\\FS_Texture.glsl");
    u32 shaderProgram_SolidColor = LoadShaderProgram("shader\\VS.glsl", "shader\\FS_SolidColor.glsl");
    u32 shaderProgram_Font = LoadShaderProgram("shader\\VS.glsl", "shader\\FS_Font.glsl");
    
    // Uniform Locations
    u32 uniform_SolidColor_windowDimensions = glGetUniformLocation(shaderProgram_SolidColor, "windowDimensions");
    u32 uniform_Texture_windowDimensions = glGetUniformLocation(shaderProgram_Texture, "windowDimensions");
    u32 uniform_Font_windowDimensions = glGetUniformLocation(shaderProgram_Font, "windowDimensions");
    
    u32 VAO;
    u32 VBO;
    u32 IBO;
    u32 colAttribOffset = sizeof(V3f);
    u32 texCoordAttribOffset = colAttribOffset + sizeof(V4f_COL);
    u32 MaxMemory = sizeof(float) * TOTAL_AMOUNT_ATTRIBS;
    u32 vtxIndices[MAX_ENTITIES * 6] = {};
    
    for(int i = 0; i < MAX_ENTITIES; i++)
    {
        vtxIndices[(i * 6) + 0] = (i * 4) + 0;
        vtxIndices[(i * 6) + 1] = (i * 4) + 1;
        vtxIndices[(i * 6) + 2] = (i * 4) + 2;
        vtxIndices[(i * 6) + 3] = (i * 4) + 1;
        vtxIndices[(i * 6) + 4] = (i * 4) + 2;
        vtxIndices[(i * 6) + 5] = (i * 4) + 3;
    }
    
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &IBO);
    GenTextures_Font(ARRAYSIZE(ti_font), ti_font);
    GenTextures_Image(ARRAYSIZE(ti_image), ti_image);
    
    glBindVertexArray(VAO);
    
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, MaxMemory, 0, GL_DYNAMIC_DRAW);
    
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), (void*)0);
    
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, false, sizeof(Vertex), (void*)colAttribOffset);
    
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 2, GL_FLOAT, false, sizeof(Vertex), (void*)texCoordAttribOffset);
    
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vtxIndices), vtxIndices, GL_STATIC_DRAW);
    
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    //SETUP FONTS
    SetupFonts(ti_font);
    
    //SETUP IMAGES
    SetupImages(ti_image);
    
    // FREE ALL IMAGE BITMAP DATA with FreeImage(TextureInfo_Image *tif);
    FreeImage(&ti_image[myPng]);
    FreeImage(&ti_image[devastator]);
    
    FindFirstFile("gamecode.dll", &gameCode.findData);
    FILETIME lastWriteTime = gameCode.findData.ftLastWriteTime;
    
    time[0].fps = 0;
    time[0].ms_per_frame = 0;
    SetupTimer(&time[1], 10.0f);
    
    while(gameState.running && !glfwWindowShouldClose(hWindow))
    {
        time[0].ms_per_frame = 1000 * (glfwGetTime() - time[0].currTime);
        time[0].fps = 1.0f / ( time[0].ms_per_frame / 1000.0f);
        time[0].currTime = glfwGetTime();
        
        // update timer
        time[1].currTime -= (glfwGetTime() - time[1].timerCountDown);
        time[1].timerCountDown = glfwGetTime(); 
        
        glfwGetWindowSize(hWindow, &winDim.width, &winDim.height);
        glfwGetCursorPos(hWindow, &input.mouseCoord.x, &input.mouseCoord.y);
        gameState.winWidth = winDim.width;
        gameState.winHeight = winDim.height;
        glViewport(0, 0, winDim.width, winDim.height);
        
        glUseProgram(shaderProgram_SolidColor);
        glUniform2f(uniform_SolidColor_windowDimensions, (float)winDim.width, (float)winDim.height);
        glUseProgram(shaderProgram_Texture);
        glUniform2f(uniform_Texture_windowDimensions, (float)winDim.width, (float)winDim.height);
        glUseProgram(shaderProgram_Font);
        glUniform2f(uniform_Font_windowDimensions, (float)winDim.width, (float)winDim.height);
        
        CheckInput(hWindow, &input);
        
        FindFirstFile("gamecode.dll", &gameCode.findData);
        if(CompareFileTime(&lastWriteTime, &gameCode.findData.ftLastWriteTime) != 0)
        {
            
            // LOADING GAMECODE
            UnloadGameCode(&gameCode);
            LoadGameCode(&gameCode);
            // UNLOADING GAMECODE
            
            lastWriteTime = gameCode.findData.ftLastWriteTime;
        }
        
        
        glClearColor(gameState.clearColor.r, gameState.clearColor.g, gameState.clearColor.b, gameState.clearColor.a);
        glClear(GL_COLOR_BUFFER_BIT);
        
        gameCode.UpdateGameCode(&gameState, &input, time, entity, vertices, ti_font, str_i);
        
        glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
        
        u32 indexOffset = 6 * sizeof(u32);
        //RenderImages
        
        
        //Render Text
        glUseProgram(shaderProgram_Texture);
        BindTexture2D(ti_image[myPng].texture);
        glDrawElements(GL_TRIANGLES, 2 * 6, GL_UNSIGNED_INT, (u32*)(0 * indexOffset));
        
        
        glUseProgram(shaderProgram_Font);
        
        BindTexture2D(ti_font[times128].texture);
        glDrawElements(GL_TRIANGLES, len_mainmenu * 6, GL_UNSIGNED_INT, (u32*)(str_e_mainmenu * indexOffset));
        
        BindTexture2D(ti_font[times32].texture);
        glDrawElements(GL_TRIANGLES, len_newgame * 6, GL_UNSIGNED_INT, (u32*)(str_e_newgame * indexOffset));
        glDrawElements(GL_TRIANGLES, len_options * 6, GL_UNSIGNED_INT, (u32*)(str_e_options * indexOffset));
        
        glDrawElements(GL_TRIANGLES, len_intro_01 * 6, GL_UNSIGNED_INT, (u32*)(str_e_intro_01 * indexOffset));
        glDrawElements(GL_TRIANGLES, len_intro_02 * 6, GL_UNSIGNED_INT, (u32*)(str_e_intro_02 * indexOffset));
        glDrawElements(GL_TRIANGLES, len_intro_03 * 6, GL_UNSIGNED_INT, (u32*)(str_e_intro_03 * indexOffset));
        
        glDrawElements(GL_TRIANGLES, 5 * 6, GL_UNSIGNED_INT, (u32*)(10 * indexOffset));
        glfwSwapBuffers(hWindow);
        glfwPollEvents();
        
    }
    
    glDeleteProgram(shaderProgram_SolidColor);
    DeleteTextures_Font(ARRAYSIZE(ti_font), ti_font);
    DeleteTextures_Image(ARRAYSIZE(ti_image), ti_image);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &IBO);
    glDeleteVertexArrays(1, &VAO);
    
    
    
    return 0;
    
}
Simon Anciaux
1341 posts
printing text is cumbersome... any advice for a better way to do it?
Try to write the api you would like to use ("Write the usage code first") and than make the system that will support that (or the closest thing to your desired api).

A few notes:
- You can define a string structure to make it a little easier:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
typedef struct string_t {
    u8* bytes;
    u32 used;
    u32 capacity;
} string_t;

/* Using this macro to set the size, and capacity. Capacity contains the null terminator. */
#define string_from_literal( literal ) { ( literal ), sizeof( literal ) - 1, sizeof( literal ) }

/* Direct */
string_t string = string_from_literal( "Hello Dave." );
display_string( string, x, y, color... );

/* Delayed */
string_t string = string_from_literal( "Hello Dave." );
string.size = 1;

if ( timer_tick( timer ) && string.size < string.capacity - 1 ) {
    string.size++;
}

display_string( string, x, y, color... );

- As shown in the example you can use the same function to display text "direct" and "delayed", you just change the size of the string.
- display_string is a function that would add an item in a command buffer for rendering. Your rendering system would use all the commands to render, that way the game code doesn't have to know how the rendering is happening (not handling vertex buffer, attributes... itself).
- str_info would be one type of command in the command buffer, other would be commands to draw quad, lines, triangles, models...
- display_string would allocate a command (pushing it in the command buffer) and fill the str_info structure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/* Pseudo code*/
int main( void ) {
     while ( running ) {
          /* game code */
          display_string( command_buffer, ... );
          display_rectangle( command_buffer, ... );
          ...

          /* rendering */
          while ( command_buffer_not_empty ) {
              command = get_next_command( command_buffer );
              if ( command.type == string_command ) {
                  opengl_things_to_render_the_string( );
              } else if ( command.type == rectangle_command ) {
                  opengl_things_to_render_a_rectangle( );
              } else if ( ... ) {
                  ...
              }
          }
     }
}