File: test.php

package info (click to toggle)
libcaca 0.99.beta20-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,264 kB
  • sloc: ansic: 25,091; php: 2,763; python: 2,637; cs: 1,213; cpp: 1,127; java: 916; objc: 836; makefile: 543; perl: 505; sh: 472; asm: 297; ruby: 215; xml: 33
file content (146 lines) | stat: -rwxr-xr-x 4,746 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/php5
<?php
  /*
   *  Test          PHP bindings test program
   *  Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>
   *
   *  This file is a Php port of "caca-sharp/test.cs"
   *  which is:
   *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
   *                2007 Sam Hocevar <sam@hocevar.net>
   *                All Rights Reserved
   *
   *  This program is free software. It comes without any warranty, to
   *  the extent permitted by applicable law. You can redistribute it
   *  and/or modify it under the terms of the Do What the Fuck You Want
   *  to Public License, Version 2, as published by Sam Hocevar. See
   *  http://www.wtfpl.net/ for more details.
   */

if (php_sapi_name() != "cli") {
    die("You have to run this program with php-cli!\n");
}

include dirname($argv[0]) . '/../caca.php';

class DemoCanvas extends Canvas
{
    private $image;

    private $startTime;
    private $d;
    private $scroll;

    function __construct()
    {
        parent::__construct();
        $this->startTime = gettimeofday(true);

        $message = " --- POWERED BY LIBCACA --- OLDSCHOOL TEXT EFFECTS ARE 100% PURE WIN";

        $this->scroll = new Canvas(strlen($message), 1);
        $this->scroll->setColorAnsi(AnsiColor::WHITE, AnsiColor::TRANSPARENT);
        $this->scroll->putStr(0, 0, $message);

        $fontList = Font::getList();
        $f = new Font($fontList[1]);
        $w = $f->getWidth() * strlen($message);
        $h = $f->getHeight();
        $this->image = imagecreatetruecolor($w, $h);
        imagealphablending($this->image, false);
        imagesavealpha($this->image, true);
        $this->d = new Dither($this->image);
        $f->Render($this->scroll, $this->image);
    }

    function Draw()
    {
        $barCount = 6;
        $t = (gettimeofday(true) - $this->startTime) * 1000;

        $this->Clear();

        $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLACK);
        for($i = 0; $i < $barCount; $i++)
        {
            $v = ((sin(($t / 500.0)
                       + (1.0 * $i / $barCount)) + 1) / 2) * $this->getHeight();
            $p1_x = 0; $p1_y = intval($v);
            $p2_x = $this->getWidth() - 1; $p2_y = intval($v);

            $this->setColorAnsi(($i + 9), AnsiColor::BLACK);
            /* drawLine is already clipped, we don't care about overflows */
            $this->drawLine($p1_x + 0, $p1_y - 2, $p2_x + 0, $p2_y - 2, ord('-'));
            $this->drawLine($p1_x + 0, $p1_y - 1, $p2_x + 0, $p2_y - 1, ord('*'));
            $this->drawLine($p1_x,     $p1_y,     $p2_x,     $p2_y,     ord('#'));
            $this->drawLine($p1_x + 0, $p1_y + 1, $p2_x + 0, $p2_y + 1, ord('*'));
            $this->drawLine($p1_x + 0, $p1_y + 2, $p2_x + 0, $p2_y + 2, ord('-'));
        }

        $w = $this->getWidth();
        $h = $this->getHeight();
        $x = intval(($t / 10) % (12 * $w));
        $y = intval($h * (2.0 + sin($t / 200.0)) / 4);
        $this->d->bitmap($this, - $x, $h / 2 - $y, $w * 12, $y * 2);
        $this->d->bitmap($this, 12 * $w - $x, $h / 2 - $y, $w * 12, $y * 2);

        $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLUE);
        $this->putStr($this->getWidth() - 30, $this->getHeight() - 2, " -=[ Powered by libcaca ]=- ");
        $this->setColorAnsi(AnsiColor::WHITE, AnsiColor::BLACK);
    }
}

class DemoDisplay extends Display
{
    private $cv;

    function __construct(DemoCanvas $_cv)
    {
        parent::__construct($_cv);
        $this->setTitle("libcaca PHP Bindings test suite");
        $this->setDisplayTime(20000); // Refresh every 20 ms
        $this->cv = $_cv;
    }

    function EventLoop()
    {
        while(! ($ev = $this->getEvent(EventType::KEY_RELEASE, 10)))
        {
            $this->cv->Draw();

            $this->Refresh();
        }

        if($ev->getKeyCh() > 0x20 && $ev->getKeyCh() < 0x7f)
            printf("Key pressed: %c\n", $ev->getKeyCh());
        else
            printf("Key pressed: 0x%x\n", $ev->getKeyCh());
    }
}

class Test
{
    static function Main()
    {
        printf("libcaca %s PHP test\n", Libcaca::getVersion());
        printf("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>\n");
        printf("(c) 2007 Sam Hocevar <sam@hocevar.net>\n");
        printf("(c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>\n");

        /* Instanciate a caca canvas */
        $cv = new DemoCanvas();

        /* We have a proper canvas, let's display it using Caca */
        $dp = new DemoDisplay($cv);

        /* Random number. This is a static method,
         not to be used with previous instance */
        printf("A random number: %d\n", Libcaca::Rand(0, 1337));

        $dp->EventLoop();
    }
}

Test::Main();

?>