Skip to content

Latest commit

 

History

History
801 lines (700 loc) · 24.1 KB

testime.c

File metadata and controls

801 lines (700 loc) · 24.1 KB
 
Jan 3, 2018
Jan 3, 2018
2
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
3
4
5
6
7
8
9
10
11
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
Oct 1, 2016
Oct 1, 2016
12
13
14
/* A simple program to test the Input Method support in the SDL library (2.0+)
If you build without SDL_ttf, you can use the GNU Unifont hex file instead.
Download at http://unifoundry.com/unifont.html */
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#ifdef HAVE_SDL_TTF
#include "SDL_ttf.h"
#endif
#include "SDL_test_common.h"
Oct 1, 2016
Oct 1, 2016
27
28
29
30
31
32
33
34
35
36
37
38
39
#define DEFAULT_PTSIZE 30
#ifdef HAVE_SDL_TTF
#ifdef __MACOSX__
#define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
#elif __WIN32__
/* Some japanese font present on at least Windows 8.1. */
#define DEFAULT_FONT "C:\\Windows\\Fonts\\yugothic.ttf"
#else
#define DEFAULT_FONT "NoDefaultFont.ttf"
#endif
#else
#define DEFAULT_FONT "unifont-9.0.02.hex"
#endif
40
41
42
43
#define MAX_TEXT_LENGTH 256
static SDLTest_CommonState *state;
static SDL_Rect textRect, markedRect;
Oct 1, 2016
Oct 1, 2016
44
static SDL_Color lineColor = {0,0,0,255};
Mar 3, 2016
Mar 3, 2016
45
static SDL_Color backColor = {255,255,255,255};
Oct 1, 2016
Oct 1, 2016
46
static SDL_Color textColor = {0,0,0,255};
47
48
49
50
static char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
static int cursor = 0;
#ifdef HAVE_SDL_TTF
static TTF_Font *font;
Oct 1, 2016
Oct 1, 2016
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
#else
#define UNIFONT_MAX_CODEPOINT 0x1ffff
#define UNIFONT_NUM_GLYPHS 0x20000
/* Using 512x512 textures that are supported everywhere. */
#define UNIFONT_TEXTURE_WIDTH 512
#define UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / 16)
#define UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW)
#define UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE)
#define UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4)
#define UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4)
#define UNIFONT_DRAW_SCALE 2
struct UnifontGlyph {
Uint8 width;
Uint8 data[32];
} *unifontGlyph;
static SDL_Texture **unifontTexture;
static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES] = {0};
/* Unifont loading code start */
static Uint8 dehex(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 255;
}
static Uint8 dehex2(char c1, char c2)
{
return (dehex(c1) << 4) | dehex(c2);
}
static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np)
{
Uint32 n = 0;
for (; len > 0; cp++, len--)
{
Uint8 c = dehex(*cp);
if (c == 255)
return 0;
n = (n << 4) | c;
}
if (np != NULL)
*np = n;
return 1;
}
Aug 28, 2017
Aug 28, 2017
102
static int unifont_init(const char *fontname)
Oct 1, 2016
Oct 1, 2016
103
104
105
106
107
108
109
110
111
112
{
Uint8 hexBuffer[65];
Uint32 numGlyphs = 0;
int lineNumber = 1;
size_t bytesRead;
SDL_RWops *hexFile;
const size_t unifontGlyphSize = UNIFONT_NUM_GLYPHS * sizeof(struct UnifontGlyph);
const size_t unifontTextureSize = UNIFONT_NUM_TEXTURES * state->num_windows * sizeof(void *);
/* Allocate memory for the glyph data so the file can be closed after initialization. */
Oct 9, 2016
Oct 9, 2016
113
unifontGlyph = (struct UnifontGlyph *)SDL_malloc(unifontGlyphSize);
Oct 1, 2016
Oct 1, 2016
114
115
116
if (unifontGlyph == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.\n", (int)(unifontGlyphSize + 1023) / 1024);
Aug 28, 2017
Aug 28, 2017
117
return -1;
Oct 1, 2016
Oct 1, 2016
118
119
120
121
}
SDL_memset(unifontGlyph, 0, unifontGlyphSize);
/* Allocate memory for texture pointers for all renderers. */
Oct 9, 2016
Oct 9, 2016
122
unifontTexture = (SDL_Texture **)SDL_malloc(unifontTextureSize);
Oct 1, 2016
Oct 1, 2016
123
124
125
if (unifontTexture == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.\n", (int)(unifontTextureSize + 1023) / 1024);
Aug 28, 2017
Aug 28, 2017
126
return -1;
Oct 1, 2016
Oct 1, 2016
127
128
129
130
131
132
133
}
SDL_memset(unifontTexture, 0, unifontTextureSize);
hexFile = SDL_RWFromFile(fontname, "rb");
if (hexFile == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s\n", fontname);
Aug 28, 2017
Aug 28, 2017
134
return -1;
Oct 1, 2016
Oct 1, 2016
135
136
137
138
139
140
141
142
143
144
145
146
147
148
}
/* Read all the glyph data into memory to make it accessible later when textures are created. */
do {
int i, codepointHexSize;
size_t bytesOverread;
Uint8 glyphWidth;
Uint32 codepoint;
bytesRead = SDL_RWread(hexFile, hexBuffer, 1, 9);
if (numGlyphs > 0 && bytesRead == 0)
break; /* EOF */
if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9))
{
Jul 9, 2017
Jul 9, 2017
149
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
Aug 28, 2017
Aug 28, 2017
150
return -1;
Oct 1, 2016
Oct 1, 2016
151
152
153
154
155
156
157
158
159
160
161
162
163
164
}
/* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */
if (hexBuffer[2] == ':')
codepointHexSize = 2;
else if (hexBuffer[4] == ':')
codepointHexSize = 4;
else if (hexBuffer[6] == ':')
codepointHexSize = 6;
else if (hexBuffer[8] == ':')
codepointHexSize = 8;
else
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.\n", lineNumber);
Aug 28, 2017
Aug 28, 2017
165
return -1;
Oct 1, 2016
Oct 1, 2016
166
167
168
169
170
}
if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.\n", lineNumber);
Aug 28, 2017
Aug 28, 2017
171
return -1;
Oct 1, 2016
Oct 1, 2016
172
173
174
175
176
177
178
179
180
181
182
183
}
if (codepoint > UNIFONT_MAX_CODEPOINT)
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.\n", lineNumber, UNIFONT_MAX_CODEPOINT);
/* If there was glyph data read in the last file read, move it to the front of the buffer. */
bytesOverread = 8 - codepointHexSize;
if (codepointHexSize < 8)
SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread);
bytesRead = SDL_RWread(hexFile, hexBuffer + bytesOverread, 1, 33 - bytesOverread);
if (bytesRead < (33 - bytesOverread))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
Aug 28, 2017
Aug 28, 2017
184
return -1;
Oct 1, 2016
Oct 1, 2016
185
186
187
188
189
190
191
192
193
194
}
if (hexBuffer[32] == '\n')
glyphWidth = 8;
else
{
glyphWidth = 16;
bytesRead = SDL_RWread(hexFile, hexBuffer + 33, 1, 32);
if (bytesRead < 32)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
Aug 28, 2017
Aug 28, 2017
195
return -1;
Oct 1, 2016
Oct 1, 2016
196
197
198
199
200
201
}
}
if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.\n", lineNumber);
Aug 28, 2017
Aug 28, 2017
202
return -1;
Oct 1, 2016
Oct 1, 2016
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
}
if (codepoint <= UNIFONT_MAX_CODEPOINT)
{
if (unifontGlyph[codepoint].width > 0)
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Ignoring duplicate codepoint 0x%08x in hex file on line %d.\n", codepoint, lineNumber);
else
{
unifontGlyph[codepoint].width = glyphWidth;
/* Pack the hex data into a more compact form. */
for (i = 0; i < glyphWidth * 2; i++)
unifontGlyph[codepoint].data[i] = dehex2(hexBuffer[i * 2], hexBuffer[i * 2 + 1]);
numGlyphs++;
}
}
lineNumber++;
} while (bytesRead > 0);
SDL_RWclose(hexFile);
SDL_Log("unifont: Loaded %u glyphs.\n", numGlyphs);
Aug 28, 2017
Aug 28, 2017
224
return 0;
Oct 1, 2016
Oct 1, 2016
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
}
static void unifont_make_rgba(Uint8 *src, Uint8 *dst, Uint8 width)
{
int i, j;
Uint8 *row = dst;
for (i = 0; i < width * 2; i++)
{
Uint8 data = src[i];
for (j = 0; j < 8; j++)
{
if (data & 0x80)
{
row[0] = textColor.r;
row[1] = textColor.g;
row[2] = textColor.b;
row[3] = textColor.a;
}
else
{
row[0] = 0;
row[1] = 0;
row[2] = 0;
row[3] = 0;
}
data <<= 1;
row += 4;
}
if (width == 8 || (width == 16 && i % 2 == 1))
{
dst += UNIFONT_TEXTURE_PITCH;
row = dst;
}
}
}
Aug 28, 2017
Aug 28, 2017
263
static int unifont_load_texture(Uint32 textureID)
Oct 1, 2016
Oct 1, 2016
264
265
266
267
268
269
270
{
int i;
Uint8 * textureRGBA;
if (textureID >= UNIFONT_NUM_TEXTURES)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %u.\n", textureID);
Aug 28, 2017
Aug 28, 2017
271
return -1;
Oct 1, 2016
Oct 1, 2016
272
273
}
Oct 9, 2016
Oct 9, 2016
274
textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE);
Oct 1, 2016
Oct 1, 2016
275
276
277
if (textureRGBA == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.\n", UNIFONT_TEXTURE_SIZE / 1024 / 1024);
Aug 28, 2017
Aug 28, 2017
278
return -1;
Oct 1, 2016
Oct 1, 2016
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
}
SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE);
/* Copy the glyphs into memory in RGBA format. */
for (i = 0; i < UNIFONT_GLYPHS_IN_TEXTURE; i++)
{
Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID + i;
if (unifontGlyph[codepoint].width > 0)
{
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
const size_t offset = (cInTex / UNIFONT_GLYPHS_IN_ROW) * UNIFONT_TEXTURE_PITCH * 16 + (cInTex % UNIFONT_GLYPHS_IN_ROW) * 16 * 4;
unifont_make_rgba(unifontGlyph[codepoint].data, textureRGBA + offset, unifontGlyph[codepoint].width);
}
}
/* Create textures and upload the RGBA data from above. */
for (i = 0; i < state->num_windows; ++i)
{
SDL_Renderer *renderer = state->renderers[i];
SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID];
if (state->windows[i] == NULL || renderer == NULL || tex != NULL)
continue;
tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, UNIFONT_TEXTURE_WIDTH, UNIFONT_TEXTURE_WIDTH);
if (tex == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %u for renderer %d.\n", textureID, i);
Aug 28, 2017
Aug 28, 2017
305
return -1;
Oct 1, 2016
Oct 1, 2016
306
307
308
309
310
311
312
313
314
315
316
}
unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex;
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
if (SDL_UpdateTexture(tex, NULL, textureRGBA, UNIFONT_TEXTURE_PITCH) != 0)
{
SDL_Log("unifont error: Failed to update texture %u data for renderer %d.\n", textureID, i);
}
}
SDL_free(textureRGBA);
unifontTextureLoaded[textureID] = 1;
Aug 28, 2017
Aug 28, 2017
317
return 0;
Oct 1, 2016
Oct 1, 2016
318
319
320
321
322
323
324
325
}
static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
{
SDL_Texture *texture;
const Uint32 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
SDL_Rect srcrect;
srcrect.w = srcrect.h = 16;
Aug 28, 2017
Aug 28, 2017
326
if (codepoint > UNIFONT_MAX_CODEPOINT) {
Oct 1, 2016
Oct 1, 2016
327
return 0;
Aug 28, 2017
Aug 28, 2017
328
329
330
331
332
333
}
if (!unifontTextureLoaded[textureID]) {
if (unifont_load_texture(textureID) < 0) {
return 0;
}
}
Oct 1, 2016
Oct 1, 2016
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
texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
if (texture != NULL)
{
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
srcrect.x = cInTex % UNIFONT_GLYPHS_IN_ROW * 16;
srcrect.y = cInTex / UNIFONT_GLYPHS_IN_ROW * 16;
SDL_RenderCopy(state->renderers[rendererID], texture, &srcrect, dstrect);
}
return unifontGlyph[codepoint].width;
}
static void unifont_cleanup()
{
int i, j;
for (i = 0; i < state->num_windows; ++i)
{
SDL_Renderer *renderer = state->renderers[i];
if (state->windows[i] == NULL || renderer == NULL)
continue;
for (j = 0; j < UNIFONT_NUM_TEXTURES; j++)
{
SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + j];
if (tex != NULL)
SDL_DestroyTexture(tex);
}
}
for (j = 0; j < UNIFONT_NUM_TEXTURES; j++)
unifontTextureLoaded[j] = 0;
SDL_free(unifontTexture);
SDL_free(unifontGlyph);
}
/* Unifont code end */
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
#endif
size_t utf8_length(unsigned char c)
{
c = (unsigned char)(0xff & c);
if (c < 0x80)
return 1;
else if ((c >> 5) ==0x6)
return 2;
else if ((c >> 4) == 0xe)
return 3;
else if ((c >> 3) == 0x1e)
return 4;
else
return 0;
}
char *utf8_next(char *p)
{
size_t len = utf8_length(*p);
size_t i = 0;
if (!len)
return 0;
for (; i < len; ++i)
{
++p;
if (!*p)
return 0;
}
return p;
}
char *utf8_advance(char *p, size_t distance)
{
size_t i = 0;
for (; i < distance && p; ++i)
{
p = utf8_next(p);
}
return p;
}
Oct 1, 2016
Oct 1, 2016
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
Uint32 utf8_decode(char *p, size_t len)
{
Uint32 codepoint = 0;
size_t i = 0;
if (!len)
return 0;
for (; i < len; ++i)
{
if (i == 0)
codepoint = (0xff >> len) & *p;
else
{
codepoint <<= 6;
codepoint |= 0x3f & *p;
}
if (!*p)
return 0;
p++;
}
return codepoint;
}
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
void usage()
{
SDL_Log("usage: testime [--font fontfile]\n");
}
void InitInput()
{
/* Prepare a rect for text input */
textRect.x = textRect.y = 100;
textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
textRect.h = 50;
text[0] = 0;
markedRect = textRect;
markedText[0] = 0;
SDL_StartTextInput();
}
void CleanupVideo()
{
SDL_StopTextInput();
#ifdef HAVE_SDL_TTF
TTF_CloseFont(font);
TTF_Quit();
Oct 1, 2016
Oct 1, 2016
461
462
#else
unifont_cleanup();
Aug 28, 2017
Aug 28, 2017
466
467
void _Redraw(int rendererID)
{
Oct 1, 2016
Oct 1, 2016
468
469
470
471
SDL_Renderer * renderer = state->renderers[rendererID];
SDL_Rect drawnTextRect, cursorRect, underlineRect;
drawnTextRect = textRect;
drawnTextRect.w = 0;
Mar 3, 2016
Mar 3, 2016
473
SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
474
475
476
477
SDL_RenderFillRect(renderer,&textRect);
if (*text)
{
Oct 1, 2016
Oct 1, 2016
478
#ifdef HAVE_SDL_TTF
479
SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
Mar 10, 2016
Mar 10, 2016
480
SDL_Texture *texture;
Oct 1, 2016
Oct 1, 2016
481
482
483
484
485
/* Vertically center text */
drawnTextRect.y = textRect.y + (textRect.h - textSur->h) / 2;
drawnTextRect.w = textSur->w;
drawnTextRect.h = textSur->h;
Mar 10, 2016
Mar 10, 2016
486
487
texture = SDL_CreateTextureFromSurface(renderer,textSur);
488
489
SDL_FreeSurface(textSur);
Oct 1, 2016
Oct 1, 2016
490
SDL_RenderCopy(renderer,texture,NULL,&drawnTextRect);
491
SDL_DestroyTexture(texture);
Oct 1, 2016
Oct 1, 2016
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#else
char *utext = text;
Uint32 codepoint;
size_t len;
SDL_Rect dstrect;
dstrect.x = textRect.x;
dstrect.y = textRect.y + (textRect.h - 16 * UNIFONT_DRAW_SCALE) / 2;
dstrect.w = 16 * UNIFONT_DRAW_SCALE;
dstrect.h = 16 * UNIFONT_DRAW_SCALE;
drawnTextRect.y = dstrect.y;
drawnTextRect.h = dstrect.h;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))))
{
Sint32 advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
dstrect.x += advance;
drawnTextRect.w += advance;
utext += len;
}
Oct 1, 2016
Oct 1, 2016
513
}
Oct 1, 2016
Oct 1, 2016
515
516
markedRect.x = textRect.x + drawnTextRect.w;
markedRect.w = textRect.w - drawnTextRect.w;
517
518
519
520
521
522
523
524
525
526
527
if (markedRect.w < 0)
{
/* Stop text input because we cannot hold any more characters */
SDL_StopTextInput();
return;
}
else
{
SDL_StartTextInput();
}
Oct 1, 2016
Oct 1, 2016
528
529
cursorRect = drawnTextRect;
cursorRect.x += cursorRect.w;
Oct 1, 2016
Oct 1, 2016
531
532
533
534
cursorRect.h = drawnTextRect.h;
drawnTextRect.x += drawnTextRect.w;
drawnTextRect.w = 0;
Mar 3, 2016
Mar 3, 2016
536
SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
537
538
539
540
541
SDL_RenderFillRect(renderer,&markedRect);
if (markedText[0])
{
#ifdef HAVE_SDL_TTF
Mar 10, 2016
Mar 10, 2016
542
543
SDL_Surface *textSur;
SDL_Texture *texture;
544
545
546
547
548
if (cursor)
{
char *p = utf8_advance(markedText, cursor);
char c = 0;
if (!p)
Mar 2, 2016
Mar 2, 2016
549
p = &markedText[SDL_strlen(markedText)];
550
551
552
c = *p;
*p = 0;
Oct 1, 2016
Oct 1, 2016
553
554
TTF_SizeUTF8(font, markedText, &drawnTextRect.w, NULL);
cursorRect.x += drawnTextRect.w;
Mar 10, 2016
Mar 10, 2016
557
textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
Oct 1, 2016
Oct 1, 2016
558
559
560
561
562
/* Vertically center text */
drawnTextRect.y = textRect.y + (textRect.h - textSur->h) / 2;
drawnTextRect.w = textSur->w;
drawnTextRect.h = textSur->h;
Mar 10, 2016
Mar 10, 2016
563
texture = SDL_CreateTextureFromSurface(renderer,textSur);
564
565
SDL_FreeSurface(textSur);
Oct 1, 2016
Oct 1, 2016
566
SDL_RenderCopy(renderer,texture,NULL,&drawnTextRect);
567
SDL_DestroyTexture(texture);
Oct 1, 2016
Oct 1, 2016
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#else
int i = 0;
char *utext = markedText;
Uint32 codepoint;
size_t len;
SDL_Rect dstrect;
dstrect.x = drawnTextRect.x;
dstrect.y = textRect.y + (textRect.h - 16 * UNIFONT_DRAW_SCALE) / 2;
dstrect.w = 16 * UNIFONT_DRAW_SCALE;
dstrect.h = 16 * UNIFONT_DRAW_SCALE;
drawnTextRect.y = dstrect.y;
drawnTextRect.h = dstrect.h;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))))
{
Sint32 advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
dstrect.x += advance;
drawnTextRect.w += advance;
if (i < cursor)
cursorRect.x += advance;
i++;
utext += len;
}
Oct 1, 2016
Oct 1, 2016
594
595
596
597
598
599
if (cursor > 0)
{
cursorRect.y = drawnTextRect.y;
cursorRect.h = drawnTextRect.h;
}
600
underlineRect = markedRect;
Oct 1, 2016
Oct 1, 2016
601
underlineRect.y = drawnTextRect.y + drawnTextRect.h - 2;
602
underlineRect.h = 2;
Oct 1, 2016
Oct 1, 2016
603
underlineRect.w = drawnTextRect.w;
Mar 3, 2016
Mar 3, 2016
605
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
Oct 1, 2016
Oct 1, 2016
606
SDL_RenderFillRect(renderer, &underlineRect);
Mar 3, 2016
Mar 3, 2016
609
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
610
611
612
613
614
SDL_RenderFillRect(renderer,&cursorRect);
SDL_SetTextInputRect(&markedRect);
}
Aug 28, 2017
Aug 28, 2017
615
616
void Redraw()
{
617
618
619
620
621
622
623
624
int i;
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
if (state->windows[i] == NULL)
continue;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
Oct 1, 2016
Oct 1, 2016
625
626
/* Sending in the window id to let the font renderers know which one we're working with. */
_Redraw(i);
627
628
629
630
631
SDL_RenderPresent(renderer);
}
}
Aug 28, 2017
Aug 28, 2017
632
633
int main(int argc, char *argv[])
{
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
int i, done;
SDL_Event event;
const char *fontname = DEFAULT_FONT;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
for (i = 1; i < argc;i++) {
SDLTest_CommonArg(state, i);
}
for (argc--, argv++; argc > 0; argc--, argv++)
{
if (strcmp(argv[0], "--help") == 0) {
usage();
return 0;
}
else if (strcmp(argv[0], "--font") == 0)
{
argc--;
argv++;
if (argc > 0)
fontname = argv[0];
else {
usage();
return 0;
}
}
}
if (!SDLTest_CommonInit(state)) {
return 2;
}
#ifdef HAVE_SDL_TTF
/* Initialize fonts */
TTF_Init();
font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
if (! font)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
Aug 28, 2017
Aug 28, 2017
683
return -1;
Oct 1, 2016
Oct 1, 2016
685
#else
Aug 28, 2017
Aug 28, 2017
686
687
688
if (unifont_init(fontname) < 0) {
return -1;
}
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#endif
SDL_Log("Using font: %s\n", fontname);
InitInput();
/* Create the windows and initialize the renderers */
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
}
Redraw();
/* Main render loop */
done = 0;
while (!done) {
/* Check for events */
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
switch(event.type) {
case SDL_KEYDOWN: {
switch (event.key.keysym.sym)
{
case SDLK_RETURN:
text[0]=0x00;
Redraw();
break;
case SDLK_BACKSPACE:
Oct 1, 2016
Oct 1, 2016
717
718
/* Only delete text if not in editing mode. */
if (!markedText[0])
Dec 1, 2015
Dec 1, 2015
720
size_t textlen = SDL_strlen(text);
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
do {
if (textlen==0)
{
break;
}
if ((text[textlen-1] & 0x80) == 0x00)
{
/* One byte */
text[textlen-1]=0x00;
break;
}
if ((text[textlen-1] & 0xC0) == 0x80)
{
/* Byte from the multibyte sequence */
text[textlen-1]=0x00;
textlen--;
}
if ((text[textlen-1] & 0xC0) == 0xC0)
{
/* First byte of multibyte sequence */
text[textlen-1]=0x00;
break;
}
} while(1);
Redraw();
}
break;
}
if (done)
{
break;
}
SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
event.key.keysym.scancode,
SDL_GetScancodeName(event.key.keysym.scancode),
event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
break;
case SDL_TEXTINPUT:
if (event.text.text[0] == '\0' || event.text.text[0] == '\n' ||
markedRect.w < 0)
break;
SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
SDL_strlcat(text, event.text.text, sizeof(text));
SDL_Log("text inputed: %s\n", text);
/* After text inputed, we can clear up markedText because it */
/* is committed */
markedText[0] = 0;
Redraw();
break;
case SDL_TEXTEDITING:
SDL_Log("text editing \"%s\", selected range (%d, %d)\n",
event.edit.text, event.edit.start, event.edit.length);
Oct 1, 2016
Oct 1, 2016
785
SDL_strlcpy(markedText, event.edit.text, SDL_TEXTEDITINGEVENT_TEXT_SIZE);
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
cursor = event.edit.start;
Redraw();
break;
}
break;
}
}
}
CleanupVideo();
SDLTest_CommonQuit(state);
return 0;
}
/* vi: set ts=4 sw=4 expandtab: */