Browse Source

* src/graphics.c:

+ Internally cache screen width and height.
  * src/bitmap.c:
    + Added alpha support to caca_draw_bitmap(). For now, we only treat 0%
      alpha as fully transparent, and any other value as fully opaque.
    + Slightly tuned colour weights in the renderer.
    + caca_set_bitmap_palette() takes unsigned ints.
  * examples/demo.c:
    + Added a crap render demo.
  * examples/aafire.c:
    + Set a 20ms delay.
    + Added alpha values to the palette.
  * examples/view.c:
    + Draw a gray checkered grid below the image so that transparent images
      look a lot nicer.
    + 'f' toggles "fullscreen" mode.
tags/v0.99.beta14
Sam Hocevar sam 21 years ago
parent
commit
c883ca4868
13 changed files with 299 additions and 133 deletions
  1. +5
    -4
      examples/aafire.c
  2. +106
    -1
      examples/demo.c
  3. +59
    -21
      examples/view.c
  4. +56
    -41
      src/bitmap.c
  5. +4
    -4
      src/box.c
  6. +3
    -19
      src/caca.c
  7. +10
    -9
      src/caca.h
  8. +4
    -0
      src/caca_internals.h
  9. +4
    -4
      src/conic.c
  10. +36
    -18
      src/graphics.c
  11. +4
    -4
      src/line.c
  12. +6
    -6
      src/sprite.c
  13. +2
    -2
      src/triangle.c

+ 5
- 4
examples/aafire.c View File

@@ -95,7 +95,7 @@ initialize (void)
{
int i;
#ifdef LIBCACA
int r[256], g[256], b[256];
int r[256], g[256], b[256], a[256];
#endif

#ifdef LIBCACA
@@ -104,6 +104,7 @@ initialize (void)
printf ("Failed to initialize libcaca\n");
exit (1);
}
caca_set_delay(20000);
#else
context = aa_autoinit (&aa_defparams);
if (context == NULL)
@@ -121,6 +122,7 @@ initialize (void)
r[i] = pal[i * 3] * 64;
g[i] = pal[i * 3 + 1] * 64;
b[i] = pal[i * 3 + 2] * 64;
a[i] = 0xfff;
}
#else
aa_setpalette (palette, i, pal[i * 3] * 4,
@@ -128,14 +130,13 @@ initialize (void)
#endif

#ifdef LIBCACA
caca_bitmap = caca_create_bitmap(8, XSIZ, YSIZ - 4, XSIZ, 0, 0, 0);
caca_set_bitmap_palette(caca_bitmap, r, g, b);
caca_bitmap = caca_create_bitmap(8, XSIZ, YSIZ - 2, XSIZ, 0, 0, 0, 0);
caca_set_bitmap_palette(caca_bitmap, r, g, b, a);
bitmap = malloc(XSIZ * YSIZ * sizeof(char));
caca_set_dithering(CACA_DITHERING_ORDERED8);
#else
aa_hidecursor (context);
#endif
fprintf(stderr, "%i %i\n", XSIZ, YSIZ);
}
static void
uninitialize (void)


+ 106
- 1
examples/demo.c View File

@@ -40,6 +40,7 @@ static void demo_boxes(void);
static void demo_ellipses(void);
static void demo_triangles(void);
static void demo_sprites(void);
static void demo_render(void);

int bounds = 0;
int outline = 0;
@@ -130,6 +131,9 @@ int main(int argc, char **argv)
case 'S':
if(sprite)
demo = demo_sprites;
case 'r':
case 'R':
demo = demo_render;
break;
}

@@ -193,8 +197,9 @@ static void display_menu(void)
caca_putstr(4, 11, "'4': triangles");
caca_putstr(4, 12, "'5': ellipses");
caca_putstr(4, 13, "'c': colour");
caca_putstr(4, 14, "'r': render");
if(sprite)
caca_putstr(4, 14, "'s': sprites");
caca_putstr(4, 15, "'s': sprites");

caca_putstr(4, 16, "settings:");
caca_printf(4, 17, "'o': outline: %s",
@@ -460,3 +465,103 @@ static void demo_sprites(void)
caca_rand(0, caca_get_height() - 1), sprite, 0);
}

#if 0
static void demo_render(void)
{
struct caca_bitmap *bitmap;
//short buffer[256*256];
//short *dest = buffer;
int buffer[256*256];
int *dest = buffer;
int x, y, z;
static int i = 0;

i = (i + 1) % 512;
z = i < 256 ? i : 511 - i;

for(x = 0; x < 256; x++)
for(y = 0; y < 256; y++)
{
//*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
*dest++ = (x << 16) | (y << 8) | (z);
}

//bitmap = caca_create_bitmap(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
bitmap, buffer);
caca_free_bitmap(bitmap);
}
#endif

static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);

static void demo_render(void)
{
struct caca_bitmap *bitmap;
int buffer[256*256];
int *dest;
int x, y, z, t, xo, yo;
static int i = 0;

i++;

dest = buffer;
for(x = 0; x < 256; x++)
for(y = 0; y < 256; y++)
{
*dest++ = 0;
}

/* red */
xo = 128 + 48 * sin(0.02 * i);
yo = 128 + 48 * cos(0.03 * i);
t = 256 * (2.0 + sin(4 + 0.017 * i)) / 3;
for(z = 0; z < 240; z++)
draw_circle(buffer, xo, yo, z, 0x00ff0000,
((255 - z) * t / 256) << 16);

/* green */
xo = 128 + 48 * sin(2 + 0.06 * i);
yo = 128 + 48 * cos(2 + 0.05 * i);
t = 256 * (2.0 + sin(8 + 0.021 * i)) / 3;
for(z = 0; z < 240; z++)
draw_circle(buffer, xo, yo, z, 0x0000ff00,
((255 - z) * t / 256) << 8);

/* blue */
xo = 128 + 48 * sin(1 + 0.04 * i);
yo = 128 + 48 * cos(1 + 0.03 * i);
t = 256 * (2.0 + sin(3 + 0.033 * i)) / 3;
for(z = 0; z < 240; z++)
draw_circle(buffer, xo, yo, z, 0x000000ff, (255 - z) * t / 256);

bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
bitmap, (char *)buffer);
caca_free_bitmap(bitmap);
}

static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
{
int t, dx, dy;

#define POINT(X,Y) \
buffer[(X) + 256 * (Y)] = (buffer[(X) + 256 * (Y)] & ~mask) | val;

for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
{
POINT(x - dx / 3, y - dy / 3);
POINT(x + dx / 3, y - dy / 3);
POINT(x - dx / 3, y + dy / 3);
POINT(x + dx / 3, y + dy / 3);

POINT(x - dy / 3, y - dx / 3);
POINT(x + dy / 3, y - dx / 3);
POINT(x - dy / 3, y + dx / 3);
POINT(x + dy / 3, y + dx / 3);

t += t > 0 ? dx - dy-- : dx;
}
}


+ 59
- 21
examples/view.c View File

@@ -32,23 +32,25 @@

#include "caca.h"

/* Local functions */
static void load_image(const char *);
static void draw_checkers(unsigned int, unsigned int,
unsigned int, unsigned int);

/* Local variables */
Imlib_Image image = NULL;
char *pixels = NULL;
struct caca_bitmap *bitmap = NULL;
int x, y, w, h;
const unsigned int rmask = 0x00ff0000, gmask = 0x0000ff00, bmask = 0x000000ff;

int dithering = CACA_DITHERING_ORDERED4;

static void load_image(const char *);

int main(int argc, char **argv)
{
int quit = 0, update = 1, help = 0, reload = 0;
int i, zoom = 0;
int quit = 0, update = 1, help = 0, reload = 0, fullscreen = 0, zoom = 0;
int dithering = CACA_DITHERING_ORDERED4;

char **list = NULL;
int current = 0, items = 0, opts = 1;
int i;

/* Initialise libcaca */
if(caca_init())
@@ -101,6 +103,11 @@ int main(int argc, char **argv)
if(items) current = (items + current - 1) % items;
reload = 1;
break;
case CACA_EVENT_KEY_PRESS | 'f':
case CACA_EVENT_KEY_PRESS | 'F':
fullscreen = ~fullscreen;
update = 1;
break;
case CACA_EVENT_KEY_PRESS | 'd':
dithering = (dithering + 1) % 5;
update = 1;
@@ -203,6 +210,7 @@ int main(int argc, char **argv)
int yo = (wh - 1) / 2;
int xn = (ww - 1) / (2 - zoom);
int yn = (wh - 1) / (2 - zoom);
draw_checkers(xo - xn, yo - yn, xo + xn, yo + yn);
caca_draw_bitmap(xo - xn, yo - yn, xo + xn, yo + yn,
bitmap, pixels);
}
@@ -216,27 +224,40 @@ int main(int argc, char **argv)
if(xn + x > w) x = w - xn;
if(yn + y > h) y = h - yn;
newbitmap = caca_create_bitmap(32, 2 * xn, 2 * yn, 4 * w,
rmask, gmask, bmask);
caca_draw_bitmap(0, 0, ww - 1, wh - 1, newbitmap,
0x00ff0000, 0x0000ff00, 0x000000ff,
0xff000000);
draw_checkers(0, fullscreen ? 0 : 1,
ww - 1, fullscreen ? wh - 1 : wh - 2);
caca_draw_bitmap(0, fullscreen ? 0 : 1,
ww - 1, fullscreen ? wh - 1 : wh - 2,
newbitmap,
pixels + 4 * (x - xn) + 4 * w * (y - yn));
caca_free_bitmap(newbitmap);
}
else
{
caca_draw_bitmap(0, 0, ww - 1, wh - 1, bitmap, pixels);
draw_checkers(0, fullscreen ? 0 : 1,
ww - 1, fullscreen ? wh - 1 : wh - 2);
caca_draw_bitmap(0, fullscreen ? 0 : 1,
ww - 1, fullscreen ? wh - 1 : wh - 2,
bitmap, pixels);
}

caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLUE);
caca_draw_line(0, 0, ww - 1, 0, ' ');
caca_draw_line(0, wh - 1, ww - 1, wh - 1, '-');
caca_putstr(0, 0, "q:Quit n/p:Next/Prev +/-/x:Zoom "
"h/j/k/l: Move d:Dithering");
caca_putstr(ww - strlen("?:Help"), 0, "?:Help");
caca_printf(3, wh - 1, "cacaview %s", VERSION);
caca_printf(ww / 2 - 5, wh - 1, "(%s dithering)",
caca_get_dithering_name(dithering));
caca_printf(ww - 14, wh - 1,
"(zoom: %s%i)", zoom > 0 ? "+" : "", zoom);

if(!fullscreen)
{
caca_draw_line(0, 0, ww - 1, 0, ' ');
caca_draw_line(0, wh - 1, ww - 1, wh - 1, '-');
caca_putstr(0, 0, "q:Quit n/p:Next/Prev +/-/x:Zoom "
"h/j/k/l: Move d:Dithering");
caca_putstr(ww - strlen("?:Help"), 0, "?:Help");
caca_printf(3, wh - 1, "cacaview %s", VERSION);
caca_printf(ww / 2 - 5, wh - 1, "(%s dithering)",
caca_get_dithering_name(dithering));
caca_printf(ww - 14, wh - 1,
"(zoom: %s%i)", zoom > 0 ? "+" : "", zoom);
}

if(help)
{
@@ -298,7 +319,8 @@ static void load_image(const char *name)
y = h / 2;

/* Create the libcaca bitmap */
bitmap = caca_create_bitmap(32, w, h, 4 * w, rmask, gmask, bmask);
bitmap = caca_create_bitmap(32, w, h, 4 * w, 0x00ff0000, 0x0000ff00,
0x000000ff, 0xff000000);
if(!bitmap)
{
imlib_free_image();
@@ -306,3 +328,19 @@ static void load_image(const char *name)
}
}

static void draw_checkers(unsigned int x1, unsigned int y1,
unsigned int x2, unsigned int y2)
{
unsigned int xn, yn;

for(yn = y1; yn <= y2; yn++)
for(xn = x1; xn <= x2; xn++)
{
if(((xn / 4) ^ (yn / 2)) & 1)
caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_DARKGRAY);
else
caca_set_color(CACA_COLOR_DARKGRAY, CACA_COLOR_LIGHTGRAY);
caca_putchar(xn, yn, ' ');
}
}


+ 56
- 41
src/bitmap.c View File

@@ -50,8 +50,9 @@ typedef unsigned int uint32_t;

static void mask2shift(unsigned int, int *, int *);

static void get_rgb_default(struct caca_bitmap *, uint8_t *, int, int,
unsigned int *, unsigned int *, unsigned int *);
static void get_rgba_default(const struct caca_bitmap *, uint8_t *, int, int,
unsigned int *, unsigned int *, unsigned int *,
unsigned int *);
static void rgb2hsv_default(int, int, int, int *, int *, int *);

/* Dithering methods */
@@ -127,11 +128,11 @@ struct caca_bitmap
{
int bpp, palette;
int w, h, pitch;
int rmask, gmask, bmask;
int rright, gright, bright;
int rleft, gleft, bleft;
int rmask, gmask, bmask, amask;
int rright, gright, bright, aright;
int rleft, gleft, bleft, aleft;
void (*get_hsv)(struct caca_bitmap *, char *, int, int);
int red[256], green[256], blue[256];
int red[256], green[256], blue[256], alpha[256];
};

static void mask2shift(unsigned int mask, int *right, int *left)
@@ -160,7 +161,8 @@ static void mask2shift(unsigned int mask, int *right, int *left)
}

struct caca_bitmap *caca_create_bitmap(int bpp, int w, int h, int pitch,
int rmask, int gmask, int bmask)
int rmask, int gmask, int bmask,
int amask)
{
struct caca_bitmap *bitmap;

@@ -182,13 +184,15 @@ struct caca_bitmap *caca_create_bitmap(int bpp, int w, int h, int pitch,
bitmap->rmask = rmask;
bitmap->gmask = gmask;
bitmap->bmask = bmask;
bitmap->amask = amask;

/* Load bitmasks */
if(rmask || gmask || bmask)
if(rmask || gmask || bmask || amask)
{
mask2shift(rmask, &bitmap->rright, &bitmap->rleft);
mask2shift(gmask, &bitmap->gright, &bitmap->gleft);
mask2shift(bmask, &bitmap->bright, &bitmap->bleft);
mask2shift(amask, &bitmap->aright, &bitmap->aleft);
}

/* In 8 bpp mode, default to a grayscale palette */
@@ -198,9 +202,10 @@ struct caca_bitmap *caca_create_bitmap(int bpp, int w, int h, int pitch,
bitmap->palette = 1;
for(i = 0; i < 256; i++)
{
bitmap->red[i] = i * 0x10;
bitmap->green[i] = i * 0x10;
bitmap->blue[i] = i * 0x10;
bitmap->red[i] = i * 0xfff / 256;
bitmap->green[i] = i * 0xfff / 256;
bitmap->blue[i] = i * 0xfff / 256;
bitmap->alpha[i] = 0xfff;
}
}

@@ -208,7 +213,8 @@ struct caca_bitmap *caca_create_bitmap(int bpp, int w, int h, int pitch,
}

void caca_set_bitmap_palette(struct caca_bitmap *bitmap,
int red[], int green[], int blue[])
unsigned int red[], unsigned int green[],
unsigned int blue[], unsigned int alpha[])
{
int i;

@@ -219,11 +225,13 @@ void caca_set_bitmap_palette(struct caca_bitmap *bitmap,
{
if(red[i] >= 0 && red[i] < 0x1000 &&
green[i] >= 0 && green[i] < 0x1000 &&
blue[i] >= 0 && blue[i] < 0x1000)
blue[i] >= 0 && blue[i] < 0x1000 &&
alpha[i] >= 0 && alpha[i] < 0x1000)
{
bitmap->red[i] = red[i];
bitmap->green[i] = green[i];
bitmap->blue[i] = blue[i];
bitmap->alpha[i] = alpha[i];
}
}
}
@@ -236,9 +244,9 @@ void caca_free_bitmap(struct caca_bitmap *bitmap)
free(bitmap);
}

static void get_rgb_default(struct caca_bitmap *bitmap, uint8_t *pixels,
int x, int y, unsigned int *r,
unsigned int *g, unsigned int *b)
static void get_rgba_default(const struct caca_bitmap *bitmap, uint8_t *pixels,
int x, int y, unsigned int *r, unsigned int *g,
unsigned int *b, unsigned int *a)
{
uint32_t bits;

@@ -254,8 +262,9 @@ static void get_rgb_default(struct caca_bitmap *bitmap, uint8_t *pixels,
#ifdef HAVE_ENDIAN_H
if(__BYTE_ORDER == __BIG_ENDIAN)
#else
static const uint32_t rmask = 0x12345678;
if(*(uint8_t *)&rmask == 0x12)
/* This is compile-time optimised with at least -O1 or -Os */
const uint32_t rmask = 0x12345678;
if(*(const uint8_t *)&rmask == 0x12)
#endif
bits = ((uint32_t)pixels[0] << 16) |
((uint32_t)pixels[1] << 8) |
@@ -280,12 +289,14 @@ static void get_rgb_default(struct caca_bitmap *bitmap, uint8_t *pixels,
*r = bitmap->red[bits];
*g = bitmap->green[bits];
*b = bitmap->blue[bits];
*a = bitmap->alpha[bits];
}
else
{
*r = ((bits & bitmap->rmask) >> bitmap->rright) << bitmap->rleft;
*g = ((bits & bitmap->gmask) >> bitmap->gright) << bitmap->gleft;
*b = ((bits & bitmap->bmask) >> bitmap->bright) << bitmap->bleft;
*a = ((bits & bitmap->amask) >> bitmap->aright) << bitmap->aleft;
}
}

@@ -320,7 +331,7 @@ static void rgb2hsv_default(int r, int g, int b, int *hue, int *sat, int *val)
}

void caca_draw_bitmap(int x1, int y1, int x2, int y2,
struct caca_bitmap *bitmap, char *pixels)
const struct caca_bitmap *bitmap, char *pixels)
{
#if !NEW_RENDERER
static const int white_colors[] =
@@ -358,18 +369,18 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2,
# define DENSITY_CHARS 13
static const char density_chars[] =
" "
" "
".` "
",`.'"
"i:-^"
"|=+;"
"ox/\\"
"<>x%"
"&$zw"
"WXKM"
"#8##"
"8@8#"
"@8@8"
". "
".. "
"...."
"::::"
";=;="
"tftf"
"%$%$"
"&KSZ"
"WXGM"
"@@@@"
"8888"
"####"
"????";

int x, y, w, h, pitch;
@@ -391,13 +402,13 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2,
int tmp = y2; y2 = y1; y1 = tmp;
}

for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)caca_get_height(); y++)
for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)_caca_height; y++)
for(x = x1 > 0 ? x1 : 0, _init_dither(y);
x <= x2 && x <= (int)caca_get_width();
x <= x2 && x <= (int)_caca_width;
x++)
{
int ch;
unsigned int r, g, b, R, G, B;
unsigned int r, g, b, a, R, G, B;
int hue, sat, val;
int fromx = w * (x - x1) / (x2 - x1 + 1);
int fromy = h * (y - y1) / (y2 - y1 + 1);
@@ -413,18 +424,20 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2,

/* First get RGB */
R = 0, G = 0, B = 0;
get_rgb_default(bitmap, pixels, fromx, fromy, &r, &g, &b);
get_rgba_default(bitmap, pixels, fromx, fromy, &r, &g, &b, &a);
if(a == 0)
continue;
#if 1
R += r; G += g; B += b;
#else
R += r; G += g; B += b;
get_rgb_default(bitmap, pixels, fromx - 1, fromy, &r, &g, &b);
get_rgba_default(bitmap, pixels, fromx - 1, fromy, &r, &g, &b, &a);
R += r; G += g; B += b;
get_rgb_default(bitmap, pixels, fromx, fromy - 1, &r, &g, &b);
get_rgba_default(bitmap, pixels, fromx, fromy - 1, &r, &g, &b, &a);
R += r; G += g; B += b;
get_rgb_default(bitmap, pixels, fromx + 1, fromy, &r, &g, &b);
get_rgba_default(bitmap, pixels, fromx + 1, fromy, &r, &g, &b, &a);
R += r; G += g; B += b;
get_rgb_default(bitmap, pixels, fromx, fromy + 1, &r, &g, &b);
get_rgba_default(bitmap, pixels, fromx, fromy + 1, &r, &g, &b, &a);
R += r; G += g; B += b;
R /= 5; G /= 5; B /= 5;
#endif
@@ -443,6 +456,7 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2,
/* distance to black */
distbg = XRATIO * val * val;
distbg += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
distbg = distbg * 2 / 4;
outbg = CACA_COLOR_BLACK;

/* distance to 30% */
@@ -509,7 +523,7 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2,
+ YRATIO * (sat - 0x1000) * (sat - 0x1000)
+ HRATIO * (hue - hue2) * (hue - hue2);
dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
dist = dist * 3 / 4;
// dist = dist * 3 / 4;
if(dist <= distbg)
{
outfg = outbg;
@@ -528,7 +542,8 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2,
+ YRATIO * (sat - 0x1000) * (sat - 0x1000)
+ HRATIO * (hue - hue2) * (hue - hue2);
dist += FUZZINESS * _get_dither() - 0x80 * FUZZINESS;
dist = dist / 2;
//dist = dist * 3 / 4;
//dist = dist / 2;
if(dist <= distbg)
{
outfg = outbg;


+ 4
- 4
src/box.c View File

@@ -58,8 +58,8 @@ void caca_draw_thin_box(int x1, int y1, int x2, int y2)
y1 = y2; y2 = tmp;
}

xmax = caca_get_width() - 1;
ymax = caca_get_height() - 1;
xmax = _caca_width - 1;
ymax = _caca_height - 1;

if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
return;
@@ -111,8 +111,8 @@ void caca_fill_box(int x1, int y1, int x2, int y2, char c)
y1 = y2; y2 = tmp;
}

xmax = caca_get_width() - 1;
ymax = caca_get_height() - 1;
xmax = _caca_width - 1;
ymax = _caca_height - 1;

if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
return;


+ 3
- 19
src/caca.c View File

@@ -38,9 +38,6 @@
#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
@@ -62,7 +59,6 @@ int _caca_attr[16*16];
#endif

#if defined(USE_CONIO)
static struct text_info ti;
char *_caca_screen;
#endif

@@ -132,24 +128,12 @@ int caca_init(void)

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
return _caca_width;
}

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
return _caca_height;
}

const char *caca_get_color_name(enum caca_color color)
@@ -212,7 +196,7 @@ void caca_end(void)
_wscroll = 1;
textcolor((enum COLORS)WHITE);
textbackground((enum COLORS)BLACK);
gotoxy(caca_get_width(), caca_get_height());
gotoxy(_caca_width, _caca_height);
cputs("\r\n");
_setcursortype(_NORMALCURSOR);
#endif


+ 10
- 9
src/caca.h View File

@@ -200,21 +200,22 @@ unsigned int caca_sqrt(unsigned int);
*/
struct caca_sprite;
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);
int caca_get_sprite_frames(const struct caca_sprite *);
int caca_get_sprite_width(const struct caca_sprite *, int);
int caca_get_sprite_height(const struct caca_sprite *, int);
int caca_get_sprite_dx(const struct caca_sprite *, int);
int caca_get_sprite_dy(const struct caca_sprite *, int);
void caca_draw_sprite(int, int, const struct caca_sprite *, int);
void caca_free_sprite(struct caca_sprite *);

/*
* Bitmap handling
*/
struct caca_bitmap;
struct caca_bitmap *caca_create_bitmap(int, int, int, int, int, int, int);
void caca_set_bitmap_palette(struct caca_bitmap *, int[], int[], int[]);
void caca_draw_bitmap(int, int, int, int, struct caca_bitmap *, char *);
struct caca_bitmap *caca_create_bitmap(int, int, int, int, int, int, int, int);
void caca_set_bitmap_palette(struct caca_bitmap *, unsigned int[],
unsigned int[], unsigned int[], unsigned int[]);
void caca_draw_bitmap(int, int, int, int, const struct caca_bitmap *, char *);
void caca_free_bitmap(struct caca_bitmap *);

#ifdef __cplusplus


+ 4
- 0
src/caca_internals.h View File

@@ -37,9 +37,13 @@ extern int _caca_attr[];
#endif

#if defined(USE_CONIO)
extern struct text_info _ti;
extern char *_caca_screen;
#endif

extern unsigned int _caca_width;
extern unsigned int _caca_height;

extern char *_caca_empty_line;
extern char *_caca_scratch_line;



+ 4
- 4
src/conic.c View File

@@ -191,13 +191,13 @@ 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())
if(xo + x >= 0 && xo + x < (int)_caca_width)
b |= 0x1;
if(xo - x >= 0 && xo - x < (int)caca_get_width())
if(xo - x >= 0 && xo - x < (int)_caca_width)
b |= 0x2;
if(yo + y >= 0 && yo + y < (int)caca_get_height())
if(yo + y >= 0 && yo + y < (int)_caca_height)
b |= 0x4;
if(yo - y >= 0 && yo - y < (int)caca_get_height())
if(yo - y >= 0 && yo - y < (int)_caca_height)
b |= 0x8;

if((b & (0x1|0x4)) == (0x1|0x4))


+ 36
- 18
src/graphics.c View File

@@ -35,6 +35,9 @@
# include <curses.h>
#elif defined(USE_CONIO)
# include <conio.h>
# if defined(SCREENUPDATE_IN_PC_H)
# include <pc.h>
# endif
#else
# error "no graphics library detected"
#endif
@@ -54,6 +57,13 @@
#include "caca.h"
#include "caca_internals.h"

#ifdef USE_CONIO
static struct text_info ti;
#endif

unsigned int _caca_width;
unsigned int _caca_height;

static unsigned int _caca_delay;
static unsigned int _caca_rendertime;

@@ -92,8 +102,8 @@ void caca_putchar(int x, int y, char c)
#if defined(USE_CONIO)
char *data;
#endif
if(x < 0 || x >= (int)caca_get_width() ||
y < 0 || y >= (int)caca_get_height())
if(x < 0 || x >= (int)_caca_width ||
y < 0 || y >= (int)_caca_height)
return;

#if defined(USE_SLANG)
@@ -103,7 +113,7 @@ void caca_putchar(int x, int y, char c)
move(y, x);
addch(c);
#elif defined(USE_CONIO)
data = _caca_screen + 2 * (x + y * caca_get_width());
data = _caca_screen + 2 * (x + y * _caca_width);
data[0] = c;
data[1] = (_caca_bgcolor << 4) | _caca_fgcolor;
// gotoxy(x + 1, y + 1);
@@ -115,7 +125,7 @@ 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())
if(y < 0 || y >= (int)_caca_height || x >= (int)_caca_width)
return;

len = strlen(s);
@@ -129,10 +139,10 @@ void caca_putstr(int x, int y, const char *s)
x = 0;
}

if(x + len >= caca_get_width())
if(x + len >= _caca_width)
{
memcpy(_caca_scratch_line, s, caca_get_width() - x);
_caca_scratch_line[caca_get_width() - x] = '\0';
memcpy(_caca_scratch_line, s, _caca_width - x);
_caca_scratch_line[_caca_width - x] = '\0';
s = _caca_scratch_line;
}

@@ -143,7 +153,7 @@ void caca_putstr(int x, int y, const char *s)
move(y, x);
addstr(s);
#elif defined(USE_CONIO)
char *buf = _caca_screen + 2 * (x + y * caca_get_width());
char *buf = _caca_screen + 2 * (x + y * _caca_width);
while(*s)
{
*buf++ = *s++;
@@ -160,19 +170,19 @@ void caca_printf(int x, int y, const char *format, ...)
char *buf = tmp;
va_list args;

if(y < 0 || y >= (int)caca_get_height() || x >= (int)caca_get_width())
if(y < 0 || y >= (int)_caca_height || x >= (int)_caca_width)
return;

if(caca_get_width() - x + 1 > BUFSIZ)
buf = malloc(caca_get_width() - x + 1);
if(_caca_width - x + 1 > BUFSIZ)
buf = malloc(_caca_width - x + 1);

va_start(args, format);
#if defined(HAVE_VSNPRINTF)
vsnprintf(buf, caca_get_width() - x + 1, format, args);
vsnprintf(buf, _caca_width - x + 1, format, args);
#else
vsprintf(buf, format, args);
#endif
buf[caca_get_width() - x] = '\0';
buf[_caca_width - x] = '\0';
va_end(args);

caca_putstr(x, y, buf);
@@ -185,7 +195,7 @@ void caca_clear(void)
{
enum caca_color oldfg = caca_get_fg_color();
enum caca_color oldbg = caca_get_bg_color();
int y = caca_get_height();
int y = _caca_height;

caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);

@@ -234,6 +244,9 @@ int _caca_init_graphics(void)
/* Disable alt charset support so that we get all 256 colour pairs */
SLtt_Has_Alt_Charset = 0;

_caca_width = SLtt_Screen_Cols;
_caca_height = SLtt_Screen_Rows;

#elif defined(USE_NCURSES)
static int curses_colors[] =
{
@@ -292,6 +305,9 @@ int _caca_init_graphics(void)
}
}

_caca_width = COLS;
_caca_height = LINES;

#elif defined(USE_CONIO)
gettextinfo(&ti);
_caca_screen = malloc(2 * ti.screenwidth * ti.screenheight);
@@ -302,13 +318,15 @@ int _caca_init_graphics(void)
# else
/* FIXME */
# endif
_caca_width = ti.screenwidth;
_caca_height = ti.screenheight;

#endif
_caca_empty_line = malloc(caca_get_width() + 1);
memset(_caca_empty_line, ' ', caca_get_width());
_caca_empty_line[caca_get_width()] = '\0';
_caca_empty_line = malloc(_caca_width + 1);
memset(_caca_empty_line, ' ', _caca_width);
_caca_empty_line[_caca_width] = '\0';

_caca_scratch_line = malloc(caca_get_width() + 1);
_caca_scratch_line = malloc(_caca_width + 1);

_caca_delay = 0;
_caca_rendertime = 0;


+ 4
- 4
src/line.c View File

@@ -171,7 +171,7 @@ static void clip_line(struct line* s)
}
else if(bits1 & (1<<1))
{
int xmax = caca_get_width() - 1;
int xmax = _caca_width - 1;
s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
s->x1 = xmax;
}
@@ -182,7 +182,7 @@ static void clip_line(struct line* s)
}
else if(bits1 & (1<<3))
{
int ymax = caca_get_height() - 1;
int ymax = _caca_height - 1;
s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
s->y1 = ymax;
}
@@ -203,12 +203,12 @@ static uint8_t clip_bits(int x, int y)

if(x < 0)
b |= (1<<0);
else if(x >= (int)caca_get_width())
else if(x >= (int)_caca_width)
b |= (1<<1);

if(y < 0)
b |= (1<<2);
else if(y >= (int)caca_get_height())
else if(y >= (int)_caca_height)
b |= (1<<3);

return b;


+ 6
- 6
src/sprite.c View File

@@ -165,7 +165,7 @@ sprite_alloc_failed:
return NULL;
}

int caca_get_sprite_frames(struct caca_sprite *sprite)
int caca_get_sprite_frames(const struct caca_sprite *sprite)
{
if(sprite == NULL)
return 0;
@@ -173,7 +173,7 @@ int caca_get_sprite_frames(struct caca_sprite *sprite)
return sprite->nf;
}

int caca_get_sprite_width(struct caca_sprite *sprite, int f)
int caca_get_sprite_width(const struct caca_sprite *sprite, int f)
{
if(sprite == NULL)
return 0;
@@ -184,7 +184,7 @@ int caca_get_sprite_width(struct caca_sprite *sprite, int f)
return sprite->frames[f].w;
}

int caca_get_sprite_height(struct caca_sprite *sprite, int f)
int caca_get_sprite_height(const struct caca_sprite *sprite, int f)
{
if(sprite == NULL)
return 0;
@@ -195,7 +195,7 @@ int caca_get_sprite_height(struct caca_sprite *sprite, int f)
return sprite->frames[f].h;
}

int caca_get_sprite_dx(struct caca_sprite *sprite, int f)
int caca_get_sprite_dx(const struct caca_sprite *sprite, int f)
{
if(sprite == NULL)
return 0;
@@ -206,7 +206,7 @@ int caca_get_sprite_dx(struct caca_sprite *sprite, int f)
return sprite->frames[f].dx;
}

int caca_get_sprite_dy(struct caca_sprite *sprite, int f)
int caca_get_sprite_dy(const struct caca_sprite *sprite, int f)
{
if(sprite == NULL)
return 0;
@@ -217,7 +217,7 @@ int caca_get_sprite_dy(struct caca_sprite *sprite, int f)
return sprite->frames[f].dy;
}

void caca_draw_sprite(int x, int y, struct caca_sprite *sprite, int f)
void caca_draw_sprite(int x, int y, const struct caca_sprite *sprite, int f)
{
int i, j;
enum caca_color oldfg, oldbg;


+ 2
- 2
src/triangle.c View File

@@ -70,8 +70,8 @@ void caca_fill_triangle(int x1, int y1, int x2, int y2, int x3, int y3, char c)
x2 *= 4;
x3 *= 4;

xmax = caca_get_width() - 1;
ymax = caca_get_height() - 1;
xmax = _caca_width - 1;
ymax = _caca_height - 1;

/* Rasterize our triangle */
for(y = y1 < 0 ? 0 : y1; y <= y3 && y <= ymax; y++)


Loading…
Cancel
Save