Advertisement
TerusTheBird

masker: sdl_text.c

Jan 7th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.63 KB | Source Code | 0 0
  1. #ifndef __text_c
  2. #define __text_c
  3.  
  4. #include "sdl_text.h"
  5.  
  6. const u64 thin_font[] = {
  7. 0b000000000100000000000100000100001110001110000100ULL,
  8. 0b000000000000000000000000000000001010001010001010ULL,
  9. 0b000000001010001010011111001010011111001010001010ULL,
  10. 0b000000000110001111010000001110000001011110000100ULL,
  11. 0b000000011001010010000010000100001001010011000000ULL,
  12. 0b000000010110001001001001010101000100001010000100ULL,
  13. 0b000000000000000000000000000000001000000100000100ULL,
  14. 0b000000000100000010000001000001000001000010000100ULL,
  15. 0b000000000010000100001000001000001000000100000010ULL,
  16. 0b000000000000001010000100011111000100001010000000ULL,
  17. 0b000000000000001000001000111110001000001000000000ULL,
  18. 0b000010000100000100000000000000000000000000000000ULL,
  19. 0b000000000000000000000000011110000000000000000000ULL,
  20. 0b000000000100000100000000000000000000000000000000ULL,
  21. 0b000000000001000010000100001000010000100000000000ULL,
  22. 0b000000001110010011010011010101011001010001001110ULL,
  23. 0b000000011111000100000100000100000101000110000100ULL,
  24. 0b000000011111010001000110001100010000010001001110ULL,
  25. 0b000000001110010001010000001100010000010001001110ULL,
  26. 0b000000011100001000011111001001001010001100001000ULL,
  27. 0b000000001110010001010000001111000001000001011111ULL,
  28. 0b000000001110010001010001001111000001000010001100ULL,
  29. 0b000000000010000010000010000100001000010001011111ULL,
  30. 0b000000001110010001010001001110010001010001001110ULL,
  31. 0b000000001110010000010000011110010001010001001110ULL,
  32. 0b000000000100000100000000000000000100000100000000ULL,
  33. 0b000010000100000100000000000000000100000100000000ULL,
  34. 0b000000001000000100000010000001000010000100001000ULL,
  35. 0b000000000000011111000000000000011111000000000000ULL,
  36. 0b000000000010000100001000010000001000000100000010ULL,
  37. 0b000000000100000000000100001000010000010001001110ULL,
  38. 0b000000001110000001011101010101011101010001001110ULL,
  39. 0b000000010001010001011111010001010001001010000100ULL,
  40. 0b000000001111010010010010001110010010010010001111ULL,
  41. 0b000000001100010010000001000001000001010010001100ULL,
  42. 0b000000001111010010010010010010010010010010001111ULL,
  43. 0b000000011111010010000010001110000010010010011111ULL,
  44. 0b000000000111000010000010001110000010010010011111ULL,
  45. 0b000000011100010010011001000001000001010010001100ULL,
  46. 0b000000010001010001010001011111010001010001010001ULL,
  47. 0b000000001110000100000100000100000100000100001110ULL,
  48. 0b000000000110001001001001001000001000001000011100ULL,
  49. 0b000000110011001010001010000110001010001010010011ULL,
  50. 0b000000011111010010000010000010000010000010000111ULL,
  51. 0b000000010001010001010001010101010101011011010001ULL,
  52. 0b000000010001010001010001011001010101010011010001ULL,
  53. 0b000000001110010001010001010001010001010001001110ULL,
  54. 0b000000000111000010000010001110010010010010001111ULL,
  55. 0b110000001110010101010001010001010001010001001110ULL,
  56. 0b000000010011001010000110001110010010010010001111ULL,
  57. 0b000000001110010001010000001110000001010001001110ULL,
  58. 0b000000001110000100000100000100000100010101011111ULL,
  59. 0b000000001110010001010001010001010001010001010001ULL,
  60. 0b000000000100001010010001010001010001010001010001ULL,
  61. 0b000000001010010101010101010101010001010001010001ULL,
  62. 0b000000010001001010001010000100001010001010010001ULL,
  63. 0b000000001110000100000100000100001010010001010001ULL,
  64. 0b000000011111010001000010000100001000010001011111ULL,
  65. 0b000000000111000001000001000001000001000001000111ULL,
  66. 0b000000100000010000001000000100000010000001000001ULL,
  67. 0b000000000111000100000100000100000100000100000111ULL,
  68. 0b000000000000000000000000010001010001001010000100ULL,
  69. 0b111111000000000000000000000000000000000000000000ULL,
  70. 0b000000000000000000000000000000001000000100000100ULL,
  71. 0b000000111110010001011110010000001110000000000000ULL,
  72. 0b000000001101010010010110001010000010000010000011ULL,
  73. 0b000000001110010001000001010001001110000000000000ULL,
  74. 0b000000110110011001011001010110010000010000011000ULL,
  75. 0b000000001110000001011111010001001110000000000000ULL,
  76. 0b000000000111000010000010000111000010010010001100ULL,
  77. 0b001111010000011110010001010001010110000000000000ULL,
  78. 0b000000010011010010010010010110001010000010000011ULL,
  79. 0b000000001110000100000100000100000110000000000100ULL,
  80. 0b001110010001010001010000010000011000000000010000ULL,
  81. 0b000000010010001010000110001010010010000010000011ULL,
  82. 0b000000001110000100000100000100000100000100000110ULL,
  83. 0b000000010101010101010101010101001011000000000000ULL,
  84. 0b000000010001010001010001010011001101000000000000ULL,
  85. 0b000000001110010001010001010001001110000000000000ULL,
  86. 0b000111000010001110010010010010001111000000000000ULL,
  87. 0b011100001000001110001001001001010110000000000000ULL,
  88. 0b000000000111000010000010010110011011000000000000ULL,
  89. 0b000000001111010000001110000001011110000000000000ULL,
  90. 0b000000001100010010000010000010001111000010000010ULL,
  91. 0b000000010110011001010001010001010001000000000000ULL,
  92. 0b000000000100001010010001010001010001000000000000ULL,
  93. 0b000000001010010101010101010101010001000000000000ULL,
  94. 0b000000010001001010000100001010010001000000000000ULL,
  95. 0b001111010000011110010001010001010001000000000000ULL,
  96. 0b000000011111000010000100001000011111000000000000ULL,
  97. 0b000000011000000100000100000011000100000100011000ULL,
  98. 0b000000000100000100000100000000000100000100000100ULL,
  99. 0b000000000011000100000100011000000100000100000011ULL,
  100. 0b000000000000000000000000000000000000001101010010ULL,
  101. 0b000000011111010001010001010001001010000100000000ULL
  102. };
  103. #define thin_font_len (sizeof(thin_font) / sizeof(thin_font[0]))
  104.  
  105.  
  106. void text_print_char( int x, int y, int c, color_t color, SDL_Surface* t ) {
  107.     register u64 letter;
  108.     register int dx, dy;
  109.     if( !t ) return;
  110.     if( c == ' ' ) return;
  111.     if( c < '!' || c > '~' ) c = '?';
  112.     c -= '!';
  113.     letter = thin_font[ c ];
  114.     for( dy = 0; dy < 8; ++dy ) {
  115.         for( dx = 0; dx < 6; ++dx ) {
  116.             if( letter & 1 ) {
  117.                 SDL_PutPixel( t, x+dx, y+dy, color );
  118.             }
  119.             letter >>= 1;
  120.         }
  121.     }
  122. }
  123.  
  124. void text_print_str(  int x, int y, char* str, color_t color, SDL_Surface* t ) {
  125.     int cx, cy;
  126.     if( !str || !t ) return;
  127.     cx = x; cy = y;
  128.     while( *str ) {
  129.         if( *str == '\n' ) {
  130.             cx = x;
  131.             cy += 8;
  132.         } else if( *str == '\r' ) {
  133.             cx = x;
  134.         } else if( *str == '\t' ) {
  135.             cx += (6*4);
  136.         } else if( *str == ' ' ) {
  137.             cx += 6;
  138.         } else {
  139.             text_print_char( cx, cy, *str, color, t );
  140.             cx += 6;
  141.         }
  142.         ++str;
  143.     }
  144. }
  145.  
  146.  
  147. void text_print_int( int x, int y, int n, color_t color, SDL_Surface* t ) {
  148.     char letters[ 16 ];
  149.     memset( letters, 0, sizeof(char)*16 );
  150.     snprintf( letters, 15, "%i", n );
  151.     text_print_str( x, y, letters, color, t );
  152. }
  153.  
  154. int text_len_get( char* str, int* w, int* h ) {
  155.     int cx, cy;
  156.     if( !str ) return -1;
  157.     if( !w && !h ) return -2;
  158.     cx = 0;
  159.     cy = 8;
  160.     if( w ) *w = 0;
  161.     if( h ) *h = 8;
  162.     while( *str ) {
  163.         if( *str == '\n' ) {
  164.             cx = 0;
  165.             cy += 8;
  166.             if( h ) if( cy > *h ) *h = cy;
  167.         } else if( *str == '\r' ) {
  168.             cx = 0;
  169.         } else if( *str == '\t' ) {
  170.             cx += (6*4);
  171.             if( w ) if( cx > *w ) *w = cx;
  172.         } else { // space or anything else
  173.             cx += 6;
  174.             if( w ) if( cx > *w ) *w = cx;
  175.         }
  176.         ++str;
  177.     }
  178.     return 0;
  179. }
  180.  
  181.  
  182. void text_print_int_with_info( int x, int y, int n, char* prefix, color_t color, SDL_Surface* t ) {
  183.     int w, h;
  184.     if( !t ) return;
  185.     if( prefix == NULL ) {
  186.         text_print_int( x, y, n, color, t );
  187.         return;
  188.     }
  189.     text_len_get( prefix, &w, &h );
  190.     text_print_str( x, y, prefix, color, t );
  191.     text_print_int( x + w, y, n, color, t );
  192. }
  193.  
  194. void text_print_two_items_float( int x, int y, char* p1, float n1, char* p2, float n2, color_t color, SDL_Surface* t ) {
  195.     char temp_buf[ 4096 ];
  196.     memset( temp_buf, 0, 4096 );
  197.     snprintf( temp_buf, 4095, "%s: %f, %s: %f", p1, n1, p2, n2 );
  198.     text_print_str( x, y, temp_buf, color, t );
  199. }
  200.  
  201. void text_print_str_double( int x, int y, char* s1, char* s2, color_t color, SDL_Surface* t ) {
  202.     int w, h;
  203.     if( !t ) return;
  204.     if( !s1 || !s2 ) return;
  205.     text_len_get( s1, &w, &h );
  206.     text_print_str( x, y, s1, color, t );
  207.     text_print_str( x+w, y, s2, color, t );
  208. }
  209.  
  210.  
  211. void text_print__str_float_float( int x, int y, char* str, float f1, float f2, color_t color, SDL_Surface* t ) {
  212.     char temp_buf[ 128 ];
  213.     memset( temp_buf, 0, 128 );
  214.     snprintf( temp_buf, 127, "%s %.4f  %.4f", str, f1, f2 );
  215.     text_print_str( x, y, temp_buf, color, t );
  216. }
  217.  
  218. void text_print__str_u32_u32( int x, int y, char* str, u32 n1, u32 n2, color_t color, SDL_Surface* t ) {
  219.     char temp_buf[ 128 ];
  220.     memset( temp_buf, 0, 128 );
  221.     snprintf( temp_buf, 127, "%s %08X  %08X", str, n1, n2 );
  222.     text_print_str( x, y, temp_buf, color, t );
  223. }
  224.  
  225. void text_print__str_int_str_int( int x, int y, char* s1, int i1, char* s2, int i2, color_t color, SDL_Surface* t ) {
  226.     char temp_buf[ 256 ];
  227.     memset( temp_buf, 0, 256 );
  228.     snprintf( temp_buf, 255, "%s %i %s %i", s1, i1, s2, i2 );
  229.     text_print_str( x, y, temp_buf, color, t );
  230. }
  231.  
  232.  
  233.  
  234. #endif
  235.  
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement