Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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