🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

810 lines
24 KiB

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