瀏覽代碼

* Add php binding for caca_draw_polyline and caca_draw_thin_polyline

* Add a sample program examples/polyline.php
tags/v0.99.beta17
Nicolas Vion nico 16 年之前
父節點
當前提交
988c4912aa
共有 2 個文件被更改,包括 130 次插入4 次删除
  1. +41
    -0
      caca-php/examples/polyline.php
  2. +89
    -4
      caca-php/php_caca.c

+ 41
- 0
caca-php/examples/polyline.php 查看文件

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

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

$tbl = array(
array(5, 2),
array(15, 2),
array(20, 4),
array(25, 2),
array(34, 2),
array(37, 4),
array(36, 9),
array(20, 16),
array(3, 9),
array(2, 4),
array(5, 2)
);

function transform($tbl, $tx, $ty, $sx, $sy) {
$result = array();
foreach($tbl as $pt)
$result[] = array($pt[0] * $sx + $tx, $pt[1] * $sy + $ty);
return $result;
}

for ($i = 0; $i < 10; $i++) {
caca_set_color_ansi($canvas, 1 + (($color += 4) % 15), CACA_TRANSPARENT);
$scale = caca_rand(4, 10) / 10;
$translate = array(caca_rand(-10, 60), caca_rand(-5, 20));
$pts = transform($tbl, $translate[0], $translate[1], $scale, $scale);
caca_draw_thin_polyline($canvas, $pts);
}

caca_put_str($canvas, 1, 1, "Caca forever...");
caca_refresh_display($display);
sleep(5);

+ 89
- 4
caca-php/php_caca.c 查看文件

@@ -375,6 +375,7 @@ void *gd_get_pixels(gdImage *img) {

//------- PHP Binding's specific functions ----------//

//TODO: register new resources in caca_get_event and remove caca_create_event
PHP_FUNCTION(caca_create_event) {
caca_event_t *event;
event = emalloc(sizeof(caca_event_t));
@@ -424,13 +425,15 @@ PHP_FUNCTION(caca_get_canvas_height) {
PHP_FUNCTION(caca_get_canvas_chars) {
caca_canvas_t *canvas;
FETCH_CANVAS(canvas);
RETURN_STRING((char *) caca_get_canvas_chars(canvas), 1); //TODO: check that return \0 terminated string
RETURN_STRING((char *) caca_get_canvas_chars(canvas), 1);
//TODO: check that return \0 terminated string
}

PHP_FUNCTION(caca_get_canvas_attrs) {
caca_canvas_t *canvas;
FETCH_CANVAS(canvas);
RETURN_STRING((char *) caca_get_canvas_attrs(canvas), 1); //TODO: check that return \0 terminated string
RETURN_STRING((char *) caca_get_canvas_attrs(canvas), 1);
//TODO: check that return \0 terminated string
}

PHP_FUNCTION(caca_rand) {
@@ -731,6 +734,51 @@ PHP_FUNCTION(caca_draw_line) {
}

PHP_FUNCTION(caca_draw_polyline) {
zval *zval_res, *zval_arr;
char *c;
long c_len;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ras", &zval_res, &zval_arr, &c, &c_len) == FAILURE) {
RETURN_FALSE;
}

if (c_len != 1) {
RETURN_FALSE;
}

caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);

HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
int lenmax = zend_hash_num_elements(arr_hash);
int *tbl_x, *tbl_y;
int tbl_count = 0;
tbl_x = malloc(sizeof(int) * lenmax);
tbl_y = malloc(sizeof(int) * lenmax);
HashPosition pos;
zval **pt, **x, **y;
for (
zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
zend_hash_move_forward_ex(arr_hash, &pos)
) {
if (
Z_TYPE_P(*pt) == IS_ARRAY
&& (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
&& (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
) {
convert_to_long_ex(x);
convert_to_long_ex(y);
tbl_x[tbl_count] = Z_LVAL_PP(x);
tbl_y[tbl_count] = Z_LVAL_PP(y);
tbl_count++;
}
}
int res = caca_draw_polyline(canvas, tbl_x, tbl_y, tbl_count - 1, c[0]);
free(tbl_x);
free(tbl_y);
RETURN_SUCCESS(res);
}

PHP_FUNCTION(caca_draw_thin_line) {
@@ -745,6 +793,44 @@ PHP_FUNCTION(caca_draw_thin_line) {
}

PHP_FUNCTION(caca_draw_thin_polyline) {
zval *zval_res, *zval_arr;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zval_res, &zval_arr) == FAILURE) {
RETURN_FALSE;
}

caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &zval_res, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);

HashTable *arr_hash = Z_ARRVAL_P(zval_arr);
int lenmax = zend_hash_num_elements(arr_hash);
int *tbl_x, *tbl_y;
int tbl_count = 0;
tbl_x = malloc(sizeof(int) * lenmax);
tbl_y = malloc(sizeof(int) * lenmax);
HashPosition pos;
zval **pt, **x, **y;
for (
zend_hash_internal_pointer_reset_ex(arr_hash, &pos);
zend_hash_get_current_data_ex(arr_hash, (void**) &pt, &pos) == SUCCESS;
zend_hash_move_forward_ex(arr_hash, &pos)
) {
if (
Z_TYPE_P(*pt) == IS_ARRAY
&& (zend_hash_index_find(Z_ARRVAL_P(*pt), 0, (void**) &x) != FAILURE)
&& (zend_hash_index_find(Z_ARRVAL_P(*pt), 1, (void**) &y) != FAILURE)
) {
convert_to_long_ex(x);
convert_to_long_ex(y);
tbl_x[tbl_count] = Z_LVAL_PP(x);
tbl_y[tbl_count] = Z_LVAL_PP(y);
tbl_count++;
}
}
int res = caca_draw_thin_polyline(canvas, tbl_x, tbl_y, tbl_count - 1);
free(tbl_x);
free(tbl_y);
RETURN_SUCCESS(res);
}

PHP_FUNCTION(caca_draw_circle) {
@@ -1281,8 +1367,7 @@ PHP_FUNCTION(caca_put_figchar) {
caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);

//TODO: get a string instead of a long
caca_put_figchar(canvas, c);
RETURN_SUCCESS(caca_put_figchar(canvas, c));
}

PHP_FUNCTION(caca_flush_figlet) {


Loading…
取消
儲存