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

Last change on this file since 3289 was 3289, checked in by bsittler, 11 years ago

copy test.cs to test.php in preparation for phpizing

  • Property svn:keywords set to Id
File size: 4.1 KB
RevLine 
[921]1/*
[2058]2 *  Test          .NET bindings test program
[921]3 *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
[2058]4 *                2007 Sam Hocevar <sam@zoy.org>
[921]5 *                All Rights Reserved
6 *
[943]7 *  $Id: test.php 3289 2008-11-04 23:57:57Z bsittler $
[921]8 *
[1462]9 *  This program is free software. It comes without any warranty, to
[1452]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
[921]13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16
17using System;
[2078]18using System.Drawing;
[2058]19using System.Runtime.InteropServices;
[921]20
[2045]21using Caca;
22
[2867]23class DemoCanvas : Canvas
[2047]24{
[2078]25    private uint[,] image;
[2058]26
[2047]27    private DateTime startTime;
[2867]28    private Dither d;
29    private Canvas scroll;
[921]30
[2047]31    public DemoCanvas()
32    {
33        startTime = DateTime.Now;
[2058]34
[2090]35        string message = " --- POWERED BY LIBCACA --- OLDSCHOOL TEXT EFFECTS ARE 100% PURE WIN";
[2082]36
[2867]37        scroll = new Canvas(new Size(message.Length, 1));
38        scroll.setColorAnsi(AnsiColor.WHITE, AnsiColor.TRANSPARENT);
[2082]39        scroll.putStr(new Point(0, 0), message);
40
[2867]41        Caca.Font f = new Caca.Font(Caca.Font.getList()[1]);
[2082]42        int w = f.Size.Width * message.Length;
43        int h = f.Size.Height;
44        image = new uint[w, h];
[2867]45        d = new Dither(32, new Size(w, h), w * 4,
[2090]46                            0xff00, 0xff0000, 0xff000000, 0xff);
[2082]47        f.Render(scroll, image, image.GetLength(0) * 4);
[2047]48    }
[921]49
[2047]50    public void Draw()
51    {
[1428]52        int barCount = 6;
[2047]53        double t = (DateTime.Now - startTime).TotalMilliseconds;
[2046]54
[2047]55        Clear();
[1424]56
[2867]57        setColorAnsi(AnsiColor.WHITE, AnsiColor.BLACK);
[2047]58        for(int i = 0; i < barCount; i++)
59        {
60            double v = ((Math.Sin((t / 500.0)
[2078]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);
[2046]64
[2867]65            setColorAnsi((uint)(i + 9), AnsiColor.BLACK);
[2047]66            /* drawLine is already clipped, we don't care about overflows */
[2078]67            drawLine(p1 + new Size(0, -2), p2 + new Size(0, -2), '-');
68            drawLine(p1 + new Size(0, -1), p2 + new Size(0, -1), '*');
[2080]69            drawLine(p1,                   p2,                   '#');
[2078]70            drawLine(p1 + new Size(0,  1), p2 + new Size(0,  1), '*');
71            drawLine(p1 + new Size(0,  2), p2 + new Size(0,  2), '-');
[2047]72        }
[921]73
[2090]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
[2867]81        setColorAnsi(AnsiColor.WHITE, AnsiColor.BLUE);
[2078]82        putStr(new Point(-30, -2) + Size, " -=[ Powered by libcaca ]=- ");
[2867]83        setColorAnsi(AnsiColor.WHITE, AnsiColor.BLACK);
[2047]84    }
85}
[921]86
[2867]87class DemoDisplay : Display
[2047]88{
89    private DemoCanvas cv;
[921]90
[2047]91    public DemoDisplay(DemoCanvas _cv) : base(_cv)
92    {
[2089]93        Title = "libcaca .NET Bindings test suite";
94        DisplayTime = 20000; // Refresh every 20 ms
[2047]95        cv = _cv;
96    }
[921]97
[2047]98    public void EventLoop()
99    {
[2867]100        Event ev;
[2052]101
[2867]102        while((ev = getEvent(EventType.KEY_RELEASE, 10)).Type == 0)
[2047]103        {
104            cv.Draw();
[921]105
[2047]106            Refresh();
107        }
[2052]108
[2089]109        if(ev.KeyCh > 0x20 && ev.KeyCh < 0x7f)
110            Console.WriteLine("Key pressed: {0}", ev.KeyUtf8);
[2052]111        else
[2089]112            Console.WriteLine("Key pressed: 0x{0:x}", ev.KeyCh);
[2047]113    }
114}
[2046]115
[2047]116class Test
117{
118    public static void Main()
119    {
[2076]120        Console.WriteLine("libcaca {0} .NET test", Libcaca.getVersion());
[2047]121        Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>");
[1428]122
[2822]123        /* Instanciate a caca canvas */
[2047]124        DemoCanvas cv = new DemoCanvas();
[1428]125
[2047]126        /* We have a proper canvas, let's display it using Caca */
127        DemoDisplay dp = new DemoDisplay(cv);
[2046]128
[2047]129        /* Random number. This is a static method,
130           not to be used with previous instance */
[2822]131        Console.WriteLine("A random number: {0}", Libcaca.Rand(0, 1337));
[1428]132
[2047]133        dp.EventLoop();
134    }
[2080]135}
[921]136
Note: See TracBrowser for help on using the repository browser.