1#include "text.h"
2#include "shaders.h"
3
4#include "../util/shader.h"
5
6#include "../libdrawtext/drawtext.h"
7
8#include <stdio.h>
9#include <string.h>
10
11#define DEFAULT_FONT_SLOTS 8
12
13static struct dtx_font *sliCurrentFont;
14static int sliCurrentSize;
15
16static struct dtx_font **sliFontList;
17static int sliNumFontSlots;
18static int sliNumFonts;
19
20static int sliAddFont(struct dtx_font *font);
21
22void sliTextInit()
23{
24 sliCurrentFont = NULL;
25 sliCurrentSize = 0;
26
27 // create our expanding array of fonts we've loaded
28 sliFontList = (struct dtx_font**)malloc(sizeof(struct dtx_font*) * DEFAULT_FONT_SLOTS);
29 sliNumFontSlots = DEFAULT_FONT_SLOTS;
30 sliNumFonts = 0;
31
32 // initialize OpenGL memory in libdrawtext
33 dtx_gl_init();
34}
35
36void sliTextDestroy()
37{
38 int i;
39
40 // delete all of the fonts we've laoded
41 for(i = 0; i < sliNumFonts; i ++)
42 {
43 dtx_close_font(sliFontList[i]);
44 }
45
46 // clear our list
47 free(sliFontList);
48
49 // free OpenGL memory
50 dtx_gl_kill();
51}
52
53double sliTextWidth(const char *text)
54{
55 double result = 0.0;
56 if(text[0] != 0)
57 result = dtx_string_width(text);
58 return result;
59}
60
61double sliTextHeight(const char *text)
62{
63 double result = 0.0;
64 if(text[0] != 0)
65 result = dtx_string_height(text);
66 return result;
67}
68
69void sliText(Mat4 *modelview, Vec4 *color, const char *text)
70{
71 // prepare our shader object in case the dtx text buffer is ready for rendering
72 shaderBind(sliTextShader);
73 shaderUniformMatrix4fv(sliTextShader, "u_Modelview", 1, (float*)modelview);
74 shaderUniform4f(sliTextShader, "u_Color", (float)color -> x, (float)color -> y, (float)color -> z, (float)color -> w);
75
76 // queue the incoming text; this may result in a render if the buffer becomes full (which is why we activate our shader)
77 dtx_string(text);
78}
79
80void sliTextFlush(Mat4 *modelview, Vec4 *color)
81{
82 // prepare our shader object
83 shaderBind(sliTextShader);
84 shaderUniformMatrix4fv(sliTextShader, "u_Modelview", 1, (float*)modelview);
85 shaderUniform4f(sliTextShader, "u_Color", (float)color -> x, (float)color -> y, (float)color -> z, (float)color -> w);
86
87 // render the buffered text
88 dtx_flush();
89}
90
91int sliLoadFont(const char *fontFilename)
92{
93 int result = -1;
94 struct dtx_font *font = dtx_open_font(fontFilename, 0);
95
96 if(font)
97 {
98 result = sliAddFont(font);
99 }
100 else
101 {
102 fprintf(stderr, "sliLoadFont() could not open font file %s", fontFilename);
103 exit(1);
104 }
105
106 return result;
107}
108
109void sliFont(int font, int fontSize)
110{
111 if(font >= 0 && font < sliNumFonts)
112 {
113 sliCurrentFont = sliFontList[font];
114 sliCurrentSize = fontSize;
115
116 dtx_prepare(sliCurrentFont, sliCurrentSize);
117 dtx_use_font(sliCurrentFont, sliCurrentSize);
118 dtx_use_interpolation(1);
119 }
120 else
121 {
122 fprintf(stderr, "sliFont(): font index %d is out of range", font);
123 exit(1);
124 }
125}
126
127void sliFontSize(int fontSize)
128{
129 if(fontSize != sliCurrentSize)
130 {
131 sliCurrentSize = fontSize;
132 if(sliCurrentFont != NULL)
133 {
134 dtx_prepare(sliCurrentFont, fontSize);
135 dtx_use_font(sliCurrentFont, sliCurrentSize);
136 dtx_use_interpolation(1);
137 }
138 }
139}
140
141static int sliAddFont(struct dtx_font *font)
142{
143 struct dtx_font **newFontList;
144
145 // allocate more space for our font list if needed
146 if(sliNumFonts >= sliNumFontSlots)
147 {
148 newFontList = (struct dtx_font**)malloc(sizeof(struct dtx_font*) * sliNumFontSlots * 2);
149 memcpy(newFontList, sliFontList, sizeof(struct dtx_font*) * sliNumFontSlots);
150 sliNumFontSlots *= 2;
151 sliFontList = newFontList;
152 }
153
154 // now insert our new font and return its index in our list
155 sliFontList[sliNumFonts] = font;
156 return sliNumFonts++;
157}
158