Debian Codegraph @

test/testime.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12 /* A simple program to test the Input Method support in the SDL library (2.0+)
13 If you build without SDL_ttf, you can use the GNU Unifont hex file instead.
14 Download at http://unifoundry.com/unifont.html */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19
20 #include "SDL.h"
21 #ifdef HAVE_SDL_TTF
22 #include "SDL_ttf.h"
23 #endif
24
25 #include "SDL_test_common.h"
26 #include "testutils.h"
27
28 #define DEFAULT_PTSIZE 30
29 #ifdef HAVE_SDL_TTF
30 #ifdef __MACOSX__
31 #define DEFAULT_FONT "/System/Library/Fonts/华文细黑.ttf"
32 #elif defined(__WIN32__)
33 /* Some japanese font present on at least Windows 8.1. */
34 #define DEFAULT_FONT "C:\\Windows\\Fonts\\yugothic.ttf"
35 #else
36 #define DEFAULT_FONT "NoDefaultFont.ttf"
37 #endif
38 #else
39 #define DEFAULT_FONT "unifont-13.0.06.hex"
40 #endif
41 #define MAX_TEXT_LENGTH 256
42
43 static SDLTest_CommonState *state;
44 static SDL_Rect textRect, markedRect;
45 static SDL_Color lineColor = { 0, 0, 0, 255 };
46 static SDL_Color backColor = { 255, 255, 255, 255 };
47 static SDL_Color textColor = { 0, 0, 0, 255 };
48 static char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
49 static int cursor = 0;
50 #ifdef HAVE_SDL_TTF
51 static TTF_Font *font;
52 #else
53 #define UNIFONT_MAX_CODEPOINT 0x1ffff
54 #define UNIFONT_NUM_GLYPHS 0x20000
55 /* Using 512x512 textures that are supported everywhere. */
56 #define UNIFONT_TEXTURE_WIDTH 512
57 #define UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / 16)
58 #define UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW)
59 #define UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE)
60 #define UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4)
61 #define UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4)
62 #define UNIFONT_DRAW_SCALE 2
63 struct UnifontGlyph
64 {
65 Uint8 width;
66 Uint8 data[32];
67 } * unifontGlyph;
68 static SDL_Texture **unifontTexture;
69 static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES] = { 0 };
70
71 /* Unifont loading code start */
72
73 static Uint8 dehex(char c)
74 {
75 if (c >= '0' && c <= '9') {
76 return c - '0';
77 } else if (c >= 'a' && c <= 'f') {
78 return c - 'a' + 10;
79 } else if (c >= 'A' && c <= 'F') {
80 return c - 'A' + 10;
81 }
82 return 255;
83 }
84
85 static Uint8 dehex2(char c1, char c2)
86 {
87 return (dehex(c1) << 4) | dehex(c2);
88 }
89
90 static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np)
91 {
92 Uint32 n = 0;
93 for (; len > 0; cp++, len--) {
94 Uint8 c = dehex(*cp);
95 if (c == 255) {
96 return 0;
97 }
98 n = (n << 4) | c;
99 }
100 if (np) {
101 *np = n;
102 }
103 return 1;
104 }
105
106 static int unifont_init(const char *fontname)
107 {
108 Uint8 hexBuffer[65];
109 Uint32 numGlyphs = 0;
110 int lineNumber = 1;
111 size_t bytesRead;
112 SDL_RWops *hexFile;
113 const size_t unifontGlyphSize = UNIFONT_NUM_GLYPHS * sizeof(struct UnifontGlyph);
114 const size_t unifontTextureSize = UNIFONT_NUM_TEXTURES * state->num_windows * sizeof(void *);
115 char *filename;
116
117 /* Allocate memory for the glyph data so the file can be closed after initialization. */
118 unifontGlyph = (struct UnifontGlyph *)SDL_malloc(unifontGlyphSize);
119 if (!unifontGlyph) {
120 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.\n", (int)(unifontGlyphSize + 1023) / 1024);
121 return -1;
122 }
123 SDL_memset(unifontGlyph, 0, unifontGlyphSize);
124
125 /* Allocate memory for texture pointers for all renderers. */
126 unifontTexture = (SDL_Texture **)SDL_malloc(unifontTextureSize);
127 if (!unifontTexture) {
128 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.\n", (int)(unifontTextureSize + 1023) / 1024);
129 return -1;
130 }
131 SDL_memset(unifontTexture, 0, unifontTextureSize);
132
133 filename = GetResourceFilename(NULL, fontname);
134 if (!filename) {
135 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory\n");
136 return -1;
137 }
138 hexFile = SDL_RWFromFile(filename, "rb");
139 SDL_free(filename);
140 if (!hexFile) {
141 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s\n", fontname);
142 return -1;
143 }
144
145 /* Read all the glyph data into memory to make it accessible later when textures are created. */
146 do {
147 int i, codepointHexSize;
148 size_t bytesOverread;
149 Uint8 glyphWidth;
150 Uint32 codepoint;
151
152 bytesRead = SDL_RWread(hexFile, hexBuffer, 1, 9);
153 if (numGlyphs > 0 && bytesRead == 0) {
154 break; /* EOF */
155 }
156 if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9)) {
157 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
158 return -1;
159 }
160
161 /* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */
162 if (hexBuffer[2] == ':') {
163 codepointHexSize = 2;
164 } else if (hexBuffer[4] == ':') {
165 codepointHexSize = 4;
166 } else if (hexBuffer[6] == ':') {
167 codepointHexSize = 6;
168 } else if (hexBuffer[8] == ':') {
169 codepointHexSize = 8;
170 } else {
171 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.\n", lineNumber);
172 return -1;
173 }
174
175 if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint)) {
176 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.\n", lineNumber);
177 return -1;
178 }
179 if (codepoint > UNIFONT_MAX_CODEPOINT) {
180 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.\n", lineNumber, UNIFONT_MAX_CODEPOINT);
181 }
182
183 /* If there was glyph data read in the last file read, move it to the front of the buffer. */
184 bytesOverread = 8 - codepointHexSize;
185 if (codepointHexSize < 8) {
186 SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread);
187 }
188 bytesRead = SDL_RWread(hexFile, hexBuffer + bytesOverread, 1, 33 - bytesOverread);
189 if (bytesRead < (33 - bytesOverread)) {
190 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
191 return -1;
192 }
193 if (hexBuffer[32] == '\n') {
194 glyphWidth = 8;
195 } else {
196 glyphWidth = 16;
197 bytesRead = SDL_RWread(hexFile, hexBuffer + 33, 1, 32);
198 if (bytesRead < 32) {
199 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
200 return -1;
201 }
202 }
203
204 if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL)) {
205 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.\n", lineNumber);
206 return -1;
207 }
208
209 if (codepoint <= UNIFONT_MAX_CODEPOINT) {
210 if (unifontGlyph[codepoint].width > 0) {
211 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Ignoring duplicate codepoint 0x%08" SDL_PRIx32 " in hex file on line %d.\n", codepoint, lineNumber);
212 } else {
213 unifontGlyph[codepoint].width = glyphWidth;
214 /* Pack the hex data into a more compact form. */
215 for (i = 0; i < glyphWidth * 2; i++) {
216 unifontGlyph[codepoint].data[i] = dehex2(hexBuffer[i * 2], hexBuffer[i * 2 + 1]);
217 }
218 numGlyphs++;
219 }
220 }
221
222 lineNumber++;
223 } while (bytesRead > 0);
224
225 SDL_RWclose(hexFile);
226 SDL_Log("unifont: Loaded %" SDL_PRIu32 " glyphs.\n", numGlyphs);
227 return 0;
228 }
229
230 static void
231 unifont_make_rgba(const Uint8 *src, Uint8 *dst, Uint8 width)
232 {
233 int i, j;
234 Uint8 *row = dst;
235
236 for (i = 0; i < width * 2; i++) {
237 Uint8 data = src[i];
238 for (j = 0; j < 8; j++) {
239 if (data & 0x80) {
240 row[0] = textColor.r;
241 row[1] = textColor.g;
242 row[2] = textColor.b;
243 row[3] = textColor.a;
244 } else {
245 row[0] = 0;
246 row[1] = 0;
247 row[2] = 0;
248 row[3] = 0;
249 }
250 data <<= 1;
251 row += 4;
252 }
253
254 if (width == 8 || (width == 16 && i % 2 == 1)) {
255 dst += UNIFONT_TEXTURE_PITCH;
256 row = dst;
257 }
258 }
259 }
260
261 static int unifont_load_texture(Uint32 textureID)
262 {
263 int i;
264 Uint8 *textureRGBA;
265
266 if (textureID >= UNIFONT_NUM_TEXTURES) {
267 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %" SDL_PRIu32 "\n", textureID);
268 return -1;
269 }
270
271 textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE);
272 if (!textureRGBA) {
273 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.\n", UNIFONT_TEXTURE_SIZE / 1024 / 1024);
274 return -1;
275 }
276 SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE);
277
278 /* Copy the glyphs into memory in RGBA format. */
279 for (i = 0; i < UNIFONT_GLYPHS_IN_TEXTURE; i++) {
280 Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID + i;
281 if (unifontGlyph[codepoint].width > 0) {
282 const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
283 const size_t offset = ((size_t)cInTex / UNIFONT_GLYPHS_IN_ROW) * UNIFONT_TEXTURE_PITCH * 16 + (cInTex % UNIFONT_GLYPHS_IN_ROW) * 16 * 4;
284 unifont_make_rgba(unifontGlyph[codepoint].data, textureRGBA + offset, unifontGlyph[codepoint].width);
285 }
286 }
287
288 /* Create textures and upload the RGBA data from above. */
289 for (i = 0; i < state->num_windows; ++i) {
290 SDL_Renderer *renderer = state->renderers[i];
291 SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID];
292 if (state->windows[i] == NULL || renderer == NULL || tex != NULL) {
293 continue;
294 }
295 tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, UNIFONT_TEXTURE_WIDTH, UNIFONT_TEXTURE_WIDTH);
296 if (tex == NULL) {
297 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %" SDL_PRIu32 " for renderer %d.\n", textureID, i);
298 return -1;
299 }
300 unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex;
301 SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
302 if (SDL_UpdateTexture(tex, NULL, textureRGBA, UNIFONT_TEXTURE_PITCH) != 0) {
303 SDL_Log("unifont error: Failed to update texture %" SDL_PRIu32 " data for renderer %d.\n", textureID, i);
304 }
305 }
306
307 SDL_free(textureRGBA);
308 unifontTextureLoaded[textureID] = 1;
309 return 0;
310 }
311
312 static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
313 {
314 SDL_Texture *texture;
315 const Uint32 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
316 SDL_Rect srcrect;
317 srcrect.w = srcrect.h = 16;
318 if (codepoint > UNIFONT_MAX_CODEPOINT) {
319 return 0;
320 }
321 if (!unifontTextureLoaded[textureID]) {
322 if (unifont_load_texture(textureID) < 0) {
323 return 0;
324 }
325 }
326 texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
327 if (texture) {
328 const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
329 srcrect.x = cInTex % UNIFONT_GLYPHS_IN_ROW * 16;
330 srcrect.y = cInTex / UNIFONT_GLYPHS_IN_ROW * 16;
331 SDL_RenderCopy(state->renderers[rendererID], texture, &srcrect, dstrect);
332 }
333 return unifontGlyph[codepoint].width;
334 }
335
336 static void unifont_cleanup(void)
337 {
338 int i, j;
339 for (i = 0; i < state->num_windows; ++i) {
340 SDL_Renderer *renderer = state->renderers[i];
341 if (state->windows[i] == NULL || !renderer) {
342 continue;
343 }
344 for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) {
345 SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + j];
346 if (tex) {
347 SDL_DestroyTexture(tex);
348 }
349 }
350 }
351
352 for (j = 0; j < UNIFONT_NUM_TEXTURES; j++) {
353 unifontTextureLoaded[j] = 0;
354 }
355
356 SDL_free(unifontTexture);
357 SDL_free(unifontGlyph);
358 }
359
360 /* Unifont code end */
361 #endif
362
363 size_t utf8_length(unsigned char c)
364 {
365 c = (unsigned char)(0xff & c);
366 if (c < 0x80) {
367 return 1;
368 } else if ((c >> 5) == 0x6) {
369 return 2;
370 } else if ((c >> 4) == 0xe) {
371 return 3;
372 } else if ((c >> 3) == 0x1e) {
373 return 4;
374 }
375 return 0;
376 }
377
378 char *utf8_next(char *p)
379 {
380 size_t len = utf8_length(*p);
381 size_t i = 0;
382 if (!len) {
383 return 0;
384 }
385
386 for (; i < len; ++i) {
387 ++p;
388 if (!*p) {
389 return 0;
390 }
391 }
392 return p;
393 }
394
395 char *utf8_advance(char *p, size_t distance)
396 {
397 size_t i = 0;
398 for (; i < distance && p; ++i) {
399 p = utf8_next(p);
400 }
401 return p;
402 }
403
404 Uint32 utf8_decode(char *p, size_t len)
405 {
406 Uint32 codepoint = 0;
407 size_t i = 0;
408 if (!len) {
409 return 0;
410 }
411
412 for (; i < len; ++i) {
413 if (i == 0) {
414 codepoint = (0xff >> len) & *p;
415 } else {
416 codepoint <<= 6;
417 codepoint |= 0x3f & *p;
418 }
419 if (!*p) {
420 return 0;
421 }
422 p++;
423 }
424
425 return codepoint;
426 }
427
428 void usage(void)
429 {
430 SDL_Log("usage: testime [--font fontfile]\n");
431 }
432
433 void InitInput(void)
434 {
435 /* Prepare a rect for text input */
436 textRect.x = textRect.y = 100;
437 textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
438 textRect.h = 50;
439
440 text[0] = 0;
441 markedRect = textRect;
442 markedText[0] = 0;
443
444 SDL_StartTextInput();
445 }
446
447 void CleanupVideo(void)
448 {
449 SDL_StopTextInput();
450 #ifdef HAVE_SDL_TTF
451 TTF_CloseFont(font);
452 TTF_Quit();
453 #else
454 unifont_cleanup();
455 #endif
456 }
457
458 void _Redraw(int rendererID)
459 {
460 SDL_Renderer *renderer = state->renderers[rendererID];
461 SDL_Rect drawnTextRect, cursorRect, underlineRect;
462 drawnTextRect = textRect;
463 drawnTextRect.w = 0;
464
465 SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
466 SDL_RenderFillRect(renderer, &textRect);
467
468 if (*text) {
469 #ifdef HAVE_SDL_TTF
470 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
471 SDL_Texture *texture;
472
473 /* Vertically center text */
474 drawnTextRect.y = textRect.y + (textRect.h - textSur->h) / 2;
475 drawnTextRect.w = textSur->w;
476 drawnTextRect.h = textSur->h;
477
478 texture = SDL_CreateTextureFromSurface(renderer, textSur);
479 SDL_FreeSurface(textSur);
480
481 SDL_RenderCopy(renderer, texture, NULL, &drawnTextRect);
482 SDL_DestroyTexture(texture);
483 #else
484 char *utext = text;
485 Uint32 codepoint;
486 size_t len;
487 SDL_Rect dstrect;
488
489 dstrect.x = textRect.x;
490 dstrect.y = textRect.y + (textRect.h - 16 * UNIFONT_DRAW_SCALE) / 2;
491 dstrect.w = 16 * UNIFONT_DRAW_SCALE;
492 dstrect.h = 16 * UNIFONT_DRAW_SCALE;
493 drawnTextRect.y = dstrect.y;
494 drawnTextRect.h = dstrect.h;
495
496 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext)))) {
497 Sint32 advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
498 dstrect.x += advance;
499 drawnTextRect.w += advance;
500 utext += len;
501 }
502 #endif
503 }
504
505 markedRect.x = textRect.x + drawnTextRect.w;
506 markedRect.w = textRect.w - drawnTextRect.w;
507 if (markedRect.w < 0) {
508 /* Stop text input because we cannot hold any more characters */
509 SDL_StopTextInput();
510 return;
511 } else {
512 SDL_StartTextInput();
513 }
514
515 cursorRect = drawnTextRect;
516 cursorRect.x += cursorRect.w;
517 cursorRect.w = 2;
518 cursorRect.h = drawnTextRect.h;
519
520 drawnTextRect.x += drawnTextRect.w;
521 drawnTextRect.w = 0;
522
523 SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
524 SDL_RenderFillRect(renderer, &markedRect);
525
526 if (markedText[0]) {
527 #ifdef HAVE_SDL_TTF
528 SDL_Surface *textSur;
529 SDL_Texture *texture;
530 if (cursor) {
531 char *p = utf8_advance(markedText, cursor);
532 char c = 0;
533 if (!p) {
534 p = &markedText[SDL_strlen(markedText)];
535 }
536
537 c = *p;
538 *p = 0;
539 TTF_SizeUTF8(font, markedText, &drawnTextRect.w, NULL);
540 cursorRect.x += drawnTextRect.w;
541 *p = c;
542 }
543 textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
544 /* Vertically center text */
545 drawnTextRect.y = textRect.y + (textRect.h - textSur->h) / 2;
546 drawnTextRect.w = textSur->w;
547 drawnTextRect.h = textSur->h;
548
549 texture = SDL_CreateTextureFromSurface(renderer, textSur);
550 SDL_FreeSurface(textSur);
551
552 SDL_RenderCopy(renderer, texture, NULL, &drawnTextRect);
553 SDL_DestroyTexture(texture);
554 #else
555 int i = 0;
556 char *utext = markedText;
557 Uint32 codepoint;
558 size_t len;
559 SDL_Rect dstrect;
560
561 dstrect.x = drawnTextRect.x;
562 dstrect.y = textRect.y + (textRect.h - 16 * UNIFONT_DRAW_SCALE) / 2;
563 dstrect.w = 16 * UNIFONT_DRAW_SCALE;
564 dstrect.h = 16 * UNIFONT_DRAW_SCALE;
565 drawnTextRect.y = dstrect.y;
566 drawnTextRect.h = dstrect.h;
567
568 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext)))) {
569 Sint32 advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
570 dstrect.x += advance;
571 drawnTextRect.w += advance;
572 if (i < cursor) {
573 cursorRect.x += advance;
574 }
575 i++;
576 utext += len;
577 }
578 #endif
579
580 if (cursor > 0) {
581 cursorRect.y = drawnTextRect.y;
582 cursorRect.h = drawnTextRect.h;
583 }
584
585 underlineRect = markedRect;
586 underlineRect.y = drawnTextRect.y + drawnTextRect.h - 2;
587 underlineRect.h = 2;
588 underlineRect.w = drawnTextRect.w;
589
590 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
591 SDL_RenderFillRect(renderer, &underlineRect);
592 }
593
594 SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
595 SDL_RenderFillRect(renderer, &cursorRect);
596
597 SDL_SetTextInputRect(&markedRect);
598 }
599
600 void Redraw(void)
601 {
602 int i;
603 for (i = 0; i < state->num_windows; ++i) {
604 SDL_Renderer *renderer = state->renderers[i];
605 if (state->windows[i] == NULL) {
606 continue;
607 }
608 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
609 SDL_RenderClear(renderer);
610
611 /* Sending in the window id to let the font renderers know which one we're working with. */
612 _Redraw(i);
613
614 SDL_RenderPresent(renderer);
615 }
616 }
617
618 int main(int argc, char *argv[])
619 {
620 int i, done;
621 SDL_Event event;
622 const char *fontname = DEFAULT_FONT;
623
624 /* Enable standard application logging */
625 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
626
627 /* Initialize test framework */
628 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
629 if (!state) {
630 return 1;
631 }
632 for (i = 1; i < argc; i++) {
633 SDLTest_CommonArg(state, i);
634 }
635 for (argc--, argv++; argc > 0; argc--, argv++) {
636 if (SDL_strcmp(argv[0], "--help") == 0) {
637 usage();
638 return 0;
639 }
640
641 else if (SDL_strcmp(argv[0], "--font") == 0) {
642 argc--;
643 argv++;
644
645 if (argc > 0) {
646 fontname = argv[0];
647 } else {
648 usage();
649 return 0;
650 }
651 }
652 }
653
654 if (!SDLTest_CommonInit(state)) {
655 return 2;
656 }
657
658 #ifdef HAVE_SDL_TTF
659 /* Initialize fonts */
660 TTF_Init();
661
662 font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
663 if (!font) {
664 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
665 return -1;
666 }
667 #else
668 if (unifont_init(fontname) < 0) {
669 return -1;
670 }
671 #endif
672
673 SDL_Log("Using font: %s\n", fontname);
674
675 InitInput();
676 /* Create the windows and initialize the renderers */
677 for (i = 0; i < state->num_windows; ++i) {
678 SDL_Renderer *renderer = state->renderers[i];
679 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
680 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
681 SDL_RenderClear(renderer);
682 }
683 Redraw();
684 /* Main render loop */
685 done = 0;
686 while (!done) {
687 /* Check for events */
688 while (SDL_PollEvent(&event)) {
689 SDLTest_CommonEvent(state, &event, &done);
690 switch (event.type) {
691 case SDL_KEYDOWN:
692 switch (event.key.keysym.sym) {
693 case SDLK_RETURN:
694 text[0] = 0x00;
695 Redraw();
696 break;
697 case SDLK_BACKSPACE:
698 /* Only delete text if not in editing mode. */
699 if (!markedText[0]) {
700 size_t textlen = SDL_strlen(text);
701
702 do {
703 if (textlen == 0) {
704 break;
705 }
706 if (!(text[textlen - 1] & 0x80)) {
707 /* One byte */
708 text[textlen - 1] = 0x00;
709 break;
710 }
711 if ((text[textlen - 1] & 0xC0) == 0x80) {
712 /* Byte from the multibyte sequence */
713 text[textlen - 1] = 0x00;
714 textlen--;
715 }
716 if ((text[textlen - 1] & 0xC0) == 0xC0) {
717 /* First byte of multibyte sequence */
718 text[textlen - 1] = 0x00;
719 break;
720 }
721 } while (1);
722
723 Redraw();
724 }
725 break;
726 }
727
728 if (done) {
729 break;
730 }
731
732 SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08" SDL_PRIX32 " = %s\n",
733 event.key.keysym.scancode,
734 SDL_GetScancodeName(event.key.keysym.scancode),
735 SDL_static_cast(Uint32, event.key.keysym.sym),
736 SDL_GetKeyName(event.key.keysym.sym));
737 break;
738
739 case SDL_TEXTINPUT:
740 if (event.text.text[0] == '\0' || event.text.text[0] == '\n' || markedRect.w < 0) {
741 break;
742 }
743
744 SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
745
746 if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text)) {
747 SDL_strlcat(text, event.text.text, sizeof(text));
748 }
749
750 SDL_Log("text inputed: %s\n", text);
751
752 /* After text inputed, we can clear up markedText because it */
753 /* is committed */
754 markedText[0] = 0;
755 Redraw();
756 break;
757
758 case SDL_TEXTEDITING:
759 SDL_Log("text editing \"%s\", selected range (%" SDL_PRIs32 ", %" SDL_PRIs32 ")\n",
760 event.edit.text, event.edit.start, event.edit.length);
761
762 SDL_strlcpy(markedText, event.edit.text, SDL_TEXTEDITINGEVENT_TEXT_SIZE);
763 cursor = event.edit.start;
764 Redraw();
765 break;
766 }
767 }
768 }
769 CleanupVideo();
770 SDLTest_CommonQuit(state);
771 return 0;
772 }
773
774
775 /* vi: set ts=4 sw=4 expandtab: */