sdf-demo.c 3.5 KB

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