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.3 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 display 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 dp The libcaca graphical context.
  27. * \param title The desired display title.
  28. * \return 0 upon success, a non-zero value if an error occurs.
  29. */
  30. int caca_set_display_title(caca_display_t *dp, char const *title)
  31. {
  32. return dp->drv.set_display_title(dp, title);
  33. }
  34. /** \brief Get the display 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 dp The libcaca graphical context.
  42. * \return The display width.
  43. */
  44. unsigned int caca_get_display_width(caca_display_t *dp)
  45. {
  46. return dp->drv.get_display_width(dp);
  47. }
  48. /** \brief Get the display 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 dp The libcaca graphical context.
  56. * \return The display height.
  57. */
  58. unsigned int caca_get_display_height(caca_display_t *dp)
  59. {
  60. return dp->drv.get_display_height(dp);
  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_refresh_display() to achieve constant framerate. See the
  66. * caca_refresh_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 dp The libcaca graphical context.
  72. * \param usec The refresh delay in microseconds.
  73. */
  74. void caca_set_delay(caca_display_t *dp, unsigned int usec)
  75. {
  76. dp->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_refresh_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 dp The libcaca graphical context.
  87. * \return The render time in microseconds.
  88. */
  89. unsigned int caca_get_rendertime(caca_display_t *dp)
  90. {
  91. return dp->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_refresh_display() is
  97. * called.
  98. *
  99. * If caca_set_delay() was called with a non-zero value,
  100. * caca_refresh_display() will use that value to achieve constant
  101. * framerate: if two consecutive calls to caca_refresh_display() are
  102. * within a time range shorter than the value set with caca_set_delay(),
  103. * the second call will be delayed before performing the screen refresh.
  104. *
  105. * \param dp The libcaca graphical context.
  106. */
  107. void caca_refresh_display(caca_display_t *dp)
  108. {
  109. #if !defined(_DOXYGEN_SKIP_ME)
  110. #define IDLE_USEC 10000
  111. #endif
  112. int ticks = dp->lastticks + _caca_getticks(&dp->timer);
  113. dp->drv.display(dp);
  114. /* Once the display is finished, we can ack resizes */
  115. if(dp->resize.resized)
  116. {
  117. dp->resize.resized = 0;
  118. _caca_handle_resize(dp);
  119. }
  120. /* Wait until dp->delay + time of last call */
  121. ticks += _caca_getticks(&dp->timer);
  122. for(ticks += _caca_getticks(&dp->timer);
  123. ticks + IDLE_USEC < (int)dp->delay;
  124. ticks += _caca_getticks(&dp->timer))
  125. {
  126. _caca_sleep(IDLE_USEC);
  127. }
  128. /* Update the sliding mean of the render time */
  129. dp->rendertime = (7 * dp->rendertime + ticks) / 8;
  130. dp->lastticks = ticks - dp->delay;
  131. /* If we drifted too much, it's bad, bad, bad. */
  132. if(dp->lastticks > (int)dp->delay)
  133. dp->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 dp 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_display_t *dp, int flag)
  145. {
  146. if(dp->drv.set_mouse)
  147. dp->drv.set_mouse(dp, flag);
  148. }
  149. /*
  150. * XXX: following functions are local
  151. */
  152. void _caca_handle_resize(caca_display_t *dp)
  153. {
  154. dp->drv.handle_resize(dp);
  155. /* Tell libcucul we changed size */
  156. if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
  157. _cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
  158. }