source: libcaca/trunk/caca-php/examples/test.php @ 3292

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

add AnsiColor?, EventType? and EventKey? constants parallel to the .NET ones

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 4.3 KB
RevLine 
[3290]1#!/usr/bin/php5
2<?php
3  /*
4   *  Test          PHP bindings test program
5   *  Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>
6   *
7   *  This file is a Php port of "caca-sharp/test.cs"
8   *  which is:
9   *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
10   *                2007 Sam Hocevar <sam@zoy.org>
11   *                All Rights Reserved
12   *
13   *  $Id: test.php 3292 2008-11-05 00:39:18Z bsittler $
14   *
15   *  This program is free software. It comes without any warranty, to
16   *  the extent permitted by applicable law. You can redistribute it
17   *  and/or modify it under the terms of the Do What The Fuck You Want
18   *  To Public License, Version 2, as published by Sam Hocevar. See
19   *  http://sam.zoy.org/wtfpl/COPYING for more details.
20   */
[921]21
[3290]22if (php_sapi_name() != "cli") {
23        die("You have to run this program with php-cli!\n");
24}
[921]25
[3290]26include dirname($argv[0]) . '/../caca.php';
[921]27
[3290]28class DemoCanvas extends Canvas
[2047]29{
[3290]30        private $image;
[2058]31
[3290]32        private $startTime;
33        private $d;
34        private $scroll;
[921]35
[3290]36        function __construct()
37        {
38                parent::__construct();
39                $this->startTime = gettimeofday(true);
[2058]40
[3290]41                $message = " --- POWERED BY LIBCACA --- OLDSCHOOL TEXT EFFECTS ARE 100% PURE WIN";
[2082]42
[3290]43                $this->scroll = new Canvas(strlen($message), 1);
[3292]44                $this->scroll->setColorAnsi(AnsiColor::WHITE, AnsiColor::TRANSPARENT);
[3290]45                $this->scroll->putStr(0, 0, $message);
[2082]46
[3290]47                $fontList = Font::getList();
48                $f = new Font($fontList[1]);
49                $w = $f->getWidth() * strlen($message);
50                $h = $f->getHeight();
51                $this->image = imagecreatetruecolor($w, $h);
52                imagefilledrectangle($this->image, 0, 0, $w - 1, $h - 1, imagecolorallocatealpha($this->image, 0, 0, 0, 127));
53                imagealphablending($this->image, false);
54                imagesavealpha($this->image, true);
55                $this->d = new Dither($this->image);
56                $f->Render($this->scroll, $this->image);
57                imagepng($this->image, "out.png");
58        }
[921]59
[3290]60        function Draw()
61        {
62                $barCount = 6;
63                $t = (gettimeofday(true) - $this->startTime) * 1000;
[2046]64
[3290]65                $this->Clear();
[1424]66
[3292]67                $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLACK);
[3290]68                for($i = 0; $i < $barCount; $i++)
69                {
70                        $v = ((sin(($t / 500.0)
71                                        + (1.0 * $i / $barCount)) + 1) / 2) * $this->getHeight();
72                        $p1_x = 0; $p1_y = intval($v);
73                        $p2_x = $this->getWidth() - 1; $p2_y = intval($v);
[2046]74
[3292]75                        $this->setColorAnsi(($i + 9), AnsiColor::BLACK);
[3290]76                        /* drawLine is already clipped, we don't care about overflows */
77                        $this->drawLine($p1_x + 0, $p1_y - 2, $p2_x + 0, $p2_y - 2, ord('-'));
78                        $this->drawLine($p1_x + 0, $p1_y - 1, $p2_x + 0, $p2_y - 1, ord('*'));
79                        $this->drawLine($p1_x,     $p1_y,     $p2_x,     $p2_y,     ord('#'));
80                        $this->drawLine($p1_x + 0, $p1_y + 1, $p2_x + 0, $p2_y + 1, ord('*'));
81                        $this->drawLine($p1_x + 0, $p1_y + 2, $p2_x + 0, $p2_y + 2, ord('-'));
82                }
[921]83
[3290]84                $w = $this->getWidth();
85                $h = $this->getHeight();
86                $x = intval(($t / 10) % (12 * $w));
87                $y = intval($h * (2.0 + sin($t / 200.0)) / 4);
88                $this->d->bitmap($this, - $x, $h / 2 - $y, $w * 12, $y * 2);
89                $this->d->bitmap($this, 12 * $w - $x, $h / 2 - $y, $w * 12, $y * 2);
90 
[3292]91                $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLUE);
[3290]92                $this->putStr($this->getWidth() - 30, $this->getHeight() - 2, " -=[ Powered by libcaca ]=- ");
[3292]93                $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLACK);
[3290]94        }
[2047]95}
[921]96
[3290]97class DemoDisplay extends Display
[2047]98{
[3290]99        private $cv;
[921]100
[3290]101        function __construct(DemoCanvas $_cv)
102        {
103                parent::__construct($_cv);
104                $this->setTitle("libcaca PHP Bindings test suite");
105                $this->setDisplayTime(20000); // Refresh every 20 ms
106                $this->cv = $_cv;
107        }
[921]108
[3290]109        function EventLoop()
110        {
[3292]111                while(! ($ev = $this->getEvent(EventType::KEY_RELEASE, 10)))
[3290]112                {
113                        $this->cv->Draw();
[2052]114
[3290]115                        $this->Refresh();
116                }
[921]117
[3290]118                if($ev->getKeyCh() > 0x20 && $ev->getKeyCh() < 0x7f)
119                        printf("Key pressed: %c\n", $ev->getKeyCh());
120                else
121                        printf("Key pressed: 0x%x\n", $ev->getKeyCh());
122        }
[2047]123}
[2046]124
[2047]125class Test
126{
[3290]127        static function Main()
128        {
129                printf("libcaca %s PHP test\n", caca_get_version());
130                printf("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>\n");
131                printf("(c) 2007 Sam Hocevar <sam@zoy.org>\n");
132                printf("(c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>\n");
[1428]133
[3290]134                /* Instanciate a caca canvas */
135                $cv = new DemoCanvas();
[1428]136
[3290]137                /* We have a proper canvas, let's display it using Caca */
138                $dp = new DemoDisplay($cv);
[2046]139
[3290]140                /* Random number. This is a static method,
141                not to be used with previous instance */
142                printf("A random number: %d\n", caca_rand(0, 1337));
[1428]143
[3290]144                $dp->EventLoop();
145        }
[2080]146}
[921]147
[3290]148Test::Main();
149
150?>
Note: See TracBrowser for help on using the repository browser.