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

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