source: libcaca/trunk/caca-php/examples/cacapig.php @ 3269

Last change on this file since 3269 was 3268, checked in by bsittler, 12 years ago

force cli on remaining examples so that apache doesn't pop up a window
(this actually happens with the cocoa driver!)

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/php5
2<?php
3/*
4 *  cacapig.php      sample program for libcaca php binding
5 *  Copyright (c) 2008 Nicolas Vion <nico@yojik.eu>
6 *
7 *  This file is a Php port of "cxx/cpptest.cpp"
8 *  which is:
9 *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
10 *                All Rights Reserved
11 *
12 *  This program is free software. It comes without any warranty, to
13 *  the extent permitted by applicable law. You can redistribute it
14 *  and/or modify it under the terms of the Do What The Fuck You Want
15 *  To Public License, Version 2, as published by Sam Hocevar. See
16 *  http://sam.zoy.org/wtfpl/COPYING for more details.
17 */
18
19if (php_sapi_name() != "cli") {
20        die("You have to run this program with php-cli!\n");
21}
22
23$pig_str = <<<EOT
24                                   
25    _._ _..._ .-',     _.._(`))   
26   '-. `     '  /-._.-'    ',/     
27      )         \            '.   
28     / _    _    |             \   
29    |  a    a    /   PHP        | 
30    \   .-.                     ;
31     '-('' ).-'       ,'       ;   
32        '-;           |      .'   
33           \           \    /   
34           | 7  .__  _.-\   \   
35           | |  |  ``/  /`  /     
36      jgs /,_|  |   /,_/   /       
37             /,_/      '`-'       
38EOT;
39
40$canvas = caca_create_canvas(0, 0);
41if (!$canvas) {
42        die("Error while creating main canvas\n");
43}
44
45$pig = caca_create_canvas(0, 0);
46if (!$pig) {
47        die("Error while creating canvas pig\n");
48}
49
50$display = caca_create_display($canvas);
51if (!$display) {
52        die("Error while attaching canvas to display\n");
53}
54
55
56caca_set_color_ansi($pig, CACA_LIGHTMAGENTA, CACA_TRANSPARENT);
57caca_set_color_ansi($canvas, CACA_LIGHTBLUE, CACA_TRANSPARENT);
58caca_import_string($pig, $pig_str, "text");
59caca_set_display_time($display, 30000);
60
61$x = $y = 0;
62$ix = $iy = 1;
63
64while (!caca_get_event($display, CACA_EVENT_KEY_PRESS)) {
65        // In case of resize ...
66        if ($x + caca_get_canvas_width($pig) - 1 >= caca_get_canvas_width($canvas) || $x < 0 )
67                $x = 0;
68        if ($y + caca_get_canvas_height($pig) - 1 >= caca_get_canvas_height($canvas) || $y < 0 )
69                $y = 0;
70
71        caca_clear_canvas($canvas);
72
73        // Draw
74        caca_blit($canvas, $x, $y, $pig);
75        caca_put_str($canvas, caca_get_canvas_width($canvas) / 2 - 10, caca_get_canvas_height($canvas) / 2, "Powered by libcaca ".caca_get_version());
76        caca_refresh_display($display);
77
78
79        // Move cursor
80        $x += $ix;
81        $y += $iy;
82        if ($x + caca_get_canvas_width($pig) >= caca_get_canvas_width($canvas) || $x < 0 )
83                $ix = -$ix;
84        if ($y + caca_get_canvas_height($pig) >= caca_get_canvas_height($canvas) || $y < 0 )
85                $iy = -$iy;
86}
87
88?>
Note: See TracBrowser for help on using the repository browser.