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

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

  • Property svn:executable set to *
File size: 2.4 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/cxxtest.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
23include dirname($argv[0]) . '/../caca.php';
24
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
42$canvas = new Canvas();
43if (!$canvas) {
44    die("Error while creating main canvas\n");
45}
46
47$pig = new Canvas();
48if (!$pig) {
49    die("Error while creating canvas pig\n");
50}
51
52$display = new Display($canvas);
53if (!$display) {
54    die("Error while attaching canvas to display\n");
55}
56
57
58$pig->setColorANSI(AnsiColor::LIGHTMAGENTA, AnsiColor::TRANSPARENT);
59$pig->importString($pig_str, "text");
60$display->setDisplayTime(20000);
61
62$x = $y = 0;
63$ix = $iy = 1;
64
65while (! $display->getEvent(EventType::KEY_PRESS)) {
66    // In case of resize ...
67    if ($x + $pig->getWidth() - 1 >= $canvas->getWidth() || $x < 0 )
68        $x = 0;
69    if ($y + $pig->getHeight() - 1 >= $canvas->getHeight() || $y < 0 )
70        $y = 0;
71
72    $canvas->Clear();
73
74    // Draw
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();
79
80
81    // Move cursor
82    $x += $ix;
83    $y += $iy;
84    if ($x + $pig->getWidth() >= $canvas->getWidth() || $x < 0 )
85        $ix = -$ix;
86    if ($y + $pig->getHeight() >= $canvas->getHeight() || $y < 0 )
87        $iy = -$iy;
88}
89
90?>
Note: See TracBrowser for help on using the repository browser.