sfntest2.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * sfntest2.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_DEBUGGLYPH
  33. #include "../ssfn.h"
  34. #include <SDL.h>
  35. /**
  36. * Load a font
  37. */
  38. ssfn_font_t *load_file(char *filename, int *size)
  39. {
  40. char *fontdata = NULL;
  41. FILE *f;
  42. f = fopen(filename, "rb");
  43. if(!f) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  44. *size = 0;
  45. fseek(f, 0, SEEK_END);
  46. *size = (int)ftell(f);
  47. fseek(f, 0, SEEK_SET);
  48. if(!*size) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  49. fontdata = (char*)malloc(*size);
  50. if(!fontdata) { fprintf(stderr,"memory allocation error\n"); exit(2); }
  51. fread(fontdata, *size, 1, f);
  52. fclose(f);
  53. return (ssfn_font_t*)fontdata;
  54. }
  55. /**
  56. * testing the SSFN library (normal renderer)
  57. */
  58. void do_test(SDL_Surface *screen, char *fontfn)
  59. {
  60. /*
  61. char *s, *str[] = {
  62. "Normal renderer with UNICODE VGA!", "Üdvözlet!", "¡Bienvenido!", "Здравствуйте!", "Καλως ηρθες!", "متعدد اللغات",
  63. NULL };
  64. char *str0 = "Bitmap fonts rendering to Bitmap";
  65. char *str1 = "Bitmap to Alpha channel rendering";
  66. char *str2 = "Bitmap Italic (not that bad)";
  67. char *str3 = "Bitmap Bold style";
  68. char *str4 = "Underline text pqg";
  69. char *str5 = "Strike-through test";
  70. char *str6 = "Color map mode test";
  71. */
  72. int err, size;
  73. ssfn_t ctx;
  74. ssfn_font_t *font;
  75. ssfn_buf_t buf;
  76. int x,y;
  77. /* initialize the normal renderer */
  78. memset(&ctx, 0, sizeof(ssfn_t));
  79. buf.ptr = (uint8_t*)screen->pixels;
  80. buf.p = screen->pitch;
  81. buf.w = screen->w;
  82. buf.h = screen->h;
  83. buf.fg = 0xFF202020;
  84. buf.bg = 0;
  85. buf.x = 4; buf.y = 48;
  86. /* load and select a font */
  87. font = load_file(fontfn ? fontfn : (char*)"../fonts/VeraR.sfn", &size);
  88. /*font = load_file(fontfn ? fontfn : "../fonts/u_vga16.sfn.gz", &size);*/
  89. /*font = load_file(fontfn ? fontfn : "../fonts/smilely.sfn", &size);*/
  90. err = ssfn_load(&ctx, font);
  91. if(err != SSFN_OK) { fprintf(stderr, "ssfn load error: err=%d %s\n", err, ssfn_error(err)); exit(2); }
  92. err = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | SSFN_STYLE_UNDERLINE, 20);
  93. if(err != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", err, ssfn_error(err)); exit(2); }
  94. err = ssfn_render(&ctx, &buf, "i");
  95. if(err < 0) { fprintf(stderr, "ssfn render error: err=%d %s\n", err, ssfn_error(err)); exit(2); }
  96. for(y = 0; y < 64; y++) {
  97. for(x = 0; x < 128; x++) {
  98. ((uint32_t*)(screen->pixels))[(128+4*y+0)*screen->pitch/4+(4*x+0)] =
  99. ((uint32_t*)(screen->pixels))[(128+4*y+0)*screen->pitch/4+(4*x+1)] =
  100. ((uint32_t*)(screen->pixels))[(128+4*y+0)*screen->pitch/4+(4*x+2)] =
  101. ((uint32_t*)(screen->pixels))[(128+4*y+0)*screen->pitch/4+(4*x+3)] =
  102. ((uint32_t*)(screen->pixels))[(128+4*y+1)*screen->pitch/4+(4*x+0)] =
  103. ((uint32_t*)(screen->pixels))[(128+4*y+1)*screen->pitch/4+(4*x+1)] =
  104. ((uint32_t*)(screen->pixels))[(128+4*y+1)*screen->pitch/4+(4*x+2)] =
  105. ((uint32_t*)(screen->pixels))[(128+4*y+1)*screen->pitch/4+(4*x+3)] =
  106. ((uint32_t*)(screen->pixels))[(128+4*y+2)*screen->pitch/4+(4*x+0)] =
  107. ((uint32_t*)(screen->pixels))[(128+4*y+2)*screen->pitch/4+(4*x+1)] =
  108. ((uint32_t*)(screen->pixels))[(128+4*y+2)*screen->pitch/4+(4*x+2)] =
  109. ((uint32_t*)(screen->pixels))[(128+4*y+2)*screen->pitch/4+(4*x+3)] =
  110. ((uint32_t*)(screen->pixels))[(128+4*y+3)*screen->pitch/4+(4*x+0)] =
  111. ((uint32_t*)(screen->pixels))[(128+4*y+3)*screen->pitch/4+(4*x+1)] =
  112. ((uint32_t*)(screen->pixels))[(128+4*y+3)*screen->pitch/4+(4*x+2)] =
  113. ((uint32_t*)(screen->pixels))[(128+4*y+3)*screen->pitch/4+(4*x+3)] =
  114. ((uint32_t*)(screen->pixels))[y*screen->pitch/4+(x)];
  115. }
  116. }
  117. printf("Memory allocated: %d %d\n", ssfn_mem(&ctx), (int)sizeof(ssfn_t));
  118. ssfn_free(&ctx);
  119. free(font);
  120. }
  121. /**
  122. * Main procedure
  123. */
  124. int main(int argc __attribute__((unused)), char **argv)
  125. {
  126. SDL_Window *window;
  127. SDL_Surface *screen;
  128. SDL_Event event;
  129. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS)) {
  130. fprintf(stderr,"SDL error %s\n", SDL_GetError());
  131. return 2;
  132. }
  133. window = SDL_CreateWindow("SSFN normal renderer bitmap font test",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,800,600,0);
  134. screen = SDL_GetWindowSurface(window);
  135. memset(screen->pixels, 0xF8, screen->pitch*screen->h);
  136. do_test(screen, argv[1]);
  137. do{ SDL_UpdateWindowSurface(window); SDL_Delay(10); } while(SDL_WaitEvent(&event) && event.type != SDL_QUIT &&
  138. event.type != SDL_MOUSEBUTTONDOWN && event.type != SDL_KEYDOWN);
  139. SDL_DestroyWindow(window);
  140. SDL_Quit();
  141. return 0;
  142. }