瀏覽代碼

* Add php bindings for functions: caca_draw_line & caca_draw_thin_line

* Create a new sample file demo.php (php port of demo.c)
tags/v0.99.beta17
Nicolas Vion nico 16 年之前
父節點
當前提交
24df44d186
共有 2 個檔案被更改,包括 109 行新增1 行删除
  1. +88
    -0
      caca-php/examples/demo.php
  2. +21
    -1
      caca-php/php_caca.c

+ 88
- 0
caca-php/examples/demo.php 查看文件

@@ -0,0 +1,88 @@
#!/usr/bin/php5
<?

function display_menu($cv, $dp) {
$xo = caca_get_canvas_width($cv) - 2;
$yo = caca_get_canvas_height($cv) - 2;

caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
caca_clear_canvas($cv);
caca_draw_thin_box($cv, 1, 1, $xo, $yo);

caca_put_str($cv, ($xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
caca_put_str($cv, ($xo - strlen("==============")) / 2, 4, "==============");

caca_put_str($cv, 4, 6, "demos:");
caca_put_str($cv, 4, 7, "'f': full");
caca_put_str($cv, 4, 8, "'1': dots");
caca_put_str($cv, 4, 9, "'2': lines");
caca_put_str($cv, 4, 10, "'3': boxes");
caca_put_str($cv, 4, 11, "'4': triangles");
caca_put_str($cv, 4, 12, "'5': ellipses");
caca_put_str($cv, 4, 13, "'c': colour");
caca_put_str($cv, 4, 14, "'r': render");

if ($sprite)
caca_put_str($cv, 4, 15, "'s': sprites");

caca_put_str($cv, 4, 16, "settings:");
caca_put_str($cv, 4, 17, "'o': outline: ".((outline == 0) ? "none" : ((outline == 1) ? "solid" : "thin")));
caca_put_str($cv, 4, 18, "'b': drawing boundaries: ".((bounds == 0) ? "screen" : "infinite"));
caca_put_str($cv, 4, $yo - 2, "'q': quit");

caca_refresh_display($dp);
}

function main() {
$cv = caca_create_canvas(0, 0);
if (!$cv) {
die("Error while creating canvas\n");
}

$dp = caca_create_display($cv);
if (!$dp) {
die("Error while attaching canvas to display\n");
}

display_menu($cv, $dp);

caca_set_display_time($dp, 40000);

/* Disable cursor */
caca_set_mouse($dp, 0);

/* Main menu */
display_menu($cv, $dp);
caca_refresh_display($dp);

sleep(1);

for ($i = 0; $i < 100; $i++) {
demo_lines($cv, true, 2);
caca_refresh_display($dp);
}

}

function demo_lines($cv, $bounds = true, $outline = 1) {
$w = caca_get_canvas_width($cv);
$h = caca_get_canvas_height($cv);

if ($bounds) {
$xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h);
$xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h);
}
else {
$xa = caca_rand(0, $w); $ya = caca_rand(0, $h);
$xb = caca_rand(0, $w); $yb = caca_rand(0, $h);
}

caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK);
if ($outline > 1)
caca_draw_thin_line($cv, $xa, $ya, $xb, $yb);
else
caca_draw_line($cv, $xa, $ya, $xb, $yb, '#');
}


main();

+ 21
- 1
caca-php/php_caca.c 查看文件

@@ -584,12 +584,32 @@ PHP_FUNCTION(caca_attr_to_ansi_bg) {
}

PHP_FUNCTION(caca_draw_line) {
zval *_zval;
char *str;
long str_len, xa, ya, xb, yb = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlllls", &_zval, &xa, &ya, &xb, &yb, &str, &str_len) == FAILURE) {
RETURN_FALSE;
}
caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
if (str_len != 1) {
RETURN_FALSE;
}
RETURN_BOOL(caca_draw_line(canvas, xa, ya, xb, yb, str[0]) == 0);
}

PHP_FUNCTION(caca_draw_polyline) {
}

PHP_FUNCTION(caca_draw_thin_line) {
zval *_zval;
long xa, ya, xb, yb = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllll", &_zval, &xa, &ya, &xb, &yb) == FAILURE) {
RETURN_FALSE;
}
caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
RETURN_BOOL(caca_draw_thin_line(canvas, xa, ya, xb, yb) == 0);
}

PHP_FUNCTION(caca_draw_thin_polyline) {
@@ -853,7 +873,7 @@ PHP_FUNCTION(caca_set_mouse) {
}
caca_display_t *display;
ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
RETURN_BOOL(caca_set_display(display, value) == 0);
RETURN_BOOL(caca_set_mouse(display, value) == 0);
}

PHP_FUNCTION(caca_set_cursor) {


Loading…
取消
儲存