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

Last change on this file since 3514 was 3296, checked in by bsittler, 12 years ago

convert to use PHP object layer similar to the C++ one used in its C++ predecessor, cxx/cxxtest.cpp; also change behavior and appearance to more closely match that version (perhaps, though, the changes should have been ported in the other direction instead...)

  • Property svn:executable set to *
File size: 2.4 KB
RevLine 
[3089]1#!/usr/bin/php5
[3126]2<?php
[3172]3/*
4 *  cacapig.php      sample program for libcaca php binding
5 *  Copyright (c) 2008 Nicolas Vion <nico@yojik.eu>
6 *
[3296]7 *  This file is a Php port of "cxx/cxxtest.cpp"
[3172]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 */
[3089]18
[3268]19if (php_sapi_name() != "cli") {
20        die("You have to run this program with php-cli!\n");
21}
[3172]22
[3296]23include dirname($argv[0]) . '/../caca.php';
24
[3104]25$pig_str = <<<EOT
26                                   
27    _._ _..._ .-',     _.._(`))   
28   '-. `     '  /-._.-'    ',/     
29      )         \            '.   
30     / _    _    |             \   
31    |  a    a    /   PHP        | 
32    \   .-.                     ;
33     '-('' ).-'       ,'       ;   
34        '-;           |      .'   
35           \           \    /   
36           | 7  .__  _.-\   \   
37           | |  |  ``/  /`  /     
38      jgs /,_|  |   /,_/   /       
39             /,_/      '`-'       
40EOT;
41
[3296]42$canvas = new Canvas();
[3104]43if (!$canvas) {
44        die("Error while creating main canvas\n");
45}
46
[3296]47$pig = new Canvas();
[3104]48if (!$pig) {
49        die("Error while creating canvas pig\n");
50}
51
[3296]52$display = new Display($canvas);
[3104]53if (!$display) {
54        die("Error while attaching canvas to display\n");
55}
56
57
[3296]58$pig->setColorANSI(AnsiColor::LIGHTMAGENTA, AnsiColor::TRANSPARENT);
59$pig->importString($pig_str, "text");
60$display->setDisplayTime(20000);
[3104]61
[3105]62$x = $y = 0;
63$ix = $iy = 1;
[3104]64
[3296]65while (! $display->getEvent(EventType::KEY_PRESS)) {
[3105]66        // In case of resize ...
[3296]67        if ($x + $pig->getWidth() - 1 >= $canvas->getWidth() || $x < 0 )
[3105]68                $x = 0;
[3296]69        if ($y + $pig->getHeight() - 1 >= $canvas->getHeight() || $y < 0 )
[3105]70                $y = 0;
[3104]71
[3296]72        $canvas->Clear();
[3105]73
[3106]74        // Draw
[3296]75        $canvas->Blit($x, $y, $pig, NULL);
76        $canvas->setColorANSI(AnsiColor::LIGHTBLUE, AnsiColor::BLACK);
77        $canvas->putStr($canvas->getWidth() / 2 - 10, $canvas->getHeight() / 2, "Powered by libcaca ".Libcaca::getVersion());
78        $display->refresh();
[3105]79
[3106]80
81        // Move cursor
[3105]82        $x += $ix;
83        $y += $iy;
[3296]84        if ($x + $pig->getWidth() >= $canvas->getWidth() || $x < 0 )
[3105]85                $ix = -$ix;
[3296]86        if ($y + $pig->getHeight() >= $canvas->getHeight() || $y < 0 )
[3105]87                $iy = -$iy;
88}
89
[3135]90?>
Note: See TracBrowser for help on using the repository browser.