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

180 lines
5.4 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 X11 and Win32 drivers.
  26. *
  27. * \param dp The libcaca graphical context.
  28. * \param title The desired display title.
  29. * \return 0 upon success, a non-zero value if an error occurs.
  30. */
  31. int caca_set_display_title(caca_display_t *dp, char const *title)
  32. {
  33. return dp->drv.set_display_title(dp, title);
  34. }
  35. /** \brief Get the display width.
  36. *
  37. * If libcaca runs in a window, get the usable window width. This value can
  38. * be used for aspect ratio calculation. If libcaca does not run in a window
  39. * or if there is no way to know the font size, most drivers will assume a
  40. * 6x10 font is being used. Note that the units are not necessarily pixels.
  41. *
  42. * \param dp The libcaca graphical context.
  43. * \return The display width.
  44. */
  45. unsigned int caca_get_display_width(caca_display_t *dp)
  46. {
  47. return dp->drv.get_display_width(dp);
  48. }
  49. /** \brief Get the display height.
  50. *
  51. * If libcaca runs in a window, get the usable window height. This value can
  52. * be used for aspect ratio calculation. If libcaca does not run in a window
  53. * or if there is no way to know the font size, assume a 6x10 font is being
  54. * used. Note that the units are not necessarily pixels.
  55. *
  56. * \param dp The libcaca graphical context.
  57. * \return The display height.
  58. */
  59. unsigned int caca_get_display_height(caca_display_t *dp)
  60. {
  61. return dp->drv.get_display_height(dp);
  62. }
  63. /** \brief Set the refresh delay.
  64. *
  65. * This function sets the refresh delay in microseconds. The refresh delay
  66. * is used by caca_refresh_display() to achieve constant framerate. See the
  67. * caca_refresh_display() documentation for more details.
  68. *
  69. * If the argument is zero, constant framerate is disabled. This is the
  70. * default behaviour.
  71. *
  72. * \param dp The libcaca graphical context.
  73. * \param usec The refresh delay in microseconds.
  74. */
  75. void caca_set_delay(caca_display_t *dp, unsigned int usec)
  76. {
  77. dp->delay = usec;
  78. }
  79. /** \brief Get the average rendering time.
  80. *
  81. * This function returns the average rendering time, which is the average
  82. * measured time between two caca_refresh_display() calls, in microseconds. If
  83. * constant framerate was activated by calling caca_set_delay(), the average
  84. * rendering time will not be considerably shorter than the requested delay
  85. * even if the real rendering time was shorter.
  86. *
  87. * \param dp The libcaca graphical context.
  88. * \return The render time in microseconds.
  89. */
  90. unsigned int caca_get_rendertime(caca_display_t *dp)
  91. {
  92. return dp->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_refresh_display() is
  98. * called.
  99. *
  100. * If caca_set_delay() was called with a non-zero value,
  101. * caca_refresh_display() will use that value to achieve constant
  102. * framerate: if two consecutive calls to caca_refresh_display() are
  103. * within a time range shorter than the value set with caca_set_delay(),
  104. * the second call will be delayed before performing the screen refresh.
  105. *
  106. * \param dp The libcaca graphical context.
  107. */
  108. void caca_refresh_display(caca_display_t *dp)
  109. {
  110. #if !defined(_DOXYGEN_SKIP_ME)
  111. #define IDLE_USEC 10000
  112. #endif
  113. int ticks = dp->lastticks + _caca_getticks(&dp->timer);
  114. dp->drv.display(dp);
  115. /* Once the display is finished, we can ack resizes */
  116. if(dp->resize.resized)
  117. {
  118. dp->resize.resized = 0;
  119. _caca_handle_resize(dp);
  120. }
  121. /* Wait until dp->delay + time of last call */
  122. ticks += _caca_getticks(&dp->timer);
  123. for(ticks += _caca_getticks(&dp->timer);
  124. ticks + IDLE_USEC < (int)dp->delay;
  125. ticks += _caca_getticks(&dp->timer))
  126. {
  127. _caca_sleep(IDLE_USEC);
  128. }
  129. /* Update the sliding mean of the render time */
  130. dp->rendertime = (7 * dp->rendertime + ticks) / 8;
  131. dp->lastticks = ticks - dp->delay;
  132. /* If we drifted too much, it's bad, bad, bad. */
  133. if(dp->lastticks > (int)dp->delay)
  134. dp->lastticks = 0;
  135. }
  136. /** \brief Show or hide the mouse pointer.
  137. *
  138. * This function shows or hides the mouse pointer, for devices that
  139. * support it.
  140. *
  141. * \param dp The libcaca graphical context.
  142. * \param flag 0 hides the pointer, 1 shows the system's default pointer
  143. * (usually an arrow). Other values are reserved for future use.
  144. */
  145. void caca_set_mouse(caca_display_t *dp, int flag)
  146. {
  147. if(dp->drv.set_mouse)
  148. dp->drv.set_mouse(dp, flag);
  149. }
  150. /*
  151. * XXX: following functions are local
  152. */
  153. void _caca_handle_resize(caca_display_t *dp)
  154. {
  155. dp->drv.handle_resize(dp);
  156. /* Tell libcucul we changed size */
  157. if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
  158. _cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
  159. }