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

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