Ignore:
Timestamp:
Oct 26, 2008, 1:29:10 PM (14 years ago)
Author:
nico
Message:
  • Add php binding for 9 new functions
  • Improve examples/demo.php
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca-php/examples/demo.php

    r3107 r3108  
    11#!/usr/bin/php5
    22<?
     3
     4function main() {
     5        $cv = caca_create_canvas(0, 0);
     6        if (!$cv) {
     7                die("Error while creating canvas\n");
     8        }
     9
     10        $dp = caca_create_display($cv);
     11        if (!$dp) {
     12                die("Error while attaching canvas to display\n");
     13        }
     14
     15        display_menu($cv, $dp);
     16
     17        caca_set_display_time($dp, 40000);
     18
     19        /* Disable cursor */
     20        caca_set_mouse($dp, 0);
     21
     22        /* Main menu */
     23        display_menu($cv, $dp);
     24        caca_refresh_display($dp);
     25
     26
     27        /* Go ! */
     28        $bounds = $outline = $dithering = 0;
     29
     30        $ev = caca_create_event();
     31        while(!$quit) {
     32                $menu = $mouse = $xmouse = $ymouse = 0;
     33
     34                while (caca_get_event($dp, CACA_EVENT_ANY, $ev, 0)) {
     35                        if ($demo and (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS)) {
     36                                $menu = 1;
     37                                $demo = false;
     38                        }
     39                        else if (caca_get_event_type($ev) & CACA_EVENT_KEY_PRESS) {
     40                                switch (caca_get_event_key_ch($ev)) {
     41                                        case ord('q'):
     42                                        case ord('Q'):
     43                                        case CACA_KEY_ESCAPE:
     44                                                $demo = false;
     45                                                $quit = 1;
     46                                                break;
     47                                        case ord('o'):
     48                                        case ord('O'):
     49                                                $outline = ($outline + 1) % 3;
     50                                                display_menu($cv, $dp);
     51                                                break;
     52                                        case ord('b'):
     53                                        case ord('B'):
     54                                                $bounds = ($bounds + 1) % 2;
     55                                                display_menu($cv, $dp);
     56                                                break;
     57                                        case ord('d'):
     58                                        case ord('D'):
     59                                                $dithering = ($dithering + 1) % 5;
     60                                                caca_set_feature($cv, $dithering);
     61                                                display_menu($cv, $dp);
     62                                                break;
     63                                        case ord('f'):
     64                                        case ord('F'):
     65                                                $demo = "demo_all";
     66                                                break;
     67                                        case ord('1'):
     68                                                $demo = "demo_dots";
     69                                                break;
     70                                        case ord('2'):
     71                                                $demo = "demo_lines";
     72                                                break;
     73                                        case ord('3'):
     74                                                $demo = "demo_boxes";
     75                                                break;
     76                                        case ord('4'):
     77                                                $demo = "demo_triangles";
     78                                                break;
     79                                        case ord('5'):
     80                                                $demo = "demo_ellipses";
     81                                                break;
     82                                        case ord('s'):
     83                                        case ord('S'):
     84                                                $demo = "demo_sprites";
     85                                                break;
     86                                        case ord('r'):
     87                                        case ord('R'):
     88                                                $demo = "demo_render";
     89                                                break;
     90                                }
     91
     92                                if ($demo) {
     93                                        caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
     94                                        caca_clear_canvas($cv);
     95                                }
     96                        }
     97                        else if(caca_get_event_type($ev) & CACA_EVENT_MOUSE_MOTION) {
     98                                $mouse = 1;
     99                                $xmouse = caca_get_event_mouse_x($ev);
     100                                $ymouse = caca_get_event_mouse_y($ev);
     101                        }
     102                        else if(caca_get_event_type($ev) & CACA_EVENT_RESIZE) {
     103                                $mouse = 1; /* old hack */
     104                        }
     105                }
     106
     107                if ($menu || ($mouse && !$demo)) {
     108                        display_menu($cv, $dp);
     109                        if ($mouse && !$demo) {
     110                                caca_set_color_ansi($cv, CACA_RED, CACA_BLACK);
     111                                caca_put_str($cv, $xmouse, $ymouse,".");
     112                                caca_put_str($cv, $xmouse, $ymouse + 1, "|\\");
     113                        }
     114                        caca_refresh_display($dp);
     115                        $mouse = $menu = 0;
     116                }
     117
     118                if ($demo) {
     119                        $demo($cv, $bounds, $outline, $dithering);
     120
     121                        caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
     122                        caca_draw_thin_box($cv, 1, 1, caca_get_canvas_width($cv) - 2, caca_get_canvas_height($cv) - 2);
     123                        $rate = (1000000 / caca_get_display_time($dp)).".".((10000000 / caca_get_display_time($dp)) % 10);
     124                        caca_put_str($cv, 4, 1, "[$rate fps]----");
     125                        caca_refresh_display($dp);
     126                }
     127        }
     128}
    3129
    4130function display_menu($cv, $dp) {
     
    34160}
    35161
    36 function main() {
    37         $cv = caca_create_canvas(0, 0);
    38         if (!$cv) {
    39                 die("Error while creating canvas\n");
    40         }
    41 
    42         $dp = caca_create_display($cv);
    43         if (!$dp) {
    44                 die("Error while attaching canvas to display\n");
    45         }
    46 
    47         display_menu($cv, $dp);
    48 
    49         caca_set_display_time($dp, 40000);
    50 
    51         /* Disable cursor */
    52         caca_set_mouse($dp, 0);
    53 
    54         /* Main menu */
    55         display_menu($cv, $dp);
    56         caca_refresh_display($dp);
    57 
    58         sleep(1);
    59 
    60         for ($i = 0; $i < 100; $i++) {
    61                 demo_lines($cv, true, 2);
    62                 caca_refresh_display($dp);
    63         }
    64 
    65 }
    66 
    67 function demo_lines($cv, $bounds = true, $outline = 1) {
     162function demo_dots($cv, $bounds, $outline, $dithering) {
     163        $xmax = caca_get_canvas_width($cv);
     164        $ymax = caca_get_canvas_height($cv);
     165
     166        $chars = array('+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W');
     167
     168        for ($i = 1000; $i--;) {
     169                /* Putpixel */
     170                caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
     171                caca_put_char($cv, caca_rand(0, $xmax), caca_rand(0, $ymax), $chars[caca_rand(0, 9)]);
     172        }
     173}
     174
     175function demo_lines($cv, $bounds, $outline, $dithering) {
    68176        $w = caca_get_canvas_width($cv);
    69177        $h = caca_get_canvas_height($cv);
     
    85193}
    86194
     195function demo_boxes($cv, $bounds, $outline, $dithering) {
     196        $w = caca_get_canvas_width($cv);
     197        $h = caca_get_canvas_height($cv);
     198
     199        if ($bounds) {
     200                $xa = caca_rand(- $w, 2 * $w); $ya = caca_rand(- $h, 2 * $h);
     201                $xb = caca_rand(- $w, 2 * $w); $yb = caca_rand(- $h, 2 * $h);
     202        }
     203        else {
     204                $xa = caca_rand(0, $w); $ya = caca_rand(0, $h);
     205                $xb = caca_rand(0, $w); $yb = caca_rand(0, $h);
     206        }
     207
     208        caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
     209        caca_fill_box($cv, $xa, $ya, $xb, $yb, '#');
     210
     211        caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK);
     212        if($outline == 2)
     213                caca_draw_thin_box($cv, $xa, $ya, $xb, $yb);
     214        else if($outline == 1)
     215                caca_draw_box($cv, $xa, $ya, $xb, $yb, '#');
     216}
     217
     218function demo_ellipses($cv, $bounds, $outline, $dithering) {
     219        $w = caca_get_canvas_width($cv);
     220        $h = caca_get_canvas_height($cv);
     221
     222        if ($bounds) {
     223                $x = caca_rand(- $w, 2 * $w); $y = caca_rand(- $h, 2 * $h);
     224                $a = caca_rand(0, $w); $b = caca_rand(0, $h);
     225        }
     226        else {
     227                do {
     228                        $x = caca_rand(0, $w); $y = caca_rand(0, $h);
     229                        $a = caca_rand(0, $w); $b = caca_rand(0, $h);
     230                } while ($x - $a < 0 || $x + $a >= $w || $y - $b < 0 || $y + $b >= $h);
     231        }
     232
     233        caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
     234        caca_fill_ellipse($cv, $x, $y, $a, $b, '#');
     235
     236        caca_set_color_ansi($cv, caca_rand(0, 16), CACA_BLACK);
     237        if ($outline == 2)
     238                caca_draw_thin_ellipse($cv, $x, $y, $a, $b);
     239        else if ($outline == 1)
     240                caca_draw_ellipse($cv, $x, $y, $a, $b, '#');
     241}
     242
     243
    87244
    88245main();
Note: See TracChangeset for help on using the changeset viewer.