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

Last change on this file since 1063 was 1006, checked in by Sam Hocevar, 14 years ago
  • Fixed libcaca prototypes so that all functions use the errno mechanism and return a value.
  • Property svn:keywords set to Id
File size: 6.3 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 *
[769]6 *  $Id: graphics.c 1006 2006-08-02 13:12:43Z sam $
7 *
[268]8 *  This library is free software; you can redistribute it and/or
[522]9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[35]12 */
[17]13
[769]14/*
[268]15 *  This file contains character and string drawing functions.
[205]16 */
17
[63]18#include "config.h"
[859]19#include "common.h"
[63]20
[1006]21#if !defined(__KERNEL__)
22#   if defined(HAVE_ERRNO_H)
23#       include <errno.h>
24#   endif
25#endif
26
[185]27#include "caca.h"
28#include "caca_internals.h"
[524]29#include "cucul.h"
30#include "cucul_internals.h"
[17]31
[819]32/** \brief Set the display title.
[343]33 *
34 *  If libcaca runs in a window, try to change its title. This works with
[1006]35 *  the OpenGL, X11 and Win32 drivers.
[343]36 *
[1006]37 *  If an error occurs, -1 is returned and \b errno is set accordingly:
38 *  - \c ENOSYS Display driver does not support setting the window title.
39 *
[964]40 *  \param dp The libcaca display context.
[819]41 *  \param title The desired display title.
[1006]42 *  \return 0 upon success, -1 if an error occurred.
[343]43 */
[819]44int caca_set_display_title(caca_display_t *dp, char const *title)
[343]45{
[1006]46    int ret = dp->drv.set_display_title(dp, title);
47
48#if defined(HAVE_ERRNO_H)
49    if(ret)
50        errno = ENOSYS;
51#endif
52
53    return ret;
[343]54}
55
[819]56/** \brief Get the display width.
[352]57 *
58 *  If libcaca runs in a window, get the usable window width. This value can
59 *  be used for aspect ratio calculation. If libcaca does not run in a window
[582]60 *  or if there is no way to know the font size, most drivers will assume a
61 *  6x10 font is being used. Note that the units are not necessarily pixels.
[352]62 *
[1006]63 *  This function never fails.
64 *
[964]65 *  \param dp The libcaca display context.
[819]66 *  \return The display width.
[352]67 */
[819]68unsigned int caca_get_display_width(caca_display_t *dp)
[352]69{
[819]70    return dp->drv.get_display_width(dp);
[352]71}
72
[819]73/** \brief Get the display height.
[352]74 *
75 *  If libcaca runs in a window, get the usable window height. This value can
76 *  be used for aspect ratio calculation. If libcaca does not run in a window
77 *  or if there is no way to know the font size, assume a 6x10 font is being
78 *  used. Note that the units are not necessarily pixels.
79 *
[1006]80 *  This function never fails.
81 *
[964]82 *  \param dp The libcaca display context.
[819]83 *  \return The display height.
[352]84 */
[819]85unsigned int caca_get_display_height(caca_display_t *dp)
[352]86{
[819]87    return dp->drv.get_display_height(dp);
[352]88}
89
[268]90/** \brief Set the refresh delay.
[257]91 *
[268]92 *  This function sets the refresh delay in microseconds. The refresh delay
[819]93 *  is used by caca_refresh_display() to achieve constant framerate. See the
94 *  caca_refresh_display() documentation for more details.
[268]95 *
96 *  If the argument is zero, constant framerate is disabled. This is the
97 *  default behaviour.
98 *
[1006]99 *  This function never fails.
100 *
[964]101 *  \param dp The libcaca display context.
[268]102 *  \param usec The refresh delay in microseconds.
[1006]103 *  \return This function always returns 0.
[257]104 */
[1006]105int caca_set_display_time(caca_display_t *dp, unsigned int usec)
[227]106{
[811]107    dp->delay = usec;
[1006]108    return 0;
[227]109}
110
[964]111/** \brief Get the display's average rendering time.
[257]112 *
[268]113 *  This function returns the average rendering time, which is the average
[819]114 *  measured time between two caca_refresh_display() calls, in microseconds. If
[964]115 *  constant framerate was activated by calling caca_set_display_time(), the
116 *  average rendering time will not be considerably shorter than the requested
117 *  delay even if the real rendering time was shorter.
[268]118 *
[1006]119 *  This function never fails.
120 *
[964]121 *  \param dp The libcaca display context.
[268]122 *  \return The render time in microseconds.
[257]123 */
[964]124unsigned int caca_get_display_time(caca_display_t *dp)
[227]125{
[811]126    return dp->rendertime;
[227]127}
128
[268]129/** \brief Flush pending changes and redraw the screen.
[257]130 *
[268]131 *  This function flushes all graphical operations and prints them to the
[819]132 *  screen. Nothing will show on the screen until caca_refresh_display() is
[331]133 *  called.
[268]134 *
[964]135 *  If caca_set_display_time() was called with a non-zero value,
[819]136 *  caca_refresh_display() will use that value to achieve constant
[964]137 *  framerate: if two consecutive calls to caca_refresh_display() are within
138 *  a time range shorter than the value set with caca_set_display_time(),
[819]139 *  the second call will be delayed before performing the screen refresh.
[773]140 *
[1006]141 *  This function never fails.
142 *
[964]143 *  \param dp The libcaca display context.
[1006]144 *  \return This function always returns 0.
[257]145 */
[1006]146int caca_refresh_display(caca_display_t *dp)
[227]147{
[322]148#if !defined(_DOXYGEN_SKIP_ME)
[985]149#define IDLE_USEC 5000
[322]150#endif
[811]151    int ticks = dp->lastticks + _caca_getticks(&dp->timer);
[227]152
[811]153    dp->drv.display(dp);
[261]154
[553]155    /* Once the display is finished, we can ack resizes */
[811]156    if(dp->resize.resized)
[349]157    {
[811]158        dp->resize.resized = 0;
159        _caca_handle_resize(dp);
[349]160    }
[347]161
[811]162    /* Wait until dp->delay + time of last call */
163    ticks += _caca_getticks(&dp->timer);
164    for(ticks += _caca_getticks(&dp->timer);
165        ticks + IDLE_USEC < (int)dp->delay;
166        ticks += _caca_getticks(&dp->timer))
[331]167    {
[335]168        _caca_sleep(IDLE_USEC);
[331]169    }
[227]170
171    /* Update the sliding mean of the render time */
[811]172    dp->rendertime = (7 * dp->rendertime + ticks) / 8;
[227]173
[811]174    dp->lastticks = ticks - dp->delay;
[227]175
176    /* If we drifted too much, it's bad, bad, bad. */
[811]177    if(dp->lastticks > (int)dp->delay)
178        dp->lastticks = 0;
[1006]179
180    return 0;
[227]181}
182
[689]183/** \brief Show or hide the mouse pointer.
[686]184 *
[689]185 *  This function shows or hides the mouse pointer, for devices that
186 *  support it.
[686]187 *
[1006]188 *  If an error occurs, -1 is returned and \b errno is set accordingly:
189 *  - \c ENOSYS Display driver does not support hiding the mouse pointer.
190 *
[964]191 *  \param dp The libcaca display context.
[689]192 *  \param flag 0 hides the pointer, 1 shows the system's default pointer
193 *              (usually an arrow). Other values are reserved for future use.
[1006]194 *  \return 0 upon success, -1 if an error occurred.
[686]195 */
[1006]196int caca_set_mouse(caca_display_t *dp, int flag)
[686]197{
[1006]198    if(!dp->drv.set_mouse)
199    {
200#if defined(HAVE_ERRNO_H)
201        errno = ENOSYS;
202#endif
203        return -1;
204    }
205
206    dp->drv.set_mouse(dp, flag);
207    return 0;
[686]208}
209
[347]210/*
[511]211 * XXX: following functions are local
[347]212 */
[524]213
[811]214void _caca_handle_resize(caca_display_t *dp)
[347]215{
[811]216    dp->drv.handle_resize(dp);
[347]217
[527]218    /* Tell libcucul we changed size */
[811]219    if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
[813]220        _cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
[347]221}
222
Note: See TracBrowser for help on using the repository browser.