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

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