source: libcaca/trunk/caca/graphics.c @ 1231

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