ssfn.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * src/ssfn.c
  3. * https://gitlab.com/bztsrc/smgui
  4. *
  5. * Copyright (C) 2024 bzt (bztsrc@gitlab), MIT license
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to
  9. * deal in the Software without restriction, including without limitation the
  10. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  11. * sell copies 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 included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
  20. * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  22. * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * @brief State-Mode GUI Scalable Screen Font demo program
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <ui_ssfn.h>
  29. #define UI_IMPLEMENTATION
  30. #include <ui.h>
  31. int main(int argc, char **argv)
  32. {
  33. /* strings are gathered into an array so that you can change the language on-the-fly */
  34. enum { WINDOW_TITLE, HELLO_WORLD };
  35. char *english[] = { "Window title", "Hello World!" };
  36. ui_t ctx; /* the main context */
  37. ui_form_t form[2] = { /* your layout */
  38. { .type = UI_LABEL, .align = UI_CENTER, .x = UI_PERCENT(50), .y = 100, .label = HELLO_WORLD },
  39. { .type = UI_END }
  40. };
  41. /* load font */
  42. ssfn_t fnt = { 0 };
  43. FILE *f;
  44. uint8_t *fntbuf;
  45. int fntsize = 0;
  46. if(argc < 2) { fprintf(stderr, "ssfn <font.sfn>\n"); return 1; }
  47. if((f = fopen(argv[1], "rb"))) {
  48. fseek(f, 0, SEEK_END); fntsize = (int)ftell(f); fseek(f, 0, SEEK_SET);
  49. if(!(fntbuf = malloc(fntsize))) { fclose(f); fprintf(stderr, "unable to allocate memory\n"); return 1; }
  50. fread(fntbuf, 1, fntsize, f); fclose(f);
  51. } else { fprintf(stderr, "unable to load font\n"); return 1; }
  52. /* set up SSFN context */
  53. ssfn_load(&fnt, fntbuf);
  54. ssfn_select(&fnt, SSFN_FAMILY_ANY, NULL, SSFN_STYLE_REGULAR, 16);
  55. /* initialize the UI context, pass it the string array, the window size and icon */
  56. ui_init(&ctx, 2, english, 640, 480, NULL);
  57. /* set SSFN font context in SMGUI */
  58. ui_font(&ctx, &fnt);
  59. /* wait until user closes the window */
  60. while(ui_event(&ctx, form)) { }
  61. /* destroy window, free resources */
  62. ui_free(&ctx);
  63. ssfn_free(&fnt);
  64. free(fntbuf);
  65. return 0;
  66. }