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 | bool success = 0;
const u32 pxlw = 1024;
const u32 pxlh = 1024;
const u32 fontf_sz = pxlw*pxlh;
unsigned char font_file[fontf_sz] = {};
unsigned char pixels[fontf_sz] = {};
stbtt_packedchar packed_char[126] = {};
stbtt_pack_context pack_c = {};
size_t bytes_read = 0;
bytes_read = fread(font_file, 1, fontf_sz, fopen("C:/windows/fonts/arial.ttf", "rb"));
// init pixel_bitmap and packing context
success = stbtt_PackBegin(&pack_c, pixels, pxlw, pxlh, 0, 1, 0);
// set detail level of characters in font which requires the pixel_bitmap to be bigger
stbtt_PackSetOversampling(&pack_c, 2, 2);
// Set what characters in the pixel_map you want STBTT_POINT_SIZE makes the charcter size in pixels
stbtt_PackFontRange(&pack_c, font_file, 0, 45, ' ', 126, packed_char);
// Invert font texture becuase stb give's it to us upside down ( y increases downward )
InvertBuffer(pixels, pxlw, pxlh);
// End the packing context
stbtt_PackEnd(&pack_c);
unsigned char myBuffer[512 * 512] = {};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, pxlw, pxlh, 0, GL_RED, GL_UNSIGNED_BYTE, pixels);
stbi_image_free(image);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
bool wglSwap = wglSwapIntervalEXT(1);
// Initialise game code
InitGame(vBuff);
while(running)
{
// start timer
win32.dts = (float)(win32.end_time.QuadPart - win32.start_time.QuadPart) / (float)win32.perf_frequency.QuadPart;
win32.dts *= 1000; // Miliseconds
QueryPerformanceCounter(&win32.start_time);
if(KeyPressed(VK_ESCAPE)) running = false;
Win32_MessageHandler(&win32);
Win32_GetWinSize(&win32);
glViewport(0, 0, win32.win_w, win32.win_h);
char letter = 'A';
if(KeyPressed('D')) letter++;
if(KeyPressed('A')) letter--;
// Gets a renderable character
stbtt_aligned_quad glyph_quad = {};
float xp = 0.0f;
float yp = 0.024f;
float t_pos_x = glyph_quad.s0;
float t_pos_y = 1.0f - glyph_quad.t1;
float t_sz_x = glyph_quad.s1 - glyph_quad.s0;
float t_sz_y = (1.0f - glyph_quad.t0) - (1.0f - glyph_quad.t1);
float adv = xp / (float)win32.win_w;
float c_w = (glyph_quad.x1 - glyph_quad.x0) / (float)win32.win_w;
float c_h = (glyph_quad.y1 - glyph_quad.y0) / (float)win32.win_h;
char line[50] = {};
strcpy(line, "get Outt,a hegj?!");
for(i32 i = 0; i < strlen(line); i++)
{
adv = xp / (float)win32.win_w;
float advy = yp / (float)win32.win_h;
stbtt_GetPackedQuad(packed_char, 1024, 1024, line[i] - ' ', &xp, &yp, &glyph_quad, 0);
t_pos_x = glyph_quad.s0;
t_pos_y = 1.0f - glyph_quad.t1;
t_sz_x = glyph_quad.s1 - glyph_quad.s0;
t_sz_y = (1.0f - glyph_quad.t0) - (1.0f - glyph_quad.t1);
// The glyph_quad.x0 & glyph_quad.y0 should be the the advancing positions in x/y
float c_x = -1.0f + ((-1.0f + glyph_quad.x0) / (float)win32.win_w);
float c_y = 0.9f + ((glyph_quad.y0) / (float)win32.win_h);
c_w = (glyph_quad.x1 - glyph_quad.x0) / (float)win32.win_w;
c_h = (glyph_quad.y1 - glyph_quad.y0) / (float)win32.win_h;
CreateQuad(vBuff, 5 + i, v2f(c_x, c_y), v2f(c_w, c_h), v4f(0.0f, 1.0f, 1.0f, 1.0f), v2f(t_pos_x, t_pos_y), v2f(t_sz_x, t_sz_y));
}
|