git-svn-id: file:///srv/caca.zoy.org/var/lib/svn/ttyvaders/trunk@191 92316355-f0b4-4df1-b90c-862c8a59935fmaster
@@ -2,7 +2,7 @@ | |||
# Automake targets and declarations for ttyvaders | |||
############################################################################### | |||
SUBDIRS = libcaca test src | |||
SUBDIRS = src | |||
DIST_SUBDIRS = $(SUBDIRS) autotools data debian | |||
EXTRA_DIST = BUGS doc/shapes.txt bootstrap | |||
@@ -60,8 +60,6 @@ CFLAGS="${CFLAGS} -Wall -Wpointer-arith -Wcast-align -Wcast-qual -Wstrict-protot | |||
AC_OUTPUT([ | |||
Makefile | |||
libcaca/Makefile | |||
test/Makefile | |||
src/Makefile | |||
autotools/Makefile | |||
data/Makefile | |||
@@ -1,6 +0,0 @@ | |||
.dirstamp | |||
Makefile | |||
Makefile.in | |||
files | |||
ttyvaders | |||
ttyvaders.substvars |
@@ -1,15 +1,15 @@ | |||
Source: ttyvaders | |||
Section: games | |||
Priority: optional | |||
Maintainer: Samuel Hocevar <sam@zoy.org> | |||
Build-Depends: debhelper (>=2.2.0), slang1-dev | |||
Standards-Version: 3.5.6 | |||
Maintainer: Sam Hocevar (Debian packages) <sam+deb@zoy.org> | |||
Build-Depends: debhelper (>= 2.2.0), slang1-dev | |||
Standards-Version: 3.6.1.0 | |||
Package: ttyvaders | |||
Section: games | |||
Architecture: any | |||
Depends: ${shlibs:Depends} | |||
Description: A vertical shoot 'em up with text-mode graphics. | |||
Description: vertical shoot 'em up with text-mode graphics | |||
TTYvaders is a vertical shoot 'em up for the console or the terminal. | |||
. | |||
It rules. |
@@ -1,20 +0,0 @@ | |||
############################################################################### | |||
# Automake targets and declarations for libcaca | |||
############################################################################### | |||
lib_LIBRARIES = libcaca.a | |||
libcaca_a_SOURCES = \ | |||
caca.c \ | |||
caca.h \ | |||
caca_internals.h \ | |||
graphics.c \ | |||
io.c \ | |||
math.c \ | |||
line.c \ | |||
box.c \ | |||
conic.c \ | |||
triangle.c \ | |||
sprite.c \ | |||
blit.c \ | |||
$(NULL) | |||
@@ -1,102 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#ifdef HAVE_INTTYPES_H | |||
# include <inttypes.h> | |||
#else | |||
typedef unsigned char uint8_t; | |||
#endif | |||
#include <stdlib.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
#include <stdio.h> | |||
void caca_blit(int x1, int y1, int x2, int y2, void *pixels, int w, int h) | |||
{ | |||
char foo[] = { ' ', '.', ':', ';', '=', '$', '%', '@', '#', '8', 'W' }; | |||
int x, y, pitch; | |||
if(x1 > x2) | |||
{ | |||
int tmp = x2; x2 = x1; x1 = tmp; | |||
} | |||
if(y1 > y2) | |||
{ | |||
int tmp = y2; y2 = y1; y1 = tmp; | |||
} | |||
pitch = (3 * w + 3) / 4 * 4; | |||
for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)caca_get_height(); y++) | |||
for(x = x1 > 0 ? x1 : 0; x <= x2 && x <= (int)caca_get_width(); x++) | |||
{ | |||
int fromx = w * (x - x1) / (x2 - x1 + 1); | |||
int fromy = h * (y - y1) / (y2 - y1 + 1); | |||
int r = ((unsigned char *)pixels)[3 * fromx + pitch * fromy]; | |||
int g = ((unsigned char *)pixels)[3 * fromx + 1 + pitch * fromy]; | |||
int b = ((unsigned char *)pixels)[3 * fromx + 2 + pitch * fromy]; | |||
if(r == g && g == b) | |||
{ | |||
caca_set_color(EE_LIGHTGRAY); | |||
} | |||
else | |||
{ | |||
static int foo_colors[6] = {EE_LIGHTRED, EE_YELLOW, EE_LIGHTGREEN, EE_LIGHTCYAN, EE_LIGHTBLUE, EE_LIGHTMAGENTA}; | |||
float min = r, max = r, delta, hue, sat; | |||
if(min > g) min = g; if(max < g) max = g; | |||
if(min > b) min = b; if(max < b) max = b; | |||
delta = max - min; | |||
sat = max / delta; | |||
if(delta > 20) | |||
{ | |||
if( r == max ) | |||
hue = (g - b) / delta; // between yellow & magenta | |||
else if( g == max ) | |||
hue = 2 + (b - r) / delta; // between cyan & yellow | |||
else | |||
hue = 4 + (r - g) / delta; // between magenta & cyan | |||
hue *= 60; // degrees | |||
if( hue < 0 ) | |||
hue += 360; | |||
caca_set_color(foo_colors[(int)(hue + 30) / 60]); | |||
} | |||
else | |||
{ | |||
caca_set_color(EE_LIGHTGRAY); | |||
} | |||
} | |||
caca_putchar(x, y, foo[(r + g + b) / 3 / 25]); | |||
} | |||
} | |||
@@ -1,122 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#include <stdlib.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
void caca_draw_box(int x1, int y1, int x2, int y2, char c) | |||
{ | |||
caca_draw_line(x1, y1, x1, y2, c); | |||
caca_draw_line(x1, y2, x2, y2, c); | |||
caca_draw_line(x2, y2, x2, y1, c); | |||
caca_draw_line(x2, y1, x1, y1, c); | |||
} | |||
void caca_draw_thin_box(int x1, int y1, int x2, int y2) | |||
{ | |||
int x, y, xmax, ymax; | |||
if(x1 > x2) | |||
{ | |||
int tmp = x1; | |||
x1 = x2; x2 = tmp; | |||
} | |||
if(y1 > y2) | |||
{ | |||
int tmp = y1; | |||
y1 = y2; y2 = tmp; | |||
} | |||
xmax = caca_get_width() - 1; | |||
ymax = caca_get_height() - 1; | |||
if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) | |||
return; | |||
/* Draw edges */ | |||
if(y1 >= 0) | |||
for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) | |||
caca_putchar(x, y1, '-'); | |||
if(y2 <= ymax) | |||
for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) | |||
caca_putchar(x, y2, '-'); | |||
if(x1 >= 0) | |||
for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) | |||
caca_putchar(x1, y, '|'); | |||
if(x2 <= xmax) | |||
for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) | |||
caca_putchar(x2, y, '|'); | |||
/* Draw corners */ | |||
if(x1 >= 0 && y1 >= 0) | |||
caca_putchar(x1, y1, ','); | |||
if(x1 >= 0 && y2 <= ymax) | |||
caca_putchar(x1, y2, '`'); | |||
if(x2 <= xmax && y1 >= 0) | |||
caca_putchar(x2, y1, '.'); | |||
if(x2 <= xmax && y2 <= ymax) | |||
caca_putchar(x2, y2, '\''); | |||
} | |||
void caca_fill_box(int x1, int y1, int x2, int y2, char c) | |||
{ | |||
int x, y, xmax, ymax; | |||
if(x1 > x2) | |||
{ | |||
int tmp = x1; | |||
x1 = x2; x2 = tmp; | |||
} | |||
if(y1 > y2) | |||
{ | |||
int tmp = y1; | |||
y1 = y2; y2 = tmp; | |||
} | |||
xmax = caca_get_width() - 1; | |||
ymax = caca_get_height() - 1; | |||
if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) | |||
return; | |||
if(x1 < 0) x1 = 0; | |||
if(y1 < 0) y1 = 0; | |||
if(x2 > xmax) x2 = xmax; | |||
if(y2 > ymax) y2 = ymax; | |||
for(y = y1; y <= y2; y++) | |||
for(x = x1; x <= x2; x++) | |||
caca_putchar(x, y, c); | |||
} | |||
@@ -1,308 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#if defined(USE_SLANG) | |||
# include <slang.h> | |||
#elif defined(USE_NCURSES) | |||
# include <curses.h> | |||
#elif defined(USE_CONIO) | |||
# include <dos.h> | |||
# include <conio.h> | |||
# if defined(SCREENUPDATE_IN_PC_H) | |||
# include <pc.h> | |||
# endif | |||
#else | |||
# error "no graphics library detected" | |||
#endif | |||
#include <stdlib.h> | |||
#include <unistd.h> | |||
#include <string.h> | |||
#include <sys/time.h> | |||
#include <time.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
static unsigned int _caca_delay; | |||
static unsigned int _caca_rendertime; | |||
char *_caca_empty_line; | |||
char *_caca_scratch_line; | |||
#if defined(USE_NCURSES) | |||
int _caca_attr[16]; | |||
#endif | |||
#if defined(USE_CONIO) | |||
static struct text_info ti; | |||
char *_caca_screen; | |||
#endif | |||
int caca_init(void) | |||
{ | |||
#if defined(USE_SLANG) | |||
static char *slang_colors[16] = | |||
{ | |||
"black", | |||
"blue", | |||
"green", | |||
"cyan", | |||
"red", | |||
"magenta", | |||
"brown", | |||
"lightgray", | |||
"gray", | |||
"brightblue", | |||
"brightgreen", | |||
"brightcyan", | |||
"brightred", | |||
"brightmagenta", | |||
"yellow", | |||
"white", | |||
}; | |||
int i; | |||
/* Initialize slang library */ | |||
SLsig_block_signals(); | |||
SLtt_get_terminfo(); | |||
if(SLkp_init() == -1) | |||
{ | |||
SLsig_unblock_signals(); | |||
return -1; | |||
} | |||
SLang_init_tty(-1, 0, 1); | |||
if(SLsmg_init_smg() == -1) | |||
{ | |||
SLsig_unblock_signals(); | |||
return -1; | |||
} | |||
SLsig_unblock_signals(); | |||
SLsmg_cls(); | |||
SLtt_set_cursor_visibility(0); | |||
SLsmg_refresh(); | |||
for(i = 0; i < 16; i++) | |||
SLtt_set_color(i + 1, NULL, slang_colors[i], "black"); | |||
#elif defined(USE_NCURSES) | |||
int i; | |||
initscr(); | |||
keypad(stdscr, TRUE); | |||
nonl(); | |||
cbreak(); | |||
noecho(); | |||
nodelay(stdscr, TRUE); | |||
curs_set(0); | |||
start_color(); | |||
init_pair(1 + EE_BLACK, COLOR_BLACK, COLOR_BLACK); | |||
init_pair(1 + EE_BLUE, COLOR_BLUE, COLOR_BLACK); | |||
init_pair(1 + EE_GREEN, COLOR_GREEN, COLOR_BLACK); | |||
init_pair(1 + EE_CYAN, COLOR_CYAN, COLOR_BLACK); | |||
init_pair(1 + EE_RED, COLOR_RED, COLOR_BLACK); | |||
init_pair(1 + EE_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); | |||
init_pair(1 + EE_BROWN, COLOR_YELLOW, COLOR_BLACK); | |||
init_pair(1 + EE_LIGHTGRAY, COLOR_WHITE, COLOR_BLACK); | |||
init_pair(1 + EE_DARKGRAY, COLOR_BLACK, COLOR_BLACK); | |||
init_pair(1 + EE_LIGHTBLUE, COLOR_BLUE, COLOR_BLACK); | |||
init_pair(1 + EE_LIGHTGREEN, COLOR_GREEN, COLOR_BLACK); | |||
init_pair(1 + EE_LIGHTCYAN, COLOR_CYAN, COLOR_BLACK); | |||
init_pair(1 + EE_LIGHTRED, COLOR_RED, COLOR_BLACK); | |||
init_pair(1 + EE_LIGHTMAGENTA, COLOR_MAGENTA, COLOR_BLACK); | |||
init_pair(1 + EE_YELLOW, COLOR_YELLOW, COLOR_BLACK); | |||
init_pair(1 + EE_WHITE, COLOR_WHITE, COLOR_BLACK); | |||
for(i = 0; i < 8; i++) | |||
{ | |||
_caca_attr[i] = COLOR_PAIR(1 + i); | |||
_caca_attr[i + 8] = A_BOLD | COLOR_PAIR(1 + i); | |||
} | |||
#elif defined(USE_CONIO) | |||
gettextinfo(&ti); | |||
_caca_screen = malloc(2 * ti.screenwidth * ti.screenheight); | |||
if(_caca_screen == NULL) | |||
return -1; | |||
_wscroll = 0; | |||
_setcursortype(_NOCURSOR); | |||
clrscr(); | |||
# if defined(SCREENUPDATE_IN_PC_H) | |||
ScreenRetrieve(_caca_screen); | |||
# else | |||
/* FIXME */ | |||
# endif | |||
#endif | |||
_caca_empty_line = malloc(caca_get_width() + 1); | |||
memset(_caca_empty_line, ' ', caca_get_width()); | |||
_caca_empty_line[caca_get_width()] = '\0'; | |||
_caca_scratch_line = malloc(caca_get_width() + 1); | |||
_caca_delay = 0; | |||
_caca_rendertime = 0; | |||
return 0; | |||
} | |||
unsigned int caca_get_width(void) | |||
{ | |||
#if defined(USE_SLANG) | |||
return SLtt_Screen_Cols; | |||
#elif defined(USE_NCURSES) | |||
return COLS; | |||
#elif defined(USE_CONIO) | |||
return ti.screenwidth; | |||
#endif | |||
} | |||
unsigned int caca_get_height(void) | |||
{ | |||
#if defined(USE_SLANG) | |||
return SLtt_Screen_Rows; | |||
#elif defined(USE_NCURSES) | |||
return LINES; | |||
#else | |||
return ti.screenheight; | |||
#endif | |||
} | |||
void caca_set_delay(unsigned int usec) | |||
{ | |||
_caca_delay = usec; | |||
} | |||
unsigned int caca_get_rendertime(void) | |||
{ | |||
return _caca_rendertime; | |||
} | |||
const char *caca_get_color_name(unsigned int color) | |||
{ | |||
static const char *color_names[16] = | |||
{ | |||
"black", | |||
"blue", | |||
"green", | |||
"cyan", | |||
"red", | |||
"magenta", | |||
"brown", | |||
"light gray", | |||
"dark gray", | |||
"light blue", | |||
"light green", | |||
"light cyan", | |||
"light red", | |||
"light magenta", | |||
"yellow", | |||
"white", | |||
}; | |||
if(color < 0 || color > 15) | |||
return "unknown color"; | |||
return color_names[color]; | |||
} | |||
static unsigned int _caca_getticks(void) | |||
{ | |||
static unsigned int last_sec = 0, last_usec = 0; | |||
struct timeval tv; | |||
unsigned int ticks = 0; | |||
gettimeofday(&tv, NULL); | |||
if(last_sec != 0) | |||
{ | |||
ticks = (tv.tv_sec - last_sec) * 1000000 + (tv.tv_usec - last_usec); | |||
} | |||
last_sec = tv.tv_sec; | |||
last_usec = tv.tv_usec; | |||
return ticks; | |||
} | |||
void caca_refresh(void) | |||
{ | |||
#define IDLE_USEC 10000 | |||
static int lastticks = 0; | |||
int ticks = lastticks + _caca_getticks(); | |||
#if defined(USE_SLANG) | |||
SLsmg_refresh(); | |||
#elif defined(USE_NCURSES) | |||
refresh(); | |||
#elif defined(USE_CONIO) | |||
# if defined(SCREENUPDATE_IN_PC_H) | |||
ScreenUpdate(_caca_screen); | |||
# else | |||
/* FIXME */ | |||
# endif | |||
#endif | |||
/* Wait until _caca_delay + time of last call */ | |||
ticks += _caca_getticks(); | |||
for(; ticks + IDLE_USEC < (int)_caca_delay; ticks += _caca_getticks()) | |||
usleep(IDLE_USEC); | |||
/* Update the sliding mean of the render time */ | |||
_caca_rendertime = (7 * _caca_rendertime + ticks) / 8; | |||
lastticks = ticks - _caca_delay; | |||
/* If we drifted too much, it's bad, bad, bad. */ | |||
if(lastticks > (int)_caca_delay) | |||
lastticks = 0; | |||
} | |||
void caca_end(void) | |||
{ | |||
#if defined(USE_SLANG) | |||
SLtt_set_cursor_visibility(1); | |||
SLang_reset_tty(); | |||
SLsmg_reset_smg(); | |||
#elif defined(USE_NCURSES) | |||
curs_set(1); | |||
endwin(); | |||
#elif defined(USE_CONIO) | |||
_wscroll = 1; | |||
textcolor((enum COLORS)WHITE); | |||
textbackground((enum COLORS)BLACK); | |||
gotoxy(caca_get_width(), caca_get_height()); | |||
cputs("\r\n"); | |||
_setcursortype(_NORMALCURSOR); | |||
#endif | |||
} | |||
@@ -1,116 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#ifndef __EE_H__ | |||
#define __EE_H__ | |||
#ifdef __cplusplus | |||
extern "C" | |||
{ | |||
#endif | |||
/* | |||
* Colors | |||
*/ | |||
enum caca_colors | |||
{ | |||
EE_BLACK = 0, | |||
EE_BLUE = 1, | |||
EE_GREEN = 2, | |||
EE_CYAN = 3, | |||
EE_RED = 4, | |||
EE_MAGENTA = 5, | |||
EE_BROWN = 6, | |||
EE_LIGHTGRAY = 7, | |||
EE_DARKGRAY = 8, | |||
EE_LIGHTBLUE = 9, | |||
EE_LIGHTGREEN = 10, | |||
EE_LIGHTCYAN = 11, | |||
EE_LIGHTRED = 12, | |||
EE_LIGHTMAGENTA = 13, | |||
EE_YELLOW = 14, | |||
EE_WHITE = 15 | |||
}; | |||
/* | |||
* Types | |||
*/ | |||
struct caca_sprite; | |||
/* | |||
* Prototypes | |||
*/ | |||
int caca_init(void); | |||
void caca_set_delay(unsigned int); | |||
unsigned int caca_get_rendertime(void); | |||
unsigned int caca_get_width(void); | |||
unsigned int caca_get_height(void); | |||
const char *caca_get_color_name(unsigned int); | |||
void caca_refresh(void); | |||
void caca_end(void); | |||
char caca_get_key(void); | |||
void caca_set_color(int); | |||
int caca_get_color(void); | |||
void caca_putchar(int, int, char); | |||
void caca_putstr(int, int, const char *); | |||
void caca_printf(int, int, const char *, ...); | |||
void caca_clear(void); | |||
void caca_draw_line(int, int, int, int, char); | |||
void caca_draw_polyline(const int[], const int[], int, char); | |||
void caca_draw_thin_line(int, int, int, int); | |||
void caca_draw_thin_polyline(const int[], const int[], int); | |||
void caca_draw_circle(int, int, int, char); | |||
void caca_draw_ellipse(int, int, int, int, char); | |||
void caca_draw_thin_ellipse(int, int, int, int); | |||
void caca_fill_ellipse(int, int, int, int, char); | |||
void caca_draw_box(int, int, int, int, char); | |||
void caca_draw_thin_box(int, int, int, int); | |||
void caca_fill_box(int, int, int, int, char); | |||
void caca_draw_triangle(int, int, int, int, int, int, char); | |||
void caca_draw_thin_triangle(int, int, int, int, int, int); | |||
void caca_fill_triangle(int, int, int, int, int, int, char); | |||
int caca_rand(int, int); | |||
unsigned int caca_sqrt(unsigned int); | |||
struct caca_sprite * caca_load_sprite(const char *); | |||
int caca_get_sprite_frames(struct caca_sprite *); | |||
int caca_get_sprite_width(struct caca_sprite *, int); | |||
int caca_get_sprite_height(struct caca_sprite *, int); | |||
int caca_get_sprite_dx(struct caca_sprite *, int); | |||
int caca_get_sprite_dy(struct caca_sprite *, int); | |||
void caca_draw_sprite(int, int, struct caca_sprite *, int); | |||
void caca_free_sprite(struct caca_sprite *); | |||
void caca_blit(int, int, int, int, void *, int, int); | |||
#ifdef __cplusplus | |||
} | |||
#endif | |||
#endif /* __EE_H__ */ |
@@ -1,37 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#ifndef __EE_INTERNALS_H__ | |||
#define __EE_INTERNALS_H__ | |||
#if defined(USE_NCURSES) | |||
extern int _caca_attr[]; | |||
#endif | |||
#if defined(USE_CONIO) | |||
extern char *_caca_screen; | |||
#endif | |||
extern char *_caca_empty_line; | |||
extern char *_caca_scratch_line; | |||
#endif /* __EE_INTERNALS_H__ */ |
@@ -1,207 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#ifdef HAVE_INTTYPES_H | |||
# include <inttypes.h> | |||
#else | |||
typedef unsigned char uint8_t; | |||
#endif | |||
#include <stdlib.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
static void ellipsepoints(int, int, int, int, char); | |||
void caca_draw_circle(int x, int y, int r, char c) | |||
{ | |||
int test, dx, dy; | |||
/* Optimized Bresenham. Kick ass. */ | |||
for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++) | |||
{ | |||
ellipsepoints(x, y, dx, dy, c); | |||
ellipsepoints(x, y, dy, dx, c); | |||
test += test > 0 ? dx - dy-- : dx; | |||
} | |||
} | |||
void caca_fill_ellipse(int xo, int yo, int a, int b, char c) | |||
{ | |||
int d2; | |||
int x = 0; | |||
int y = b; | |||
int d1 = b*b - (a*a*b) + (a*a/4); | |||
while(a*a*y - a*a/2 > b*b*(x+1)) | |||
{ | |||
if(d1 < 0) | |||
{ | |||
d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ | |||
} | |||
else | |||
{ | |||
d1 += b*b*(2*x*1) + a*a*(-2*y+2); | |||
caca_draw_line(xo - x, yo - y, xo + x, yo - y, c); | |||
caca_draw_line(xo - x, yo + y, xo + x, yo + y, c); | |||
y--; | |||
} | |||
x++; | |||
} | |||
caca_draw_line(xo - x, yo - y, xo + x, yo - y, c); | |||
caca_draw_line(xo - x, yo + y, xo + x, yo + y, c); | |||
d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; | |||
while(y > 0) | |||
{ | |||
if(d2 < 0) | |||
{ | |||
d2 += b*b*(2*x+2) + a*a*(-2*y+3); | |||
x++; | |||
} | |||
else | |||
{ | |||
d2 += a*a*(-2*y+3); | |||
} | |||
y--; | |||
caca_draw_line(xo - x, yo - y, xo + x, yo - y, c); | |||
caca_draw_line(xo - x, yo + y, xo + x, yo + y, c); | |||
} | |||
} | |||
void caca_draw_ellipse(int xo, int yo, int a, int b, char c) | |||
{ | |||
int d2; | |||
int x = 0; | |||
int y = b; | |||
int d1 = b*b - (a*a*b) + (a*a/4); | |||
ellipsepoints(xo, yo, x, y, c); | |||
while(a*a*y - a*a/2 > b*b*(x+1)) | |||
{ | |||
if(d1 < 0) | |||
{ | |||
d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ | |||
} | |||
else | |||
{ | |||
d1 += b*b*(2*x*1) + a*a*(-2*y+2); | |||
y--; | |||
} | |||
x++; | |||
ellipsepoints(xo, yo, x, y, c); | |||
} | |||
d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; | |||
while(y > 0) | |||
{ | |||
if(d2 < 0) | |||
{ | |||
d2 += b*b*(2*x+2) + a*a*(-2*y+3); | |||
x++; | |||
} | |||
else | |||
{ | |||
d2 += a*a*(-2*y+3); | |||
} | |||
y--; | |||
ellipsepoints(xo, yo, x, y, c); | |||
} | |||
} | |||
void caca_draw_thin_ellipse(int xo, int yo, int a, int b) | |||
{ | |||
/* FIXME: this is not correct */ | |||
int d2; | |||
int x = 0; | |||
int y = b; | |||
int d1 = b*b - (a*a*b) + (a*a/4); | |||
ellipsepoints(xo, yo, x, y, '-'); | |||
while(a*a*y - a*a/2 > b*b*(x+1)) | |||
{ | |||
if(d1 < 0) | |||
{ | |||
d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */ | |||
} | |||
else | |||
{ | |||
d1 += b*b*(2*x*1) + a*a*(-2*y+2); | |||
y--; | |||
} | |||
x++; | |||
ellipsepoints(xo, yo, x, y, '-'); | |||
} | |||
d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b; | |||
while(y > 0) | |||
{ | |||
if(d2 < 0) | |||
{ | |||
d2 += b*b*(2*x+2) + a*a*(-2*y+3); | |||
x++; | |||
} | |||
else | |||
{ | |||
d2 += a*a*(-2*y+3); | |||
} | |||
y--; | |||
ellipsepoints(xo, yo, x, y, '|'); | |||
} | |||
} | |||
static void ellipsepoints(int xo, int yo, int x, int y, char c) | |||
{ | |||
uint8_t b = 0; | |||
if(xo + x >= 0 && xo + x < (int)caca_get_width()) | |||
b |= 0x1; | |||
if(xo - x >= 0 && xo - x < (int)caca_get_width()) | |||
b |= 0x2; | |||
if(yo + y >= 0 && yo + y < (int)caca_get_height()) | |||
b |= 0x4; | |||
if(yo - y >= 0 && yo - y < (int)caca_get_height()) | |||
b |= 0x8; | |||
if((b & (0x1|0x4)) == (0x1|0x4)) | |||
caca_putchar(xo + x, yo + y, c); | |||
if((b & (0x2|0x4)) == (0x2|0x4)) | |||
caca_putchar(xo - x, yo + y, c); | |||
if((b & (0x1|0x8)) == (0x1|0x8)) | |||
caca_putchar(xo + x, yo - y, c); | |||
if((b & (0x2|0x8)) == (0x2|0x8)) | |||
caca_putchar(xo - x, yo - y, c); | |||
} | |||
@@ -1,158 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#if defined(USE_SLANG) | |||
# include <slang.h> | |||
#elif defined(USE_NCURSES) | |||
# include <curses.h> | |||
#elif defined(USE_CONIO) | |||
# include <conio.h> | |||
#else | |||
# error "no graphics library detected" | |||
#endif | |||
#include <string.h> | |||
#include <stdlib.h> | |||
#include <stdarg.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
static int _caca_color = 0; | |||
void caca_set_color(int color) | |||
{ | |||
if(color < 0 || color > 15) | |||
return; | |||
_caca_color = color; | |||
#if defined(USE_SLANG) | |||
SLsmg_set_color(color + 1); | |||
#elif defined(USE_NCURSES) | |||
attrset(_caca_attr[color]); | |||
#elif defined(USE_CONIO) | |||
textcolor(color); | |||
#endif | |||
} | |||
int caca_get_color(void) | |||
{ | |||
return _caca_color; | |||
} | |||
void caca_putchar(int x, int y, char c) | |||
{ | |||
if(x < 0 || x >= (int)caca_get_width() || | |||
y < 0 || y >= (int)caca_get_height()) | |||
return; | |||
#if defined(USE_SLANG) | |||
SLsmg_gotorc(y, x); | |||
SLsmg_write_char(c); | |||
#elif defined(USE_NCURSES) | |||
move(y, x); | |||
addch(c); | |||
#elif defined(USE_CONIO) | |||
_caca_screen[2 * (x + y * caca_get_width())] = c; | |||
_caca_screen[2 * (x + y * caca_get_width()) + 1] = _caca_color; | |||
// gotoxy(x + 1, y + 1); | |||
// putch(c); | |||
#endif | |||
} | |||
void caca_putstr(int x, int y, const char *s) | |||
{ | |||
unsigned int len; | |||
if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width()) | |||
return; | |||
len = strlen(s); | |||
if(x < 0) | |||
{ | |||
len -= -x; | |||
if(len < 0) | |||
return; | |||
s += -x; | |||
x = 0; | |||
} | |||
if(x + len >= caca_get_width()) | |||
{ | |||
memcpy(_caca_scratch_line, s, caca_get_width() - x); | |||
_caca_scratch_line[caca_get_width() - x] = '\0'; | |||
s = _caca_scratch_line; | |||
} | |||
#if defined(USE_SLANG) | |||
SLsmg_gotorc(y, x); | |||
SLsmg_write_string(s); | |||
#elif defined(USE_NCURSES) | |||
move(y, x); | |||
addstr(s); | |||
#elif defined(USE_CONIO) | |||
char *buf = _caca_screen + 2 * (x + y * caca_get_width()); | |||
while(*s) | |||
{ | |||
*buf++ = *s++; | |||
*buf++ = _caca_color; | |||
} | |||
// gotoxy(x + 1, y + 1); | |||
// cputs(s); | |||
#endif | |||
} | |||
void caca_printf(int x, int y, const char *format, ...) | |||
{ | |||
char tmp[BUFSIZ]; | |||
char *buf = tmp; | |||
va_list args; | |||
if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width()) | |||
return; | |||
if(caca_get_width() - x + 1 > BUFSIZ) | |||
buf = malloc(caca_get_width() - x + 1); | |||
va_start(args, format); | |||
vsnprintf(buf, caca_get_width() - x + 1, format, args); | |||
buf[caca_get_width() - x] = '\0'; | |||
va_end(args); | |||
caca_putstr(x, y, buf); | |||
if(buf != tmp) | |||
free(buf); | |||
} | |||
void caca_clear(void) | |||
{ | |||
/* We could use SLsmg_cls() etc., but drawing empty lines is much faster */ | |||
int y = caca_get_height(); | |||
while(y--) | |||
caca_putstr(0, y, _caca_empty_line); | |||
} | |||
@@ -1,52 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#if defined(USE_SLANG) | |||
# include <slang.h> | |||
#elif defined(USE_NCURSES) | |||
# include <curses.h> | |||
#elif defined(USE_CONIO) | |||
# include <conio.h> | |||
#else | |||
# error "no graphics library detected" | |||
#endif | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
char caca_get_key(void) | |||
{ | |||
#if defined(USE_SLANG) | |||
return SLang_input_pending(0) ? SLang_getkey() : 0; | |||
#elif defined(USE_NCURSES) | |||
char key = getch(); | |||
return key != ERR ? key : 0; | |||
#elif defined(USE_CONIO) | |||
return _conio_kbhit() ? getch() : 0; | |||
#endif | |||
} | |||
@@ -1,375 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#ifdef HAVE_INTTYPES_H | |||
# include <inttypes.h> | |||
#else | |||
typedef unsigned char uint8_t; | |||
#endif | |||
#include <stdlib.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
struct line | |||
{ | |||
int x1, y1; | |||
int x2, y2; | |||
char c; | |||
void (*draw) (struct line*); | |||
}; | |||
static void clip_line(struct line*); | |||
static uint8_t clip_bits(int, int); | |||
static void draw_solid_line(struct line*); | |||
static void draw_thin_line(struct line*); | |||
/** | |||
* \brief Draw a line on the screen using the given character. | |||
* | |||
* \param x1 X coordinate of the first point. | |||
* \param y1 Y coordinate of the first point. | |||
* \param x2 X coordinate of the second point. | |||
* \param y2 Y coordinate of the second point. | |||
* \param c Character to draw the line with. | |||
* \return nothing | |||
*/ | |||
void caca_draw_line(int x1, int y1, int x2, int y2, char c) | |||
{ | |||
struct line s; | |||
s.x1 = x1; | |||
s.y1 = y1; | |||
s.x2 = x2; | |||
s.y2 = y2; | |||
s.c = c; | |||
s.draw = draw_solid_line; | |||
clip_line(&s); | |||
} | |||
void caca_draw_polyline(const int x[], const int y[], int n, char c) | |||
{ | |||
int i; | |||
struct line s; | |||
s.c = c; | |||
s.draw = draw_solid_line; | |||
for(i = 0; i < n; i++) | |||
{ | |||
s.x1 = x[i]; | |||
s.y1 = y[i]; | |||
s.x2 = x[i+1]; | |||
s.y2 = y[i+1]; | |||
clip_line(&s); | |||
} | |||
} | |||
/** | |||
* \brief Draw a thin line on the screen, using ASCII art. | |||
* | |||
* \param x1 X coordinate of the first point. | |||
* \param y1 Y coordinate of the first point. | |||
* \param x2 X coordinate of the second point. | |||
* \param y2 Y coordinate of the second point. | |||
* \return nothing | |||
*/ | |||
void caca_draw_thin_line(int x1, int y1, int x2, int y2) | |||
{ | |||
struct line s; | |||
s.x1 = x1; | |||
s.y1 = y1; | |||
s.x2 = x2; | |||
s.y2 = y2; | |||
s.draw = draw_thin_line; | |||
clip_line(&s); | |||
} | |||
void caca_draw_thin_polyline(const int x[], const int y[], int n) | |||
{ | |||
int i; | |||
struct line s; | |||
s.draw = draw_thin_line; | |||
for(i = 0; i < n; i++) | |||
{ | |||
s.x1 = x[i]; | |||
s.y1 = y[i]; | |||
s.x2 = x[i+1]; | |||
s.y2 = y[i+1]; | |||
clip_line(&s); | |||
} | |||
} | |||
/* | |||
* XXX: The following functions are local. | |||
*/ | |||
/** | |||
* \brief Generic Cohen-Sutherland line clipping function. | |||
* | |||
* \param s a line structure | |||
* \return nothing | |||
*/ | |||
static void clip_line(struct line* s) | |||
{ | |||
uint8_t bits1, bits2; | |||
bits1 = clip_bits(s->x1, s->y1); | |||
bits2 = clip_bits(s->x2, s->y2); | |||
if(bits1 & bits2) | |||
return; | |||
if(bits1 == 0) | |||
{ | |||
if(bits2 == 0) | |||
s->draw(s); | |||
else | |||
{ | |||
int tmp; | |||
tmp = s->x1; s->x1 = s->x2; s->x2 = tmp; | |||
tmp = s->y1; s->y1 = s->y2; s->y2 = tmp; | |||
clip_line(s); | |||
} | |||
return; | |||
} | |||
if(bits1 & (1<<0)) | |||
{ | |||
s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1); | |||
s->x1 = 0; | |||
} | |||
else if(bits1 & (1<<1)) | |||
{ | |||
int xmax = caca_get_width() - 1; | |||
s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1); | |||
s->x1 = xmax; | |||
} | |||
else if(bits1 & (1<<2)) | |||
{ | |||
s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1); | |||
s->y1 = 0; | |||
} | |||
else if(bits1 & (1<<3)) | |||
{ | |||
int ymax = caca_get_height() - 1; | |||
s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1); | |||
s->y1 = ymax; | |||
} | |||
clip_line(s); | |||
} | |||
/** | |||
* \brief Helper function for clip_line(). | |||
* | |||
* \param x X coordinate of the point. | |||
* \param y Y coordinate of the point. | |||
* \return b The clipping bits for the given point. | |||
*/ | |||
static uint8_t clip_bits(int x, int y) | |||
{ | |||
uint8_t b = 0; | |||
if(x < 0) | |||
b |= (1<<0); | |||
else if(x >= caca_get_width()) | |||
b |= (1<<1); | |||
if(y < 0) | |||
b |= (1<<2); | |||
else if(y >= caca_get_height()) | |||
b |= (1<<3); | |||
return b; | |||
} | |||
/** | |||
* \brief Solid line drawing function, using Bresenham's mid-point line | |||
* scan-conversion algorithm. | |||
* | |||
* \param s a line structure | |||
* \return nothing | |||
*/ | |||
static void draw_solid_line(struct line* s) | |||
{ | |||
int x1, y1, x2, y2; | |||
int dx, dy; | |||
int xinc, yinc; | |||
x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2; | |||
dx = abs(x2 - x1); | |||
dy = abs(y2 - y1); | |||
xinc = (x1 > x2) ? -1 : 1; | |||
yinc = (y1 > y2) ? -1 : 1; | |||
if(dx >= dy) | |||
{ | |||
int dpr = dy << 1; | |||
int dpru = dpr - (dx << 1); | |||
int delta = dpr - dx; | |||
for(; dx>=0; dx--) | |||
{ | |||
caca_putchar(x1, y1, s->c); | |||
if(delta > 0) | |||
{ | |||
x1 += xinc; | |||
y1 += yinc; | |||
delta += dpru; | |||
} | |||
else | |||
{ | |||
x1 += xinc; | |||
delta += dpr; | |||
} | |||
} | |||
} | |||
else | |||
{ | |||
int dpr = dx << 1; | |||
int dpru = dpr - (dy << 1); | |||
int delta = dpr - dy; | |||
for(; dy >= 0; dy--) | |||
{ | |||
caca_putchar(x1, y1, s->c); | |||
if(delta > 0) | |||
{ | |||
x1 += xinc; | |||
y1 += yinc; | |||
delta += dpru; | |||
} | |||
else | |||
{ | |||
y1 += yinc; | |||
delta += dpr; | |||
} | |||
} | |||
} | |||
} | |||
/** | |||
* \brief Thin line drawing function, using Bresenham's mid-point line | |||
* scan-conversion algorithm and ASCII art graphics. | |||
* | |||
* \param s a line structure | |||
* \return nothing | |||
*/ | |||
static void draw_thin_line(struct line* s) | |||
{ | |||
char *charmapx, *charmapy; | |||
int x1, y1, x2, y2; | |||
int dx, dy; | |||
int yinc; | |||
if(s->x2 >= s->x1) | |||
{ | |||
if(s->y1 > s->y2) | |||
charmapx = ",'"; | |||
else | |||
charmapx = "`."; | |||
x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2; | |||
} | |||
else | |||
{ | |||
if(s->y1 > s->y2) | |||
charmapx = "`."; | |||
else | |||
charmapx = ",'"; | |||
x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2; | |||
} | |||
dx = abs(x2 - x1); | |||
dy = abs(y2 - y1); | |||
if(y1 > y2) | |||
{ | |||
charmapy = ",'"; | |||
yinc = -1; | |||
} | |||
else | |||
{ | |||
yinc = 1; | |||
charmapy = "`."; | |||
} | |||
if(dx >= dy) | |||
{ | |||
int dpr = dy << 1; | |||
int dpru = dpr - (dx << 1); | |||
int delta = dpr - dx; | |||
int prev = 0; | |||
for(; dx>=0; dx--) | |||
{ | |||
if(delta > 0) | |||
{ | |||
caca_putchar(x1, y1, charmapy[1]); | |||
x1++; | |||
y1 += yinc; | |||
delta += dpru; | |||
prev = 1; | |||
} | |||
else | |||
{ | |||
if(prev) | |||
caca_putchar(x1, y1, charmapy[0]); | |||
else | |||
caca_putchar(x1, y1, '-'); | |||
x1++; | |||
delta += dpr; | |||
prev = 0; | |||
} | |||
} | |||
} | |||
else | |||
{ | |||
int dpr = dx << 1; | |||
int dpru = dpr - (dy << 1); | |||
int delta = dpr - dy; | |||
for(; dy >= 0; dy--) | |||
{ | |||
if(delta > 0) | |||
{ | |||
caca_putchar(x1, y1, charmapx[0]); | |||
caca_putchar(x1 + 1, y1, charmapx[1]); | |||
x1++; | |||
y1 += yinc; | |||
delta += dpru; | |||
} | |||
else | |||
{ | |||
caca_putchar(x1, y1, '|'); | |||
y1 += yinc; | |||
delta += dpr; | |||
} | |||
} | |||
} | |||
} | |||
@@ -1,59 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#include <stdlib.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
int caca_rand(int min, int max) | |||
{ | |||
return min + (int)((1.0*(max-min+1)) * rand() / (RAND_MAX+1.0)); | |||
} | |||
unsigned int caca_sqrt(unsigned int a) | |||
{ | |||
if(a == 0) | |||
return 0; | |||
if(a < 1000000000) | |||
{ | |||
unsigned int x = a < 10 ? 1 | |||
: a < 1000 ? 10 | |||
: a < 100000 ? 100 | |||
: a < 10000000 ? 1000 | |||
: 10000; | |||
/* Newton's method. Three iterations would be more than enough. */ | |||
x = (x * x + a) / x / 2; | |||
x = (x * x + a) / x / 2; | |||
x = (x * x + a) / x / 2; | |||
x = (x * x + a) / x / 2; | |||
return x; | |||
} | |||
return 2 * caca_sqrt(a / 4); | |||
} | |||
@@ -1,262 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
struct caca_frame | |||
{ | |||
int w, h; | |||
int dx, dy; | |||
char *chars; | |||
int *color; | |||
}; | |||
struct caca_sprite | |||
{ | |||
int nf; | |||
struct caca_frame *frames; | |||
}; | |||
struct caca_sprite *caca_load_sprite(const char *file) | |||
{ | |||
char buf[BUFSIZ]; | |||
struct caca_sprite *sprite; | |||
FILE *fd; | |||
fd = fopen(file, "r"); | |||
if(fd == NULL) | |||
return NULL; | |||
sprite = malloc(sizeof(struct caca_sprite)); | |||
if(sprite == NULL) | |||
goto sprite_alloc_failed; | |||
sprite->nf = 0; | |||
sprite->frames = NULL; | |||
while(!feof(fd)) | |||
{ | |||
int x, y; | |||
int w = 0, h = 0, dx = 0, dy = 0; | |||
struct caca_frame *frame; | |||
/* Get width and height */ | |||
if(!fgets(buf, BUFSIZ, fd)) | |||
break; | |||
sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy); | |||
if(w <= 0 || h <= 0 || w > BUFSIZ / 2) | |||
break; | |||
if(sprite->nf) | |||
{ | |||
void *tmp = realloc(sprite->frames, | |||
(sprite->nf + 1) * sizeof(struct caca_frame)); | |||
if(tmp == NULL) | |||
goto frame_failed; | |||
sprite->frames = tmp; | |||
sprite->nf++; | |||
} | |||
else | |||
{ | |||
sprite->frames = malloc((sprite->nf + 1) * sizeof(struct caca_frame)); | |||
if(sprite->frames == NULL) | |||
goto sprite_failed; | |||
sprite->nf++; | |||
} | |||
frame = &sprite->frames[sprite->nf - 1]; | |||
frame->w = w; | |||
frame->h = h; | |||
frame->dx = dx; | |||
frame->dy = dy; | |||
frame->chars = malloc(w * h * sizeof(char)); | |||
if(frame->chars == NULL) | |||
{ | |||
sprite->nf--; | |||
goto frame_failed; | |||
} | |||
frame->color = malloc(w * h * sizeof(int)); | |||
if(frame->color == NULL) | |||
{ | |||
free(frame->chars); | |||
sprite->nf--; | |||
goto frame_failed; | |||
} | |||
for(y = 0; y < h; y++) | |||
{ | |||
if(!fgets(buf, BUFSIZ, fd)) | |||
goto frame_failed; | |||
for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++) | |||
frame->chars[w * y + x] = buf[x]; | |||
for(; x < w; x++) | |||
frame->chars[w * y + x] = ' '; | |||
} | |||
for(y = 0; y < h; y++) | |||
{ | |||
if(!fgets(buf, BUFSIZ, fd)) | |||
goto frame_failed; | |||
for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++) | |||
frame->color[w * y + x] = buf[x] - 'a'; | |||
for(; x < w; x++) | |||
frame->color[w * y + x] = ' ' - 'a'; | |||
} | |||
continue; | |||
} | |||
if(sprite->nf == 0) | |||
goto sprite_failed; | |||
fclose(fd); | |||
return sprite; | |||
frame_failed: | |||
while(sprite->nf) | |||
{ | |||
free(sprite->frames[sprite->nf - 1].color); | |||
free(sprite->frames[sprite->nf - 1].chars); | |||
sprite->nf--; | |||
} | |||
sprite_failed: | |||
free(sprite); | |||
sprite_alloc_failed: | |||
fclose(fd); | |||
return NULL; | |||
} | |||
int caca_get_sprite_frames(struct caca_sprite *sprite) | |||
{ | |||
if(sprite == NULL) | |||
return 0; | |||
return sprite->nf; | |||
} | |||
int caca_get_sprite_width(struct caca_sprite *sprite, int f) | |||
{ | |||
if(sprite == NULL) | |||
return 0; | |||
if(f < 0 || f >= sprite->nf) | |||
return 0; | |||
return sprite->frames[f].w; | |||
} | |||
int caca_get_sprite_height(struct caca_sprite *sprite, int f) | |||
{ | |||
if(sprite == NULL) | |||
return 0; | |||
if(f < 0 || f >= sprite->nf) | |||
return 0; | |||
return sprite->frames[f].h; | |||
} | |||
int caca_get_sprite_dx(struct caca_sprite *sprite, int f) | |||
{ | |||
if(sprite == NULL) | |||
return 0; | |||
if(f < 0 || f >= sprite->nf) | |||
return 0; | |||
return sprite->frames[f].dx; | |||
} | |||
int caca_get_sprite_dy(struct caca_sprite *sprite, int f) | |||
{ | |||
if(sprite == NULL) | |||
return 0; | |||
if(f < 0 || f >= sprite->nf) | |||
return 0; | |||
return sprite->frames[f].dy; | |||
} | |||
void caca_draw_sprite(int x, int y, struct caca_sprite *sprite, int f) | |||
{ | |||
int i, j, oldcol; | |||
struct caca_frame *frame; | |||
if(sprite == NULL) | |||
return; | |||
if(f < 0 || f >= sprite->nf) | |||
return; | |||
frame = &sprite->frames[f]; | |||
oldcol = caca_get_color(); | |||
for(j = 0; j < frame->h; j++) | |||
{ | |||
for(i = 0; i < frame->w; i++) | |||
{ | |||
int col = frame->color[frame->w * j + i]; | |||
if(col >= 0) | |||
{ | |||
caca_set_color(col); | |||
caca_putchar(x + i - frame->dx, y + j - frame->dy, | |||
frame->chars[frame->w * j + i]); | |||
} | |||
} | |||
} | |||
caca_set_color(oldcol); | |||
} | |||
void caca_free_sprite(struct caca_sprite *sprite) | |||
{ | |||
int i; | |||
if(sprite == NULL) | |||
return; | |||
for(i = sprite->nf; i--;) | |||
{ | |||
struct caca_frame *frame = &sprite->frames[i]; | |||
free(frame->chars); | |||
free(frame->color); | |||
} | |||
free(sprite->frames); | |||
free(sprite); | |||
} | |||
@@ -1,102 +0,0 @@ | |||
/* | |||
* libcaca ASCII-Art library | |||
* Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#include <stdlib.h> | |||
#include "caca.h" | |||
#include "caca_internals.h" | |||
void caca_draw_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) | |||
{ | |||
caca_draw_line(x1, y1, x2, y2, c); | |||
caca_draw_line(x2, y2, x3, y3, c); | |||
caca_draw_line(x3, y3, x1, y1, c); | |||
} | |||
void caca_draw_thin_triangle(int x1, int y1, int x2, int y2, int x3, int y3) | |||
{ | |||
caca_draw_thin_line(x1, y1, x2, y2); | |||
caca_draw_thin_line(x2, y2, x3, y3); | |||
caca_draw_thin_line(x3, y3, x1, y1); | |||
} | |||
void caca_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c) | |||
{ | |||
int x, y, xa, xb, xmax, ymax; | |||
/* Bubble-sort y1 <= y2 <= y3 */ | |||
if(y1 > y2) | |||
{ | |||
caca_fill_triangle(x2, y2, x1, y1, x3, y3, c); | |||
return; | |||
} | |||
if(y2 > y3) | |||
{ | |||
caca_fill_triangle(x1, y1, x3, y3, x2, y2, c); | |||
return; | |||
} | |||
/* Promote precision */ | |||
x1 *= 4; | |||
x2 *= 4; | |||
x3 *= 4; | |||
xmax = caca_get_width() - 1; | |||
ymax = caca_get_height() - 1; | |||
/* Rasterize our triangle */ | |||
for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++) | |||
{ | |||
if(y <= y2) | |||
{ | |||
xa = (y1 == y2) ? x2 : x1 + (x2 - x1) * (y - y1) / (y2 - y1); | |||
xb = (y1 == y3) ? x3 : x1 + (x3 - x1) * (y - y1) / (y3 - y1); | |||
} | |||
else | |||
{ | |||
xa = (y3 == y2) ? x2 : x3 + (x2 - x3) * (y - y3) / (y2 - y3); | |||
xb = (y3 == y1) ? x1 : x3 + (x1 - x3) * (y - y3) / (y1 - y3); | |||
} | |||
if(xb < xa) | |||
{ | |||
int tmp = xb; | |||
xb = xa; xa = tmp; | |||
} | |||
/* Rescale xa and xb, slightly cropping */ | |||
xa = (xa + 2) / 4; | |||
xb = (xb - 2) / 4; | |||
if(xb < 0) continue; | |||
if(xa > xmax) continue; | |||
if(xa < 0) xa = 0; | |||
if(xb > xmax) xb = xmax; | |||
for(x = xa; x <= xb; x++) | |||
caca_putchar(x, y, c); | |||
} | |||
} | |||
@@ -2,7 +2,7 @@ | |||
# Automake targets and declarations for ttyvaders | |||
############################################################################### | |||
AM_CPPFLAGS = -I$(top_srcdir)/libcaca | |||
AM_CPPFLAGS = -I$(top_srcdir)/../libcaca/src | |||
if USE_SLANG | |||
LDFLAGS_slang = -lslang | |||
@@ -29,5 +29,5 @@ ttyvaders_SOURCES = \ | |||
tunnel.c \ | |||
weapons.c \ | |||
$(NULL) | |||
ttyvaders_LDADD = ../libcaca/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm | |||
ttyvaders_LDADD = ../../libcaca/src/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm | |||
@@ -1,23 +0,0 @@ | |||
############################################################################### | |||
# Automake targets and declarations for libcaca tests | |||
############################################################################### | |||
AM_CPPFLAGS = -I$(top_srcdir)/libcaca | |||
if USE_SLANG | |||
LDFLAGS_slang = -lslang | |||
endif | |||
if USE_NCURSES | |||
LDFLAGS_ncurses = -lncurses | |||
endif | |||
bin_PROGRAMS = demo spritedit | |||
demo_SOURCES = demo.c | |||
demo_LDADD = ../libcaca/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm | |||
demo_CFLAGS = `pkg-config --cflags gtk+-2.0` | |||
demo_LDFLAGS = `pkg-config --libs gtk+-2.0` | |||
spritedit_SOURCES = spritedit.c | |||
spritedit_LDADD = ../libcaca/libcaca.a $(LDFLAGS_slang) $(LDFLAGS_ncurses) -lm | |||
@@ -1,462 +0,0 @@ | |||
/* | |||
* demo demo using libcaca | |||
* Copyright (c) 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#include <math.h> | |||
#include <string.h> | |||
#include <stdio.h> | |||
#include <gdk/gdk.h> | |||
#include <gdk/gdkpixbuf.h> | |||
#include "caca.h" | |||
static void display_menu(void); | |||
static void demo_all(void); | |||
static void demo_color(void); | |||
static void demo_dots(void); | |||
static void demo_lines(void); | |||
static void demo_boxes(void); | |||
static void demo_ellipses(void); | |||
static void demo_triangles(void); | |||
static void demo_sprites(void); | |||
static void demo_blit(void); | |||
int bounds = 0; | |||
int outline = 0; | |||
struct caca_sprite *sprite = NULL; | |||
GdkPixbuf *pixbuf; | |||
char *pixels; | |||
int bufx, bufy, bufpitch; | |||
int main(int argc, char **argv) | |||
{ | |||
void (*demo)(void) = NULL; | |||
int quit = 0; | |||
if(caca_init()) | |||
{ | |||
return 1; | |||
} | |||
caca_set_delay(40000); | |||
/* Initialize data */ | |||
sprite = caca_load_sprite("data/barboss.txt"); | |||
gdk_init (&argc, &argv); | |||
//pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gally4.jpeg", NULL); | |||
//pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/badge1.jpeg", NULL); | |||
//pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/union.png", NULL); | |||
pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/pikachu.jpeg", NULL); | |||
if(!pixbuf) return -2; | |||
pixels = gdk_pixbuf_get_pixels(pixbuf); | |||
bufx = gdk_pixbuf_get_width(pixbuf); | |||
bufy = gdk_pixbuf_get_height(pixbuf); | |||
bufpitch = gdk_pixbuf_get_rowstride(pixbuf); | |||
fprintf(stderr, "bits: %i\n", gdk_pixbuf_get_bits_per_sample(pixbuf)); | |||
fprintf(stderr, "w %i, h %i, stride %i\n", bufx, bufy, bufpitch); | |||
/* Main menu */ | |||
display_menu(); | |||
caca_refresh(); | |||
/* Go ! */ | |||
while(!quit) | |||
{ | |||
char key = caca_get_key(); | |||
if(key && demo) | |||
{ | |||
display_menu(); | |||
caca_refresh(); | |||
demo = NULL; | |||
} | |||
else if(key) | |||
{ | |||
handle_key: | |||
switch(key) | |||
{ | |||
case 'q': | |||
case 'Q': | |||
demo = NULL; | |||
quit = 1; | |||
break; | |||
case 'o': | |||
case 'O': | |||
outline = (outline + 1) % 3; | |||
display_menu(); | |||
break; | |||
case 'b': | |||
case 'B': | |||
bounds = (bounds + 1) % 2; | |||
display_menu(); | |||
break; | |||
case 'c': | |||
demo = demo_color; | |||
break; | |||
case 'f': | |||
case 'F': | |||
demo = demo_all; | |||
break; | |||
case '1': | |||
demo = demo_dots; | |||
break; | |||
case '2': | |||
demo = demo_lines; | |||
break; | |||
case '3': | |||
demo = demo_boxes; | |||
break; | |||
case '4': | |||
demo = demo_triangles; | |||
break; | |||
case '5': | |||
demo = demo_ellipses; | |||
break; | |||
case 's': | |||
case 'S': | |||
demo = demo_sprites; | |||
break; | |||
case 'i': | |||
case 'I': | |||
demo = demo_blit; | |||
break; | |||
} | |||
if(demo) | |||
caca_clear(); | |||
key = caca_get_key(); | |||
if(key) | |||
goto handle_key; | |||
caca_refresh(); | |||
} | |||
if(demo) | |||
{ | |||
demo(); | |||
caca_set_color(EE_WHITE); | |||
caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2); | |||
caca_printf(4, 1, "[%i.%i fps]----", | |||
1000000 / caca_get_rendertime(), | |||
(10000000 / caca_get_rendertime()) % 10); | |||
caca_refresh(); | |||
} | |||
} | |||
/* Clean up */ | |||
caca_free_sprite(sprite); | |||
caca_end(); | |||
return 0; | |||
} | |||
static void display_menu(void) | |||
{ | |||
int xo = caca_get_width() - 2; | |||
int yo = caca_get_height() - 2; | |||
caca_clear(); | |||
caca_set_color(EE_WHITE); | |||
caca_draw_thin_box(1, 1, xo, yo); | |||
caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo"); | |||
caca_putstr((xo - strlen("==============")) / 2, 4, "=============="); | |||
caca_putstr(4, 6, "demos:"); | |||
caca_putstr(4, 7, "'f': full"); | |||
caca_putstr(4, 8, "'1': dots"); | |||
caca_putstr(4, 9, "'2': lines"); | |||
caca_putstr(4, 10, "'3': boxes"); | |||
caca_putstr(4, 11, "'4': triangles"); | |||
caca_putstr(4, 12, "'5': ellipses"); | |||
caca_putstr(4, 13, "'s': sprites"); | |||
caca_putstr(4, 14, "'c': color"); | |||
caca_putstr(4, 15, "'i': image blit"); | |||
caca_putstr(4, 17, "settings:"); | |||
caca_printf(4, 18, "'o': outline: %s", | |||
outline == 0 ? "none" : outline == 1 ? "solid" : "thin"); | |||
caca_printf(4, 19, "'b': drawing boundaries: %s", | |||
bounds == 0 ? "screen" : "infinite"); | |||
caca_putstr(4, yo - 2, "'q': quit"); | |||
} | |||
static void demo_all(void) | |||
{ | |||
static int i = 0; | |||
int j, xo, yo, xa, ya, xb, yb, xc, yc; | |||
i++; | |||
caca_clear(); | |||
/* Draw the sun */ | |||
caca_set_color(EE_YELLOW); | |||
xo = caca_get_width() / 4; | |||
yo = caca_get_height() / 4 + 5 * sin(0.03*i); | |||
for(j = 0; j < 16; j++) | |||
{ | |||
xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8); | |||
ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8); | |||
caca_draw_thin_line(xo, yo, xa, ya); | |||
} | |||
j = 15 + sin(0.03*i) * 8; | |||
caca_set_color(EE_WHITE); | |||
caca_fill_ellipse(xo, yo, j, j / 2, '#'); | |||
caca_set_color(EE_YELLOW); | |||
caca_draw_ellipse(xo, yo, j, j / 2, '#'); | |||
/* Draw the pyramid */ | |||
xo = caca_get_width() * 5 / 8; | |||
yo = 2; | |||
xa = caca_get_width() / 8 + sin(0.03*i) * 5; | |||
ya = caca_get_height() / 2 + cos(0.03*i) * 5; | |||
xb = caca_get_width() - 10 - cos(0.02*i) * 10; | |||
yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5; | |||
xc = caca_get_width() / 4 - sin(0.02*i) * 5; | |||
yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5; | |||
caca_set_color(EE_GREEN); | |||
caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%'); | |||
caca_set_color(EE_YELLOW); | |||
caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya); | |||
caca_set_color(EE_RED); | |||
caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#'); | |||
caca_set_color(EE_YELLOW); | |||
caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc); | |||
caca_set_color(EE_BLUE); | |||
caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%'); | |||
caca_set_color(EE_YELLOW); | |||
caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc); | |||
/* Draw a background triangle */ | |||
xa = 2; | |||
ya = 2; | |||
xb = caca_get_width() - 3; | |||
yb = caca_get_height() / 2; | |||
xc = caca_get_width() / 3; | |||
yc = caca_get_height() - 3; | |||
caca_set_color(EE_CYAN); | |||
caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc); | |||
xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3; | |||
yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2; | |||
caca_draw_thin_line(xa, ya, xo, yo); | |||
caca_draw_thin_line(xb, yb, xo, yo); | |||
caca_draw_thin_line(xc, yc, xo, yo); | |||
/* Draw a sprite on the pyramid */ | |||
caca_draw_sprite(xo, yo, sprite, 0); | |||
/* Draw a trail behind the foreground sprite */ | |||
for(j = i - 60; j < i; j++) | |||
{ | |||
int delta = caca_rand(-5, 5); | |||
caca_set_color(caca_rand(0, 15)); | |||
caca_putchar(caca_get_width() / 2 | |||
+ cos(0.02*j) * (delta + caca_get_width() / 4), | |||
caca_get_height() / 2 | |||
+ sin(0.02*j) * (delta + caca_get_height() / 3), | |||
'#'); | |||
} | |||
/* Draw foreground sprite */ | |||
caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4, | |||
caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3, | |||
sprite, 0); | |||
} | |||
static void demo_dots(void) | |||
{ | |||
int xmax = caca_get_width() - 1; | |||
int ymax = caca_get_height() - 1; | |||
int i; | |||
for(i = 1000; i--;) | |||
{ | |||
/* Putpixel */ | |||
caca_set_color(caca_rand(0, 15)); | |||
caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax), '#'); | |||
} | |||
} | |||
static void demo_color(void) | |||
{ | |||
int i; | |||
char buf[BUFSIZ]; | |||
caca_clear(); | |||
for(i = 0; i < 16; i++) | |||
{ | |||
sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i)); | |||
caca_set_color(EE_WHITE); | |||
caca_putstr(4, i + 3, buf); | |||
caca_set_color(i); | |||
caca_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------"); | |||
} | |||
} | |||
static void demo_lines(void) | |||
{ | |||
int w = caca_get_width(); | |||
int h = caca_get_height(); | |||
int xa, ya, xb, yb; | |||
if(bounds) | |||
{ | |||
xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h); | |||
xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h); | |||
} | |||
else | |||
{ | |||
xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1); | |||
xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1); | |||
} | |||
caca_set_color(caca_rand(0, 15)); | |||
if(outline > 1) | |||
caca_draw_thin_line(xa, ya, xb, yb); | |||
else | |||
caca_draw_line(xa, ya, xb, yb, '#'); | |||
} | |||
static void demo_boxes(void) | |||
{ | |||
int w = caca_get_width(); | |||
int h = caca_get_height(); | |||
int xa, ya, xb, yb; | |||
if(bounds) | |||
{ | |||
xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h); | |||
xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h); | |||
} | |||
else | |||
{ | |||
xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1); | |||
xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1); | |||
} | |||
caca_set_color(caca_rand(0, 15)); | |||
caca_fill_box(xa, ya, xb, yb, '#'); | |||
caca_set_color(caca_rand(0, 15)); | |||
if(outline == 2) | |||
caca_draw_thin_box(xa, ya, xb, yb); | |||
else if(outline == 1) | |||
caca_draw_box(xa, ya, xb, yb, '#'); | |||
} | |||
static void demo_ellipses(void) | |||
{ | |||
int w = caca_get_width(); | |||
int h = caca_get_height(); | |||
int x, y, a, b; | |||
if(bounds) | |||
{ | |||
x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h); | |||
a = caca_rand(0, w); b = caca_rand(0, h); | |||
} | |||
else | |||
{ | |||
do | |||
{ | |||
x = caca_rand(0, w); y = caca_rand(0, h); | |||
a = caca_rand(0, w); b = caca_rand(0, h); | |||
} while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h); | |||
} | |||
caca_set_color(caca_rand(0, 15)); | |||
caca_fill_ellipse(x, y, a, b, '#'); | |||
caca_set_color(caca_rand(0, 15)); | |||
if(outline == 2) | |||
caca_draw_thin_ellipse(x, y, a, b); | |||
else if(outline == 1) | |||
caca_draw_ellipse(x, y, a, b, '#'); | |||
} | |||
static void demo_triangles(void) | |||
{ | |||
int w = caca_get_width(); | |||
int h = caca_get_height(); | |||
int xa, ya, xb, yb, xc, yc; | |||
if(bounds) | |||
{ | |||
xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h); | |||
xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h); | |||
xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h); | |||
} | |||
else | |||
{ | |||
xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1); | |||
xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1); | |||
xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1); | |||
} | |||
caca_set_color(caca_rand(0, 15)); | |||
caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#'); | |||
caca_set_color(caca_rand(0, 15)); | |||
if(outline == 2) | |||
caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc); | |||
else if(outline == 1) | |||
caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#'); | |||
} | |||
static void demo_sprites(void) | |||
{ | |||
caca_draw_sprite(caca_rand(0, caca_get_width() - 1), | |||
caca_rand(0, caca_get_height() - 1), sprite, 0); | |||
} | |||
static void demo_blit(void) | |||
{ | |||
caca_set_color(EE_LIGHTGRAY); | |||
caca_blit(6, 4, caca_get_width() - 6, caca_get_height() - 4, pixels, bufx, bufy); | |||
} | |||
@@ -1,115 +0,0 @@ | |||
/* | |||
* spritedit sprite editor using libcaca | |||
* Copyright (c) 2003 Sam Hocevar <sam@zoy.org> | |||
* All Rights Reserved | |||
* | |||
* $Id$ | |||
* | |||
* This program is free software; you can redistribute it and/or modify | |||
* it under the terms of the GNU General Public License as published by | |||
* the Free Software Foundation; either version 2 of the License, or | |||
* (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
* GNU General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU General Public License | |||
* along with this program; if not, write to the Free Software | |||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |||
*/ | |||
#include "config.h" | |||
#include <stdio.h> | |||
#include "caca.h" | |||
int main(int argc, char **argv) | |||
{ | |||
int quit = 0; | |||
struct caca_sprite *sprite; | |||
int frame = 0; | |||
if(argc < 2) | |||
{ | |||
fprintf(stderr, "%s: missing argument (filename).\n", argv[0]); | |||
return 1; | |||
} | |||
if(caca_init()) | |||
return 1; | |||
sprite = caca_load_sprite(argv[1]); | |||
if(!sprite) | |||
{ | |||
caca_end(); | |||
fprintf(stderr, "%s: could not open `%s'.\n", argv[0], argv[1]); | |||
return 1; | |||
} | |||
/* Go ! */ | |||
while(!quit) | |||
{ | |||
int xa, ya, xb, yb; | |||
char buf[BUFSIZ]; | |||
switch(caca_get_key()) | |||
{ | |||
case 0: | |||
break; | |||
case 'q': | |||
quit = 1; | |||
break; | |||
case '-': | |||
if(frame > 0) | |||
frame--; | |||
break; | |||
case '+': | |||
if(frame < caca_get_sprite_frames(sprite) - 1) | |||
frame++; | |||
break; | |||
} | |||
caca_clear(); | |||
caca_set_color(EE_WHITE); | |||
caca_draw_thin_box(0, 0, caca_get_width() - 1, caca_get_height() - 1); | |||
caca_putstr(3, 0, "[ Sprite editor for libcaca ]"); | |||
sprintf(buf, "sprite `%s'", argv[1]); | |||
caca_putstr(3, 2, buf); | |||
sprintf(buf, "frame %i/%i", frame, caca_get_sprite_frames(sprite) - 1); | |||
caca_putstr(3, 3, buf); | |||
/* Crosshair */ | |||
caca_draw_thin_line(57, 2, 57, 18); | |||
caca_draw_thin_line(37, 10, 77, 10); | |||
caca_putchar(57, 10, '+'); | |||
/* Boxed sprite */ | |||
xa = -1 - caca_get_sprite_dx(sprite, frame); | |||
ya = -1 - caca_get_sprite_dy(sprite, frame); | |||
xb = xa + 1 + caca_get_sprite_width(sprite, frame); | |||
yb = ya + 1 + caca_get_sprite_height(sprite, frame); | |||
caca_set_color(EE_BLACK); | |||
caca_fill_box(57 + xa, 10 + ya, 57 + xb, 10 + yb, ' '); | |||
caca_set_color(EE_WHITE); | |||
caca_draw_thin_box(57 + xa, 10 + ya, 57 + xb, 10 + yb); | |||
caca_draw_sprite(57, 10, sprite, frame); | |||
/* Free sprite */ | |||
caca_draw_sprite(20, 10, sprite, frame); | |||
caca_refresh(); | |||
} | |||
/* Clean up */ | |||
caca_end(); | |||
return 0; | |||
} | |||