sdf-demo.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * sdf-demo - SDF font rasterizer demo
  7. */
  8. #include <stdio.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <unistd.h>
  14. #include <getopt.h>
  15. #include <toaru/yutani.h>
  16. #include <toaru/graphics.h>
  17. #include <toaru/decorations.h>
  18. #include <toaru/sdf.h>
  19. /* Pointer to graphics memory */
  20. static yutani_t * yctx;
  21. static yutani_window_t * window = NULL;
  22. static gfx_context_t * ctx = NULL;
  23. static int width = 500;
  24. static int height = 500;
  25. static int left = 200;
  26. static int top = 200;
  27. static int size = 16;
  28. static void decors() {
  29. render_decorations(window, ctx, "SDF Demo");
  30. }
  31. void redraw() {
  32. draw_fill(ctx, rgb(255,255,255));
  33. decors();
  34. draw_sdf_string(ctx, 30, 30, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_THIN);
  35. draw_sdf_string(ctx, 30, 60, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_THIN);
  36. draw_sdf_string(ctx, 30, 90, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_BOLD);
  37. draw_sdf_string(ctx, 30,120, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_BOLD);
  38. }
  39. void resize_finish(int w, int h) {
  40. yutani_window_resize_accept(yctx, window, w, h);
  41. reinit_graphics_yutani(ctx, window);
  42. struct decor_bounds bounds;
  43. decor_get_bounds(window, &bounds);
  44. width = w - bounds.left_width - bounds.right_width;
  45. height = h - bounds.top_height - bounds.bottom_height;
  46. redraw();
  47. yutani_window_resize_done(yctx, window);
  48. yutani_flip(yctx, window);
  49. }
  50. int main(int argc, char * argv[]) {
  51. yctx = yutani_init();
  52. if (!yctx) {
  53. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  54. return 1;
  55. }
  56. init_decorations();
  57. struct decor_bounds bounds;
  58. decor_get_bounds(NULL, &bounds);
  59. window = yutani_window_create(yctx, width + bounds.width, height + bounds.height);
  60. yutani_window_move(yctx, window, left, top);
  61. yutani_window_advertise_icon(yctx, window, "SDF Demo", "sdf");
  62. ctx = init_graphics_yutani(window);
  63. redraw();
  64. yutani_flip(yctx, window);
  65. int playing = 1;
  66. while (playing) {
  67. yutani_msg_t * m = yutani_poll(yctx);
  68. if (m) {
  69. switch (m->type) {
  70. case YUTANI_MSG_KEY_EVENT:
  71. {
  72. struct yutani_msg_key_event * ke = (void*)m->data;
  73. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  74. playing = 0;
  75. } else if (ke->event.action == KEY_ACTION_DOWN) {
  76. if (size <= 20) {
  77. size += 1;
  78. } else if (size > 20) {
  79. size += 5;
  80. }
  81. if (size > 100) {
  82. size = 1;
  83. }
  84. redraw();
  85. yutani_flip(yctx,window);
  86. }
  87. }
  88. break;
  89. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  90. {
  91. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  92. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  93. if (win) {
  94. win->focused = wf->focused;
  95. decors();
  96. yutani_flip(yctx, window);
  97. }
  98. }
  99. break;
  100. case YUTANI_MSG_RESIZE_OFFER:
  101. {
  102. struct yutani_msg_window_resize * wr = (void*)m->data;
  103. resize_finish(wr->width, wr->height);
  104. }
  105. break;
  106. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  107. {
  108. int result = decor_handle_event(yctx, m);
  109. switch (result) {
  110. case DECOR_CLOSE:
  111. playing = 0;
  112. break;
  113. default:
  114. /* Other actions */
  115. break;
  116. }
  117. }
  118. break;
  119. case YUTANI_MSG_SESSION_END:
  120. playing = 0;
  121. break;
  122. default:
  123. break;
  124. }
  125. }
  126. free(m);
  127. }
  128. yutani_close(yctx, window);
  129. return 0;
  130. }