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

Last change on this file since 2052 was 2052, checked in by Sam Hocevar, 13 years ago
  • Adapt the C# bindings to the event API change.
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1/*
2 *  Test      .NET bindings test program
3 *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
4 *                All Rights Reserved
5 *
6 *  $Id: test.cs 2052 2007-11-25 11:12:25Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15
16using System;
17
18using Cucul;
19using Caca;
20
21class DemoCanvas : CuculCanvas
22{
23    private DateTime startTime;
24
25    public DemoCanvas()
26    {
27        startTime = DateTime.Now;
28    }
29
30    public void Draw()
31    {
32        int barCount = 6;
33        double t = (DateTime.Now - startTime).TotalMilliseconds;
34
35        Clear();
36
37        setColorAnsi(Libcucul.WHITE, Libcucul.BLACK);
38        for(int i = 0; i < barCount; i++)
39        {
40            double v = ((Math.Sin((t / 500.0)
41                          + (i / ((double)barCount))) + 1) / 2) * height;
42            int y = (int)v;
43
44            setColorAnsi(i + 9, Libcucul.BLACK);
45            /* drawLine is already clipped, we don't care about overflows */
46            drawLine(0, y - 2, width, y - 2, '-');
47            drawLine(0, y - 1, width, y - 1, '*');
48            drawLine(0, y,     width, y,     '#');
49            drawLine(0, y + 1, width, y + 1, '*');
50            drawLine(0, y + 2, width, y + 2, '-');
51        }
52
53        setColorAnsi(Libcucul.WHITE, Libcucul.BLUE);
54        putStr(width - 30, height - 2, " -=[ Powered by libcaca ]=- ");
55        setColorAnsi(Libcucul.WHITE, Libcucul.BLACK);
56    }
57}
58
59class DemoDisplay : CacaDisplay
60{
61    private DemoCanvas cv;
62
63    public DemoDisplay(DemoCanvas _cv) : base(_cv)
64    {
65        displayTime = 20000; // Refresh every 20 ms
66        title = "libcaca .NET Bindings test suite";
67        cv = _cv;
68    }
69
70    public void EventLoop()
71    {
72        CacaEvent ev;
73
74        while((ev = getEvent(CacaEventType.KEY_RELEASE, 10)).type == 0)
75        {
76            cv.Draw();
77
78            Refresh();
79        }
80
81        if(ev.keyCh > 0x20 && ev.keyCh < 0x7f)
82            Console.WriteLine("Key pressed: {0}", ev.keyUtf8);
83        else
84            Console.WriteLine("Key pressed: 0x{0:x}", ev.keyCh);
85    }
86}
87
88class Test
89{
90    public static void Main()
91    {
92        Console.WriteLine("libcaca .NET test");
93        Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>");
94
95        /* Instanciate a cucul canvas */
96        DemoCanvas cv = new DemoCanvas();
97
98        /* We have a proper canvas, let's display it using Caca */
99        DemoDisplay dp = new DemoDisplay(cv);
100
101        /* Random number. This is a static method,
102           not to be used with previous instance */
103        Console.WriteLine("A random number: {0}", Libcucul.Rand(0, 1337));
104
105        dp.EventLoop();
106
107        /* Force deletion of our instances for fun */
108        dp.Dispose();
109        cv.Dispose();
110    }
111
112}
Note: See TracBrowser for help on using the repository browser.