25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

graphics.c 6.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "common.h"
  18. #if !defined(__KERNEL__)
  19. # if defined(HAVE_ERRNO_H)
  20. # include <errno.h>
  21. # endif
  22. #endif
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. /** \brief Set the display title.
  28. *
  29. * If libcaca runs in a window, try to change its title. This works with
  30. * the OpenGL, X11 and Win32 drivers.
  31. *
  32. * If an error occurs, -1 is returned and \b errno is set accordingly:
  33. * - \c ENOSYS Display driver does not support setting the window title.
  34. *
  35. * \param dp The libcaca display context.
  36. * \param title The desired display title.
  37. * \return 0 upon success, -1 if an error occurred.
  38. */
  39. int caca_set_display_title(caca_display_t *dp, char const *title)
  40. {
  41. int ret = dp->drv.set_display_title(dp, title);
  42. #if defined(HAVE_ERRNO_H)
  43. if(ret)
  44. errno = ENOSYS;
  45. #endif
  46. return ret;
  47. }
  48. /** \brief Get the display width.
  49. *
  50. * If libcaca runs in a window, get the usable window width. 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, most drivers will assume a
  53. * 6x10 font is being used. Note that the units are not necessarily pixels.
  54. *
  55. * This function never fails.
  56. *
  57. * \param dp The libcaca display context.
  58. * \return The display width.
  59. */
  60. unsigned int caca_get_display_width(caca_display_t *dp)
  61. {
  62. return dp->drv.get_display_width(dp);
  63. }
  64. /** \brief Get the display height.
  65. *
  66. * If libcaca runs in a window, get the usable window height. This value can
  67. * be used for aspect ratio calculation. If libcaca does not run in a window
  68. * or if there is no way to know the font size, assume a 6x10 font is being
  69. * used. Note that the units are not necessarily pixels.
  70. *
  71. * This function never fails.
  72. *
  73. * \param dp The libcaca display context.
  74. * \return The display height.
  75. */
  76. unsigned int caca_get_display_height(caca_display_t *dp)
  77. {
  78. return dp->drv.get_display_height(dp);
  79. }
  80. /** \brief Set the refresh delay.
  81. *
  82. * Set the refresh delay in microseconds. The refresh delay is used by
  83. * caca_refresh_display() to achieve constant framerate. See the
  84. * caca_refresh_display() documentation for more details.
  85. *
  86. * If the argument is zero, constant framerate is disabled. This is the
  87. * default behaviour.
  88. *
  89. * This function never fails.
  90. *
  91. * \param dp The libcaca display context.
  92. * \param usec The refresh delay in microseconds.
  93. * \return This function always returns 0.
  94. */
  95. int caca_set_display_time(caca_display_t *dp, unsigned int usec)
  96. {
  97. dp->delay = usec;
  98. return 0;
  99. }
  100. /** \brief Get the display's average rendering time.
  101. *
  102. * Get the average rendering time, which is the average measured time
  103. * between two caca_refresh_display() calls, in microseconds. If constant
  104. * framerate was activated by calling caca_set_display_time(), the average
  105. * rendering time will be close to the requested delay even if the real
  106. * rendering time was shorter.
  107. *
  108. * This function never fails.
  109. *
  110. * \param dp The libcaca display context.
  111. * \return The render time in microseconds.
  112. */
  113. unsigned int caca_get_display_time(caca_display_t *dp)
  114. {
  115. return dp->rendertime;
  116. }
  117. /** \brief Flush pending changes and redraw the screen.
  118. *
  119. * Flush all graphical operations and print them to the display device.
  120. * Nothing will show on the screen until this function is called.
  121. *
  122. * If caca_set_display_time() was called with a non-zero value,
  123. * caca_refresh_display() will use that value to achieve constant
  124. * framerate: if two consecutive calls to caca_refresh_display() are within
  125. * a time range shorter than the value set with caca_set_display_time(),
  126. * the second call will be delayed before performing the screen refresh.
  127. *
  128. * This function never fails.
  129. *
  130. * \param dp The libcaca display context.
  131. * \return This function always returns 0.
  132. */
  133. int caca_refresh_display(caca_display_t *dp)
  134. {
  135. #if !defined(_DOXYGEN_SKIP_ME)
  136. #define IDLE_USEC 5000
  137. #endif
  138. int ticks = dp->lastticks + _caca_getticks(&dp->timer);
  139. dp->drv.display(dp);
  140. /* Once the display is finished, we can ack resizes */
  141. if(dp->resize.resized)
  142. {
  143. dp->resize.resized = 0;
  144. _caca_handle_resize(dp);
  145. }
  146. /* Wait until dp->delay + time of last call */
  147. ticks += _caca_getticks(&dp->timer);
  148. for(ticks += _caca_getticks(&dp->timer);
  149. ticks + IDLE_USEC < (int)dp->delay;
  150. ticks += _caca_getticks(&dp->timer))
  151. {
  152. _caca_sleep(IDLE_USEC);
  153. }
  154. /* Update the sliding mean of the render time */
  155. dp->rendertime = (7 * dp->rendertime + ticks) / 8;
  156. dp->lastticks = ticks - dp->delay;
  157. /* If we drifted too much, it's bad, bad, bad. */
  158. if(dp->lastticks > (int)dp->delay)
  159. dp->lastticks = 0;
  160. return 0;
  161. }
  162. /** \brief Show or hide the mouse pointer.
  163. *
  164. * Show or hide the mouse pointer, for devices that support such a feature.
  165. *
  166. * If an error occurs, -1 is returned and \b errno is set accordingly:
  167. * - \c ENOSYS Display driver does not support hiding the mouse pointer.
  168. *
  169. * \param dp The libcaca display context.
  170. * \param flag 0 hides the pointer, 1 shows the system's default pointer
  171. * (usually an arrow). Other values are reserved for future use.
  172. * \return 0 upon success, -1 if an error occurred.
  173. */
  174. int caca_set_mouse(caca_display_t *dp, int flag)
  175. {
  176. if(!dp->drv.set_mouse)
  177. {
  178. #if defined(HAVE_ERRNO_H)
  179. errno = ENOSYS;
  180. #endif
  181. return -1;
  182. }
  183. dp->drv.set_mouse(dp, flag);
  184. return 0;
  185. }
  186. /*
  187. * XXX: following functions are local
  188. */
  189. void _caca_handle_resize(caca_display_t *dp)
  190. {
  191. dp->drv.handle_resize(dp);
  192. /* Tell libcucul we changed size */
  193. if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
  194. _cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
  195. }