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

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