source: libcaca/trunk/libee/graphics.c @ 159

Last change on this file since 159 was 159, checked in by Sam Hocevar, 20 years ago
  • libee/ee.h: + Added ee_color_names[] containing our 16 color names.
  • libee/ee.c: + Extended our color set to the full 16 instead of 10. + Precalculate ncurses attributes.
  • libee/graphics.c: + Clip color value in ee_set_color(). + Clip characters in ee_putchar(). + Partially clip characters in ee_putstr(), overflows aren't checked yet.
  • libee/ee_internals.h: + New file to share extern variables within libee.
  • test/demo.c: + Added a simple demo_color() to output all colors. + Replaced four ee_draw_line() with ee_draw_thin_box(). + Replaced x1, y1, x2 etc. with xa, ya, xb etc. because <math.h> already

defines y1.

+ Randomized colours from 0 to 15 instead of 1 to 10.

  • src/Makefiles.am: + Added -lm to the ttyvaders linking flags because of the intro.
  • README BUGS TODO: + Updated.
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1/*
2 *   libee         ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: graphics.c 159 2003-11-12 21:18:50Z sam $
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "config.h"
24
25#if defined(USE_SLANG)
26#   include <slang.h>
27#elif defined(USE_NCURSES)
28#   include <curses.h>
29#elif defined(USE_CONIO)
30#   include <conio.h>
31#else
32#   error "no graphics library detected"
33#endif
34
35#include <string.h>
36#include <stdlib.h>
37
38#include "ee.h"
39#include "ee_internals.h"
40
41static int ee_color = 0;
42
43void ee_set_color(int color)
44{
45    if(color < 0 || color > 15)
46        return;
47
48    ee_color = color;
49#if defined(USE_SLANG)
50    SLsmg_set_color(color + 1);
51#elif defined(USE_NCURSES)
52    attrset(_ee_attr[color]);
53#elif defined(USE_CONIO)
54    textcolor(color);
55#endif
56}
57
58int ee_get_color(void)
59{
60    return ee_color;
61}
62
63void ee_putchar(int x, int y, char c)
64{
65    if(x < 0 || x >= ee_get_width() || y < 0 || y >= ee_get_height())
66        return;
67
68#if defined(USE_SLANG)
69    SLsmg_gotorc(y, x);
70    SLsmg_write_char(c);
71#elif defined(USE_NCURSES)
72    move(y, x);
73    addch(c);
74#elif defined(USE_CONIO)
75    _ee_screen[2 * (x + y * ee_get_width())] = c;
76    _ee_screen[2 * (x + y * ee_get_width()) + 1] = ee_color;
77//    gotoxy(x + 1, y + 1);
78//    putch(c);
79#endif
80}
81
82void ee_putstr(int x, int y, char *s)
83{
84    int len;
85
86    if(y < 0 || y >= ee_get_height())
87        return;
88
89    len = strlen(s);
90
91    if(x < 0)
92    {
93        len -= -x;
94        if(len < 0)
95            return;
96        s += -x;
97        x = 0;
98    }
99
100#if defined(USE_SLANG)
101    SLsmg_gotorc(y, x);
102    SLsmg_write_string(s);
103#elif defined(USE_NCURSES)
104    move(y, x);
105    addstr(s);
106#elif defined(USE_CONIO)
107    char *buf = _ee_screen + 2 * (x + y * ee_get_width());
108    while(*s)
109    {
110        *buf++ = *s++;
111        *buf++ = ee_color;
112    }
113//    gotoxy(x + 1, y + 1);
114//    cputs(s);
115#endif
116}
117
118void ee_clear(void)
119{
120    /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
121    int x = ee_get_width();
122    int y = ee_get_height();
123    char *empty_line = malloc((x + 1) * sizeof(char));
124
125    memset(empty_line, ' ', x);
126    empty_line[x] = '\0';
127
128    while(y--)
129    {
130        ee_putstr(0, y, empty_line);
131    }
132
133    free(empty_line);
134}
135
Note: See TracBrowser for help on using the repository browser.