Changeset 3290 for libcaca/trunk


Ignore:
Timestamp:
Nov 5, 2008, 1:04:35 AM (11 years ago)
Author:
bsittler
Message:

preliminary port of jylam and sam's test.cs to PHP using an early version of nico's caca.php; right now it looks in dirname($argv[0]) . "/.." for caca.php, which will need to be fixed once caca.php is installed in a php standard include directory

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca-php/examples/test.php

    • Property svn:executable set to *
    r3289 r3290  
    1 /*
    2  *  Test          .NET bindings test program
    3  *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
    4  *                2007 Sam Hocevar <sam@zoy.org>
    5  *                All Rights Reserved
    6  *
    7  *  $Id$
    8  *
    9  *  This program is free software. It comes without any warranty, to
    10  *  the extent permitted by applicable law. You can redistribute it
    11  *  and/or modify it under the terms of the Do What The Fuck You Want
    12  *  To Public License, Version 2, as published by Sam Hocevar. See
    13  *  http://sam.zoy.org/wtfpl/COPYING for more details.
    14  */
     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$
     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   */
    1521
    16 
    17 using System;
    18 using System.Drawing;
    19 using System.Runtime.InteropServices;
    20 
    21 using Caca;
    22 
    23 class DemoCanvas : Canvas
    24 {
    25     private uint[,] image;
    26 
    27     private DateTime startTime;
    28     private Dither d;
    29     private Canvas scroll;
    30 
    31     public DemoCanvas()
    32     {
    33         startTime = DateTime.Now;
    34 
    35         string message = " --- POWERED BY LIBCACA --- OLDSCHOOL TEXT EFFECTS ARE 100% PURE WIN";
    36 
    37         scroll = new Canvas(new Size(message.Length, 1));
    38         scroll.setColorAnsi(AnsiColor.WHITE, AnsiColor.TRANSPARENT);
    39         scroll.putStr(new Point(0, 0), message);
    40 
    41         Caca.Font f = new Caca.Font(Caca.Font.getList()[1]);
    42         int w = f.Size.Width * message.Length;
    43         int h = f.Size.Height;
    44         image = new uint[w, h];
    45         d = new Dither(32, new Size(w, h), w * 4,
    46                             0xff00, 0xff0000, 0xff000000, 0xff);
    47         f.Render(scroll, image, image.GetLength(0) * 4);
    48     }
    49 
    50     public void Draw()
    51     {
    52         int barCount = 6;
    53         double t = (DateTime.Now - startTime).TotalMilliseconds;
    54 
    55         Clear();
    56 
    57         setColorAnsi(AnsiColor.WHITE, AnsiColor.BLACK);
    58         for(int i = 0; i < barCount; i++)
    59         {
    60             double v = ((Math.Sin((t / 500.0)
    61                           + (i / ((double)barCount))) + 1) / 2) * Size.Height;
    62             Point p1 = new Point(0, (int)v);
    63             Point p2 = new Point(Size.Width - 1, (int)v);
    64 
    65             setColorAnsi((uint)(i + 9), AnsiColor.BLACK);
    66             /* drawLine is already clipped, we don't care about overflows */
    67             drawLine(p1 + new Size(0, -2), p2 + new Size(0, -2), '-');
    68             drawLine(p1 + new Size(0, -1), p2 + new Size(0, -1), '*');
    69             drawLine(p1,                   p2,                   '#');
    70             drawLine(p1 + new Size(0,  1), p2 + new Size(0,  1), '*');
    71             drawLine(p1 + new Size(0,  2), p2 + new Size(0,  2), '-');
    72         }
    73 
    74         int w = Size.Width;
    75         int h = Size.Height;
    76         int x = (int)(t / 10) % (12 * w);
    77         int y = (int)(h * (2.0 + Math.Sin(t / 200.0)) / 4);
    78         ditherBitmap(new Rectangle(- x, h / 2 - y, w * 12, y * 2), d, image);
    79         ditherBitmap(new Rectangle(12 * w - x, h / 2 - y, w * 12, y * 2), d, image);
    80 
    81         setColorAnsi(AnsiColor.WHITE, AnsiColor.BLUE);
    82         putStr(new Point(-30, -2) + Size, " -=[ Powered by libcaca ]=- ");
    83         setColorAnsi(AnsiColor.WHITE, AnsiColor.BLACK);
    84     }
     22if (php_sapi_name() != "cli") {
     23        die("You have to run this program with php-cli!\n");
    8524}
    8625
    87 class DemoDisplay : Display
     26include dirname($argv[0]) . '/../caca.php';
     27
     28class DemoCanvas extends Canvas
    8829{
    89     private DemoCanvas cv;
     30        private $image;
    9031
    91     public DemoDisplay(DemoCanvas _cv) : base(_cv)
    92     {
    93         Title = "libcaca .NET Bindings test suite";
    94         DisplayTime = 20000; // Refresh every 20 ms
    95         cv = _cv;
    96     }
     32        private $startTime;
     33        private $d;
     34        private $scroll;
    9735
    98     public void EventLoop()
    99     {
    100         Event ev;
     36        function __construct()
     37        {
     38                parent::__construct();
     39                $this->startTime = gettimeofday(true);
    10140
    102         while((ev = getEvent(EventType.KEY_RELEASE, 10)).Type == 0)
    103         {
    104             cv.Draw();
     41                $message = " --- POWERED BY LIBCACA --- OLDSCHOOL TEXT EFFECTS ARE 100% PURE WIN";
    10542
    106             Refresh();
    107         }
     43                $this->scroll = new Canvas(strlen($message), 1);
     44                $this->scroll->setColorAnsi(CACA_WHITE, CACA_TRANSPARENT);
     45                $this->scroll->putStr(0, 0, $message);
    10846
    109         if(ev.KeyCh > 0x20 && ev.KeyCh < 0x7f)
    110             Console.WriteLine("Key pressed: {0}", ev.KeyUtf8);
    111         else
    112             Console.WriteLine("Key pressed: 0x{0:x}", ev.KeyCh);
    113     }
     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        }
     59
     60        function Draw()
     61        {
     62                $barCount = 6;
     63                $t = (gettimeofday(true) - $this->startTime) * 1000;
     64
     65                $this->Clear();
     66
     67                $this->setColorAnsi(CACA_WHITE, CACA_BLACK);
     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);
     74
     75                        $this->setColorAnsi(($i + 9), CACA_BLACK);
     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                }
     83
     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 
     91                $this->setColorAnsi(CACA_WHITE, CACA_BLUE);
     92                $this->putStr($this->getWidth() - 30, $this->getHeight() - 2, " -=[ Powered by libcaca ]=- ");
     93                $this->setColorAnsi(CACA_WHITE, CACA_BLACK);
     94        }
     95}
     96
     97class DemoDisplay extends Display
     98{
     99        private $cv;
     100
     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        }
     108
     109        function EventLoop()
     110        {
     111                while(! ($ev = $this->getEvent(CACA_EVENT_KEY_RELEASE, 10)))
     112                {
     113                        $this->cv->Draw();
     114
     115                        $this->Refresh();
     116                }
     117
     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        }
    114123}
    115124
    116125class Test
    117126{
    118     public static void Main()
    119     {
    120         Console.WriteLine("libcaca {0} .NET test", Libcaca.getVersion());
    121         Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>");
     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");
    122133
    123         /* Instanciate a caca canvas */
    124         DemoCanvas cv = new DemoCanvas();
     134                /* Instanciate a caca canvas */
     135                $cv = new DemoCanvas();
    125136
    126         /* We have a proper canvas, let's display it using Caca */
    127         DemoDisplay dp = new DemoDisplay(cv);
     137                /* We have a proper canvas, let's display it using Caca */
     138                $dp = new DemoDisplay($cv);
    128139
    129         /* Random number. This is a static method,
    130            not to be used with previous instance */
    131         Console.WriteLine("A random number: {0}", Libcaca.Rand(0, 1337));
     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));
    132143
    133         dp.EventLoop();
    134     }
     144                $dp->EventLoop();
     145        }
    135146}
    136147
     148Test::Main();
     149
     150?>
Note: See TracChangeset for help on using the changeset viewer.