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 819 2006-04-19 10:10:58Z 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 display 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 dp The libcaca graphical context. |
---|
31 | * \param title The desired display title. |
---|
32 | * \return 0 upon success, a non-zero value if an error occurs. |
---|
33 | */ |
---|
34 | int caca_set_display_title(caca_display_t *dp, char const *title) |
---|
35 | { |
---|
36 | return dp->drv.set_display_title(dp, title); |
---|
37 | } |
---|
38 | |
---|
39 | /** \brief Get the display 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 dp The libcaca graphical context. |
---|
47 | * \return The display width. |
---|
48 | */ |
---|
49 | unsigned int caca_get_display_width(caca_display_t *dp) |
---|
50 | { |
---|
51 | return dp->drv.get_display_width(dp); |
---|
52 | } |
---|
53 | |
---|
54 | /** \brief Get the display 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 dp The libcaca graphical context. |
---|
62 | * \return The display height. |
---|
63 | */ |
---|
64 | unsigned int caca_get_display_height(caca_display_t *dp) |
---|
65 | { |
---|
66 | return dp->drv.get_display_height(dp); |
---|
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_refresh_display() to achieve constant framerate. See the |
---|
73 | * caca_refresh_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 dp The libcaca graphical context. |
---|
79 | * \param usec The refresh delay in microseconds. |
---|
80 | */ |
---|
81 | void caca_set_delay(caca_display_t *dp, unsigned int usec) |
---|
82 | { |
---|
83 | dp->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_refresh_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 dp The libcaca graphical context. |
---|
95 | * \return The render time in microseconds. |
---|
96 | */ |
---|
97 | unsigned int caca_get_rendertime(caca_display_t *dp) |
---|
98 | { |
---|
99 | return dp->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_refresh_display() is |
---|
106 | * called. |
---|
107 | * |
---|
108 | * If caca_set_delay() was called with a non-zero value, |
---|
109 | * caca_refresh_display() will use that value to achieve constant |
---|
110 | * framerate: if two consecutive calls to caca_refresh_display() are |
---|
111 | * within a time range shorter than the value set with caca_set_delay(), |
---|
112 | * the second call will be delayed before performing the screen refresh. |
---|
113 | * |
---|
114 | * \param dp The libcaca graphical context. |
---|
115 | */ |
---|
116 | void caca_refresh_display(caca_display_t *dp) |
---|
117 | { |
---|
118 | #if !defined(_DOXYGEN_SKIP_ME) |
---|
119 | #define IDLE_USEC 10000 |
---|
120 | #endif |
---|
121 | int ticks = dp->lastticks + _caca_getticks(&dp->timer); |
---|
122 | |
---|
123 | dp->drv.display(dp); |
---|
124 | |
---|
125 | /* Once the display is finished, we can ack resizes */ |
---|
126 | if(dp->resize.resized) |
---|
127 | { |
---|
128 | dp->resize.resized = 0; |
---|
129 | _caca_handle_resize(dp); |
---|
130 | } |
---|
131 | |
---|
132 | /* Wait until dp->delay + time of last call */ |
---|
133 | ticks += _caca_getticks(&dp->timer); |
---|
134 | for(ticks += _caca_getticks(&dp->timer); |
---|
135 | ticks + IDLE_USEC < (int)dp->delay; |
---|
136 | ticks += _caca_getticks(&dp->timer)) |
---|
137 | { |
---|
138 | _caca_sleep(IDLE_USEC); |
---|
139 | } |
---|
140 | |
---|
141 | /* Update the sliding mean of the render time */ |
---|
142 | dp->rendertime = (7 * dp->rendertime + ticks) / 8; |
---|
143 | |
---|
144 | dp->lastticks = ticks - dp->delay; |
---|
145 | |
---|
146 | /* If we drifted too much, it's bad, bad, bad. */ |
---|
147 | if(dp->lastticks > (int)dp->delay) |
---|
148 | dp->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 dp 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 | */ |
---|
160 | void caca_set_mouse(caca_display_t *dp, int flag) |
---|
161 | { |
---|
162 | if(dp->drv.set_mouse) |
---|
163 | dp->drv.set_mouse(dp, flag); |
---|
164 | } |
---|
165 | |
---|
166 | /* |
---|
167 | * XXX: following functions are local |
---|
168 | */ |
---|
169 | |
---|
170 | void _caca_handle_resize(caca_display_t *dp) |
---|
171 | { |
---|
172 | dp->drv.handle_resize(dp); |
---|
173 | |
---|
174 | /* Tell libcucul we changed size */ |
---|
175 | if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height) |
---|
176 | _cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h); |
---|
177 | } |
---|
178 | |
---|