1 | /* |
---|
2 | * libcaca 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 539 2006-03-06 23:01:59Z sam $ |
---|
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 | #if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME) |
---|
23 | # include <inttypes.h> |
---|
24 | #else |
---|
25 | typedef unsigned int uint32_t; |
---|
26 | typedef unsigned char uint8_t; |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <stdio.h> /* BUFSIZ */ |
---|
30 | #include <string.h> |
---|
31 | #include <stdlib.h> |
---|
32 | #if defined(HAVE_UNISTD_H) |
---|
33 | # include <unistd.h> |
---|
34 | #endif |
---|
35 | #include <stdarg.h> |
---|
36 | |
---|
37 | #include "caca.h" |
---|
38 | #include "caca_internals.h" |
---|
39 | #include "cucul.h" |
---|
40 | #include "cucul_internals.h" |
---|
41 | |
---|
42 | /* |
---|
43 | * Local functions |
---|
44 | */ |
---|
45 | static void caca_handle_resize(caca_t *kk); |
---|
46 | |
---|
47 | #if !defined(_DOXYGEN_SKIP_ME) |
---|
48 | int _caca_init_graphics(caca_t *kk) |
---|
49 | { |
---|
50 | int ret = kk->driver.init_graphics(kk); |
---|
51 | |
---|
52 | if(!ret) |
---|
53 | return ret; |
---|
54 | |
---|
55 | kk->delay = 0; |
---|
56 | kk->rendertime = 0; |
---|
57 | |
---|
58 | return 0; |
---|
59 | } |
---|
60 | |
---|
61 | int _caca_end_graphics(caca_t *kk) |
---|
62 | { |
---|
63 | return kk->driver.end_graphics(kk); |
---|
64 | } |
---|
65 | #endif /* _DOXYGEN_SKIP_ME */ |
---|
66 | |
---|
67 | /** \brief Set the window title. |
---|
68 | * |
---|
69 | * If libcaca runs in a window, try to change its title. This works with |
---|
70 | * the X11 and Win32 drivers. |
---|
71 | * |
---|
72 | * \param title The desired window title. |
---|
73 | * \return 0 upon success, a non-zero value if an error occurs. |
---|
74 | */ |
---|
75 | int caca_set_window_title(caca_t *kk, char const *title) |
---|
76 | { |
---|
77 | return kk->driver.set_window_title(kk, title); |
---|
78 | } |
---|
79 | |
---|
80 | /** \brief Get the window width. |
---|
81 | * |
---|
82 | * If libcaca runs in a window, get the usable window width. This value can |
---|
83 | * be used for aspect ratio calculation. If libcaca does not run in a window |
---|
84 | * or if there is no way to know the font size, assume a 6x10 font is being |
---|
85 | * used. Note that the units are not necessarily pixels. |
---|
86 | * |
---|
87 | * \return The window width. |
---|
88 | */ |
---|
89 | unsigned int caca_get_window_width(caca_t *kk) |
---|
90 | { |
---|
91 | return kk->driver.get_window_width(kk); |
---|
92 | } |
---|
93 | |
---|
94 | /** \brief Get the window height. |
---|
95 | * |
---|
96 | * If libcaca runs in a window, get the usable window height. This value can |
---|
97 | * be used for aspect ratio calculation. If libcaca does not run in a window |
---|
98 | * or if there is no way to know the font size, assume a 6x10 font is being |
---|
99 | * used. Note that the units are not necessarily pixels. |
---|
100 | * |
---|
101 | * \return The window height. |
---|
102 | */ |
---|
103 | unsigned int caca_get_window_height(caca_t *kk) |
---|
104 | { |
---|
105 | return kk->driver.get_window_height(kk); |
---|
106 | } |
---|
107 | |
---|
108 | /** \brief Set the refresh delay. |
---|
109 | * |
---|
110 | * This function sets the refresh delay in microseconds. The refresh delay |
---|
111 | * is used by caca_display() to achieve constant framerate. See the |
---|
112 | * caca_display() documentation for more details. |
---|
113 | * |
---|
114 | * If the argument is zero, constant framerate is disabled. This is the |
---|
115 | * default behaviour. |
---|
116 | * |
---|
117 | * \param usec The refresh delay in microseconds. |
---|
118 | */ |
---|
119 | void caca_set_delay(caca_t *kk, unsigned int usec) |
---|
120 | { |
---|
121 | kk->delay = usec; |
---|
122 | } |
---|
123 | |
---|
124 | /** \brief Get the average rendering time. |
---|
125 | * |
---|
126 | * This function returns the average rendering time, which is the average |
---|
127 | * measured time between two caca_display() calls, in microseconds. If |
---|
128 | * constant framerate was activated by calling caca_set_delay(), the average |
---|
129 | * rendering time will not be considerably shorter than the requested delay |
---|
130 | * even if the real rendering time was shorter. |
---|
131 | * |
---|
132 | * \return The render time in microseconds. |
---|
133 | */ |
---|
134 | unsigned int caca_get_rendertime(caca_t *kk) |
---|
135 | { |
---|
136 | return kk->rendertime; |
---|
137 | } |
---|
138 | |
---|
139 | /** \brief Flush pending changes and redraw the screen. |
---|
140 | * |
---|
141 | * This function flushes all graphical operations and prints them to the |
---|
142 | * screen. Nothing will show on the screen until caca_display() is |
---|
143 | * called. |
---|
144 | * |
---|
145 | * If caca_set_delay() was called with a non-zero value, caca_display() |
---|
146 | * will use that value to achieve constant framerate: if two consecutive |
---|
147 | * calls to caca_display() are within a time range shorter than the value |
---|
148 | * set with caca_set_delay(), the second call will wait a bit before |
---|
149 | * performing the screen refresh. |
---|
150 | */ |
---|
151 | void caca_display(caca_t *kk) |
---|
152 | { |
---|
153 | #if !defined(_DOXYGEN_SKIP_ME) |
---|
154 | #define IDLE_USEC 10000 |
---|
155 | #endif |
---|
156 | int ticks = kk->lastticks + _caca_getticks(&kk->timer); |
---|
157 | |
---|
158 | kk->driver.display(kk); |
---|
159 | |
---|
160 | /* FIXME handle this somewhere else */ |
---|
161 | if(kk->resize) |
---|
162 | { |
---|
163 | kk->resize = 0; |
---|
164 | caca_handle_resize(kk); |
---|
165 | } |
---|
166 | |
---|
167 | /* Wait until kk->delay + time of last call */ |
---|
168 | ticks += _caca_getticks(&kk->timer); |
---|
169 | for(ticks += _caca_getticks(&kk->timer); |
---|
170 | ticks + IDLE_USEC < (int)kk->delay; |
---|
171 | ticks += _caca_getticks(&kk->timer)) |
---|
172 | { |
---|
173 | _caca_sleep(IDLE_USEC); |
---|
174 | } |
---|
175 | |
---|
176 | /* Update the sliding mean of the render time */ |
---|
177 | kk->rendertime = (7 * kk->rendertime + ticks) / 8; |
---|
178 | |
---|
179 | kk->lastticks = ticks - kk->delay; |
---|
180 | |
---|
181 | /* If we drifted too much, it's bad, bad, bad. */ |
---|
182 | if(kk->lastticks > (int)kk->delay) |
---|
183 | kk->lastticks = 0; |
---|
184 | } |
---|
185 | |
---|
186 | /* |
---|
187 | * XXX: following functions are local |
---|
188 | */ |
---|
189 | |
---|
190 | static void caca_handle_resize(caca_t *kk) |
---|
191 | { |
---|
192 | unsigned int new_width, new_height; |
---|
193 | |
---|
194 | new_width = kk->qq->width; |
---|
195 | new_height = kk->qq->height; |
---|
196 | |
---|
197 | kk->driver.handle_resize(kk); |
---|
198 | |
---|
199 | /* Tell libcucul we changed size */ |
---|
200 | if(new_width != kk->qq->width || new_height != kk->qq->height) |
---|
201 | cucul_set_size(kk->qq, new_width, new_height); |
---|
202 | } |
---|
203 | |
---|