-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtext_engine.c
More file actions
executable file
·406 lines (303 loc) · 11.2 KB
/
text_engine.c
File metadata and controls
executable file
·406 lines (303 loc) · 11.2 KB
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
#include "text_engine.h"
font* newFont(int height, int arraySize, int char_widths[arraySize], int char_offsets[arraySize], int graphic_width, phrase* graphic, phrase *shadow){
int i;
font* fontStruct = malloc(sizeof(font));
fontStruct->height = height;
fontStruct->width = graphic_width;
fontStruct->kerning = 0;
fontStruct->line_spacing = 0;
fontStruct->char_widths = malloc(sizeof(int)*arraySize);
fontStruct->char_offsets = malloc(sizeof(int)*arraySize);
for(i = 0; i != arraySize; i++){
fontStruct->char_widths[i] = char_widths[i];
fontStruct->char_offsets[i] = char_offsets[i];
}
fontStruct->graphic = new_screen();
set_simple_screen(DEPTH8, graphic_width, height, fontStruct->graphic, graphic);
if(shadow != NULL){
fontStruct->shadow = new_screen();
set_simple_screen(DEPTH8, graphic_width, height, fontStruct->shadow, shadow);
}
else{
fontStruct->shadow = NULL;
}
return fontStruct;
};
textBox* newTextBox(char* text, int box_width, int box_height, font* fontStruct, int padding, display *d, int spriteXPosition, int spriteYPosition, int displayLayerForSprite, int shadow){
textBox* tb = malloc(sizeof(textBox));
tb->char_idx = 0;
tb->type_rate = 0;
tb->type_rate_counter = 0;
tb->padding = padding;
tb->draw_pos_x = tb->padding;
tb->draw_pos_y = 0;
tb->char_count = (int)strlen(text); // minus our null terminator
tb->real_width = 0;
tb->line_center = 0;
tb->line_count = 1; //we always have at least 1 line
tb->shadow = shadow; //off/on
tb->fontStruct = fontStruct;
tb->text = malloc(sizeof(char) * (tb->char_count + 1)); // +1 for the null terminator so the user doesn't have to worry about adding it
strncpy(tb->text, text, tb->char_count); //only copy over the character count regardless of how many characters are given as input.
tb->text[tb->char_count] = '\0'; //make sure we are null terminated at the end of the text string
lineBreakAndWordWrap(tb, box_width);
//reset box_height for screen if our line count exceeds the user defined box_height
int textBoxHeight = (tb->fontStruct->height + tb->fontStruct->line_spacing) * tb->line_count + 1;
//add an extra pixel for shadow.
if(shadow == 1){
textBoxHeight++;
}
int height = 0;
if(box_height < textBoxHeight){
height = textBoxHeight;
}
else{
height = box_height;
}
tb->tbScreen = new_screen();
alloc_simple_screen(DEPTH8, box_width, height, tb->tbScreen);
tb->tbSprite = sprite_of_screen(spriteXPosition, spriteYPosition, tb->tbScreen);
clear_screen(tb->tbScreen);
if(d != NULL){
tb->tbSprite->invisible = 1;
attach_sprite_to_display_at_layer(tb->tbSprite, d, displayLayerForSprite);
}
return tb;
};
textBox* freeTextBox(textBox *textBoxToBeUnloaded){
if(textBoxToBeUnloaded->text != NULL){
free(textBoxToBeUnloaded->text);
textBoxToBeUnloaded->text = NULL;
}
if(textBoxToBeUnloaded->tbSprite != NULL){
textBoxToBeUnloaded->tbSprite->invisible = 1;
detach_sprite_from_display(textBoxToBeUnloaded->tbSprite);
free(textBoxToBeUnloaded->tbSprite);
textBoxToBeUnloaded->tbSprite = NULL;
}
if(textBoxToBeUnloaded->tbScreen->data != NULL){
free(textBoxToBeUnloaded->tbScreen->data);
textBoxToBeUnloaded->tbScreen->data = NULL;
}
if(textBoxToBeUnloaded->tbScreen != NULL){
free(textBoxToBeUnloaded->tbScreen);
textBoxToBeUnloaded->tbScreen = NULL;
}
if(textBoxToBeUnloaded != NULL){
textBoxToBeUnloaded->fontStruct = NULL;
free(textBoxToBeUnloaded);
textBoxToBeUnloaded = NULL;
}
textBox *tb = NULL;
return tb; //You can't set NULL to a struct inside of a function, so we have to return it instead to the original struct
};
void resetTextScreen(textBox *tb){
clear_screen(tb->tbScreen);
tb->char_idx = 0;
tb->draw_pos_x = tb->padding;
tb->draw_pos_y = 0;
tb->real_width = 0;
tb->line_center = 0;
tb->line_count = 1;
};
void lineBreakAndWordWrap(textBox *tb, int box_width){
//word wraping
int i = 0;
int wordWrapPixelTracker = 0;
int previousLineBreak = 0;
int boxWidthMax = (box_width - (tb->padding*2));
//scan the text string and calculate the line breaks for word wrapping
for(i = 0; i != tb->char_count; i++){
int error = 0;
int idx = tb->text[i] - 32;
wordWrapPixelTracker += tb->fontStruct->char_widths[idx] + tb->fontStruct->kerning;
if(tb->text[i] == '^'){
wordWrapPixelTracker = 0;
}
//if we exceed the max width of the word wrap, scan backwards to the previous space and add a line break
//recalculate the box_width to take padding into account
if(wordWrapPixelTracker >= boxWidthMax){
int ii = 0;
for(ii = i; ii > (previousLineBreak - 1); ii--){
if(tb->text[ii] == ' '){
tb->text[ii] = '^';
wordWrapPixelTracker = 0;
i = ii + 1;//start tracking characters from the character after the new line break we just added.
previousLineBreak = i;
break;
}
if(ii == previousLineBreak){
free(tb->text);
tb->char_count = 17;
tb->text = malloc(sizeof(char) * (tb->char_count + 1)); // +1 for the null terminator so the user doesn't have to worry about adding it
strcpy(tb->text, "ER:NoLnBrk4WrdWrp"); //only copy over the character count regardless of how many characters are given as input.
tb->text[tb->char_count] = '\0';
error = 1;
break;
}
}
}
if(error == 1){
break;
}
}
tb->line_count = 1;
for(i = 0; i != tb->char_count; i++){
//check for predefined line break, if we hit one of these before we reach the wordwrap limit, start the scan over
if(tb->text[i] == '^'){
tb->line_count++;
wordWrapPixelTracker = 0;
}
}
};
void drawTextBoxAtOnce(textBox* tb){
if(tb->tbSprite->invisible == 1){
tb->tbSprite->invisible = 0;
}
for(tb->char_idx = tb->char_idx; tb->char_idx < tb->char_count; tb->char_idx++){
processCharacter(tb);
}
};
void updateTextBox(textBox* tb){
resetTextScreen(tb);
lineBreakAndWordWrap(tb, tb->tbScreen->width);
for(tb->char_idx = tb->char_idx; tb->char_idx < tb->char_count; tb->char_idx++){
processCharacter(tb);
}
if(tb->tbSprite->invisible == 1){
tb->tbSprite->invisible = 0;
}
};
void textRangeColorChange(textBox* tb, int rangeStart, int endOffset, uint8_t findCLUTID, uint8_t newCLUTID){
int i = 0;
int ii = 0;
int min = rangeStart;
int max = min + endOffset;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
//check for valid range
if(max > tb->char_count){
max = tb->char_count;
}
if(min > max){
min = max;
}
if(min < 0){
min = 0;
}
//determine scan range for painting
//Get x1
x1 += tb->padding;
for(i = 0; i < min; i++){
int idx = tb->text[i] - 32;
if(tb->text[i] == '^'){
x1 = tb->padding;// new line
}
else{
x1 += tb->fontStruct->char_widths[idx] + tb->fontStruct->kerning;
}
}
//get y postition for top left corner of text (text line)
for(i = 0; i < max; i++){
if(tb->text[i] == '^'){
y1 += tb->fontStruct->height + tb->fontStruct->line_spacing;
}
}
// y1 += tb->padding; //vertical padding doesn't exist at this point.
//get x2 bottom right corner of box to scan and change.
if(min != max){
for(i = min; i < max; i++){
int idx = tb->text[i] - 32;
if(tb->text[i] == '^'){
x1 = tb->padding;// new line
x2 = tb->padding;// new line
}
else{
x2 += tb->fontStruct->char_widths[idx] + tb->fontStruct->kerning;
}
}
}
else{
x2 += tb->fontStruct->char_widths[(tb->text[min] - 32)] + tb->fontStruct->kerning;
}
//offset x2 based on x1 position.
x2 += x1;
//y2 is just the height of the font origin graphic
y2 = y1 + tb->fontStruct->height;
//make sure we don't go out the bounds of the data array for both x2 and y2
if(x2 > tb->tbScreen->width){
x2 = tb->tbScreen->width;
}
if(y2 > tb->tbScreen->height){
y2 = tb->tbScreen->height;
}
//scan screen buffer and change pixels
for(i = y1; i != y2; i++){
int selectedPixelRow = i * tb->tbScreen->width;
for(ii = x1; ii != x2; ii++){
int selectedPixel = selectedPixelRow + ii;
uint8_t *dataPtr = (uint8_t*)tb->tbScreen->data;
if(dataPtr[selectedPixel] == findCLUTID){
dataPtr[selectedPixel] = newCLUTID;
}
}
}
};
void drawCharacter(textBox* tb){
int idx = tb->text[tb->char_idx] - 32;
tb->fontStruct->graphic->x = tb->fontStruct->char_offsets[idx];
tb->fontStruct->shadow->x = tb->fontStruct->char_offsets[idx];
//draw character shadow
if(tb->shadow != 0 && tb->fontStruct->shadow != NULL){
tb->tbScreen->x = tb->draw_pos_x + 1;
tb->tbScreen->y = tb->draw_pos_y + 1;
fb2d_copy_straight(tb->fontStruct->shadow, tb->tbScreen, tb->fontStruct->char_widths[idx], tb->fontStruct->height, MODE_TRANSPARENT);
}
//draw character
tb->tbScreen->x = tb->draw_pos_x;
tb->tbScreen->y = tb->draw_pos_y;
fb2d_copy_straight(tb->fontStruct->graphic, tb->tbScreen, tb->fontStruct->char_widths[idx], tb->fontStruct->height, MODE_TRANSPARENT);
tb->draw_pos_x += tb->fontStruct->char_widths[idx] + tb->fontStruct->kerning;
//get center of text based on first line
if(tb->line_count == 1 && tb->line_center == 0){
tb->line_center = tb->real_width / 2;
}
if(tb->line_count == 0){
tb->real_width += tb->fontStruct->char_widths[idx] + tb->fontStruct->kerning;
}
if((tb->char_idx + 2) == tb->char_count && tb->line_center == 0){
tb->line_center = tb->real_width / 2;
}
};
int processNewLine(textBox* tb){
if(tb->text[tb->char_idx] == '^'){
tb->draw_pos_y += (tb->fontStruct->height + tb->fontStruct->line_spacing);
tb->draw_pos_x = tb->padding;
return 0;
}
return 1;
};
void processCharacter(textBox* tb){
if(tb->text[tb->char_idx] - 32 > -1){
if(processNewLine(tb)){
drawCharacter(tb);
}
}
};
//convert interger to string
int itostring_c(char* buffer, int value, int base, int written){
if((value/base) > 0)
{
written = itostring_c(buffer, value/base, base, written);
}
buffer[written++] = ("0123456789ABCDEF"[value % base]);
return written;
};
int itostring(char* buffer, int value, int base){
int written;
written = itostring_c(buffer, value, base, 0);
buffer[written] = '\0';
return written;
};