You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

164 lines
4.7 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file graphics.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Character drawing
  15. *
  16. * This file contains character and string drawing functions.
  17. */
  18. #include "config.h"
  19. #include "caca.h"
  20. #include "caca_internals.h"
  21. #include "cucul.h"
  22. #include "cucul_internals.h"
  23. /*
  24. * Local functions
  25. */
  26. void _caca_handle_resize(caca_t *kk);
  27. /** \brief Set the window title.
  28. *
  29. * If libcaca runs in a window, try to change its title. This works with
  30. * the X11 and Win32 drivers.
  31. *
  32. * \param title The desired window title.
  33. * \return 0 upon success, a non-zero value if an error occurs.
  34. */
  35. int caca_set_window_title(caca_t *kk, char const *title)
  36. {
  37. return kk->drv.set_window_title(kk, title);
  38. }
  39. /** \brief Get the window width.
  40. *
  41. * If libcaca runs in a window, get the usable window width. This value can
  42. * be used for aspect ratio calculation. If libcaca does not run in a window
  43. * or if there is no way to know the font size, most drivers will assume a
  44. * 6x10 font is being used. Note that the units are not necessarily pixels.
  45. *
  46. * \return The window width.
  47. */
  48. unsigned int caca_get_window_width(caca_t *kk)
  49. {
  50. return kk->drv.get_window_width(kk);
  51. }
  52. /** \brief Get the window height.
  53. *
  54. * If libcaca runs in a window, get the usable window height. This value can
  55. * be used for aspect ratio calculation. If libcaca does not run in a window
  56. * or if there is no way to know the font size, assume a 6x10 font is being
  57. * used. Note that the units are not necessarily pixels.
  58. *
  59. * \return The window height.
  60. */
  61. unsigned int caca_get_window_height(caca_t *kk)
  62. {
  63. return kk->drv.get_window_height(kk);
  64. }
  65. /** \brief Set the refresh delay.
  66. *
  67. * This function sets the refresh delay in microseconds. The refresh delay
  68. * is used by caca_display() to achieve constant framerate. See the
  69. * caca_display() documentation for more details.
  70. *
  71. * If the argument is zero, constant framerate is disabled. This is the
  72. * default behaviour.
  73. *
  74. * \param usec The refresh delay in microseconds.
  75. */
  76. void caca_set_delay(caca_t *kk, unsigned int usec)
  77. {
  78. kk->delay = usec;
  79. }
  80. /** \brief Get the average rendering time.
  81. *
  82. * This function returns the average rendering time, which is the average
  83. * measured time between two caca_display() calls, in microseconds. If
  84. * constant framerate was activated by calling caca_set_delay(), the average
  85. * rendering time will not be considerably shorter than the requested delay
  86. * even if the real rendering time was shorter.
  87. *
  88. * \return The render time in microseconds.
  89. */
  90. unsigned int caca_get_rendertime(caca_t *kk)
  91. {
  92. return kk->rendertime;
  93. }
  94. /** \brief Flush pending changes and redraw the screen.
  95. *
  96. * This function flushes all graphical operations and prints them to the
  97. * screen. Nothing will show on the screen until caca_display() is
  98. * called.
  99. *
  100. * If caca_set_delay() was called with a non-zero value, caca_display()
  101. * will use that value to achieve constant framerate: if two consecutive
  102. * calls to caca_display() are within a time range shorter than the value
  103. * set with caca_set_delay(), the second call will wait a bit before
  104. * performing the screen refresh.
  105. */
  106. void caca_display(caca_t *kk)
  107. {
  108. #if !defined(_DOXYGEN_SKIP_ME)
  109. #define IDLE_USEC 10000
  110. #endif
  111. int ticks = kk->lastticks + _caca_getticks(&kk->timer);
  112. kk->drv.display(kk);
  113. /* Once the display is finished, we can ack resizes */
  114. if(kk->resize.resized)
  115. {
  116. kk->resize.resized = 0;
  117. _caca_handle_resize(kk);
  118. }
  119. /* Wait until kk->delay + time of last call */
  120. ticks += _caca_getticks(&kk->timer);
  121. for(ticks += _caca_getticks(&kk->timer);
  122. ticks + IDLE_USEC < (int)kk->delay;
  123. ticks += _caca_getticks(&kk->timer))
  124. {
  125. _caca_sleep(IDLE_USEC);
  126. }
  127. /* Update the sliding mean of the render time */
  128. kk->rendertime = (7 * kk->rendertime + ticks) / 8;
  129. kk->lastticks = ticks - kk->delay;
  130. /* If we drifted too much, it's bad, bad, bad. */
  131. if(kk->lastticks > (int)kk->delay)
  132. kk->lastticks = 0;
  133. }
  134. /*
  135. * XXX: following functions are local
  136. */
  137. void _caca_handle_resize(caca_t *kk)
  138. {
  139. kk->drv.handle_resize(kk);
  140. /* Tell libcucul we changed size */
  141. if(kk->resize.w != kk->qq->width || kk->resize.h != kk->qq->height)
  142. _cucul_set_size(kk->qq, kk->resize.w, kk->resize.h);
  143. }