sfntest3.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * sfntest3.c
  3. *
  4. * Copyright (C) 2019 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 renderer scaling
  27. *
  28. */
  29. #include <stdio.h>
  30. #define SSFN_IMPLEMENTATION
  31. #include "../ssfn.h"
  32. #include <SDL.h>
  33. /**
  34. * Load a font
  35. */
  36. ssfn_font_t *load_file(char *filename, int *size)
  37. {
  38. char *fontdata = NULL;
  39. FILE *f;
  40. f = fopen(filename, "rb");
  41. if(!f) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  42. *size = 0;
  43. fseek(f, 0, SEEK_END);
  44. *size = (int)ftell(f);
  45. fseek(f, 0, SEEK_SET);
  46. if(!*size) { fprintf(stderr,"unable to load %s\n", filename); exit(3); }
  47. fontdata = (char*)malloc(*size);
  48. if(!fontdata) { fprintf(stderr,"memory allocation error\n"); exit(2); }
  49. fread(fontdata, *size, 1, f);
  50. fclose(f);
  51. return (ssfn_font_t*)fontdata;
  52. }
  53. /**
  54. * testing the SSFN library (normal renderer)
  55. */
  56. void do_test(SDL_Surface *screen, char *fontfn, int argc)
  57. {
  58. char *s, str0[64];
  59. int i, ret, size;
  60. ssfn_t ctx;
  61. ssfn_font_t *font;
  62. ssfn_buf_t buf;
  63. /* initialize the normal renderer */
  64. memset(&ctx, 0, sizeof(ssfn_t));
  65. buf.ptr = (uint8_t*)screen->pixels;
  66. buf.p = screen->pitch;
  67. buf.w = screen->w;
  68. buf.h = screen->h;
  69. buf.fg = 0xFF202020;
  70. buf.bg = 0;
  71. /* load and select a font */
  72. font = load_file(fontfn ? fontfn : "../fonts/FreeSerif.sfn", &size);
  73. ret = ssfn_load(&ctx, font);
  74. if(ret != SSFN_OK) { fprintf(stderr, "ssfn load error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  75. buf.y = 10;
  76. for(i=8; buf.y < screen->h - 60; i++) {
  77. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), i);
  78. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  79. sprintf(str0,"%d How much can you read this?",i);
  80. s = str0;
  81. buf.x = 5;
  82. while((ret = ssfn_render(&ctx, &buf, s)) > 0) {
  83. s += ret;
  84. if(*s == 'H') buf.x = 64;
  85. }
  86. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  87. buf.y += i;
  88. }
  89. /* meaning of... */
  90. i = 42;
  91. sprintf(str0,"%d How much can you read this?",i);
  92. ret = ssfn_select(&ctx, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR | (argc>2? SSFN_STYLE_NOKERN | SSFN_STYLE_NOCACHE:0), i);
  93. if(ret != SSFN_OK) { fprintf(stderr, "ssfn select error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  94. s = str0;
  95. buf.x = 5;
  96. buf.y += 10;
  97. while((ret = ssfn_render(&ctx, &buf, s)) > 0) {
  98. s += ret;
  99. if(*s == 'H') buf.x = 64;
  100. }
  101. if(ret != SSFN_OK) { fprintf(stderr, "ssfn render error: err=%d %s\n", ret, ssfn_error(ret)); exit(2); }
  102. printf("Memory allocated: %d\n", ssfn_mem(&ctx));
  103. ssfn_free(&ctx);
  104. free(font);
  105. }
  106. /**
  107. * Main procedure
  108. */
  109. int main(int argc __attribute__((unused)), char **argv)
  110. {
  111. SDL_Window *window;
  112. SDL_Surface *screen;
  113. SDL_Event event;
  114. if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS)) {
  115. fprintf(stderr,"SDL error %s\n", SDL_GetError());
  116. return 2;
  117. }
  118. window = SDL_CreateWindow("SSFN scaling test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
  119. screen = SDL_GetWindowSurface(window);
  120. memset(screen->pixels, 0xF8, screen->pitch*screen->h);
  121. do_test(screen, argv[1], argc);
  122. do{ SDL_UpdateWindowSurface(window); SDL_Delay(10); } while(SDL_WaitEvent(&event) && event.type != SDL_QUIT &&
  123. event.type != SDL_MOUSEBUTTONDOWN && event.type != SDL_KEYDOWN);
  124. SDL_DestroyWindow(window);
  125. SDL_Quit();
  126. return 0;
  127. }