Ver código fonte

* Add php binding for 9 new functions

* Improve examples/demo.php
tags/v0.99.beta17
Nicolas Vion nico 16 anos atrás
pai
commit
0a20917152
3 arquivos alterados com 290 adições e 40 exclusões
  1. +184
    -27
      caca-php/examples/demo.php
  2. +103
    -12
      caca-php/php_caca.c
  3. +3
    -1
      caca-php/php_caca.h

+ 184
- 27
caca-php/examples/demo.php Ver arquivo

@@ -1,6 +1,132 @@
#!/usr/bin/php5
<?

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);


/* Go ! */
$bounds = $outline = $dithering = 0;

$ev = caca_create_event();
while(!$quit) {
$menu = $mouse = $xmouse = $ymouse = 0;

while (caca_get_event($dp, CACA_EVENT_ANY, $ev, 0)) {
if ($demo and (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS)) {
$menu = 1;
$demo = false;
}
else if (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS) {
switch (caca_get_event_key_ch($ev)) {
case ord('q'):
case ord('Q'):
case CACA_KEY_ESCAPE:
$demo = false;
$quit = 1;
break;
case ord('o'):
case ord('O'):
$outline = ($outline + 1) % 3;
display_menu($cv, $dp);
break;
case ord('b'):
case ord('B'):
$bounds = ($bounds + 1) % 2;
display_menu($cv, $dp);
break;
case ord('d'):
case ord('D'):
$dithering = ($dithering + 1) % 5;
caca_set_feature($cv, $dithering);
display_menu($cv, $dp);
break;
case ord('f'):
case ord('F'):
$demo = "demo_all";
break;
case ord('1'):
$demo = "demo_dots";
break;
case ord('2'):
$demo = "demo_lines";
break;
case ord('3'):
$demo = "demo_boxes";
break;
case ord('4'):
$demo = "demo_triangles";
break;
case ord('5'):
$demo = "demo_ellipses";
break;
case ord('s'):
case ord('S'):
$demo = "demo_sprites";
break;
case ord('r'):
case ord('R'):
$demo = "demo_render";
break;
}

if ($demo) {
caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
caca_clear_canvas($cv);
}
}
else if(caca_get_event_type($ev) & CACA_EVENT_MOUSE_MOTION) {
$mouse = 1;
$xmouse = caca_get_event_mouse_x($ev);
$ymouse = caca_get_event_mouse_y($ev);
}
else if(caca_get_event_type($ev) & CACA_EVENT_RESIZE) {
$mouse = 1; /* old hack */
}
}

if ($menu || ($mouse && !$demo)) {
display_menu($cv, $dp);
if ($mouse && !$demo) {
caca_set_color_ansi($cv, CACA_RED, CACA_BLACK);
caca_put_str($cv, $xmouse, $ymouse,".");
caca_put_str($cv, $xmouse, $ymouse + 1, "|\\");
}
caca_refresh_display($dp);
$mouse = $menu = 0;
}

if ($demo) {
$demo($cv, $bounds, $outline, $dithering);

caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
caca_draw_thin_box($cv, 1, 1, caca_get_canvas_width($cv) - 2, caca_get_canvas_height($cv) - 2);
$rate = (1000000 / caca_get_display_time($dp)).".".((10000000 / caca_get_display_time($dp)) % 10);
caca_put_str($cv, 4, 1, "[$rate fps]----");
caca_refresh_display($dp);
}
}
}

function display_menu($cv, $dp) {
$xo = caca_get_canvas_width($cv) - 2;
$yo = caca_get_canvas_height($cv) - 2;
@@ -33,38 +159,20 @@ function display_menu($cv, $dp) {
caca_refresh_display($dp);
}

function main() {
$cv = caca_create_canvas(0, 0);
if (!$cv) {
die("Error while creating canvas\n");
}
function demo_dots($cv, $bounds, $outline, $dithering) {
$xmax = caca_get_canvas_width($cv);
$ymax = caca_get_canvas_height($cv);

$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);
$chars = array('+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W');

/* 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);
for ($i = 1000; $i--;) {
/* Putpixel */
caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
caca_put_char($cv, caca_rand(0, $xmax), caca_rand(0, $ymax), $chars[caca_rand(0, 9)]);
}

}

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

@@ -84,5 +192,54 @@ function demo_lines($cv, $bounds = true, $outline = 1) {
caca_draw_line($cv, $xa, $ya, $xb, $yb, '#');
}

function demo_boxes($cv, $bounds, $outline, $dithering) {
$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_rand(0, 16));
caca_fill_box($cv, $xa, $ya, $xb, $yb, '#');

caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK);
if($outline == 2)
caca_draw_thin_box($cv, $xa, $ya, $xb, $yb);
else if($outline == 1)
caca_draw_box($cv, $xa, $ya, $xb, $yb, '#');
}

function demo_ellipses($cv, $bounds, $outline, $dithering) {
$w = caca_get_canvas_width($cv);
$h = caca_get_canvas_height($cv);

if ($bounds) {
$x = caca_rand(- $w, 2 * $w); $y = caca_rand(- $h, 2 * $h);
$a = caca_rand(0, $w); $b = caca_rand(0, $h);
}
else {
do {
$x = caca_rand(0, $w); $y = caca_rand(0, $h);
$a = caca_rand(0, $w); $b = caca_rand(0, $h);
} while ($x - $a < 0 || $x + $a >= $w || $y - $b < 0 || $y + $b >= $h);
}

caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
caca_fill_ellipse($cv, $x, $y, $a, $b, '#');

caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK);
if ($outline == 2)
caca_draw_thin_ellipse($cv, $x, $y, $a, $b);
else if ($outline == 1)
caca_draw_ellipse($cv, $x, $y, $a, $b, '#');
}



main();

+ 103
- 12
caca-php/php_caca.c Ver arquivo

@@ -18,6 +18,7 @@
#include "php_caca.h"

static function_entry caca_functions[] = {
PHP_FE(caca_create_event, NULL)
PHP_FE(caca_create_canvas, NULL)
PHP_FE(caca_manage_canvas, NULL)
PHP_FE(caca_unmanage_canvas, NULL)
@@ -34,7 +35,6 @@ static function_entry caca_functions[] = {
PHP_FE(caca_put_char, NULL)
PHP_FE(caca_get_char, NULL)
PHP_FE(caca_put_str, NULL)
PHP_FE(caca_printf, NULL)
PHP_FE(caca_clear_canvas, NULL)
PHP_FE(caca_set_canvas_handle, NULL)
PHP_FE(caca_get_canvas_handle_x, NULL)
@@ -66,6 +66,7 @@ static function_entry caca_functions[] = {
PHP_FE(caca_fill_ellipse, NULL)
PHP_FE(caca_draw_box, NULL)
PHP_FE(caca_draw_thin_box, NULL)
PHP_FE(caca_draw_cp437_box, NULL)
PHP_FE(caca_fill_box, NULL)
PHP_FE(caca_draw_triangle, NULL)
PHP_FE(caca_draw_thin_triangle, NULL)
@@ -167,7 +168,7 @@ PHP_MINFO_FUNCTION(caca) {
php_info_print_table_end();
}

//--------CACA'S RESSOURCES DESTRUCTORS-----------//
//-------- Caca's ressources destructors -----------//

static void php_caca_canvas_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
caca_free_canvas(rsrc->ptr);
@@ -189,9 +190,10 @@ static void php_caca_display_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
}

static void php_caca_event_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) {
efree((caca_event_t *) rsrc->ptr);
}

//--------INITIALIZATION---------//
//-------- Initialization ---------//

PHP_MINIT_FUNCTION(caca) {

@@ -291,7 +293,7 @@ PHP_MINIT_FUNCTION(caca) {
return SUCCESS;
}

//----------SOME USEFULL MACROS---------------//
//---------- Some usefull macros ---------------//

#define FETCH_CANVAS(canvas) \
zval *_zval; \
@@ -320,8 +322,15 @@ PHP_MINIT_FUNCTION(caca) {
str[1] = '\0'; \
RETURN_STRING(str, 0);

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

//-------CACA'S FUNCTIONS----------------//
PHP_FUNCTION(caca_create_event) {
caca_event_t *event;
event = emalloc(sizeof(caca_event_t));
ZEND_REGISTER_RESOURCE(return_value, event, le_caca_event);
}

//------- Caca's functions ----------------//

PHP_FUNCTION(caca_create_canvas) {
long width, height = 0;
@@ -413,13 +422,17 @@ PHP_FUNCTION(caca_get_cursor_y) {

PHP_FUNCTION(caca_put_char) {
zval *_zval;
long x, y, c = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlll", &_zval, &x, &y, &c) == FAILURE) {
char *str;
long str_len, x, y = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlls", &_zval, &x, &y, &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);
RETURN_BOOL(caca_put_char(canvas, x, y, c) == 0);
if (str_len != 1) {
RETURN_FALSE;
}
RETURN_BOOL(caca_put_char(canvas, x, y, str[0]) == 0);
}

PHP_FUNCTION(caca_get_char) {
@@ -445,9 +458,6 @@ PHP_FUNCTION(caca_put_str) {
RETURN_BOOL(caca_put_str(canvas, x, y, str) == 0);
}

PHP_FUNCTION(caca_printf) {
}

PHP_FUNCTION(caca_clear_canvas) {
caca_canvas_t *canvas;
FETCH_CANVAS(canvas);
@@ -619,21 +629,96 @@ PHP_FUNCTION(caca_draw_circle) {
}

PHP_FUNCTION(caca_draw_ellipse) {
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_ellipse(canvas, xa, ya, xb, yb, str[0]) == 0);
}

PHP_FUNCTION(caca_draw_thin_ellipse) {
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_ellipse(canvas, xa, ya, xb, yb) == 0);
}

PHP_FUNCTION(caca_fill_ellipse) {
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_fill_ellipse(canvas, xa, ya, xb, yb, str[0]) == 0);
}

PHP_FUNCTION(caca_draw_box) {
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_box(canvas, xa, ya, xb, yb, str[0]) == 0);
}

PHP_FUNCTION(caca_draw_thin_box) {
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_box(canvas, xa, ya, xb, yb) == 0);
}

PHP_FUNCTION(caca_draw_cp437_box) {
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_cp437_box(canvas, xa, ya, xb, yb) == 0);
}

PHP_FUNCTION(caca_fill_box) {
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_fill_box(canvas, xa, ya, xb, yb, str[0]) == 0);
}

PHP_FUNCTION(caca_draw_triangle) {
@@ -900,7 +985,7 @@ PHP_FUNCTION(caca_get_event) {
if (_zval2) {
ZEND_FETCH_RESOURCE(event, caca_event_t*, &_zval2, -1, PHP_CACA_EVENT_RES_NAME, le_caca_event);
}
RETURN_BOOL(caca_get_event(display, g, event, aa) == 0);
RETURN_LONG(caca_get_event(display, g, event, aa));
}

PHP_FUNCTION(caca_get_mouse_x) {
@@ -916,9 +1001,15 @@ PHP_FUNCTION(caca_get_mouse_y) {
}

PHP_FUNCTION(caca_get_event_type) {
caca_event_t *event;
FETCH_EVENT(event);
RETURN_LONG(caca_get_event_type(event));
}

PHP_FUNCTION(caca_get_event_key_ch) {
caca_event_t *event;
FETCH_EVENT(event);
RETURN_LONG(caca_get_event_key_ch(event));
}

PHP_FUNCTION(caca_get_event_mouse_button) {


+ 3
- 1
caca-php/php_caca.h Ver arquivo

@@ -35,6 +35,8 @@ int le_caca_event;
PHP_MINIT_FUNCTION(caca);
PHP_MINFO_FUNCTION(caca);

PHP_FUNCTION(caca_create_event);

PHP_FUNCTION(caca_create_canvas);
PHP_FUNCTION(caca_manage_canvas);
PHP_FUNCTION(caca_unmanage_canvas);
@@ -51,7 +53,6 @@ PHP_FUNCTION(caca_get_cursor_y);
PHP_FUNCTION(caca_put_char);
PHP_FUNCTION(caca_get_char);
PHP_FUNCTION(caca_put_str);
PHP_FUNCTION(caca_printf);
PHP_FUNCTION(caca_clear_canvas);
PHP_FUNCTION(caca_set_canvas_handle);
PHP_FUNCTION(caca_get_canvas_handle_x);
@@ -83,6 +84,7 @@ PHP_FUNCTION(caca_draw_thin_ellipse);
PHP_FUNCTION(caca_fill_ellipse);
PHP_FUNCTION(caca_draw_box);
PHP_FUNCTION(caca_draw_thin_box);
PHP_FUNCTION(caca_draw_cp437_box);
PHP_FUNCTION(caca_fill_box);
PHP_FUNCTION(caca_draw_triangle);
PHP_FUNCTION(caca_draw_thin_triangle);


Carregando…
Cancelar
Salvar