sfntest4.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * sfntest4.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 normal renderer with bitmap fonts
  27. *
  28. */
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #define SSFN_IMPLEMENTATION
  32. #define SSFN_CONSOLEBITMAP_TRUECOLOR
  33. #include "../ssfn.h"
  34. #if HAS_ZLIB
  35. #include <zlib.h>
  36. #endif
  37. #include <SDL.h>
  38. /**
  39. * Load a font
  40. */
  41. ssfn_font_t *load_font(char *filename)
  42. {
  43. char *fontdata = NULL;
  44. long int size;
  45. FILE *f;
  46. #if HAS_ZLIB
  47. unsigned char hdr[2];
  48. gzFile g;
  49. #endif
  50. f = fopen(filename, "rb");
  51. if(!f) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  52. size = 0;
  53. #if HAS_ZLIB
  54. fread(&hdr, 2, 1, f);
  55. if(hdr[0]==0x1f && hdr[1]==0x8b) {
  56. fseek(f, -4L, SEEK_END);
  57. fread(&size, 4, 1, f);
  58. } else {
  59. fseek(f, 0, SEEK_END);
  60. size = ftell(f);
  61. }
  62. fclose(f);
  63. g = gzopen(filename,"r");
  64. #else
  65. fseek(f, 0, SEEK_END);
  66. size = ftell(f);
  67. fseek(f, 0, SEEK_SET);
  68. #endif
  69. if(!size) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  70. fontdata = malloc(size);
  71. if(!fontdata) { fprintf(stderr,"memory allocation error\n"); exit(2); }
  72. #if HAS_ZLIB
  73. gzread(g, fontdata, size);
  74. gzclose(g);
  75. #else
  76. fread(fontdata, size, 1, f);
  77. fclose(f);
  78. #endif
  79. return (ssfn_font_t*)fontdata;
  80. }
  81. /**
  82. * testing the SSFN library (normal renderer)
  83. */
  84. void do_test(SDL_Surface *screen, char *fontfn)
  85. {
  86. /*
  87. char *s, *str[] = {
  88. "Normal renderer with UNICODE VGA!", "Üdvözlet!", "¡Bienvenido!", "Здравствуйте!", "Καλως ηρθες!", "متعدد اللغات",
  89. NULL };
  90. char *str0 = "Bitmap fonts rendering to Bitmap";
  91. char *str1 = "Bitmap to Alpha channel rendering";
  92. char *str2 = "Bitmap Italic (not that bad)";
  93. char *str3 = "Bitmap Bold style";
  94. char *str4 = "Underline text pqg";
  95. char *str5 = "Strike-through test";
  96. char *str6 = "Color map mode test";
  97. */
  98. int err, x, y, i;
  99. ssfn_t ctx;
  100. ssfn_font_t *font;
  101. /* initialize the normal renderer */
  102. memset(&ctx, 0, sizeof(ssfn_t));
  103. ssfn_dst.ptr = (uint8_t*)screen->pixels;
  104. ssfn_dst.p = screen->pitch;
  105. ssfn_dst.w = screen->w;
  106. ssfn_dst.h = screen->h;
  107. ssfn_dst.fg = 0xFF202020;
  108. ssfn_dst.bg = 0;
  109. ssfn_dst.x = 4; ssfn_dst.y = 48;
  110. /* load and select a font */
  111. font = load_font(fontfn ? fontfn : (char*)"../fonts/VeraR.sfn");
  112. /*font = load_file(fontfn ? fontfn : "../fonts/u_vga16.sfn.gz", &size);*/
  113. /*font = load_file(fontfn ? fontfn : "../fonts/smilely.sfn", &size);*/
  114. err = ssfn_load(&ctx, font);
  115. if(err != SSFN_OK) { fprintf(stderr, "ssfn load error: err=%d %s\n", err, ssfn_error(err)); exit(2); }
  116. err = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR, 20);
  117. if(err != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", err, ssfn_error(err)); exit(2); }
  118. for(y = 0; y < 19; y++) {
  119. for(x = 0; x < 48; x++) {
  120. i = (255 - ((y < 10 ? y : 20 - y) * 255 / 10)) & 0xff;
  121. ((uint32_t*)(screen->pixels))[(y+30)*screen->pitch/4+(x)] = 0xFF0000FF | (i << 16) | (i << 8);
  122. }
  123. for(x = 76; x < 110; x++) {
  124. i = (255 - ((y < 10 ? y : 20 - y) * 255 / 10)) & 0xff;
  125. ((uint32_t*)(screen->pixels))[(y+30)*screen->pitch/4+(x)] = 0xFF0000FF | (i << 16) | (i << 8);
  126. }
  127. }
  128. err = ssfn_render(&ctx, &ssfn_dst, "A");
  129. if(err < 0) { fprintf(stderr, "ssfn render error: err=%d %s\n", err, ssfn_error(err)); exit(2); }
  130. ssfn_dst.x = 28; ssfn_dst.bg = 0xFF00FF00;
  131. err = ssfn_render(&ctx, &ssfn_dst, "A");
  132. if(err < 0) { fprintf(stderr, "ssfn render error: err=%d %s\n", err, ssfn_error(err)); exit(2); }
  133. /* initialize the simple renderer */
  134. ssfn_src = load_font("../fonts/u_vga16.sfn.gz");
  135. ssfn_dst.bg = 0;
  136. ssfn_dst.x = 80; ssfn_dst.y = 32;
  137. ssfn_putc(65);
  138. ssfn_dst.x = 96; ssfn_dst.bg = 0xFF00FF00;
  139. ssfn_putc(65);
  140. /* zoom */
  141. for(y = 0; y < 64; y++) {
  142. for(x = 0; x < 128; x++) {
  143. ((uint32_t*)(screen->pixels))[(128+4*y+0)*screen->pitch/4+(4*x+0)] =
  144. ((uint32_t*)(screen->pixels))[(128+4*y+0)*screen->pitch/4+(4*x+1)] =
  145. ((uint32_t*)(screen->pixels))[(128+4*y+0)*screen->pitch/4+(4*x+2)] =
  146. ((uint32_t*)(screen->pixels))[(128+4*y+0)*screen->pitch/4+(4*x+3)] =
  147. ((uint32_t*)(screen->pixels))[(128+4*y+1)*screen->pitch/4+(4*x+0)] =
  148. ((uint32_t*)(screen->pixels))[(128+4*y+1)*screen->pitch/4+(4*x+1)] =
  149. ((uint32_t*)(screen->pixels))[(128+4*y+1)*screen->pitch/4+(4*x+2)] =
  150. ((uint32_t*)(screen->pixels))[(128+4*y+1)*screen->pitch/4+(4*x+3)] =
  151. ((uint32_t*)(screen->pixels))[(128+4*y+2)*screen->pitch/4+(4*x+0)] =
  152. ((uint32_t*)(screen->pixels))[(128+4*y+2)*screen->pitch/4+(4*x+1)] =
  153. ((uint32_t*)(screen->pixels))[(128+4*y+2)*screen->pitch/4+(4*x+2)] =
  154. ((uint32_t*)(screen->pixels))[(128+4*y+2)*screen->pitch/4+(4*x+3)] =
  155. ((uint32_t*)(screen->pixels))[(128+4*y+3)*screen->pitch/4+(4*x+0)] =
  156. ((uint32_t*)(screen->pixels))[(128+4*y+3)*screen->pitch/4+(4*x+1)] =
  157. ((uint32_t*)(screen->pixels))[(128+4*y+3)*screen->pitch/4+(4*x+2)] =
  158. ((uint32_t*)(screen->pixels))[(128+4*y+3)*screen->pitch/4+(4*x+3)] =
  159. ((uint32_t*)(screen->pixels))[y*screen->pitch/4+(x)];
  160. }
  161. }
  162. printf("Memory allocated: %d %d\n", ssfn_mem(&ctx), (int)sizeof(ssfn_t));
  163. ssfn_free(&ctx);
  164. free(font);
  165. }
  166. /**
  167. * Main procedure
  168. */
  169. int main(int argc __attribute__((unused)), char **argv)
  170. {
  171. SDL_Window *window;
  172. SDL_Surface *screen;
  173. SDL_Event event;
  174. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS)) {
  175. fprintf(stderr,"SDL error %s\n", SDL_GetError());
  176. return 2;
  177. }
  178. window = SDL_CreateWindow("SSFN normal renderer bitmap font test",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,800,600,0);
  179. screen = SDL_GetWindowSurface(window);
  180. memset(screen->pixels, 0xF8, screen->pitch*screen->h);
  181. do_test(screen, argv[1]);
  182. do{ SDL_UpdateWindowSurface(window); SDL_Delay(10); } while(SDL_WaitEvent(&event) && event.type != SDL_QUIT &&
  183. event.type != SDL_MOUSEBUTTONDOWN && event.type != SDL_KEYDOWN);
  184. SDL_DestroyWindow(window);
  185. SDL_Quit();
  186. return 0;
  187. }