source: libcaca/trunk/csharp/test.cs @ 2078

Last change on this file since 2078 was 2078, checked in by Sam Hocevar, 13 years ago
  • Use System.Drawing.Rectangle etc. for cucul-sharp and caca-sharp arguments, allowing for much cleaner code (at the expense of an extra dependency).
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
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: test.cs 2078 2007-11-27 00:58:16Z sam $
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 */
15
16
17using System;
18using System.Drawing;
19using System.Runtime.InteropServices;
20
21using Cucul;
22using Caca;
23
24class DemoCanvas : CuculCanvas
25{
26    private uint[,] image;
27
28    private DateTime startTime;
29    private CuculDither d;
30
31    public DemoCanvas()
32    {
33        startTime = DateTime.Now;
34
35        image = new uint[16,16];
36        d = new CuculDither(32, new Size(16, 16), 16 * 4,
37                            0xff0000, 0xff00, 0xff, 0x0);
38    }
39
40    public void Draw()
41    {
42        int barCount = 6;
43        double t = (DateTime.Now - startTime).TotalMilliseconds;
44
45        Clear();
46
47        double cos = Math.Cos(t / 500.0);
48        double sin = Math.Sin(t / 500.0);
49
50        for(int y = 0; y < 16; y++)
51            for(int x = 0; x < 16; x++)
52            {
53                double xt = (double)(x - 8);
54                double yt = (double)(y - 8);
55                int x2 = (int)(xt * cos + yt * sin + 8.0);
56                int y2 = (int)(xt * sin - yt * cos + 8.0);
57                if(x2 < 0) x2 = 0;
58                if(y2 < 0) y2 = 0;
59
60                image[x,y] = (uint)((x2 + y2) << 16)
61                              | (uint)(x2 << 8)
62                              | (uint)(y2);
63            }
64        ditherBitmap(Rectangle, d, image);
65
66        setColorAnsi(Libcucul.WHITE, Libcucul.BLACK);
67        for(int i = 0; i < barCount; i++)
68        {
69            double v = ((Math.Sin((t / 500.0)
70                          + (i / ((double)barCount))) + 1) / 2) * Size.Height;
71            Point p1 = new Point(0, (int)v);
72            Point p2 = new Point(Size.Width - 1, (int)v);
73
74            setColorAnsi(i + 9, Libcucul.BLACK);
75            /* drawLine is already clipped, we don't care about overflows */
76            drawLine(p1 + new Size(0, -2), p2 + new Size(0, -2), '-');
77            drawLine(p1 + new Size(0, -1), p2 + new Size(0, -1), '*');
78            drawLine(p1,                    p2,                    '#');
79            drawLine(p1 + new Size(0,  1), p2 + new Size(0,  1), '*');
80            drawLine(p1 + new Size(0,  2), p2 + new Size(0,  2), '-');
81        }
82
83        setColorAnsi(Libcucul.WHITE, Libcucul.BLUE);
84        putStr(new Point(-30, -2) + Size, " -=[ Powered by libcaca ]=- ");
85        setColorAnsi(Libcucul.WHITE, Libcucul.BLACK);
86    }
87}
88
89class DemoDisplay : CacaDisplay
90{
91    private DemoCanvas cv;
92
93    public DemoDisplay(DemoCanvas _cv) : base(_cv)
94    {
95        displayTime = 20000; // Refresh every 20 ms
96        title = "libcaca .NET Bindings test suite";
97        cv = _cv;
98    }
99
100    public void EventLoop()
101    {
102        CacaEvent ev;
103
104        while((ev = getEvent(CacaEventType.KEY_RELEASE, 10)).type == 0)
105        {
106            cv.Draw();
107
108            Refresh();
109        }
110
111        if(ev.keyCh > 0x20 && ev.keyCh < 0x7f)
112            Console.WriteLine("Key pressed: {0}", ev.keyUtf8);
113        else
114            Console.WriteLine("Key pressed: 0x{0:x}", ev.keyCh);
115    }
116}
117
118class Test
119{
120    public static void Main()
121    {
122        Console.WriteLine("libcaca {0} .NET test", Libcaca.getVersion());
123        Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>");
124
125        /* Instanciate a cucul canvas */
126        DemoCanvas cv = new DemoCanvas();
127
128        /* We have a proper canvas, let's display it using Caca */
129        DemoDisplay dp = new DemoDisplay(cv);
130
131        /* Random number. This is a static method,
132           not to be used with previous instance */
133        Console.WriteLine("A random number: {0}", Libcucul.Rand(0, 1337));
134
135        dp.EventLoop();
136
137        /* Force deletion of our instances for fun */
138        dp.Dispose();
139        cv.Dispose();
140    }
141
142}
Note: See TracBrowser for help on using the repository browser.