SDL 2.0
testime.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#include "SDL_test_common.h"
+ Include dependency graph for testime.c:

Go to the source code of this file.

Data Structures

struct  UnifontGlyph
 

Macros

#define DEFAULT_PTSIZE   30
 
#define DEFAULT_FONT   "unifont-9.0.02.hex"
 
#define MAX_TEXT_LENGTH   256
 
#define UNIFONT_MAX_CODEPOINT   0x1ffff
 
#define UNIFONT_NUM_GLYPHS   0x20000
 
#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
 

Functions

static Uint8 dehex (char c)
 
static Uint8 dehex2 (char c1, char c2)
 
static Uint8 validate_hex (const char *cp, size_t len, Uint32 *np)
 
static int unifont_init (const char *fontname)
 
static void unifont_make_rgba (Uint8 *src, Uint8 *dst, Uint8 width)
 
static int unifont_load_texture (Uint32 textureID)
 
static Sint32 unifont_draw_glyph (Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
 
static void unifont_cleanup ()
 
size_t utf8_length (unsigned char c)
 
char * utf8_next (char *p)
 
char * utf8_advance (char *p, size_t distance)
 
Uint32 utf8_decode (char *p, size_t len)
 
void usage ()
 
void InitInput ()
 
void CleanupVideo ()
 
void _Redraw (int rendererID)
 
void Redraw ()
 
int main (int argc, char *argv[])
 

Variables

static SDLTest_CommonStatestate
 
static SDL_Rect textRect
 
static SDL_Rect markedRect
 
static SDL_Color lineColor = {0,0,0,255}
 
static SDL_Color backColor = {255,255,255,255}
 
static SDL_Color textColor = {0,0,0,255}
 
static char text [MAX_TEXT_LENGTH]
 
static char markedText [SDL_TEXTEDITINGEVENT_TEXT_SIZE]
 
static int cursor = 0
 
struct UnifontGlyphunifontGlyph
 
static SDL_Texture ** unifontTexture
 
static Uint8 unifontTextureLoaded [UNIFONT_NUM_TEXTURES] = {0}
 

Macro Definition Documentation

◆ DEFAULT_FONT

#define DEFAULT_FONT   "unifont-9.0.02.hex"

Definition at line 38 of file testime.c.

◆ DEFAULT_PTSIZE

#define DEFAULT_PTSIZE   30

Definition at line 27 of file testime.c.

◆ MAX_TEXT_LENGTH

#define MAX_TEXT_LENGTH   256

Definition at line 40 of file testime.c.

◆ UNIFONT_DRAW_SCALE

#define UNIFONT_DRAW_SCALE   2

Definition at line 61 of file testime.c.

◆ UNIFONT_GLYPHS_IN_ROW

#define UNIFONT_GLYPHS_IN_ROW   (UNIFONT_TEXTURE_WIDTH / 16)

Definition at line 56 of file testime.c.

◆ UNIFONT_GLYPHS_IN_TEXTURE

#define UNIFONT_GLYPHS_IN_TEXTURE   (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW)

Definition at line 57 of file testime.c.

◆ UNIFONT_MAX_CODEPOINT

#define UNIFONT_MAX_CODEPOINT   0x1ffff

Definition at line 52 of file testime.c.

◆ UNIFONT_NUM_GLYPHS

#define UNIFONT_NUM_GLYPHS   0x20000

Definition at line 53 of file testime.c.

◆ UNIFONT_NUM_TEXTURES

#define UNIFONT_NUM_TEXTURES   ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE)

Definition at line 58 of file testime.c.

◆ UNIFONT_TEXTURE_PITCH

#define UNIFONT_TEXTURE_PITCH   (UNIFONT_TEXTURE_WIDTH * 4)

Definition at line 60 of file testime.c.

◆ UNIFONT_TEXTURE_SIZE

#define UNIFONT_TEXTURE_SIZE   (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4)

Definition at line 59 of file testime.c.

◆ UNIFONT_TEXTURE_WIDTH

#define UNIFONT_TEXTURE_WIDTH   512

Definition at line 55 of file testime.c.

Function Documentation

◆ _Redraw()

void _Redraw ( int  rendererID)

Definition at line 466 of file testime.c.

467{
468 SDL_Renderer * renderer = state->renderers[rendererID];
469 SDL_Rect drawnTextRect, cursorRect, underlineRect;
470 drawnTextRect = textRect;
471 drawnTextRect.w = 0;
472
475
476 if (*text)
477 {
478#ifdef HAVE_SDL_TTF
479 SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
481
482 /* Vertically center text */
483 drawnTextRect.y = textRect.y + (textRect.h - textSur->h) / 2;
484 drawnTextRect.w = textSur->w;
485 drawnTextRect.h = textSur->h;
486
488 SDL_FreeSurface(textSur);
489
490 SDL_RenderCopy(renderer,texture,NULL,&drawnTextRect);
492#else
493 char *utext = text;
494 Uint32 codepoint;
495 size_t len;
496 SDL_Rect dstrect;
497
498 dstrect.x = textRect.x;
499 dstrect.y = textRect.y + (textRect.h - 16 * UNIFONT_DRAW_SCALE) / 2;
500 dstrect.w = 16 * UNIFONT_DRAW_SCALE;
501 dstrect.h = 16 * UNIFONT_DRAW_SCALE;
502 drawnTextRect.y = dstrect.y;
503 drawnTextRect.h = dstrect.h;
504
505 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))))
506 {
507 Sint32 advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
508 dstrect.x += advance;
509 drawnTextRect.w += advance;
510 utext += len;
511 }
512#endif
513 }
514
515 markedRect.x = textRect.x + drawnTextRect.w;
516 markedRect.w = textRect.w - drawnTextRect.w;
517 if (markedRect.w < 0)
518 {
519 /* Stop text input because we cannot hold any more characters */
521 return;
522 }
523 else
524 {
526 }
527
528 cursorRect = drawnTextRect;
529 cursorRect.x += cursorRect.w;
530 cursorRect.w = 2;
531 cursorRect.h = drawnTextRect.h;
532
533 drawnTextRect.x += drawnTextRect.w;
534 drawnTextRect.w = 0;
535
538
539 if (markedText[0])
540 {
541#ifdef HAVE_SDL_TTF
542 SDL_Surface *textSur;
544 if (cursor)
545 {
547 char c = 0;
548 if (!p)
550
551 c = *p;
552 *p = 0;
553 TTF_SizeUTF8(font, markedText, &drawnTextRect.w, NULL);
554 cursorRect.x += drawnTextRect.w;
555 *p = c;
556 }
557 textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
558 /* Vertically center text */
559 drawnTextRect.y = textRect.y + (textRect.h - textSur->h) / 2;
560 drawnTextRect.w = textSur->w;
561 drawnTextRect.h = textSur->h;
562
564 SDL_FreeSurface(textSur);
565
566 SDL_RenderCopy(renderer,texture,NULL,&drawnTextRect);
568#else
569 int i = 0;
570 char *utext = markedText;
571 Uint32 codepoint;
572 size_t len;
573 SDL_Rect dstrect;
574
575 dstrect.x = drawnTextRect.x;
576 dstrect.y = textRect.y + (textRect.h - 16 * UNIFONT_DRAW_SCALE) / 2;
577 dstrect.w = 16 * UNIFONT_DRAW_SCALE;
578 dstrect.h = 16 * UNIFONT_DRAW_SCALE;
579 drawnTextRect.y = dstrect.y;
580 drawnTextRect.h = dstrect.h;
581
582 while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))))
583 {
584 Sint32 advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
585 dstrect.x += advance;
586 drawnTextRect.w += advance;
587 if (i < cursor)
588 cursorRect.x += advance;
589 i++;
590 utext += len;
591 }
592#endif
593
594 if (cursor > 0)
595 {
596 cursorRect.y = drawnTextRect.y;
597 cursorRect.h = drawnTextRect.h;
598 }
599
600 underlineRect = markedRect;
601 underlineRect.y = drawnTextRect.y + drawnTextRect.h - 2;
602 underlineRect.h = 2;
603 underlineRect.w = drawnTextRect.w;
604
606 SDL_RenderFillRect(renderer, &underlineRect);
607 }
608
610 SDL_RenderFillRect(renderer,&cursorRect);
611
613}
#define SDL_RenderFillRect
#define SDL_DestroyTexture
#define SDL_SetRenderDrawColor
#define SDL_CreateTextureFromSurface
#define SDL_SetTextInputRect
#define SDL_StartTextInput
#define SDL_StopTextInput
#define SDL_strlen
#define SDL_RenderCopy
#define SDL_FreeSurface
GLenum GLsizei len
const GLubyte * c
GLfloat GLfloat p
GLenum GLenum GLuint texture
uint32_t Uint32
Definition: SDL_stdinc.h:203
int32_t Sint32
Definition: SDL_stdinc.h:197
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
Uint8 r
Definition: SDL_pixels.h:297
Uint8 b
Definition: SDL_pixels.h:299
Uint8 a
Definition: SDL_pixels.h:300
Uint8 g
Definition: SDL_pixels.h:298
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
SDL_Renderer ** renderers
static SDL_Renderer * renderer
static SDL_Color backColor
Definition: testime.c:45
#define UNIFONT_DRAW_SCALE
Definition: testime.c:61
static SDL_Rect markedRect
Definition: testime.c:43
static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
Definition: testime.c:320
static SDL_Color lineColor
Definition: testime.c:44
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
static SDL_Color textColor
Definition: testime.c:46
static char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: testime.c:47
static SDL_Rect textRect
Definition: testime.c:43
char * utf8_advance(char *p, size_t distance)
Definition: testime.c:402
size_t utf8_length(unsigned char c)
Definition: testime.c:371
static SDLTest_CommonState * state
Definition: testime.c:42
Uint32 utf8_decode(char *p, size_t len)
Definition: testime.c:412
static int cursor
Definition: testime.c:48

References SDL_Color::a, SDL_Color::b, backColor, cursor, SDL_Color::g, SDL_Rect::h, SDL_Surface::h, i, lineColor, markedRect, markedText, NULL, SDL_Color::r, renderer, SDLTest_CommonState::renderers, SDL_CreateTextureFromSurface, SDL_DestroyTexture, SDL_FreeSurface, SDL_RenderCopy, SDL_RenderFillRect, SDL_SetRenderDrawColor, SDL_SetTextInputRect, SDL_StartTextInput, SDL_StopTextInput, SDL_strlen, state, text, textColor, textRect, unifont_draw_glyph(), UNIFONT_DRAW_SCALE, utf8_advance(), utf8_decode(), utf8_length(), SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by Redraw().

◆ CleanupVideo()

void CleanupVideo ( )

Definition at line 455 of file testime.c.

456{
458#ifdef HAVE_SDL_TTF
459 TTF_CloseFont(font);
460 TTF_Quit();
461#else
463#endif
464}
static void unifont_cleanup()
Definition: testime.c:345

References SDL_StopTextInput, and unifont_cleanup().

Referenced by main().

◆ dehex()

static Uint8 dehex ( char  c)
static

Definition at line 71 of file testime.c.

72{
73 if (c >= '0' && c <= '9')
74 return c - '0';
75 else if (c >= 'a' && c <= 'f')
76 return c - 'a' + 10;
77 else if (c >= 'A' && c <= 'F')
78 return c - 'A' + 10;
79 return 255;
80}

Referenced by dehex2(), and validate_hex().

◆ dehex2()

static Uint8 dehex2 ( char  c1,
char  c2 
)
static

Definition at line 82 of file testime.c.

83{
84 return (dehex(c1) << 4) | dehex(c2);
85}
static Uint8 dehex(char c)
Definition: testime.c:71

References dehex().

Referenced by unifont_init().

◆ InitInput()

void InitInput ( )

Definition at line 441 of file testime.c.

442{
443 /* Prepare a rect for text input */
444 textRect.x = textRect.y = 100;
446 textRect.h = 50;
447
448 text[0] = 0;
450 markedText[0] = 0;
451
453}
#define DEFAULT_WINDOW_WIDTH

References DEFAULT_WINDOW_WIDTH, SDL_Rect::h, markedRect, markedText, SDL_StartTextInput, text, textRect, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 632 of file testime.c.

633{
634 int i, done;
636 const char *fontname = DEFAULT_FONT;
637
638 /* Enable standard application logging */
640
641 /* Initialize test framework */
643 if (!state) {
644 return 1;
645 }
646 for (i = 1; i < argc;i++) {
648 }
649 for (argc--, argv++; argc > 0; argc--, argv++)
650 {
651 if (strcmp(argv[0], "--help") == 0) {
652 usage();
653 return 0;
654 }
655
656 else if (strcmp(argv[0], "--font") == 0)
657 {
658 argc--;
659 argv++;
660
661 if (argc > 0)
662 fontname = argv[0];
663 else {
664 usage();
665 return 0;
666 }
667 }
668 }
669
671 return 2;
672 }
673
674
675#ifdef HAVE_SDL_TTF
676 /* Initialize fonts */
677 TTF_Init();
678
679 font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
680 if (! font)
681 {
682 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
683 return -1;
684 }
685#else
686 if (unifont_init(fontname) < 0) {
687 return -1;
688 }
689#endif
690
691 SDL_Log("Using font: %s\n", fontname);
692
693 InitInput();
694 /* Create the windows and initialize the renderers */
695 for (i = 0; i < state->num_windows; ++i) {
698 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
700 }
701 Redraw();
702 /* Main render loop */
703 done = 0;
704 while (!done) {
705 /* Check for events */
706 while (SDL_PollEvent(&event)) {
708 switch(event.type) {
709 case SDL_KEYDOWN: {
710 switch (event.key.keysym.sym)
711 {
712 case SDLK_RETURN:
713 text[0]=0x00;
714 Redraw();
715 break;
716 case SDLK_BACKSPACE:
717 /* Only delete text if not in editing mode. */
718 if (!markedText[0])
719 {
720 size_t textlen = SDL_strlen(text);
721
722 do {
723 if (textlen==0)
724 {
725 break;
726 }
727 if ((text[textlen-1] & 0x80) == 0x00)
728 {
729 /* One byte */
730 text[textlen-1]=0x00;
731 break;
732 }
733 if ((text[textlen-1] & 0xC0) == 0x80)
734 {
735 /* Byte from the multibyte sequence */
736 text[textlen-1]=0x00;
737 textlen--;
738 }
739 if ((text[textlen-1] & 0xC0) == 0xC0)
740 {
741 /* First byte of multibyte sequence */
742 text[textlen-1]=0x00;
743 break;
744 }
745 } while(1);
746
747 Redraw();
748 }
749 break;
750 }
751
752 if (done)
753 {
754 break;
755 }
756
757 SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
758 event.key.keysym.scancode,
759 SDL_GetScancodeName(event.key.keysym.scancode),
760 event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
761 break;
762
763 case SDL_TEXTINPUT:
764 if (event.text.text[0] == '\0' || event.text.text[0] == '\n' ||
765 markedRect.w < 0)
766 break;
767
768 SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
769
770 if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
771 SDL_strlcat(text, event.text.text, sizeof(text));
772
773 SDL_Log("text inputed: %s\n", text);
774
775 /* After text inputed, we can clear up markedText because it */
776 /* is committed */
777 markedText[0] = 0;
778 Redraw();
779 break;
780
781 case SDL_TEXTEDITING:
782 SDL_Log("text editing \"%s\", selected range (%d, %d)\n",
783 event.edit.text, event.edit.start, event.edit.length);
784
786 cursor = event.edit.start;
787 Redraw();
788 break;
789 }
790 break;
791
792 }
793 }
794 }
795 CleanupVideo();
797 return 0;
798}
#define SDL_INIT_VIDEO
Definition: SDL.h:79
@ SDL_BLENDMODE_NONE
Definition: SDL_blendmode.h:42
#define SDL_PollEvent
#define SDL_GetScancodeName
#define SDL_strlcat
#define SDL_SetRenderDrawBlendMode
#define SDL_strlcpy
#define SDL_LogSetPriority
#define SDL_RenderClear
#define SDL_LogError
#define SDL_GetKeyName
#define SDL_Log
#define SDL_TEXTEDITINGEVENT_TEXT_SIZE
Definition: SDL_events.h:223
@ SDL_TEXTEDITING
Definition: SDL_events.h:98
@ SDL_TEXTINPUT
Definition: SDL_events.h:99
@ SDL_KEYDOWN
Definition: SDL_events.h:96
@ SDLK_BACKSPACE
Definition: SDL_keycode.h:56
@ SDLK_RETURN
Definition: SDL_keycode.h:54
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
struct _cl_event * event
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
int done
Definition: checkkeys.c:28
#define DEFAULT_FONT
Definition: testime.c:38
void usage()
Definition: testime.c:436
static int unifont_init(const char *fontname)
Definition: testime.c:102
#define DEFAULT_PTSIZE
Definition: testime.c:27
void InitInput()
Definition: testime.c:441
void CleanupVideo()
Definition: testime.c:455
void Redraw()
Definition: testime.c:615
General event structure.
Definition: SDL_events.h:558

References CleanupVideo(), cursor, DEFAULT_FONT, DEFAULT_PTSIZE, done, i, InitInput(), markedRect, markedText, SDLTest_CommonState::num_windows, Redraw(), renderer, SDLTest_CommonState::renderers, SDL_BLENDMODE_NONE, SDL_GetKeyName, SDL_GetScancodeName, SDL_INIT_VIDEO, SDL_KEYDOWN, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_PollEvent, SDL_RenderClear, SDL_SetRenderDrawBlendMode, SDL_SetRenderDrawColor, SDL_strlcat, SDL_strlcpy, SDL_strlen, SDL_TEXTEDITING, SDL_TEXTEDITINGEVENT_TEXT_SIZE, SDL_TEXTINPUT, SDLK_BACKSPACE, SDLK_RETURN, SDLTest_CommonArg(), SDLTest_CommonCreateState(), SDLTest_CommonEvent(), SDLTest_CommonInit(), SDLTest_CommonQuit(), state, text, unifont_init(), usage(), and SDL_Rect::w.

◆ Redraw()

void Redraw ( )

Definition at line 615 of file testime.c.

616{
617 int i;
618 for (i = 0; i < state->num_windows; ++i) {
620 if (state->windows[i] == NULL)
621 continue;
622 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
624
625 /* Sending in the window id to let the font renderers know which one we're working with. */
626 _Redraw(i);
627
629 }
630}
#define SDL_RenderPresent
SDL_Window ** windows
void _Redraw(int rendererID)
Definition: testime.c:466

References _Redraw(), i, NULL, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_RenderClear, SDL_RenderPresent, SDL_SetRenderDrawColor, state, and SDLTest_CommonState::windows.

Referenced by main().

◆ unifont_cleanup()

static void unifont_cleanup ( )
static

Definition at line 345 of file testime.c.

346{
347 int i, j;
348 for (i = 0; i < state->num_windows; ++i)
349 {
351 if (state->windows[i] == NULL || renderer == NULL)
352 continue;
353 for (j = 0; j < UNIFONT_NUM_TEXTURES; j++)
354 {
356 if (tex != NULL)
358 }
359 }
360
361 for (j = 0; j < UNIFONT_NUM_TEXTURES; j++)
363
366}
#define SDL_free
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES]
Definition: testime.c:67
struct UnifontGlyph * unifontGlyph
static SDL_Texture ** unifontTexture
Definition: testime.c:66
#define UNIFONT_NUM_TEXTURES
Definition: testime.c:58

References i, j, NULL, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_DestroyTexture, SDL_free, state, UNIFONT_NUM_TEXTURES, unifontGlyph, unifontTexture, unifontTextureLoaded, and SDLTest_CommonState::windows.

Referenced by CleanupVideo().

◆ unifont_draw_glyph()

static Sint32 unifont_draw_glyph ( Uint32  codepoint,
int  rendererID,
SDL_Rect dstrect 
)
static

Definition at line 320 of file testime.c.

321{
323 const Uint32 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
324 SDL_Rect srcrect;
325 srcrect.w = srcrect.h = 16;
326 if (codepoint > UNIFONT_MAX_CODEPOINT) {
327 return 0;
328 }
329 if (!unifontTextureLoaded[textureID]) {
330 if (unifont_load_texture(textureID) < 0) {
331 return 0;
332 }
333 }
334 texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
335 if (texture != NULL)
336 {
337 const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
338 srcrect.x = cInTex % UNIFONT_GLYPHS_IN_ROW * 16;
339 srcrect.y = cInTex / UNIFONT_GLYPHS_IN_ROW * 16;
340 SDL_RenderCopy(state->renderers[rendererID], texture, &srcrect, dstrect);
341 }
342 return unifontGlyph[codepoint].width;
343}
Uint8 width
Definition: testime.c:63
static int unifont_load_texture(Uint32 textureID)
Definition: testime.c:263
#define UNIFONT_GLYPHS_IN_ROW
Definition: testime.c:56
#define UNIFONT_MAX_CODEPOINT
Definition: testime.c:52
#define UNIFONT_GLYPHS_IN_TEXTURE
Definition: testime.c:57

References SDL_Rect::h, NULL, SDLTest_CommonState::renderers, SDL_RenderCopy, state, UNIFONT_GLYPHS_IN_ROW, UNIFONT_GLYPHS_IN_TEXTURE, unifont_load_texture(), UNIFONT_MAX_CODEPOINT, UNIFONT_NUM_TEXTURES, unifontGlyph, unifontTexture, unifontTextureLoaded, SDL_Rect::w, UnifontGlyph::width, SDL_Rect::x, and SDL_Rect::y.

Referenced by _Redraw().

◆ unifont_init()

static int unifont_init ( const char *  fontname)
static

Definition at line 102 of file testime.c.

103{
104 Uint8 hexBuffer[65];
105 Uint32 numGlyphs = 0;
106 int lineNumber = 1;
107 size_t bytesRead;
108 SDL_RWops *hexFile;
109 const size_t unifontGlyphSize = UNIFONT_NUM_GLYPHS * sizeof(struct UnifontGlyph);
110 const size_t unifontTextureSize = UNIFONT_NUM_TEXTURES * state->num_windows * sizeof(void *);
111
112 /* Allocate memory for the glyph data so the file can be closed after initialization. */
113 unifontGlyph = (struct UnifontGlyph *)SDL_malloc(unifontGlyphSize);
114 if (unifontGlyph == NULL)
115 {
116 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.\n", (int)(unifontGlyphSize + 1023) / 1024);
117 return -1;
118 }
119 SDL_memset(unifontGlyph, 0, unifontGlyphSize);
120
121 /* Allocate memory for texture pointers for all renderers. */
122 unifontTexture = (SDL_Texture **)SDL_malloc(unifontTextureSize);
123 if (unifontTexture == NULL)
124 {
125 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.\n", (int)(unifontTextureSize + 1023) / 1024);
126 return -1;
127 }
128 SDL_memset(unifontTexture, 0, unifontTextureSize);
129
130 hexFile = SDL_RWFromFile(fontname, "rb");
131 if (hexFile == NULL)
132 {
133 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s\n", fontname);
134 return -1;
135 }
136
137 /* Read all the glyph data into memory to make it accessible later when textures are created. */
138 do {
139 int i, codepointHexSize;
140 size_t bytesOverread;
141 Uint8 glyphWidth;
142 Uint32 codepoint;
143
144 bytesRead = SDL_RWread(hexFile, hexBuffer, 1, 9);
145 if (numGlyphs > 0 && bytesRead == 0)
146 break; /* EOF */
147 if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9))
148 {
149 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
150 return -1;
151 }
152
153 /* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */
154 if (hexBuffer[2] == ':')
155 codepointHexSize = 2;
156 else if (hexBuffer[4] == ':')
157 codepointHexSize = 4;
158 else if (hexBuffer[6] == ':')
159 codepointHexSize = 6;
160 else if (hexBuffer[8] == ':')
161 codepointHexSize = 8;
162 else
163 {
164 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.\n", lineNumber);
165 return -1;
166 }
167
168 if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint))
169 {
170 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.\n", lineNumber);
171 return -1;
172 }
173 if (codepoint > UNIFONT_MAX_CODEPOINT)
174 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.\n", lineNumber, UNIFONT_MAX_CODEPOINT);
175
176 /* If there was glyph data read in the last file read, move it to the front of the buffer. */
177 bytesOverread = 8 - codepointHexSize;
178 if (codepointHexSize < 8)
179 SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread);
180 bytesRead = SDL_RWread(hexFile, hexBuffer + bytesOverread, 1, 33 - bytesOverread);
181 if (bytesRead < (33 - bytesOverread))
182 {
183 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
184 return -1;
185 }
186 if (hexBuffer[32] == '\n')
187 glyphWidth = 8;
188 else
189 {
190 glyphWidth = 16;
191 bytesRead = SDL_RWread(hexFile, hexBuffer + 33, 1, 32);
192 if (bytesRead < 32)
193 {
194 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
195 return -1;
196 }
197 }
198
199 if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL))
200 {
201 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.\n", lineNumber);
202 return -1;
203 }
204
205 if (codepoint <= UNIFONT_MAX_CODEPOINT)
206 {
207 if (unifontGlyph[codepoint].width > 0)
208 SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Ignoring duplicate codepoint 0x%08x in hex file on line %d.\n", codepoint, lineNumber);
209 else
210 {
211 unifontGlyph[codepoint].width = glyphWidth;
212 /* Pack the hex data into a more compact form. */
213 for (i = 0; i < glyphWidth * 2; i++)
214 unifontGlyph[codepoint].data[i] = dehex2(hexBuffer[i * 2], hexBuffer[i * 2 + 1]);
215 numGlyphs++;
216 }
217 }
218
219 lineNumber++;
220 } while (bytesRead > 0);
221
222 SDL_RWclose(hexFile);
223 SDL_Log("unifont: Loaded %u glyphs.\n", numGlyphs);
224 return 0;
225}
#define SDL_memset
#define SDL_RWread
#define SDL_malloc
#define SDL_RWFromFile
#define SDL_LogWarn
#define SDL_memmove
#define SDL_RWclose
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
GLenum const void GLbitfield GLsizei numGlyphs
uint8_t Uint8
Definition: SDL_stdinc.h:179
static Uint8 dehex2(char c1, char c2)
Definition: testime.c:82
static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np)
Definition: testime.c:87
#define UNIFONT_NUM_GLYPHS
Definition: testime.c:53

References dehex2(), i, NULL, SDLTest_CommonState::num_windows, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_LogWarn, SDL_malloc, SDL_memmove, SDL_memset, SDL_RWclose, SDL_RWFromFile, SDL_RWread, state, UNIFONT_MAX_CODEPOINT, UNIFONT_NUM_GLYPHS, UNIFONT_NUM_TEXTURES, unifontGlyph, unifontTexture, validate_hex(), and UnifontGlyph::width.

Referenced by main().

◆ unifont_load_texture()

static int unifont_load_texture ( Uint32  textureID)
static

Definition at line 263 of file testime.c.

264{
265 int i;
266 Uint8 * textureRGBA;
267
268 if (textureID >= UNIFONT_NUM_TEXTURES)
269 {
270 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %u.\n", textureID);
271 return -1;
272 }
273
274 textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE);
275 if (textureRGBA == NULL)
276 {
277 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.\n", UNIFONT_TEXTURE_SIZE / 1024 / 1024);
278 return -1;
279 }
280 SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE);
281
282 /* Copy the glyphs into memory in RGBA format. */
283 for (i = 0; i < UNIFONT_GLYPHS_IN_TEXTURE; i++)
284 {
285 Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID + i;
286 if (unifontGlyph[codepoint].width > 0)
287 {
288 const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
289 const size_t offset = (cInTex / UNIFONT_GLYPHS_IN_ROW) * UNIFONT_TEXTURE_PITCH * 16 + (cInTex % UNIFONT_GLYPHS_IN_ROW) * 16 * 4;
290 unifont_make_rgba(unifontGlyph[codepoint].data, textureRGBA + offset, unifontGlyph[codepoint].width);
291 }
292 }
293
294 /* Create textures and upload the RGBA data from above. */
295 for (i = 0; i < state->num_windows; ++i)
296 {
298 SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID];
299 if (state->windows[i] == NULL || renderer == NULL || tex != NULL)
300 continue;
302 if (tex == NULL)
303 {
304 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %u for renderer %d.\n", textureID, i);
305 return -1;
306 }
307 unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex;
309 if (SDL_UpdateTexture(tex, NULL, textureRGBA, UNIFONT_TEXTURE_PITCH) != 0)
310 {
311 SDL_Log("unifont error: Failed to update texture %u data for renderer %d.\n", textureID, i);
312 }
313 }
314
315 SDL_free(textureRGBA);
316 unifontTextureLoaded[textureID] = 1;
317 return 0;
318}
@ SDL_BLENDMODE_BLEND
Definition: SDL_blendmode.h:44
#define SDL_CreateTexture
#define SDL_SetTextureBlendMode
#define SDL_UpdateTexture
GLintptr offset
@ SDL_PIXELFORMAT_ARGB8888
Definition: SDL_pixels.h:248
@ SDL_TEXTUREACCESS_STATIC
Definition: SDL_render.h:93
#define UNIFONT_TEXTURE_WIDTH
Definition: testime.c:55
#define UNIFONT_TEXTURE_PITCH
Definition: testime.c:60
#define UNIFONT_TEXTURE_SIZE
Definition: testime.c:59
static void unifont_make_rgba(Uint8 *src, Uint8 *dst, Uint8 width)
Definition: testime.c:227

References i, NULL, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_BLENDMODE_BLEND, SDL_CreateTexture, SDL_free, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_malloc, SDL_memset, SDL_PIXELFORMAT_ARGB8888, SDL_SetTextureBlendMode, SDL_TEXTUREACCESS_STATIC, SDL_UpdateTexture, state, UNIFONT_GLYPHS_IN_ROW, UNIFONT_GLYPHS_IN_TEXTURE, unifont_make_rgba(), UNIFONT_NUM_TEXTURES, UNIFONT_TEXTURE_PITCH, UNIFONT_TEXTURE_SIZE, UNIFONT_TEXTURE_WIDTH, unifontGlyph, unifontTexture, unifontTextureLoaded, and SDLTest_CommonState::windows.

Referenced by unifont_draw_glyph().

◆ unifont_make_rgba()

static void unifont_make_rgba ( Uint8 src,
Uint8 dst,
Uint8  width 
)
static

Definition at line 227 of file testime.c.

228{
229 int i, j;
230 Uint8 *row = dst;
231
232 for (i = 0; i < width * 2; i++)
233 {
234 Uint8 data = src[i];
235 for (j = 0; j < 8; j++)
236 {
237 if (data & 0x80)
238 {
239 row[0] = textColor.r;
240 row[1] = textColor.g;
241 row[2] = textColor.b;
242 row[3] = textColor.a;
243 }
244 else
245 {
246 row[0] = 0;
247 row[1] = 0;
248 row[2] = 0;
249 row[3] = 0;
250 }
251 data <<= 1;
252 row += 4;
253 }
254
255 if (width == 8 || (width == 16 && i % 2 == 1))
256 {
258 row = dst;
259 }
260 }
261}
GLenum src
GLenum GLenum dst
GLenum GLenum void * row

References SDL_Color::a, SDL_Color::b, SDL_Color::g, i, j, SDL_Color::r, textColor, and UNIFONT_TEXTURE_PITCH.

Referenced by unifont_load_texture().

◆ usage()

void usage ( )

Definition at line 436 of file testime.c.

437{
438 SDL_Log("usage: testime [--font fontfile]\n");
439}

References SDL_Log.

Referenced by main().

◆ utf8_advance()

char * utf8_advance ( char *  p,
size_t  distance 
)

Definition at line 402 of file testime.c.

403{
404 size_t i = 0;
405 for (; i < distance && p; ++i)
406 {
407 p = utf8_next(p);
408 }
409 return p;
410}
GLsizei GLsizei GLfloat distance
char * utf8_next(char *p)
Definition: testime.c:386

References i, and utf8_next().

Referenced by _Redraw().

◆ utf8_decode()

Uint32 utf8_decode ( char *  p,
size_t  len 
)

Definition at line 412 of file testime.c.

413{
414 Uint32 codepoint = 0;
415 size_t i = 0;
416 if (!len)
417 return 0;
418
419 for (; i < len; ++i)
420 {
421 if (i == 0)
422 codepoint = (0xff >> len) & *p;
423 else
424 {
425 codepoint <<= 6;
426 codepoint |= 0x3f & *p;
427 }
428 if (!*p)
429 return 0;
430 p++;
431 }
432
433 return codepoint;
434}

References i.

Referenced by _Redraw().

◆ utf8_length()

size_t utf8_length ( unsigned char  c)

Definition at line 371 of file testime.c.

372{
373 c = (unsigned char)(0xff & c);
374 if (c < 0x80)
375 return 1;
376 else if ((c >> 5) ==0x6)
377 return 2;
378 else if ((c >> 4) == 0xe)
379 return 3;
380 else if ((c >> 3) == 0x1e)
381 return 4;
382 else
383 return 0;
384}

Referenced by _Redraw(), and utf8_next().

◆ utf8_next()

char * utf8_next ( char *  p)

Definition at line 386 of file testime.c.

387{
388 size_t len = utf8_length(*p);
389 size_t i = 0;
390 if (!len)
391 return 0;
392
393 for (; i < len; ++i)
394 {
395 ++p;
396 if (!*p)
397 return 0;
398 }
399 return p;
400}

References i, and utf8_length().

Referenced by utf8_advance().

◆ validate_hex()

static Uint8 validate_hex ( const char *  cp,
size_t  len,
Uint32 np 
)
static

Definition at line 87 of file testime.c.

88{
89 Uint32 n = 0;
90 for (; len > 0; cp++, len--)
91 {
92 Uint8 c = dehex(*cp);
93 if (c == 255)
94 return 0;
95 n = (n << 4) | c;
96 }
97 if (np != NULL)
98 *np = n;
99 return 1;
100}
GLdouble n
static const double cp
Definition: e_pow.c:96

References cp, dehex(), and NULL.

Referenced by unifont_init().

Variable Documentation

◆ backColor

SDL_Color backColor = {255,255,255,255}
static

Definition at line 45 of file testime.c.

Referenced by _Redraw().

◆ cursor

int cursor = 0
static

Definition at line 48 of file testime.c.

Referenced by _Redraw(), and main().

◆ lineColor

SDL_Color lineColor = {0,0,0,255}
static

Definition at line 44 of file testime.c.

Referenced by _Redraw().

◆ markedRect

SDL_Rect markedRect
static

Definition at line 43 of file testime.c.

Referenced by _Redraw(), InitInput(), and main().

◆ markedText

char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
static

Definition at line 47 of file testime.c.

Referenced by _Redraw(), InitInput(), and main().

◆ state

◆ text

◆ textColor

SDL_Color textColor = {0,0,0,255}
static

Definition at line 46 of file testime.c.

Referenced by _Redraw(), and unifont_make_rgba().

◆ textRect

SDL_Rect textRect
static

Definition at line 43 of file testime.c.

Referenced by _Redraw(), and InitInput().

◆ unifontGlyph

◆ unifontTexture

SDL_Texture** unifontTexture
static

Definition at line 66 of file testime.c.

Referenced by unifont_cleanup(), unifont_draw_glyph(), unifont_init(), and unifont_load_texture().

◆ unifontTextureLoaded

Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES] = {0}
static

Definition at line 67 of file testime.c.

Referenced by unifont_cleanup(), unifont_draw_glyph(), and unifont_load_texture().