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

Last change on this file since 688 was 688, checked in by Jean-Yves Lamoureux, 14 years ago
  • Fixed bug in show/hide cursor, implemented X11 cursor
  • Property svn:keywords set to Id
File size: 5.0 KB
RevLine 
[35]1/*
[672]2 *  libcaca       Colour ASCII-Art library
[527]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
[268]4 *                All Rights Reserved
[35]5 *
[268]6 *  This library is free software; you can redistribute it and/or
[522]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.
[35]10 */
[17]11
[268]12/** \file graphics.c
13 *  \version \$Id: graphics.c 688 2006-03-24 16:25:39Z jylam $
14 *  \author Sam Hocevar <sam@zoy.org>
[298]15 *  \brief Character drawing
[205]16 *
[268]17 *  This file contains character and string drawing functions.
[205]18 */
19
[63]20#include "config.h"
21
[185]22#include "caca.h"
23#include "caca_internals.h"
[524]24#include "cucul.h"
25#include "cucul_internals.h"
[17]26
[343]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 */
[524]35int caca_set_window_title(caca_t *kk, char const *title)
[343]36{
[550]37    return kk->drv.set_window_title(kk, title);
[343]38}
39
[352]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
[582]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.
[352]46 *
47 *  \return The window width.
48 */
[524]49unsigned int caca_get_window_width(caca_t *kk)
[352]50{
[550]51    return kk->drv.get_window_width(kk);
[352]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 */
[524]63unsigned int caca_get_window_height(caca_t *kk)
[352]64{
[550]65    return kk->drv.get_window_height(kk);
[352]66}
67
[268]68/** \brief Set the refresh delay.
[257]69 *
[268]70 *  This function sets the refresh delay in microseconds. The refresh delay
[527]71 *  is used by caca_display() to achieve constant framerate. See the
72 *  caca_display() documentation for more details.
[268]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.
[257]78 */
[524]79void caca_set_delay(caca_t *kk, unsigned int usec)
[227]80{
[524]81    kk->delay = usec;
[227]82}
83
[268]84/** \brief Get the average rendering time.
[257]85 *
[268]86 *  This function returns the average rendering time, which is the average
[527]87 *  measured time between two caca_display() calls, in microseconds. If
[268]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.
[257]93 */
[524]94unsigned int caca_get_rendertime(caca_t *kk)
[227]95{
[524]96    return kk->rendertime;
[227]97}
98
[268]99/** \brief Flush pending changes and redraw the screen.
[257]100 *
[268]101 *  This function flushes all graphical operations and prints them to the
[527]102 *  screen. Nothing will show on the screen until caca_display() is
[331]103 *  called.
[268]104 *
[527]105 *  If caca_set_delay() was called with a non-zero value, caca_display()
[268]106 *  will use that value to achieve constant framerate: if two consecutive
[527]107 *  calls to caca_display() are within a time range shorter than the value
[268]108 *  set with caca_set_delay(), the second call will wait a bit before
109 *  performing the screen refresh.
[257]110 */
[527]111void caca_display(caca_t *kk)
[227]112{
[322]113#if !defined(_DOXYGEN_SKIP_ME)
[227]114#define IDLE_USEC 10000
[322]115#endif
[527]116    int ticks = kk->lastticks + _caca_getticks(&kk->timer);
[227]117
[550]118    kk->drv.display(kk);
[261]119
[553]120    /* Once the display is finished, we can ack resizes */
121    if(kk->resize.resized)
[349]122    {
[553]123        kk->resize.resized = 0;
124        _caca_handle_resize(kk);
[349]125    }
[347]126
[524]127    /* Wait until kk->delay + time of last call */
[527]128    ticks += _caca_getticks(&kk->timer);
129    for(ticks += _caca_getticks(&kk->timer);
[524]130        ticks + IDLE_USEC < (int)kk->delay;
[527]131        ticks += _caca_getticks(&kk->timer))
[331]132    {
[335]133        _caca_sleep(IDLE_USEC);
[331]134    }
[227]135
136    /* Update the sliding mean of the render time */
[524]137    kk->rendertime = (7 * kk->rendertime + ticks) / 8;
[227]138
[527]139    kk->lastticks = ticks - kk->delay;
[227]140
141    /* If we drifted too much, it's bad, bad, bad. */
[527]142    if(kk->lastticks > (int)kk->delay)
143        kk->lastticks = 0;
[227]144}
145
[686]146/** \brief Show cursor on capable drivers
147 *
148 *  This function shows the cursor on drivers permitting it.
149 *
150 */
151void 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 */
162void caca_hide_cursor(caca_t *kk)
163{
[688]164    if(kk->drv.hide_cursor)
165        kk->drv.hide_cursor(kk);
[686]166}
167
168
[347]169/*
[511]170 * XXX: following functions are local
[347]171 */
[524]172
[553]173void _caca_handle_resize(caca_t *kk)
[347]174{
[553]175    kk->drv.handle_resize(kk);
[347]176
[527]177    /* Tell libcucul we changed size */
[553]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);
[347]180}
181
Note: See TracBrowser for help on using the repository browser.