source: ttyvaders/trunk/libee/graphics.c @ 160

Last change on this file since 160 was 160, checked in by Sam Hocevar, 16 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
RevLine 
[36]1/*
[80]2 *   libee         ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
[36]4 *                 All Rights Reserved
5 *
[76]6 *   $Id: graphics.c 160 2003-11-12 21:18:50Z sam $
[36]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 */
[18]22
[64]23#include "config.h"
24
[158]25#if defined(USE_SLANG)
[148]26#   include <slang.h>
[158]27#elif defined(USE_NCURSES)
[148]28#   include <curses.h>
[158]29#elif defined(USE_CONIO)
[156]30#   include <conio.h>
31#else
32#   error "no graphics library detected"
[148]33#endif
34
35#include <string.h>
[18]36#include <stdlib.h>
37
[80]38#include "ee.h"
[160]39#include "ee_internals.h"
[18]40
[154]41static int ee_color = 0;
42
43void ee_set_color(int color)
[18]44{
[160]45    if(color < 0 || color > 15)
46        return;
47
[154]48    ee_color = color;
[158]49#if defined(USE_SLANG)
[160]50    SLsmg_set_color(color + 1);
[158]51#elif defined(USE_NCURSES)
[160]52    attrset(_ee_attr[color]);
[158]53#elif defined(USE_CONIO)
[160]54    textcolor(color);
[18]55#endif
56}
57
[154]58int ee_get_color(void)
59{
60    return ee_color;
61}
62
[148]63void ee_putchar(int x, int y, char c)
[94]64{
[160]65    if(x < 0 || x >= ee_get_width() || y < 0 || y >= ee_get_height())
66        return;
67
[158]68#if defined(USE_SLANG)
[160]69    SLsmg_gotorc(y, x);
[148]70    SLsmg_write_char(c);
[158]71#elif defined(USE_NCURSES)
[160]72    move(y, x);
[148]73    addch(c);
[158]74#elif defined(USE_CONIO)
[160]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);
[158]78//    putch(c);
[18]79#endif
80}
81
[148]82void ee_putstr(int x, int y, char *s)
[18]83{
[160]84    int len;
85
86    if(y < 0 || y >= ee_get_height())
[158]87        return;
[160]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
[158]100#if defined(USE_SLANG)
[160]101    SLsmg_gotorc(y, x);
[148]102    SLsmg_write_string(s);
[158]103#elif defined(USE_NCURSES)
[160]104    move(y, x);
[148]105    addstr(s);
[158]106#elif defined(USE_CONIO)
[160]107    char *buf = _ee_screen + 2 * (x + y * ee_get_width());
[158]108    while(*s)
109    {
110        *buf++ = *s++;
[160]111        *buf++ = ee_color;
[158]112    }
[160]113//    gotoxy(x + 1, y + 1);
[158]114//    cputs(s);
[18]115#endif
116}
117
[80]118void ee_clear(void)
[64]119{
[156]120    /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
[160]121    int x = ee_get_width();
122    int y = ee_get_height();
[82]123    char *empty_line = malloc((x + 1) * sizeof(char));
124
125    memset(empty_line, ' ', x);
126    empty_line[x] = '\0';
127
[84]128    while(y--)
[76]129    {
[148]130        ee_putstr(0, y, empty_line);
[76]131    }
[82]132
133    free(empty_line);
[64]134}
135
Note: See TracBrowser for help on using the repository browser.