sfntest1.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * sfntest1.c
  3. *
  4. * Copyright (C) 2020 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * @brief testing Scalable Screen Font simple renderer
  27. *
  28. */
  29. #include <stdio.h>
  30. #define SSFN_CONSOLEBITMAP_TRUECOLOR
  31. /*#define SSFN_CONSOLEBITMAP_CONTROL*/
  32. #include "../ssfn.h"
  33. #if HAS_ZLIB
  34. #include <zlib.h>
  35. #endif
  36. #include <SDL.h>
  37. /**
  38. * Load a font
  39. */
  40. ssfn_font_t *load_font(char *filename)
  41. {
  42. char *fontdata = NULL;
  43. long int size;
  44. FILE *f;
  45. #if HAS_ZLIB
  46. unsigned char hdr[2];
  47. gzFile g;
  48. #endif
  49. f = fopen(filename, "rb");
  50. if(!f) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  51. size = 0;
  52. #if HAS_ZLIB
  53. fread(&hdr, 2, 1, f);
  54. if(hdr[0]==0x1f && hdr[1]==0x8b) {
  55. fseek(f, -4L, SEEK_END);
  56. fread(&size, 4, 1, f);
  57. } else {
  58. fseek(f, 0, SEEK_END);
  59. size = ftell(f);
  60. }
  61. fclose(f);
  62. g = gzopen(filename,"r");
  63. #else
  64. fseek(f, 0, SEEK_END);
  65. size = ftell(f);
  66. fseek(f, 0, SEEK_SET);
  67. #endif
  68. if(!size) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  69. fontdata = malloc(size);
  70. if(!fontdata) { fprintf(stderr,"memory allocation error\n"); exit(2); }
  71. #if HAS_ZLIB
  72. gzread(g, fontdata, size);
  73. gzclose(g);
  74. #else
  75. fread(fontdata, size, 1, f);
  76. fclose(f);
  77. #endif
  78. return (ssfn_font_t*)fontdata;
  79. }
  80. /**
  81. * testing the SSFN library (simple bitmap renderer)
  82. */
  83. void do_test(SDL_Surface *screen, char *fn)
  84. {
  85. char *s, *str[] = {
  86. "Simple renderer with UNICODE VGA!", "Üdvözlet!", "¡Bienvenido!", "Здравствуйте!", "Καλως ηρθες!", "متعدد اللغات",
  87. NULL };
  88. char *str0 = "Should appear on the left";
  89. char *str1 = "Should not appear on the left";
  90. char *str2 = "Többnyelvű 多种语言 丕世丽乇 Многоязычный with GNU unifont";
  91. char *str3 = "01234567890123456789012345678901234567890";
  92. int i;
  93. /* initialize the simple renderer */
  94. ssfn_src = load_font(fn && *fn ? fn : "../fonts/u_vga16.sfn.gz");
  95. ssfn_dst.ptr = (uint8_t*)screen->pixels;
  96. ssfn_dst.p = screen->pitch;
  97. ssfn_dst.fg = 0xFFFFFF;
  98. ssfn_dst.bg = 0;
  99. for(i = 0; i < screen->pitch / 4 * screen->h; i += 14)
  100. ((uint32_t*)(screen->pixels))[i] = 0xFF202020;
  101. printf("Testing simple renderer with fixed size bitmap font\n");
  102. /* display strings */
  103. for(i=0; str[i]; i++) {
  104. s = str[i];
  105. ssfn_dst.x = 20;
  106. ssfn_dst.y = 16*(i+1);
  107. while(*s)
  108. ssfn_putc(ssfn_utf8(&s));
  109. }
  110. printf("Testing simple renderer screen edge cutting\n");
  111. /* check screen edges */
  112. for(i=0; i<8; i++) {
  113. ssfn_dst.bg = i & 1 ? 0x444444 : 0;
  114. ssfn_dst.y = 16*(i+3)+128;
  115. ssfn_dst.x = 440+2*i;
  116. s = str0;
  117. while(*s)
  118. ssfn_putc(ssfn_utf8(&s));
  119. }
  120. ssfn_dst.w = screen->w;
  121. for(i=0; i<8; i++) {
  122. ssfn_dst.bg = i & 1 ? 0x444444 : 0;
  123. ssfn_dst.y = 16*(i+3);
  124. ssfn_dst.x = 440+2*i;
  125. s = str1;
  126. while(*s)
  127. ssfn_putc(ssfn_utf8(&s));
  128. }
  129. printf("Testing simple renderer with varying sized bitmap font\n");
  130. /* load another font */
  131. free(ssfn_src);
  132. ssfn_src = load_font("../fonts/unifont.sfn.gz");
  133. /* display strings */
  134. s = str2;
  135. ssfn_dst.bg = 0;
  136. ssfn_dst.x = 16;
  137. ssfn_dst.y = 320;
  138. while(*s)
  139. ssfn_putc(ssfn_utf8(&s));
  140. s = str3;
  141. ssfn_dst.x = 16;
  142. ssfn_dst.y = 336;
  143. while(*s)
  144. ssfn_putc(ssfn_utf8(&s));
  145. printf("Memory allocated: %d\n", 0);
  146. free(ssfn_src);
  147. }
  148. /**
  149. * Main procedure
  150. */
  151. int main(int argc __attribute__((unused)), char **argv __attribute__((unused)))
  152. {
  153. SDL_Window *window;
  154. SDL_Surface *screen;
  155. SDL_Event event;
  156. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS)) {
  157. fprintf(stderr,"SDL error %s\n", SDL_GetError());
  158. return 2;
  159. }
  160. window = SDL_CreateWindow("SSFN simple renderer test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0);
  161. screen = SDL_GetWindowSurface(window);
  162. do_test(screen, argv[1]);
  163. do{ SDL_UpdateWindowSurface(window); SDL_Delay(10); } while(SDL_WaitEvent(&event) && event.type != SDL_QUIT &&
  164. event.type != SDL_MOUSEBUTTONDOWN && event.type != SDL_KEYDOWN);
  165. SDL_DestroyWindow(window);
  166. SDL_Quit();
  167. return 0;
  168. }