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

Last change on this file since 1231 was 1231, checked in by Sam Hocevar, 14 years ago
  • Removed "This function..." constructs from documentation. Fixed a few documentation errors or imprecisions.
  • Property svn:keywords set to Id
File size: 6.2 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 1231 2006-10-25 22:06:21Z 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 *
[1231]92 *  Set the refresh delay in microseconds. The refresh delay is used by
93 *  caca_refresh_display() to achieve constant framerate. See the
[819]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 *
[1231]113 *  Get the average rendering time, which is the average measured time
114 *  between two caca_refresh_display() calls, in microseconds. If constant
115 *  framerate was activated by calling caca_set_display_time(), the average
116 *  rendering time will be close to the requested delay even if the real
117 *  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 *
[1231]131 *  Flush all graphical operations and print them to the display device.
132 *  Nothing will show on the screen until this function is called.
[268]133 *
[964]134 *  If caca_set_display_time() was called with a non-zero value,
[819]135 *  caca_refresh_display() will use that value to achieve constant
[964]136 *  framerate: if two consecutive calls to caca_refresh_display() are within
137 *  a time range shorter than the value set with caca_set_display_time(),
[819]138 *  the second call will be delayed before performing the screen refresh.
[773]139 *
[1006]140 *  This function never fails.
141 *
[964]142 *  \param dp The libcaca display context.
[1006]143 *  \return This function always returns 0.
[257]144 */
[1006]145int caca_refresh_display(caca_display_t *dp)
[227]146{
[322]147#if !defined(_DOXYGEN_SKIP_ME)
[985]148#define IDLE_USEC 5000
[322]149#endif
[811]150    int ticks = dp->lastticks + _caca_getticks(&dp->timer);
[227]151
[811]152    dp->drv.display(dp);
[261]153
[553]154    /* Once the display is finished, we can ack resizes */
[811]155    if(dp->resize.resized)
[349]156    {
[811]157        dp->resize.resized = 0;
158        _caca_handle_resize(dp);
[349]159    }
[347]160
[811]161    /* Wait until dp->delay + time of last call */
162    ticks += _caca_getticks(&dp->timer);
163    for(ticks += _caca_getticks(&dp->timer);
164        ticks + IDLE_USEC < (int)dp->delay;
165        ticks += _caca_getticks(&dp->timer))
[331]166    {
[335]167        _caca_sleep(IDLE_USEC);
[331]168    }
[227]169
170    /* Update the sliding mean of the render time */
[811]171    dp->rendertime = (7 * dp->rendertime + ticks) / 8;
[227]172
[811]173    dp->lastticks = ticks - dp->delay;
[227]174
175    /* If we drifted too much, it's bad, bad, bad. */
[811]176    if(dp->lastticks > (int)dp->delay)
177        dp->lastticks = 0;
[1006]178
179    return 0;
[227]180}
181
[689]182/** \brief Show or hide the mouse pointer.
[686]183 *
[1231]184 *  Show or hide the mouse pointer, for devices that support such a feature.
[686]185 *
[1006]186 *  If an error occurs, -1 is returned and \b errno is set accordingly:
187 *  - \c ENOSYS Display driver does not support hiding the mouse pointer.
188 *
[964]189 *  \param dp The libcaca display context.
[689]190 *  \param flag 0 hides the pointer, 1 shows the system's default pointer
191 *              (usually an arrow). Other values are reserved for future use.
[1006]192 *  \return 0 upon success, -1 if an error occurred.
[686]193 */
[1006]194int caca_set_mouse(caca_display_t *dp, int flag)
[686]195{
[1006]196    if(!dp->drv.set_mouse)
197    {
198#if defined(HAVE_ERRNO_H)
199        errno = ENOSYS;
200#endif
201        return -1;
202    }
203
204    dp->drv.set_mouse(dp, flag);
205    return 0;
[686]206}
207
[347]208/*
[511]209 * XXX: following functions are local
[347]210 */
[524]211
[811]212void _caca_handle_resize(caca_display_t *dp)
[347]213{
[811]214    dp->drv.handle_resize(dp);
[347]215
[527]216    /* Tell libcucul we changed size */
[811]217    if(dp->resize.w != dp->cv->width || dp->resize.h != dp->cv->height)
[813]218        _cucul_set_canvas_size(dp->cv, dp->resize.w, dp->resize.h);
[347]219}
220
Note: See TracBrowser for help on using the repository browser.