1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This library is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
8 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
10 | */ |
---|
11 | |
---|
12 | /** \file graphics.c |
---|
13 | * \version \$Id: graphics.c 688 2006-03-24 16:25:39Z jylam $ |
---|
14 | * \author Sam Hocevar <sam@zoy.org> |
---|
15 | * \brief Character drawing |
---|
16 | * |
---|
17 | * This file contains character and string drawing functions. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #include "caca.h" |
---|
23 | #include "caca_internals.h" |
---|
24 | #include "cucul.h" |
---|
25 | #include "cucul_internals.h" |
---|
26 | |
---|
27 | /** \brief Set the window title. |
---|
28 | * |
---|
29 | * If libcaca runs in a window, try to change its title. This works with |
---|
30 | * the X11 and Win32 drivers. |
---|
31 | * |
---|
32 | * \param title The desired window title. |
---|
33 | * \return 0 upon success, a non-zero value if an error occurs. |
---|
34 | */ |
---|
35 | int caca_set_window_title(caca_t *kk, char const *title) |
---|
36 | { |
---|
37 | return kk->drv.set_window_title(kk, title); |
---|
38 | } |
---|
39 | |
---|
40 | /** \brief Get the window 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 | * \return The window width. |
---|
48 | */ |
---|
49 | unsigned 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 | * \return The window height. |
---|
62 | */ |
---|
63 | unsigned int caca_get_window_height(caca_t *kk) |
---|
64 | { |
---|
65 | return kk->drv.get_window_height(kk); |
---|
66 | } |
---|
67 | |
---|
68 | /** \brief Set the refresh delay. |
---|
69 | * |
---|
70 | * This function sets the refresh delay in microseconds. The refresh delay |
---|
71 | * is used by caca_display() to achieve constant framerate. See the |
---|
72 | * caca_display() documentation for more details. |
---|
73 | * |
---|
74 | * If the argument is zero, constant framerate is disabled. This is the |
---|
75 | * default behaviour. |
---|
76 | * |
---|
77 | * \param usec The refresh delay in microseconds. |
---|
78 | */ |
---|
79 | void caca_set_delay(caca_t *kk, unsigned int usec) |
---|
80 | { |
---|
81 | kk->delay = usec; |
---|
82 | } |
---|
83 | |
---|
84 | /** \brief Get the average rendering time. |
---|
85 | * |
---|
86 | * This function returns the average rendering time, which is the average |
---|
87 | * measured time between two caca_display() calls, in microseconds. If |
---|
88 | * constant framerate was activated by calling caca_set_delay(), the average |
---|
89 | * rendering time will not be considerably shorter than the requested delay |
---|
90 | * even if the real rendering time was shorter. |
---|
91 | * |
---|
92 | * \return The render time in microseconds. |
---|
93 | */ |
---|
94 | unsigned int caca_get_rendertime(caca_t *kk) |
---|
95 | { |
---|
96 | return kk->rendertime; |
---|
97 | } |
---|
98 | |
---|
99 | /** \brief Flush pending changes and redraw the screen. |
---|
100 | * |
---|
101 | * This function flushes all graphical operations and prints them to the |
---|
102 | * screen. Nothing will show on the screen until caca_display() is |
---|
103 | * called. |
---|
104 | * |
---|
105 | * If caca_set_delay() was called with a non-zero value, caca_display() |
---|
106 | * will use that value to achieve constant framerate: if two consecutive |
---|
107 | * calls to caca_display() are within a time range shorter than the value |
---|
108 | * set with caca_set_delay(), the second call will wait a bit before |
---|
109 | * performing the screen refresh. |
---|
110 | */ |
---|
111 | void caca_display(caca_t *kk) |
---|
112 | { |
---|
113 | #if !defined(_DOXYGEN_SKIP_ME) |
---|
114 | #define IDLE_USEC 10000 |
---|
115 | #endif |
---|
116 | int ticks = kk->lastticks + _caca_getticks(&kk->timer); |
---|
117 | |
---|
118 | kk->drv.display(kk); |
---|
119 | |
---|
120 | /* Once the display is finished, we can ack resizes */ |
---|
121 | if(kk->resize.resized) |
---|
122 | { |
---|
123 | kk->resize.resized = 0; |
---|
124 | _caca_handle_resize(kk); |
---|
125 | } |
---|
126 | |
---|
127 | /* Wait until kk->delay + time of last call */ |
---|
128 | ticks += _caca_getticks(&kk->timer); |
---|
129 | for(ticks += _caca_getticks(&kk->timer); |
---|
130 | ticks + IDLE_USEC < (int)kk->delay; |
---|
131 | ticks += _caca_getticks(&kk->timer)) |
---|
132 | { |
---|
133 | _caca_sleep(IDLE_USEC); |
---|
134 | } |
---|
135 | |
---|
136 | /* Update the sliding mean of the render time */ |
---|
137 | kk->rendertime = (7 * kk->rendertime + ticks) / 8; |
---|
138 | |
---|
139 | kk->lastticks = ticks - kk->delay; |
---|
140 | |
---|
141 | /* If we drifted too much, it's bad, bad, bad. */ |
---|
142 | if(kk->lastticks > (int)kk->delay) |
---|
143 | kk->lastticks = 0; |
---|
144 | } |
---|
145 | |
---|
146 | /** \brief Show cursor on capable drivers |
---|
147 | * |
---|
148 | * This function shows the cursor on drivers permitting it. |
---|
149 | * |
---|
150 | */ |
---|
151 | void caca_show_cursor(caca_t *kk) |
---|
152 | { |
---|
153 | if(kk->drv.show_cursor) |
---|
154 | kk->drv.show_cursor(kk); |
---|
155 | } |
---|
156 | |
---|
157 | /** \brief Hide cursor on capable drivers |
---|
158 | * |
---|
159 | * This function hides the cursor on drivers permitting it. |
---|
160 | * |
---|
161 | */ |
---|
162 | void caca_hide_cursor(caca_t *kk) |
---|
163 | { |
---|
164 | if(kk->drv.hide_cursor) |
---|
165 | kk->drv.hide_cursor(kk); |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | /* |
---|
170 | * XXX: following functions are local |
---|
171 | */ |
---|
172 | |
---|
173 | void _caca_handle_resize(caca_t *kk) |
---|
174 | { |
---|
175 | kk->drv.handle_resize(kk); |
---|
176 | |
---|
177 | /* Tell libcucul we changed size */ |
---|
178 | if(kk->resize.w != kk->qq->width || kk->resize.h != kk->qq->height) |
---|
179 | _cucul_set_size(kk->qq, kk->resize.w, kk->resize.h); |
---|
180 | } |
---|
181 | |
---|