sdf-demo.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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*1, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_THIN);
  35. draw_sdf_string(ctx, 30,30*2, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_THIN);
  36. draw_sdf_string(ctx, 30,30*3, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_BOLD);
  37. draw_sdf_string(ctx, 30,30*4, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_BOLD);
  38. draw_sdf_string(ctx, 30,30*5, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_OBLIQUE);
  39. draw_sdf_string(ctx, 30,30*6, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_OBLIQUE);
  40. draw_sdf_string(ctx, 30,30*7, "ABCDEFGHIJKLMNOPQRSTUVWXYZABC", size, rgb(0,0,0), SDF_FONT_BOLD_OBLIQUE);
  41. draw_sdf_string(ctx, 30,30*8, "abcdefghijklmnopqrstuvwxyzabc", size, rgb(0,0,0), SDF_FONT_BOLD_OBLIQUE);
  42. }
  43. void resize_finish(int w, int h) {
  44. yutani_window_resize_accept(yctx, window, w, h);
  45. reinit_graphics_yutani(ctx, window);
  46. struct decor_bounds bounds;
  47. decor_get_bounds(window, &bounds);
  48. width = w - bounds.left_width - bounds.right_width;
  49. height = h - bounds.top_height - bounds.bottom_height;
  50. redraw();
  51. yutani_window_resize_done(yctx, window);
  52. yutani_flip(yctx, window);
  53. }
  54. int main(int argc, char * argv[]) {
  55. yctx = yutani_init();
  56. if (!yctx) {
  57. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  58. return 1;
  59. }
  60. init_decorations();
  61. struct decor_bounds bounds;
  62. decor_get_bounds(NULL, &bounds);
  63. window = yutani_window_create(yctx, width + bounds.width, height + bounds.height);
  64. yutani_window_move(yctx, window, left, top);
  65. yutani_window_advertise_icon(yctx, window, "SDF Demo", "sdf");
  66. ctx = init_graphics_yutani(window);
  67. redraw();
  68. yutani_flip(yctx, window);
  69. int playing = 1;
  70. while (playing) {
  71. yutani_msg_t * m = yutani_poll(yctx);
  72. if (m) {
  73. switch (m->type) {
  74. case YUTANI_MSG_KEY_EVENT:
  75. {
  76. struct yutani_msg_key_event * ke = (void*)m->data;
  77. if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
  78. playing = 0;
  79. } else if (ke->event.action == KEY_ACTION_DOWN) {
  80. if (size <= 20) {
  81. size += 1;
  82. } else if (size > 20) {
  83. size += 5;
  84. }
  85. if (size > 100) {
  86. size = 1;
  87. }
  88. redraw();
  89. yutani_flip(yctx,window);
  90. }
  91. }
  92. break;
  93. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  94. {
  95. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  96. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  97. if (win) {
  98. win->focused = wf->focused;
  99. decors();
  100. yutani_flip(yctx, window);
  101. }
  102. }
  103. break;
  104. case YUTANI_MSG_RESIZE_OFFER:
  105. {
  106. struct yutani_msg_window_resize * wr = (void*)m->data;
  107. resize_finish(wr->width, wr->height);
  108. }
  109. break;
  110. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  111. {
  112. int result = decor_handle_event(yctx, m);
  113. switch (result) {
  114. case DECOR_CLOSE:
  115. playing = 0;
  116. break;
  117. default:
  118. /* Other actions */
  119. break;
  120. }
  121. }
  122. break;
  123. case YUTANI_MSG_SESSION_END:
  124. playing = 0;
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. free(m);
  131. }
  132. yutani_close(yctx, window);
  133. return 0;
  134. }