/** \brief Draw a box on the canvas using the given character. * * This function never fails. * * \param cv The handle to the libcaca canvas. * \param x X coordinate of the upper-left corner of the box. * \param y Y coordinate of the upper-left corner of the box. * \param w Width of the box. * \param h Height of the box. * \param ch UTF-32 character to be used to draw the box. * \return This function always returns 0. */ int caca_draw_box(caca_canvas_t *cv, int x, int y, int w, int h, uint32_t ch) { int x2 = x + w - 1; int y2 = y + h - 1; caca_draw_line(cv, x, y, x, y2, ch); caca_draw_line(cv, x, y2, x2, y2, ch); caca_draw_line(cv, x2, y2, x2, y, ch); caca_draw_line(cv, x2, y, x, y, ch); return 0; }
int main(int argc, char *argv[]) { char const * const *list; caca_display_t *dp; caca_canvas_t *cv; list = caca_get_display_driver_list(); dp = caca_create_display(NULL); if(dp == NULL) { printf("cannot create display\n"); return -1; } cv = caca_get_canvas(dp); caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK); while(1) { char const *driver; int i, cur = 0; caca_put_str(cv, 1, 0, "Available drivers:"); driver = caca_get_display_driver(dp); for(i = 0; list[i]; i += 2) { int match = !strcmp(list[i], driver); if(match) cur = i; caca_draw_line(cv, 0, i + 2, 9999, i + 2, ' '); caca_printf(cv, 2, i + 2, "%c %s (%s)", match ? '*' : ' ', list[i], list[i + 1]); } caca_put_str(cv, 1, i + 2, "Switching driver in 5 seconds"); caca_refresh_display(dp); if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, 5000000)) break; do { cur += 2; if(list[cur] && !strcmp(list[cur], "raw")) cur += 2; if(!list[cur]) cur = 0; } while(caca_set_display_driver(dp, list[cur])); } caca_free_display(dp); return 0; }
void line(int x0, int y0, int x1, int y1) { if (!pt.PT_USE_DITHERING) { caca_draw_line(pt.cv, x0 * X_SCALE, y0, x1 * X_SCALE, y1, pt.C_pixel); } else { int dx = abs( x1 - x0 ); int dy = abs( y1 - y0 ); int sx = 0; int sy = 0; if ( x0 < x1 ) { sx = 1; } else { sx = -1; } if ( y0 < y1 ) { sy = 1; } else { sy = -1; } int err = ( dx - dy ); while (1) { set_pixel_in_bitmap(x0, y0, pt.C_color); if ( x0 == x1 && y0 == y1 ) { break; } int e2 = 2 * err; if ( e2 > dy * -1 ) { err = err - dy; x0 = x0 + sx; } if ( e2 < dx ) { err = err + dx; y0 = y0 + sy; } } } }
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; }
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; }
JNIEXPORT void JNICALL Java_org_zoy_caca_Canvas_canvasDrawLine(JNIEnv *env, jclass cls, jlong ptr, jint x1, jint y1, jint x2, jint y2, jint ch) { caca_draw_line((caca_canvas_t *)ptr, x1, y1, x2, y2, ch); }