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

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