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

Last change on this file since 632 was 582, checked in by Sam Hocevar, 15 years ago
  • Use the internal version of cucul_set_size in output drivers so that they can force a resize. We need to find a way to make those specific cucul - caca interactions unavailable to the clueless user.
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
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 582 2006-03-10 10:01:22Z 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#include "caca.h"
23#include "caca_internals.h"
24#include "cucul.h"
25#include "cucul_internals.h"
26
27/*
28 * Local functions
29 */
30void _caca_handle_resize(caca_t *kk);
31
32/** \brief Set the window title.
33 *
34 *  If libcaca runs in a window, try to change its title. This works with
35 *  the X11 and Win32 drivers.
36 *
37 *  \param title The desired window title.
38 *  \return 0 upon success, a non-zero value if an error occurs.
39 */
40int caca_set_window_title(caca_t *kk, char const *title)
41{
42    return kk->drv.set_window_title(kk, title);
43}
44
45/** \brief Get the window width.
46 *
47 *  If libcaca runs in a window, get the usable window width. This value can
48 *  be used for aspect ratio calculation. If libcaca does not run in a window
49 *  or if there is no way to know the font size, most drivers will assume a
50 *  6x10 font is being used. Note that the units are not necessarily pixels.
51 *
52 *  \return The window width.
53 */
54unsigned int caca_get_window_width(caca_t *kk)
55{
56    return kk->drv.get_window_width(kk);
57}
58
59/** \brief Get the window height.
60 *
61 *  If libcaca runs in a window, get the usable window height. This value can
62 *  be used for aspect ratio calculation. If libcaca does not run in a window
63 *  or if there is no way to know the font size, assume a 6x10 font is being
64 *  used. Note that the units are not necessarily pixels.
65 *
66 *  \return The window height.
67 */
68unsigned int caca_get_window_height(caca_t *kk)
69{
70    return kk->drv.get_window_height(kk);
71}
72
73/** \brief Set the refresh delay.
74 *
75 *  This function sets the refresh delay in microseconds. The refresh delay
76 *  is used by caca_display() to achieve constant framerate. See the
77 *  caca_display() documentation for more details.
78 *
79 *  If the argument is zero, constant framerate is disabled. This is the
80 *  default behaviour.
81 *
82 *  \param usec The refresh delay in microseconds.
83 */
84void caca_set_delay(caca_t *kk, unsigned int usec)
85{
86    kk->delay = usec;
87}
88
89/** \brief Get the average rendering time.
90 *
91 *  This function returns the average rendering time, which is the average
92 *  measured time between two caca_display() calls, in microseconds. If
93 *  constant framerate was activated by calling caca_set_delay(), the average
94 *  rendering time will not be considerably shorter than the requested delay
95 *  even if the real rendering time was shorter.
96 *
97 *  \return The render time in microseconds.
98 */
99unsigned int caca_get_rendertime(caca_t *kk)
100{
101    return kk->rendertime;
102}
103
104/** \brief Flush pending changes and redraw the screen.
105 *
106 *  This function flushes all graphical operations and prints them to the
107 *  screen. Nothing will show on the screen until caca_display() is
108 *  called.
109 *
110 *  If caca_set_delay() was called with a non-zero value, caca_display()
111 *  will use that value to achieve constant framerate: if two consecutive
112 *  calls to caca_display() are within a time range shorter than the value
113 *  set with caca_set_delay(), the second call will wait a bit before
114 *  performing the screen refresh.
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/*
152 * XXX: following functions are local
153 */
154
155void _caca_handle_resize(caca_t *kk)
156{
157    kk->drv.handle_resize(kk);
158
159    /* Tell libcucul we changed size */
160    if(kk->resize.w != kk->qq->width || kk->resize.h != kk->qq->height)
161        _cucul_set_size(kk->qq, kk->resize.w, kk->resize.h);
162}
163
Note: See TracBrowser for help on using the repository browser.