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.
 
 
 
 
 
 

179 lines
5.2 KiB

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