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

Last change on this file since 1006 was 1006, checked in by Sam Hocevar, 14 years ago
  • Fixed libcaca prototypes so that all functions use the errno mechanism and return a value.
  • Property svn:keywords set to Id
File size: 6.3 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 1006 2006-08-02 13:12:43Z 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 *  This function sets the refresh delay in microseconds. The refresh delay
93 *  is used by 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 *  This function returns the average rendering time, which is the average
114 *  measured time between two caca_refresh_display() calls, in microseconds. If
115 *  constant framerate was activated by calling caca_set_display_time(), the
116 *  average rendering time will not be considerably shorter than the requested
117 *  delay even if the real 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 *  This function flushes all graphical operations and prints them to the
132 *  screen. Nothing will show on the screen until caca_refresh_display() is
133 *  called.
134 *
135 *  If caca_set_display_time() was called with a non-zero value,
136 *  caca_refresh_display() will use that value to achieve constant
137 *  framerate: if two consecutive calls to caca_refresh_display() are within
138 *  a time range shorter than the value set with caca_set_display_time(),
139 *  the second call will be delayed before performing the screen refresh.
140 *
141 *  This function never fails.
142 *
143 *  \param dp The libcaca display context.
144 *  \return This function always returns 0.
145 */
146int caca_refresh_display(caca_display_t *dp)
147{
148#if !defined(_DOXYGEN_SKIP_ME)
149#define IDLE_USEC 5000
150#endif
151    int ticks = dp->lastticks + _caca_getticks(&dp->timer);
152
153    dp->drv.display(dp);
154
155    /* Once the display is finished, we can ack resizes */
156    if(dp->resize.resized)
157    {
158        dp->resize.resized = 0;
159        _caca_handle_resize(dp);
160    }
161
162    /* Wait until dp->delay + time of last call */
163    ticks += _caca_getticks(&dp->timer);
164    for(ticks += _caca_getticks(&dp->timer);
165        ticks + IDLE_USEC < (int)dp->delay;
166        ticks += _caca_getticks(&dp->timer))
167    {
168        _caca_sleep(IDLE_USEC);
169    }
170
171    /* Update the sliding mean of the render time */
172    dp->rendertime = (7 * dp->rendertime + ticks) / 8;
173
174    dp->lastticks = ticks - dp->delay;
175
176    /* If we drifted too much, it's bad, bad, bad. */
177    if(dp->lastticks > (int)dp->delay)
178        dp->lastticks = 0;
179
180    return 0;
181}
182
183/** \brief Show or hide the mouse pointer.
184 *
185 *  This function shows or hides the mouse pointer, for devices that
186 *  support it.
187 *
188 *  If an error occurs, -1 is returned and \b errno is set accordingly:
189 *  - \c ENOSYS Display driver does not support hiding the mouse pointer.
190 *
191 *  \param dp The libcaca display context.
192 *  \param flag 0 hides the pointer, 1 shows the system's default pointer
193 *              (usually an arrow). Other values are reserved for future use.
194 *  \return 0 upon success, -1 if an error occurred.
195 */
196int caca_set_mouse(caca_display_t *dp, int flag)
197{
198    if(!dp->drv.set_mouse)
199    {
200#if defined(HAVE_ERRNO_H)
201        errno = ENOSYS;
202#endif
203        return -1;
204    }
205
206    dp->drv.set_mouse(dp, flag);
207    return 0;
208}
209
210/*
211 * XXX: following functions are local
212 */
213
214void _caca_handle_resize(caca_display_t *dp)
215{
216    dp->drv.handle_resize(dp);
217
218    /* Tell libcucul we changed size */
219    if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
220        _cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
221}
222
Note: See TracBrowser for help on using the repository browser.