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

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