Debian Codegraph @

caca-php/examples/test.php

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