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

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