Exemplo n.º 1
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv, *pig;
    void *buffer;
    size_t len;
    int i, j;

    pig = caca_create_canvas(0, 0);
    caca_import_canvas_from_memory(pig, STRING, strlen(STRING), "text");

    cv = caca_create_canvas(caca_get_canvas_width(pig) * 2,
                             caca_get_canvas_height(pig) * 2);

    if(cv == NULL || pig == NULL)
    {
        printf("Can't created canvas\n");
        return -1;
    }

    caca_blit(cv, 0, 0, pig, NULL);
    caca_flip(pig);
    caca_blit(cv, caca_get_canvas_width(pig), 0, pig, NULL);
    caca_flip(pig);
    caca_flop(pig);
    caca_blit(cv, 0, caca_get_canvas_height(pig), pig, NULL);
    caca_flop(pig);
    caca_rotate_180(pig);
    caca_blit(cv, caca_get_canvas_width(pig),
                   caca_get_canvas_height(pig), pig, NULL);

    for(j = 0; j < caca_get_canvas_height(cv); j++)
    {
        for(i = 0; i < caca_get_canvas_width(cv); i += 2)
        {
            unsigned long int a;
            caca_set_color_ansi(cv, CACA_LIGHTBLUE + (i + j) % 6,
                                 CACA_DEFAULT);
            a = caca_get_attr(cv, -1, -1);
            caca_put_attr(cv, i, j, a);
            caca_put_attr(cv, i + 1, j, a);
        }
    }

    buffer = caca_export_canvas_to_memory(cv, "utf8", &len);
    fwrite(buffer, len, 1, stdout);
    free(buffer);

    caca_rotate_left(cv);
    buffer = caca_export_canvas_to_memory(cv, "utf8", &len);
    fwrite(buffer, len, 1, stdout);
    free(buffer);

    caca_free_canvas(pig);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 2
0
static int preinit(struct vo *vo, const char *arg)
{
    if (arg) {
        mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: Unknown subdevice: %s\n", arg);
        return ENOSYS;
    }

    canvas = caca_create_canvas(0, 0);
    if (canvas == NULL) {
        mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to create canvas\n");
        return ENOSYS;
    }

    display = caca_create_display(canvas);

    if (display == NULL) {
        mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to create display\n");
        caca_free_canvas(canvas);
        return ENOSYS;
    }

    caca_set_display_title(display, "MPlayer");

    return 0;
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv;
    int tests = 0, passed = 0;

    cv = caca_create_canvas(0, 0);
    caca_put_char(cv, 0, 0, 'x');
    TEST(caca_get_char(cv, 0, 0) != 'x');

    caca_rotate_180(cv);

    caca_set_canvas_size(cv, 1, 1);
    TEST(caca_get_char(cv, 0, 0) != 'x');
    TEST(caca_get_char(cv, 0, 0) == ' ');

    caca_put_char(cv, 0, 0, 'y');
    TEST(caca_get_char(cv, 0, 0) == 'y');

    caca_set_canvas_size(cv, 1000, 1000);
    TEST(caca_get_canvas_width(cv) == 1000);

    caca_put_char(cv, 999, 999, 'z');
    TEST(caca_get_char(cv, 999, 999) == 'z');

    caca_free_canvas(cv);

    fprintf(stderr, "%i tests, %i errors\n", tests, tests - passed);

    return 0;
}
Exemplo n.º 4
0
int init() {
    initMillis () ;
    initKeyboard();

    pthread_t k_t;
    pthread_create(&k_t, NULL, &keyboard_thread, NULL);

    printf("init ");
    sprintf(pt.density, " .',-+:;=o&%%/$*W@#");

    pt.C_stroke_r = 255;
    pt.C_stroke_g = 255;
    pt.C_stroke_b = 255;

    pt.C_color = 0;
    pt.C_pixel = '.';
    pt.running = 1;
    pt.PT_paused = 0;
    pt.PT_keyblocked = 0;

 

    pt.PT_bitmap_width  = 120;
    pt.PT_bitmap_height = 80;

    ellipseMode(CENTER);
    pt.cv = caca_create_canvas(0, 0);
    if (!pt.cv) {
        fprintf(stderr, "unable to initialise libcaca\n");
        return 1;
    }

    pt.dp = caca_create_display_with_driver(pt.cv, "ncurses"); // x11, raw..
    if (pt.dp == NULL ) {
        printf("Failed to create display\n");
        return 1;
    }
    if (!pt.PT_USE_DITHERING) {
        width = caca_get_canvas_width(pt.cv) / X_SCALE;
        height = caca_get_canvas_height(pt.cv);
    } else {
        width = pt.PT_bitmap_width;
        height = pt.PT_bitmap_height;

    }
   // caca_set_display_time(pt.dp,40000);
    useDithering();
    stroke(255);
    setDitherResolution(caca_get_canvas_width(pt.cv), caca_get_canvas_height(pt.cv));
    background(0);
    printf("done ");

    return 0;
}
Exemplo n.º 5
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv, *sprite;
    caca_display_t *dp;

    cv = caca_create_canvas(80, 24);
    if(cv == NULL)
    {
        printf("Failed to create canvas\n");
        return 1;
    }

    dp = caca_create_display(cv);
    if(dp == NULL)
    {
        printf("Failed to create display\n");
        return 1;
    }

    sprite = caca_create_canvas(0, 0);
    caca_set_color_ansi(sprite, CACA_LIGHTRED, CACA_BLACK);
    caca_import_canvas_from_memory(sprite, pig, strlen(pig), "text");
    caca_set_canvas_handle(sprite, caca_get_canvas_width(sprite) / 2,
                           caca_get_canvas_height(sprite) / 2);

    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
    caca_put_str(cv, 0, 0, "Centered sprite");

    caca_blit(cv, caca_get_canvas_width(cv) / 2,
              caca_get_canvas_height(cv) / 2, sprite, NULL);

    caca_refresh_display(dp);

    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);

    caca_free_display(dp);
    caca_free_canvas(sprite);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 6
0
static int flush_tiny(context_t *cx)
{
    cx->torender = cx->cv;
    caca_set_canvas_size(cx->torender, cx->w, cx->h);

    cx->ew = 16;
    cx->eh = 2;
    cx->x = cx->y = 0;
    cx->w = cx->h = 0;
    cx->cv = caca_create_canvas(cx->ew, cx->eh);

    return 0;
}
Exemplo n.º 7
0
cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buf, char const *format)
{
    caca_canvas_t *cv = caca_create_canvas(0, 0);
    int ret = caca_import_canvas_from_memory(cv,
                                             (unsigned char const *)buf->data,
                                             buf->size, format);
    if(ret < 0)
    {
        caca_free_canvas(cv);
        return NULL;
    }

    return cv;
}
Exemplo n.º 8
0
void
_mod_caca_plasma_anim (lw6sys_context_t * sys_context, _mod_caca_context_t * caca_context)
{
  if (frame != 0)
    {
      _increment_frame (sys_context, caca_context, NULL, NULL, NULL);
      return;
    }
  int wc, hc;
  caca_canvas_t *cv = NULL;
  caca_font_t *f = NULL;
  caca_dither_t *d = NULL;
  uint8_t *buf = NULL;
  char const *const *fonts;

  cv = caca_create_canvas (10, 1);
  if (cv == NULL)
    {
      lw6sys_log (sys_context, LW6SYS_LOG_ERROR, _x_ ("Can't create tmp canvas"));
      return;
    }
  hc = caca_get_canvas_height (caca_context->canvas);
  wc = caca_get_canvas_width (caca_context->canvas);
  _plasma (sys_context, PREPARE, caca_context->canvas);
  _plasma (sys_context, INIT, caca_context->canvas);

  caca_set_color_ansi (cv, CACA_BLACK, CACA_TRANSPARENT);
  caca_put_str (cv, 0, 0, "LiquidWar6");
  fonts = caca_get_font_list ();
  if (fonts[0] == NULL || fonts[1] == NULL)
    {
      lw6sys_log (sys_context, LW6SYS_LOG_ERROR, _x_ ("libcaca was compiled without any fonts"));
      return;
    }
  f = caca_load_font (fonts[1], 0);
  if (f == NULL)
    {
      lw6sys_log (sys_context, LW6SYS_LOG_ERROR, _x_ ("could not load font : '%s'"), fonts[0]);
      return;
    }
  buf = malloc (4 * wc * hc);
  if (buf == NULL)
    return;
  caca_render_canvas (cv, f, buf, wc, hc, 4 * wc);
  d = caca_create_dither (32, wc, hc, 4 * wc, 0xff00, 0xff0000, 0xff000000, 0xff);
  _increment_frame (sys_context, caca_context, f, d, buf);
  _mod_caca_splash_free (sys_context, caca_context, f, d, buf, 1);
  caca_free_canvas (cv);
}
Exemplo n.º 9
0
static void update_figfont_settings(caca_canvas_t *cv)
{
    caca_charfont_t *ff = cv->ff;

    if (!cv->ff)
        return;

    /* from TOIlet’s figlet.c */
    if (ff->full_layout & 0x3f)
        ff->hsmushrule = ff->full_layout & 0x3f;
    else if (ff->old_layout > 0)
        ff->hsmushrule = ff->old_layout;

    switch (ff->hmode)
    {
    case H_DEFAULT:
        if (ff->old_layout == -1)
            ff->hmode = H_NONE;
        else if (ff->old_layout == 0 && (ff->full_layout & 0xc0) == 0x40)
            ff->hmode = H_KERN;
        else if ((ff->old_layout & 0x3f) && (ff->full_layout & 0x3f)
                 && (ff->full_layout & 0x80))
        {
            ff->hmode = H_SMUSH;
            ff->hsmushrule = ff->full_layout & 0x3f;
        }
        else if (ff->old_layout == 0 && (ff->full_layout & 0xbf) == 0x80)
        {
            ff->hmode = H_SMUSH;
            ff->hsmushrule = 0x3f;
        }
        else
            ff->hmode = H_OVERLAP;
        break;
    default:
        break;
    }

    if (ff->charcv)
        caca_free_canvas(ff->charcv);
    ff->charcv = caca_create_canvas(ff->max_length - 2, ff->height);

    free(ff->left);
    free(ff->right);
    ff->left = malloc(ff->height * sizeof(int));
    ff->right = malloc(ff->height * sizeof(int));
}
Exemplo n.º 10
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv;
    caca_display_t *dp;

    int x, y;

    cv = caca_create_canvas(32, 16);
    if(cv == NULL)
    {
        printf("Failed to create canvas\n");
        return 1;
    }

    dp = caca_create_display(cv);
    if(dp == NULL)
    {
        printf("Failed to create display\n");
        return 1;
    }

    for(y = 0; y < 16; y++)
        for(x = 0; x < 16; x++)
    {
        uint16_t bgcolor = 0xff00 | (y << 4) | x;
        uint16_t fgcolor = 0xf000 | ((15 - y) << 4) | ((15 - x) << 8);

        caca_set_color_argb(cv, fgcolor, bgcolor);
        caca_put_str(cv, x * 2, y, "CA");
    }

    caca_set_color_ansi(cv, CACA_WHITE, CACA_LIGHTBLUE);
    caca_put_str(cv, 2, 1, " truecolor libcaca ");

    caca_refresh_display(dp);

    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);

    caca_free_display(dp);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 11
0
static int preinit(struct vo *vo)
{
    canvas = caca_create_canvas(0, 0);
    if (canvas == NULL) {
        mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to create canvas\n");
        return ENOSYS;
    }

    display = caca_create_display(canvas);

    if (display == NULL) {
        mp_msg(MSGT_VO, MSGL_ERR, "vo_caca: failed to create display\n");
        caca_free_canvas(canvas);
        return ENOSYS;
    }

    caca_set_display_title(display, "mpv");

    return 0;
}
Exemplo n.º 12
0
/** \brief Attach a specific caca graphical context to a caca canvas.
 *
 *  Create a graphical context using device-dependent features (ncurses for
 *  terminals, an X11 window, a DOS command window...) that attaches to a
 *  libcaca canvas. Everything that gets drawn in the libcaca canvas can
 *  then be displayed by the libcaca driver.
 *
 *  If no caca canvas is provided, a new one is created. Its handle can be
 *  retrieved using caca_get_canvas() and it is automatically destroyed when
 *  caca_free_display() is called.
 *
 *  If no driver name is provided, \e libcaca will try to autodetect the best
 *  output driver it can.
 *
 *  See also caca_create_display().
 *
 *  If an error occurs, NULL is returned and \b errno is set accordingly:
 *  - \c ENOMEM Not enough memory.
 *  - \c ENODEV Graphical device could not be initialised.
 *
 *  \param cv The caca canvas or NULL to create a canvas automatically.
 *  \param driver A string describing the desired output driver or NULL to
 *                choose the best driver automatically.
 *  \return The caca graphical context or NULL if an error occurred.
 */
caca_display_t * caca_create_display_with_driver(caca_canvas_t *cv,
                                                 char const *driver)
{
    caca_display_t *dp = malloc(sizeof(caca_display_t));

    if(!dp)
    {
        seterrno(ENOMEM);
        return NULL;
    }

    if((dp->autorelease = (cv == NULL)))
    {
        cv = caca_create_canvas(0, 0);
    }

    dp->cv = cv;

    if(caca_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp))
    {
        if(dp->autorelease)
            caca_free_canvas(dp->cv);
        free(dp);
        seterrno(EBUSY);
        return NULL;
    }

    if(caca_install_driver(dp, driver))
    {
        caca_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp);
        if(dp->autorelease)
            caca_free_canvas(dp->cv);
        free(dp);
        seterrno(ENODEV);
        return NULL;
    }

    return dp;
}
Exemplo n.º 13
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv;
    caca_display_t *dp;
    caca_font_t *f;
    caca_dither_t *d;
    uint8_t *buf;
    unsigned int w, h;
    char const * const * fonts;

    /* Create a canvas */
    cv = caca_create_canvas(8, 2);
    if(cv == NULL)
    {
        printf("Can't create canvas\n");
        return -1;
    }


    /* Draw stuff on our canvas */
    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
    caca_put_str(cv, 0, 0, "ABcde");
    caca_set_color_ansi(cv, CACA_LIGHTRED, CACA_BLACK);
    caca_put_str(cv, 5, 0, "\\o/");
    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
    caca_put_str(cv, 0, 1, "&$âøÿØ?!");

    /* Load a libcaca internal font */
    fonts = caca_get_font_list();
    if(fonts[0] == NULL)
    {
        fprintf(stderr, "error: libcaca was compiled without any fonts\n");
        return -1;
    }
    f = caca_load_font(fonts[0], 0);
    if(f == NULL)
    {
        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
        return -1;
    }

    /* Create our bitmap buffer (32-bit ARGB) */
    w = caca_get_canvas_width(cv) * caca_get_font_width(f);
    h = caca_get_canvas_height(cv) * caca_get_font_height(f);
    buf = malloc(4 * w * h);

    /* Render the canvas onto our image buffer */
    caca_render_canvas(cv, f, buf, w, h, 4 * w);

    /* Just for fun, render the image using libcaca */
    caca_set_canvas_size(cv, 80, 32);
    dp = caca_create_display(cv);

    {
#if defined(HAVE_ENDIAN_H)
        if(__BYTE_ORDER == __BIG_ENDIAN)
#else
        /* This is compile-time optimised with at least -O1 or -Os */
        uint32_t const tmp = 0x12345678;
        if(*(uint8_t const *)&tmp == 0x12)
#endif
            d = caca_create_dither(32, w, h, 4 * w,
                                    0xff0000, 0xff00, 0xff, 0xff000000);
        else
            d = caca_create_dither(32, w, h, 4 * w,
                                    0xff00, 0xff0000, 0xff000000, 0xff);
    }

    caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv),
                                  caca_get_canvas_height(cv), d, buf);
    caca_refresh_display(dp);

    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);

    /* Free everything */
    caca_free_display(dp);
    free(buf);
    caca_free_dither(d);
    caca_free_font(f);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 14
0
JNIEXPORT jlong JNICALL
Java_org_zoy_caca_Canvas_createCanvas(JNIEnv *env, jclass cls, jint w, jint h)
{
    return (jlong)caca_create_canvas(w, h);
}
Exemplo n.º 15
0
int main(int argc, char **argv)
{
    int refresh = 1, file = 1;
    unsigned int iw = 0, ih = 0;

    if(argc < 2)
    {
        fprintf(stderr, "%s: missing argument (filename).\n", argv[0]);
        return 1;
    }

    cv = caca_create_canvas(0, 0);
    if(!cv)
        return 1;
    dp = caca_create_display(cv);
    if(!dp)
        return 1;

    for(;;)
    {
        caca_event_t ev;
        unsigned int w, h;
        int dx = 0, dy = 0;

        if(!image)
        {
            image = caca_create_canvas(0, 0);
            if(caca_import_canvas_from_file(image, argv[file], "ansi") < 0)
            {
                fprintf(stderr, "%s: invalid file `%s'.\n", argv[0], argv[1]);
                return 1;
            }

            ih = caca_get_canvas_height(image);
            iw = caca_get_canvas_width(image);
            x = y = 0;

            caca_set_display_title(dp, argv[file]);

            refresh = 1;
        }

        if(refresh)
        {
            refresh_screen();
            refresh = 0;
        }

        while(caca_get_event(dp, CACA_EVENT_ANY, &ev, -1))
        {
            switch(caca_get_event_type(&ev))
            {
                case CACA_EVENT_QUIT:
                    goto quit;
                case CACA_EVENT_KEY_PRESS:
                    switch(caca_get_event_key_ch(&ev))
                    {
                    case CACA_KEY_LEFT: dx -= 2; break;
                    case CACA_KEY_RIGHT: dx += 2; break;
                    case CACA_KEY_UP: dy--; break;
                    case CACA_KEY_DOWN: dy++; break;
                    case CACA_KEY_PAGEUP: dy -= 12; break;
                    case CACA_KEY_PAGEDOWN: dy += 12; break;
                    case CACA_KEY_ESCAPE:
                    case 'q':
                        goto quit;
                    case 'n':
                        file = file + 1 < argc ? file + 1 : 1;
                        caca_free_canvas(image);
                        image = NULL;
                        goto stopevents;
                    case 'p':
                        file = file > 1 ? file - 1 : argc - 1;
                        caca_free_canvas(image);
                        image = NULL;
                        goto stopevents;
                    default:
                        break;
                    }
                case CACA_EVENT_RESIZE:
                    refresh = 1;
                    goto stopevents;
                default:
                    break;
            }
        }
stopevents:

        w = caca_get_canvas_width(cv);
        h = caca_get_canvas_height(cv);

        if(dx | dy)
        {
            refresh = 1;
            x += dx;
            y += dy;

            if(x < 0) x = 0; else if(x + w > iw) x = iw > w ? iw - w : 0;
            if(y < 0) y = 0; else if(y + h > ih) y = ih > h ? ih - h : 0;
        }
    }
quit:

    /* Clean up */
    caca_free_display(dp);
    caca_free_canvas(image);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 16
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv;
    caca_dither_t *dither;
    void *buffer;
    char *file, *format;
    char const * const * exports, * const * p;
    size_t len;
    int x, y;

    exports = caca_get_export_list();

    if(argc < 2 || argc > 3)
    {
        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
        fprintf(stderr, "usage: %s [file] <format>\n", argv[0]);
        fprintf(stderr, "where <format> is one of:\n");
        for(p = exports; *p; p += 2)
            fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1));
        exit(-1);
    }

    if(argc == 2)
    {
        file = NULL;
        format = argv[1];
    }
    else
    {
        file = argv[1];
        format = argv[2];
    }

    for(p = exports; *p; p += 2)
        if(!strcasecmp(format, *p))
            break;

    if(!*p)
    {
        fprintf(stderr, "%s: unknown format `%s'\n", argv[0], format);
        fprintf(stderr, "please use one of:\n");
        for(p = exports; *p; p += 2)
            fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1));
        exit(-1);
    }

    if(file)
    {
        cv = caca_create_canvas(0, 0);
        if(caca_import_canvas_from_file(cv, file, "") < 0)
        {
            fprintf(stderr, "%s: `%s' has unknown format\n", argv[0], file);
            exit(-1);
        }
    }
    else
    {
        cv = caca_create_canvas(WIDTH, HEIGHT);

        for(y = 0; y < 256; y++)
        {
            for(x = 0; x < 256; x++)
            {
                uint32_t r = x;
                uint32_t g = (255 - y + x) / 2;
                uint32_t b = y * (255 - x) / 256;
                pixels[y * 256 + x] = (r << 16) | (g << 8) | (b << 0);
            }
        }

        dither = caca_create_dither(32, 256, 256, 4 * 256,
                                     0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
        if(!strcmp(format, "ansi") || !strcmp(format, "utf8"))
            caca_set_dither_charset(dither, "shades");
        caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv),
                            caca_get_canvas_height(cv), dither, pixels);
        caca_free_dither(dither);

        caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
        caca_draw_thin_box(cv, 0, 0, WIDTH - 1, HEIGHT - 1);

        caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
        caca_fill_ellipse(cv, WIDTH / 2, HEIGHT / 2,
                               WIDTH / 4, HEIGHT / 4, ' ');

        caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
        caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 6,
                      "   lightgray on black   ");
        caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
        caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 5,
                      " default on transparent ");
        caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
        caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 4,
                      "     black on white     ");

        caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
        caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]");
        caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]");
        caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ");
        caca_put_str(cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>");

        caca_set_attr(cv, CACA_BOLD);
        caca_put_str(cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold");
        caca_set_attr(cv, CACA_BLINK);
        caca_put_str(cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink");
        caca_set_attr(cv, CACA_ITALICS);
        caca_put_str(cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics");
        caca_set_attr(cv, CACA_UNDERLINE);
        caca_put_str(cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline");
        caca_set_attr(cv, 0);

        caca_set_color_ansi(cv, CACA_WHITE, CACA_LIGHTBLUE);
        caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2, "    LIBCACA    ");

        for(x = 0; x < 16; x++)
        {
            caca_set_color_argb(cv, 0xff00 | x, 0xf00f | (x << 4));
            caca_put_char(cv, WIDTH / 2 - 7 + x, HEIGHT / 2 + 6, '#');
        }
    }

    buffer = caca_export_canvas_to_memory(cv, format, &len);
    fwrite(buffer, len, 1, stdout);
    free(buffer);

    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 17
0
int main(void)
{
  caca_canvas_t *cv;
  caca_display_t *dp;
  caca_event_t ev;

  // Create canvas and display
  cv = caca_create_canvas(100,80);
  if(!cv) return 1;

  dp = caca_create_display_with_driver(cv, "ncurses");
  if(!dp) return 1;

  caca_set_display_title(dp, "WPC 66");
  caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);

  // Tree stump
  caca_fill_box (cv, 47, 50, 6, 4, '|');

  // Tree body
  caca_draw_thin_triangle (cv,50,10,10,50,90,50);

  // Pot
  caca_fill_triangle(cv, 35, 53, 40, 60, 40, 53, '@');
  caca_fill_box(cv, 40, 53, 19, 8, '@');
  caca_fill_triangle(cv, 59, 60, 64, 53, 59, 53, '@');

  // Balls
  int balls[] = {50, 25, 35, 35, 25, 43, 58, 32, 68, 41, 50, 46};
  for (int i = 0; i <= 10; i += 2)
      caca_draw_thin_ellipse(cv, balls[i], balls[i+1], 2, 2);

  // Sparkly stuff
  int sparks[] = {54, 20, 42, 30, 40, 45, 46, 38, 60, 43, 75, 48};
  for (int i = 0; i <= 10; i += 2)
  {
    caca_put_char(cv, sparks[i], sparks[i+1], '*');
    caca_put_char(cv, sparks[i]-1, sparks[i+1], '<');
    caca_put_char(cv, sparks[i]+1, sparks[i+1], '>');
    caca_put_char(cv, sparks[i], sparks[i+1]-1, '^');
    caca_put_char(cv, sparks[i], sparks[i+1]+1, 'v');
  }

  // Guirlandes
  caca_draw_thin_line(cv, 32, 44, 70, 35);
  caca_draw_thin_line(cv, 17, 49, 30, 46);
  caca_draw_thin_line(cv, 60, 47, 80, 43);
  caca_draw_thin_line(cv, 43, 35, 52, 33);
  caca_draw_thin_line(cv, 55, 28, 60, 27);
  caca_draw_thin_line(cv, 40, 25, 45, 24);

  // Star outline
  caca_fill_triangle(cv, 50, 16, 40, 10, 60, 10, ' ');
  int star_x[] = {50, 45, 40, 45, 40, 45, 50, 55, 60, 55, 60, 55, 50};
  int star_y[] = { 4,  7,  7, 10, 13, 13, 16, 13, 13, 10,  7,  7,  4};
  caca_draw_thin_polyline(cv, star_x, star_y, 12);

  // Inside of star
  int star[] = {50,8,50,9,50,10,50,11,50,12,49,9,48,9,51,9,52,9,49,10,51,10,49,11,48,11,51,11,52,11};
  for (int i = 0; i <= 29; i += 2)
      caca_put_char(cv, star[i], star[i+1], '*');

  // Display and wait for key press
  caca_refresh_display(dp);
  caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1);
  caca_free_display(dp);
  return 0;
}
Exemplo n.º 18
0
int main(int argc, char **argv){
	SDL_AudioSpec requested, obtained;

	int quit = 0;
	int xo, yo;
	int i, j, k;
	int meter[4];

	static char chars[10] =
	{
			'+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
	};

	caca_display_t *dp;
	caca_canvas_t *cv;
	caca_canvas_t *pineapple;

	if(SDL_Init( SDL_INIT_AUDIO ) < 0){
		err(1, "Couldnt initialize SDL\n");
		exit(1);
	}

	cv = caca_create_canvas(80, 24);
	pineapple = caca_create_canvas(0, 0);
	if((cv == NULL) || (pineapple == NULL)){
		printf("failed to create canvas\n");
		return 1;
	}
	dp = caca_create_display(cv);
	caca_set_display_time(dp, 20000);
	if(dp == NULL){
		printf("Failed to create display\n");
		return 1;
	}

	caca_import_file(pineapple, "./pineapple", "");

	atexit(SDL_Quit);

	requested.freq = 16000;
	requested.format = AUDIO_U8;
	requested.samples = 256;
	requested.callback = audiocb;
	requested.channels = 1;

	if(SDL_OpenAudio(&requested, &obtained) == -1){
		err(1, "SDL_OpenAudio");
	}

	initchip();

	loadfile(argv[1]);

	SDL_PauseAudio(0);
	silence();
	startplaysong(0);


	while(!quit)
	{
		caca_event_t ev;
		caca_set_color_ansi(cv, CACA_DEFAULT, CACA_DEFAULT);
		caca_clear_canvas(cv);
		xo = caca_get_canvas_width(cv);
		yo = caca_get_canvas_height(cv);
		//caca_blit(cv, 0, 0, pineapple, NULL);
		caca_blit(cv, 55, 0, pineapple, NULL);
		caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16));
		caca_put_str(cv, (xo - strlen("pineapple player")) / 2, (yo / 2) - 5, "pineapple player");
		caca_set_color_ansi(cv, caca_rand(0, 16), caca_rand(0, 16));
		caca_printf(cv, (xo - strlen("song pos ->   ")) / 2, (yo / 2) - 3, "song pos -> %x", songpos);
		
		for(i = 0; i < 4; i ++)
			meter[i] = (osc[i].volume*20)/255;
		/* note visualizer */
		i = 0;
		for(j = 0; j < 25; j=j+6){
				for(k = 0; k < 4; k++){
				caca_draw_line(cv, (((xo/2)+10)-j)-k, yo, (((xo/2)+10)-j)-k, yo - meter[i], 
					chars[caca_rand(0, 9)]);
				}
			i++;
		}

		for(i = 0; i < 4; i ++)
			caca_printf(cv, 0, i, "%0x", osc[i].volume);

    while(caca_get_event(dp, CACA_EVENT_ANY, &ev, 0))
    {
    	if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
    	{
				switch(caca_get_event_key_ch(&ev))
				{
					case 'q':
					case 'Q':
					case CACA_KEY_ESCAPE:
						quit = 1;
						break;
				}
			}
		}
		caca_refresh_display(dp);
	}
	silence();
	caca_free_display(dp);
	caca_free_canvas(cv);
	return 0;
}
Exemplo n.º 19
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv;
    caca_display_t *dp;

    cv = caca_create_canvas(0, 0);
    if(cv == NULL)
    {
        printf("Can't created canvas\n");
        return -1;
    }
    dp = caca_create_display(cv);
    if(dp == NULL)
    {
        printf("Can't create display\n");
        return -1;
    }
    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
    caca_put_str(cv, 1, 1, "Basic Unicode support");

    caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
    caca_put_str(cv, 1, 2, "This is ASCII:    | abc DEF 123 !@# |");
    caca_put_str(cv, 1, 3, "This is Unicode:  | äßç δεφ ☺♥♀ ╞╬╗ |");
    caca_put_str(cv, 1, 4, "And this is, too: | ἀβϛ ΔЗҒ ᚴᛒᛯ ♩♔✈ |");
    caca_put_str(cv, 41, 4, "Size test: 018adxmygWX'_ÍçÕĔŷ ﻙ が本");

    caca_put_str(cv, 1, 5, "If the three lines do not have the same length, there is a bug somewhere.");

    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
    caca_put_str(cv, 1, 7, "Gradient glyphs");

    caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
    caca_put_str(cv, 31,  8, "  0%");
    caca_put_str(cv, 31,  9, " 25%");
    caca_put_str(cv, 31, 10, " 50%");
    caca_put_str(cv, 31, 11, " 75%");
    caca_put_str(cv, 31, 12, "100%");

    caca_set_color_ansi(cv, CACA_LIGHTRED, CACA_LIGHTGREEN);
    caca_put_str(cv, 1,  8, "                             ");
    caca_put_str(cv, 1,  9, "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░");
    caca_put_str(cv, 1, 10, "▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒");
    caca_put_str(cv, 1, 11, "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓");
    caca_put_str(cv, 1, 12, "█████████████████████████████");

    caca_set_color_ansi(cv, CACA_LIGHTGREEN, CACA_LIGHTRED);
    caca_put_str(cv, 36,  8, "█████████████████████████████");
    caca_put_str(cv, 36,  9, "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓");
    caca_put_str(cv, 36, 10, "▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒");
    caca_put_str(cv, 36, 11, "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░");
    caca_put_str(cv, 36, 12, "                             ");

    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
    caca_put_str(cv, 1, 14, "Double width characters");

    caca_set_color_ansi(cv, CACA_LIGHTRED, CACA_TRANSPARENT);
    caca_put_str(cv, 1, 15, "| ドラゴン ボーレ |");
    caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
    caca_put_str(cv, 1, 16, "| ()()()() ()()() |");
    caca_set_color_ansi(cv, CACA_YELLOW, CACA_TRANSPARENT);
    caca_put_str(cv, 1, 17, "| ドラゴン");
    caca_put_str(cv, 12, 17, "ボーレ |");

    caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
    caca_put_str(cv, 1, 18, "If the three lines do not have the same length, there is a bug somewhere.");

    caca_put_str(cv, 1, 20, "CP437 glyphs: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼");
    caca_put_str(cv, 1, 21, "more CP437: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■");
    caca_put_str(cv, 1, 22, "drawing blocks: ███ ▓▓▓ ▒▒▒ ░░░ ▀ ▄ ▌ ▐ █ ▖ ▗ ▘ ▝ ▚ ▞ ▙ ▛ ▜ ▟ ─ │ ┌ ┐ └ ┘ ├ ┤");
    caca_put_str(cv, 1, 23, "more drawing: ┬ ┴ ┼ ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬");
    caca_put_str(cv, 1, 24, "misc Unicode: ● ☭ ☮ ☯ ♔ ♛ ♙ ♞ ⚒ ⚓ ⚠");

    caca_refresh_display(dp);

    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);

    caca_free_display(dp);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 20
0
int main(int argc, char **argv)
{
    caca_event_t *events;
    int i, h, quit;

    cv = caca_create_canvas(80, 24);
    if(cv == NULL)
    {
        printf("Failed to create canvas\n");
        return 1;
    }

    dp = caca_create_display(cv);
    if(dp == NULL)
    {
        printf("Failed to create display\n");
        return 1;
    }

    h = caca_get_canvas_height(cv) - 1;

    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
    caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' ');

    caca_draw_line(cv, 0, h, caca_get_canvas_width(cv) - 1, h, ' ');
    caca_put_str(cv, 0, h, "type \"quit\" to exit");

    caca_refresh_display(dp);

    events = malloc(h * sizeof(caca_event_t));
    memset(events, 0, h * sizeof(caca_event_t));

    for(quit = 0; quit < 4; )
    {
        caca_event_t ev;
        static char const * quit_string[] = { "", "q", "qu", "qui", "quit" };
        int ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, -1);

        if(!ret)
            continue;

        do
        {
            /* "quit" quits */
            if(caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
            {
                int key = caca_get_event_key_ch(&ev);
                if((key == 'q' && quit == 0) || (key == 'u' && quit == 1)
                    || (key == 'i' && quit == 2) || (key == 't' && quit == 3))
                    quit++;
                else if(key == 'q')
                    quit = 1;
                else
                    quit = 0;
            }

            memmove(events + 1, events, (h - 1) * sizeof(caca_event_t));
            events[0] = ev;

            ret = caca_get_event(dp, CACA_EVENT_ANY, &ev, 0);
        }
        while(ret);

        caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
        caca_clear_canvas(cv);

        /* Print current event */
        caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
        caca_draw_line(cv, 0, 0, caca_get_canvas_width(cv) - 1, 0, ' ');
        print_event(0, 0, events);

        caca_draw_line(cv, 0, h, caca_get_canvas_width(cv) - 1, h, ' ');
        caca_printf(cv, 0, h, "type \"quit\" to exit: %s", quit_string[quit]);

        /* Print previous events */
        caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
        for(i = 1; i < h && caca_get_event_type(&events[i]); i++)
            print_event(0, i, events + i);

        caca_refresh_display(dp);
    }

    /* Clean up */
    free(events);
    caca_free_display(dp);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 21
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv, *image, *tmp, *sprite;
    caca_display_t *dp;

    cv = caca_create_canvas(0, 0);
    if(cv == NULL)
    {
        printf("Can't created canvas\n");
        return -1;
    }
    dp = caca_create_display(cv);
    if(dp == NULL)
    {
        printf("Can't create display\n");
        return -1;
    }

    image = caca_create_canvas(70, 6);
    tmp = caca_create_canvas(70, 6);
    sprite = caca_create_canvas(0, 0);

    caca_set_color_ansi(sprite, CACA_LIGHTMAGENTA, CACA_BLACK);
    caca_import_canvas_from_memory(sprite, pig, strlen(pig), "text");
    caca_blit(image, 55, 0, sprite, NULL);

    caca_set_color_ansi(sprite, CACA_LIGHTGREEN, CACA_BLACK);
    caca_import_canvas_from_memory(sprite, duck, strlen(duck), "text");
    caca_blit(image, 30, 1, sprite, NULL);

    caca_set_color_ansi(image, CACA_LIGHTCYAN, CACA_BLACK);
    caca_put_str(image, 1, 1, "hahaha mais vieux porc immonde !! [⽼ ⾗]");
    caca_set_color_ansi(image, CACA_LIGHTRED, CACA_BLACK);
    caca_put_char(image, 38, 1, '|');

    caca_set_color_ansi(image, CACA_YELLOW, CACA_BLACK);
    caca_put_str(image, 4, 2, "\\o\\ \\o| _o/ \\o_ |o/ /o/");

    caca_set_color_ansi(image, CACA_WHITE, CACA_LIGHTRED);
    caca_put_str(image, 7, 3, "▙▘▌▙▘▞▖▞▖▌ ▞▖▌ ▌▌");
    caca_put_str(image, 7, 4, "▛▖▌▛▖▚▘▚▘▚▖▚▘▚▖▖▖");
    caca_set_color_ansi(image, CACA_BLACK, CACA_LIGHTRED);
    caca_put_str(image, 4, 3, "▓▒░");
    caca_put_str(image, 4, 4, "▓▒░");
    caca_put_str(image, 24, 3, "░▒▓");
    caca_put_str(image, 24, 4, "░▒▓");

    /* Blit the transformed canvas onto the main canvas */
    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
    caca_put_str(cv, 0, 0, "normal");
    caca_blit(cv, 10, 0, image, NULL);

    caca_put_str(cv, 0, 6, "flip");
    caca_blit(tmp, 0, 0, image, NULL);
    caca_flip(tmp);
    caca_blit(cv, 10, 6, tmp, NULL);

    caca_put_str(cv, 0, 12, "flop");
    caca_blit(tmp, 0, 0, image, NULL);
    caca_flop(tmp);
    caca_blit(cv, 10, 12, tmp, NULL);

    caca_put_str(cv, 0, 18, "rotate");
    caca_blit(tmp, 0, 0, image, NULL);
    caca_rotate_180(tmp);
    caca_blit(cv, 10, 18, tmp, NULL);

    caca_refresh_display(dp);

    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);

    caca_free_display(dp);
    caca_free_canvas(tmp);
    caca_free_canvas(sprite);
    caca_free_canvas(image);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 22
0
static caca_charfont_t * open_charfont(char const *path)
{
    char buf[2048];
    char hardblank[10];
    caca_charfont_t *ff;
    char *data = NULL;
    caca_file_t *f;
#if !defined __KERNEL__ && (defined HAVE_SNPRINTF || defined HAVE_SPRINTF_S)
    int const pathlen = 2048;
    char *altpath = NULL;
#endif
    int i, j, size, comment_lines;

    ff = malloc(sizeof(caca_charfont_t));
    if(!ff)
    {
        seterrno(ENOMEM);
        return NULL;
    }

    /* Open font: if not found, try .tlf, then .flf */
    f = caca_file_open(path, "r");
#if !defined __KERNEL__ && (defined HAVE_SNPRINTF || defined HAVE_SPRINTF_S)
    if(!f)
        altpath = malloc(pathlen);
    if(!f)
    {
#if defined HAVE_SPRINTF_S
        sprintf_s(altpath, pathlen - 1, "%s.tlf", path);
#else
        snprintf(altpath, pathlen - 1, "%s.tlf", path);
#endif
        altpath[pathlen - 1] = '\0';
        f = caca_file_open(altpath, "r");
    }
    if(!f)
    {
#if defined HAVE_SPRINTF_S
        sprintf_s(altpath, pathlen - 1, "%s.flf", path);
#else
        snprintf(altpath, pathlen - 1, "%s.flf", path);
#endif
        altpath[pathlen - 1] = '\0';
        f = caca_file_open(altpath, "r");
    }
    if (altpath)
        free(altpath);
#endif
    if(!f)
    {
        free(ff);
        seterrno(ENOENT);
        return NULL;
    }

    /* Read header */
    ff->print_direction = 0;
    ff->full_layout = 0;
    ff->codetag_count = 0;
    caca_file_gets(f, buf, 2048);
    if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank,
              &ff->height, &ff->baseline, &ff->max_length,
              &ff->old_layout, &comment_lines, &ff->print_direction,
              &ff->full_layout, &ff->codetag_count) < 6)
    {
        debug("figfont error: `%s' has invalid header: %s", path, buf);
        caca_file_close(f);
        free(ff);
        seterrno(EINVAL);
        return NULL;
    }

    if(ff->old_layout < -1 || ff->old_layout > 63 || ff->full_layout > 32767
        || ((ff->full_layout & 0x80) && (ff->full_layout & 0x3f) == 0
            && ff->old_layout))
    {
        debug("figfont error: `%s' has invalid layout %i/%u",
                path, ff->old_layout, ff->full_layout);
        caca_file_close(f);
        free(ff);
        seterrno(EINVAL);
        return NULL;
    }

    ff->hardblank = caca_utf8_to_utf32(hardblank, NULL);

    /* Skip comment lines */
    for(i = 0; i < comment_lines; i++)
        caca_file_gets(f, buf, 2048);

    /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223)
     * then read additional characters. */
    ff->glyphs = 0;
    ff->lookup = NULL;

    for(i = 0, size = 0; !caca_file_eof(f); ff->glyphs++)
    {
        if((ff->glyphs % 2048) == 0)
            ff->lookup = realloc(ff->lookup,
                                   (ff->glyphs + 2048) * 2 * sizeof(int));

        if(ff->glyphs < STD_GLYPHS)
        {
            ff->lookup[ff->glyphs * 2] = 32 + ff->glyphs;
        }
        else if(ff->glyphs < EXT_GLYPHS)
        {
            static int const tab[7] = { 196, 214, 220, 228, 246, 252, 223 };
            ff->lookup[ff->glyphs * 2] = tab[ff->glyphs - STD_GLYPHS];
        }
        else
        {
            unsigned int tmp;

            if(caca_file_gets(f, buf, 2048) == NULL)
                break;

            /* Ignore blank lines, as in jacky.flf */
            if(buf[0] == '\n' || buf[0] == '\r')
                continue;

            /* Ignore negative indices for now, as in ivrit.flf */
            if(buf[0] == '-')
            {
                for(j = 0; j < ff->height; j++)
                    caca_file_gets(f, buf, 2048);
                continue;
            }

            if(!buf[0] || buf[0] < '0' || buf[0] > '9')
            {
                debug("figfont error: glyph #%u in `%s'", ff->glyphs, path);
                free(data);
                free(ff->lookup);
                free(ff);
                seterrno(EINVAL);
                return NULL;
            }

            sscanf(buf, buf[1] == 'x' ? "%x" : "%u", &tmp);
            ff->lookup[ff->glyphs * 2] = tmp;
        }

        ff->lookup[ff->glyphs * 2 + 1] = 0;

        for(j = 0; j < ff->height; j++)
        {
            if(i + 2048 >= size)
                data = realloc(data, size += 2048);

            caca_file_gets(f, data + i, 2048);
            i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data;
        }
    }

    caca_file_close(f);

    if(ff->glyphs < EXT_GLYPHS)
    {
        debug("figfont error: only %u glyphs in `%s', expected at least %u",
                        ff->glyphs, path, EXT_GLYPHS);
        free(data);
        free(ff->lookup);
        free(ff);
        seterrno(EINVAL);
        return NULL;
    }

    /* Remaining initialisation */
    ff->charcv = NULL;
    ff->left = NULL;
    ff->right = NULL;

    /* Import buffer into canvas */
    ff->fontcv = caca_create_canvas(0, 0);
    caca_import_canvas_from_memory(ff->fontcv, data, i, "utf8");
    free(data);

    /* Remove EOL characters. For now we ignore hardblanks, don’t do any
     * smushing, nor any kind of error checking. */
    for(j = 0; j < ff->height * ff->glyphs; j++)
    {
        uint32_t ch, oldch = 0;

        for(i = ff->max_length; i--;)
        {
            ch = caca_get_char(ff->fontcv, i, j);

            /* Replace hardblanks with U+00A0 NO-BREAK SPACE */
            if(ch == ff->hardblank)
                caca_put_char(ff->fontcv, i, j, ch = 0xa0);

            if(oldch && ch != oldch)
            {
                if(!ff->lookup[j / ff->height * 2 + 1])
                    ff->lookup[j / ff->height * 2 + 1] = i + 1;
            }
            else if(oldch && ch == oldch)
                caca_put_char(ff->fontcv, i, j, ' ');
            else if(ch != ' ')
            {
                oldch = ch;
                caca_put_char(ff->fontcv, i, j, ' ');
            }
        }
    }

    return ff;
}
Exemplo n.º 23
0
int main(int argc, char *argv[])
{
    textentry entries[TEXT_ENTRIES];
    caca_canvas_t *cv;
    caca_display_t *dp;
    unsigned int i, e = 0, running = 1;

    cv = caca_create_canvas(0, 0);
    if(cv == NULL)
    {
        printf("Can't create canvas\n");
        return -1;
    }
    dp = caca_create_display(cv);
    if(dp == NULL)
    {
        printf("Can't create display\n");
        return -1;
    }
    caca_set_cursor(dp, 1);

    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
    caca_put_str(cv, 1, 1, "Text entries - press tab to cycle");

    for(i = 0; i < TEXT_ENTRIES; i++)
    {
        entries[i].buffer[0] = 0;
        entries[i].size = 0;
        entries[i].cursor = 0;
        entries[i].changed = 1;
        caca_printf(cv, 3, 3 * i + 4, "[entry %i]", i + 1);
    }

    /* Put Unicode crap in the last text entry */
    entries[TEXT_ENTRIES - 1].buffer[0] = 'A';
    entries[TEXT_ENTRIES - 1].buffer[1] = 'b';
    entries[TEXT_ENTRIES - 1].buffer[2] = caca_utf8_to_utf32("Ç", NULL);
    entries[TEXT_ENTRIES - 1].buffer[3] = caca_utf8_to_utf32("đ", NULL);
    entries[TEXT_ENTRIES - 1].buffer[4] = caca_utf8_to_utf32("ボ", NULL);
    entries[TEXT_ENTRIES - 1].buffer[5] = CACA_MAGIC_FULLWIDTH;
    entries[TEXT_ENTRIES - 1].buffer[6] = caca_utf8_to_utf32("♥", NULL);
    entries[TEXT_ENTRIES - 1].size = 7;

    while(running)
    {
        caca_event_t ev;

        for(i = 0; i < TEXT_ENTRIES; i++)
        {
            unsigned int j, start, size;

            if(!entries[i].changed)
                continue;

            caca_set_color_ansi(cv, CACA_BLACK, CACA_LIGHTGRAY);
            caca_fill_box(cv, 2, 3 * i + 5, BUFFER_SIZE + 1, 1, ' ');

            start = 0;
            size = entries[i].size;

            for(j = 0; j < size; j++)
            {
                caca_put_char(cv, 2 + j, 3 * i + 5,
                              entries[i].buffer[start + j]);
            }

            entries[i].changed = 0;
        }

        /* Put the cursor on the active textentry */
        caca_gotoxy(cv, 2 + entries[e].cursor, 3 * e + 5);

        caca_refresh_display(dp);

        if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0)
            continue;

        switch(caca_get_event_key_ch(&ev))
        {
            case CACA_KEY_ESCAPE:
                running = 0;
                break;
            case CACA_KEY_TAB:
            case CACA_KEY_RETURN:
                e = (e + 1) % TEXT_ENTRIES;
                break;
            case CACA_KEY_HOME:
                entries[e].cursor = 0;
                break;
            case CACA_KEY_END:
                entries[e].cursor = entries[e].size;
                break;
            case CACA_KEY_LEFT:
                if(entries[e].cursor)
                    entries[e].cursor--;
                break;
            case CACA_KEY_RIGHT:
                if(entries[e].cursor < entries[e].size)
                    entries[e].cursor++;
                break;
            case CACA_KEY_DELETE:
                if(entries[e].cursor < entries[e].size)
                {
                    memmove(entries[e].buffer + entries[e].cursor,
                            entries[e].buffer + entries[e].cursor + 1,
                            (entries[e].size - entries[e].cursor + 1) * 4);
                    entries[e].size--;
                    entries[e].changed = 1;
                }
                break;
            case CACA_KEY_BACKSPACE:
                if(entries[e].cursor)
                {
                    memmove(entries[e].buffer + entries[e].cursor - 1,
                            entries[e].buffer + entries[e].cursor,
                            (entries[e].size - entries[e].cursor) * 4);
                    entries[e].size--;
                    entries[e].cursor--;
                    entries[e].changed = 1;
                }
                break;
            default:
                if(entries[e].size < BUFFER_SIZE)
                {
                    memmove(entries[e].buffer + entries[e].cursor + 1,
                            entries[e].buffer + entries[e].cursor,
                            (entries[e].size - entries[e].cursor) * 4);
                    entries[e].buffer[entries[e].cursor] =
                                              caca_get_event_key_utf32(&ev);
                    entries[e].size++;
                    entries[e].cursor++;
                    entries[e].changed = 1;
                }
                break;
        }
    }

    caca_free_display(dp);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 24
0
int main(int argc, char **argv)
{
    static caca_display_t *dp;
    static caca_canvas_t *frontcv, *backcv, *mask;

    int demo, next = -1, paused = 0, next_transition = DEMO_FRAMES;
    unsigned int i;
    int tmode = caca_rand(0, TRANSITION_COUNT);

    /* Set up two canvases, a mask, and attach a display to the front one */
    frontcv = caca_create_canvas(0, 0);
    backcv = caca_create_canvas(0, 0);
    mask = caca_create_canvas(0, 0);

    dp = caca_create_display(frontcv);
    if(!dp)
        return 1;

    caca_set_canvas_size(backcv, caca_get_canvas_width(frontcv),
                         caca_get_canvas_height(frontcv));
    caca_set_canvas_size(mask, caca_get_canvas_width(frontcv),
                         caca_get_canvas_height(frontcv));

    caca_set_display_time(dp, 20000);

    /* Initialise all demos' lookup tables */
    for(i = 0; i < DEMOS; i++)
        fn[i](PREPARE, frontcv);

    /* Choose a demo at random */
    demo = caca_rand(0, DEMOS);
    fn[demo](INIT, frontcv);

    for(;;)
    {
        /* Handle events */
        caca_event_t ev;
        while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
                             | CACA_EVENT_QUIT, &ev, 0))
        {
            if(caca_get_event_type(&ev) == CACA_EVENT_QUIT)
                goto end;

            switch(caca_get_event_key_ch(&ev))
            {
            case CACA_KEY_ESCAPE:
            case CACA_KEY_CTRL_C:
            case CACA_KEY_CTRL_Z:
                goto end;
            case ' ':
                paused = !paused;
                break;
            case '\r':
                if(next == -1)
                    next_transition = frame;
                break;
            }
        }

        /* Resize the spare canvas, just in case the main one changed */
        caca_set_canvas_size(backcv, caca_get_canvas_width(frontcv),
                             caca_get_canvas_height(frontcv));
        caca_set_canvas_size(mask, caca_get_canvas_width(frontcv),
                             caca_get_canvas_height(frontcv));

        if(paused)
            goto _paused;

        /* Update demo's data */
        fn[demo](UPDATE, frontcv);

        /* Handle transitions */
        if(frame == next_transition)
        {
            next = caca_rand(0, DEMOS);
            if(next == demo)
                next = (next + 1) % DEMOS;
            fn[next](INIT, backcv);
        }
        else if(frame == next_transition + TRANSITION_FRAMES)
        {
            fn[demo](FREE, frontcv);
            demo = next;
            next = -1;
            next_transition = frame + DEMO_FRAMES;
            tmode = caca_rand(0, TRANSITION_COUNT);
        }

        if(next != -1)
            fn[next](UPDATE, backcv);

        frame++;
_paused:
        /* Render main demo's canvas */
        fn[demo](RENDER, frontcv);

        /* If a transition is on its way, render it */
        if(next != -1)
        {
            fn[next](RENDER, backcv);
            caca_set_color_ansi(mask, CACA_LIGHTGRAY, CACA_BLACK);
            caca_clear_canvas(mask);
            caca_set_color_ansi(mask, CACA_WHITE, CACA_WHITE);
            transition(mask, tmode,
                       100 * (frame - next_transition) / TRANSITION_FRAMES);
            caca_blit(frontcv, 0, 0, backcv, mask);
        }

        caca_set_color_ansi(frontcv, CACA_WHITE, CACA_BLUE);
        if(frame < 100)
            caca_put_str(frontcv, caca_get_canvas_width(frontcv) - 30,
                         caca_get_canvas_height(frontcv) - 2,
                         " -=[ Powered by libcaca ]=- ");
        caca_refresh_display(dp);
    }
end:
    if(next != -1)
        fn[next](FREE, frontcv);
    fn[demo](FREE, frontcv);

    caca_free_display(dp);
    caca_free_canvas(mask);
    caca_free_canvas(backcv);
    caca_free_canvas(frontcv);

    return 0;
}
Exemplo n.º 25
0
int main(int argc, char *argv[])
{
    caca_canvas_t *cv, *caca, *line;
    caca_display_t *dp;

    unsigned int i;

    cv = caca_create_canvas(0, 0);
    if(cv == NULL)
    {
        printf("Can't created canvas\n");
        return -1;
    }
    dp = caca_create_display(cv);
    if(dp == NULL)
    {
        printf("Can't create display\n");
        return -1;
    }

    caca = caca_create_canvas(6, 10);
    line = caca_create_canvas(2, 1);

    /* Line of x's */
    for(i = 0; i < 10; i++)
    {
        caca_set_color_ansi(caca, CACA_WHITE, CACA_BLUE);
        caca_put_str(caca, 0, i, CACA);
        caca_set_color_ansi(caca, CACA_WHITE, CACA_RED);
        caca_put_char(caca, i - 2, i, 'x');
    }

    caca_blit(cv, 1, 1, caca, NULL);

    /* Line of ホ's */
    for(i = 0; i < 10; i++)
    {
        caca_set_color_ansi(caca, CACA_WHITE, CACA_BLUE);
        caca_put_str(caca, 0, i, CACA);
        caca_set_color_ansi(caca, CACA_WHITE, CACA_GREEN);
        caca_put_str(caca, i - 2, i, "ホ");
    }

    caca_blit(cv, 15, 1, caca, NULL);

    /* Line of canvas */
    caca_set_color_ansi(line, CACA_WHITE, CACA_MAGENTA);
    caca_put_str(line, 0, 0, "ほ");
    for(i = 0; i < 10; i++)
    {
        caca_set_color_ansi(caca, CACA_WHITE, CACA_BLUE);
        caca_put_str(caca, 0, i, CACA);
        caca_blit(caca, i - 2, i, line, NULL);
    }

    caca_blit(cv, 29, 1, caca, NULL);

    caca_refresh_display(dp);

    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);

    caca_free_display(dp);

    caca_free_canvas(line);
    caca_free_canvas(caca);
    caca_free_canvas(cv);

    return 0;
}
Exemplo n.º 26
0
int main(int argc, char *argv[]) {

	caca_canvas_t *cv;
	caca_canvas_t *figcv;
	caca_display_t *dp;
	uint32_t w, h, fw, fh;

	char *format = "%R:%S"; 
	char *font   = "/usr/share/figlet/mono12.tlf";


	for(;;)
	{
		int option_index = 0;
		static struct caca_option long_options[] =
		{
			{ "font",        1, NULL, 'f' },
			{ "dateformat",  1, NULL, 'd' },
			{ "help",        0, NULL, 'h' },
			{ "version",     0, NULL, 'v' },
		};
		int c = caca_getopt(argc, argv, "f:d:hv",
				long_options, &option_index);
		if(c == -1)
			break;

		switch(c)
		{
			case 'h': /* --help       */
				usage(argc, argv);
				return 0;
				break;
			case 'v': /* --version    */
				version();
				return 0;
				break;
			case 'f': /* --font       */
				font = caca_optarg;
				break;
			case 'd': /* --dateformat */
				format = caca_optarg;
				break;
			default:
				return 1;
				break;
		}
	}



	cv = caca_create_canvas(0, 0);
	figcv = caca_create_canvas(0, 0);
	if(!cv || !figcv)
	{  
		fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
		return 1;
	}

	if(caca_canvas_set_figfont(figcv, font))
	{  
		fprintf(stderr, "Could not open font\n");
		return -1;
	}


	dp = caca_create_display(cv);
	if(!dp) {
		printf("Can't open window. CACA_DRIVER problem ?\n");
		return -1;
	}

	caca_set_color_ansi(figcv, CACA_DEFAULT, CACA_DEFAULT);
	caca_clear_canvas(cv);
	for(;;) {
		caca_event_t ev;

		while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
					| CACA_EVENT_QUIT, &ev, 1))
		{  
			if(caca_get_event_type(&ev))
				goto end;
		}
		char *d = get_date(format);
		uint32_t o = 0;

		// figfont API is not complete, and does not allow us to put a string
		// at another position than 0,0
		// So, we have to create a canvas which will hold the figfont string,
		// then blit this canvas to the main one at the desired position.
		caca_clear_canvas(cv);
		caca_clear_canvas(figcv);
		while(d[o])
		{  
			caca_put_figchar(figcv, d[o++]);
		}
		caca_flush_figlet (figcv);
		free(d);

		w = caca_get_canvas_width (cv);
		h = caca_get_canvas_height(cv);
        fw = caca_get_canvas_width (figcv);
		fh = caca_get_canvas_height(figcv);	

		uint32_t x = (w/2) - (fw/2);
		uint32_t y = (h/2) - (fh/2);

		caca_blit(cv, x, y, figcv, NULL);
		caca_refresh_display(dp);
		usleep(250000);
	}
end:

	caca_free_canvas(figcv);
	caca_free_canvas(cv);
	caca_free_display(dp);

	return 0;
}
Exemplo n.º 27
0
int main(int argc, char *argv[])
{

    /* libcaca/libcaca contexts */
    caca_canvas_t *cv;
    caca_display_t *dp;
    caca_canvas_t *tex;

    /* cached canvas size */
    int ww, wh, tw, th;

    /* logic */
    int quit = 0;
    int update = 1;
    int px, py;
    float angle = 0;


    float square[6][2] = {
        {-SQUARE_SIZE, -SQUARE_SIZE},
        {SQUARE_SIZE, -SQUARE_SIZE},
        {SQUARE_SIZE, SQUARE_SIZE},
        {-SQUARE_SIZE, SQUARE_SIZE},
    };
    float uv1[6] = {
        0, 0,
        1, 0,
        1, 1
    };
    float uv2[6] = {
        0, 0,
        1, 1,
        0, 1
    };


    float rotated[4][2];
    int coords1[6], coords2[6];

    /* Create displayed canvas */
    cv = caca_create_canvas(0, 0);
    if (!cv)
    {
        fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
        return 1;
    }

    /* Create texture holding canvas */
    tex = caca_create_canvas(16, 16);
    if (!tex)
    {
        fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
        return 1;
    }

    /* Open window */
    dp = caca_create_display(cv);
    if (!dp)
    {
        fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
        return 1;
    }



    /* Set the window title */
    caca_set_display_title(dp, "trifiller");

    /* Frame duration */
    caca_set_display_time(dp, 10000);

    /* Get displayed canvas size */
    ww = caca_get_canvas_width(cv);
    wh = caca_get_canvas_height(cv);

    /* Texture size */
    tw = caca_get_canvas_width(tex);
    th = caca_get_canvas_height(tex);

    /* Load texture if any */
    if (argc == 2)
    {
        struct image *im = load_image(argv[1]);
        if (!im)
        {
            fprintf(stderr, "%s: unable to load image '%s'\n", argv[0],
                    argv[1]);
            return 1;
        }

        caca_set_dither_algorithm(im->dither,
                                  caca_get_dither_algorithm_list(NULL)[4]);
        caca_dither_bitmap(tex, 0, 0, tw, th, im->dither, im->pixels);
        unload_image(im);
    }
    /* or generate one */
    else
    {

        int i;
        for (i = 0; i < 16; i++)
        {
            caca_set_color_ansi(tex, (i + 1) % 0xF, i % 0xF);
            caca_put_str(tex, 0, i, "0123456789ABCDEF");
        }
    }


    px = 0;
    py = 0;

    while (!quit)
    {
        caca_event_t ev;
        unsigned int const event_mask = CACA_EVENT_KEY_PRESS
            | CACA_EVENT_RESIZE | CACA_EVENT_QUIT;
        int event;

        if (update)
            event = caca_get_event(dp, event_mask, &ev, 0);
        else
            event = caca_get_event(dp, event_mask, &ev, -1);

        while (event)
        {
            if (caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
                switch (caca_get_event_key_ch(&ev))
                {
                case 'q':
                case 'Q':
                case CACA_KEY_ESCAPE:
                    quit = 1;
                    break;
                case CACA_KEY_UP:
                    py--;
                    break;
                case CACA_KEY_DOWN:
                    py++;
                    break;
                case CACA_KEY_LEFT:
                    px--;
                    break;
                case CACA_KEY_RIGHT:
                    px++;
                    break;
                case 'a':
                    angle += 1.0f;
                    break;
                case 's':
                    angle -= 1.0f;
                    break;
                }
            else if (caca_get_event_type(&ev) == CACA_EVENT_RESIZE)
            {
                caca_refresh_display(dp);
                ww = caca_get_event_resize_width(&ev);
                wh = caca_get_event_resize_height(&ev);
                update = 1;
            }
            else if (caca_get_event_type(&ev) & CACA_EVENT_QUIT)
                quit = 1;

            event = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0);
        }



        /* 2D Rotation around screen center */
        int p;
        for (p = 0; p < 4; p++)
        {
            rotated[p][0] =
                square[p][0] * cos(angle * M_PI / 180.0f) -
                square[p][1] * sin(angle * M_PI / 180.0f);
            rotated[p][1] =
                square[p][0] * sin(angle * M_PI / 180.0f) +
                square[p][1] * cos(angle * M_PI / 180.0f);

            rotated[p][0] += ww / 2 + px;
            rotated[p][1] += wh / 2 + py;
        }

        angle += 1.0f;


        /* Reaarange coordinates to fit libcaca's format */
        coords1[0] = rotated[0][0];
        coords1[1] = rotated[0][1];
        coords1[2] = rotated[1][0];
        coords1[3] = rotated[1][1];
        coords1[4] = rotated[2][0];
        coords1[5] = rotated[2][1];

        coords2[0] = rotated[0][0];
        coords2[1] = rotated[0][1];
        coords2[2] = rotated[2][0];
        coords2[3] = rotated[2][1];
        coords2[4] = rotated[3][0];
        coords2[5] = rotated[3][1];

        /* Display two triangles */
        caca_fill_triangle_textured(cv, /* canvas */
                                    coords1,    /* triangle coordinates */
                                    tex,        /* texture canvas */
                                    uv1);       /* texture coordinates */
        caca_fill_triangle_textured(cv, coords2, tex, uv2);

        /* Refresh display and clear for next frame */
        caca_refresh_display(dp);
        caca_clear_canvas(cv);

    }

    caca_free_display(dp);
    caca_free_canvas(cv);
    caca_free_canvas(tex);

    return 0;
}