1 /*
2  *  Test          .NET bindings test program
3  *  Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
4  *                2007 Sam Hocevar <sam@hocevar.net>
5  *                All Rights Reserved
6  *
7  *  This program is free software. It comes without any warranty, to
8  *  the extent permitted by applicable law. You can redistribute it
9  *  and/or modify it under the terms of the Do What the Fuck You Want
10  *  to Public License, Version 2, as published by Sam Hocevar. See
11  *  http://www.wtfpl.net/ for more details.
12  */
13 
14 
15 using System;
16 using System.Drawing;
17 using System.Runtime.InteropServices;
18 
19 using Caca;
20 
21 class DemoCanvas : Canvas
22 {
23     private uint[,] image;
24 
25     private DateTime startTime;
26     private Dither d;
27     private Canvas scroll;
28 
DemoCanvas()29     public DemoCanvas()
30     {
31         startTime = DateTime.Now;
32 
33         string message = " --- POWERED BY LIBCACA --- OLDSCHOOL TEXT EFFECTS ARE 100% PURE WIN";
34 
35         scroll = new Canvas(new Size(message.Length, 1));
36         scroll.setColorAnsi(AnsiColor.WHITE, AnsiColor.TRANSPARENT);
37         scroll.putStr(new Point(0, 0), message);
38 
39         Caca.Font f = new Caca.Font(Caca.Font.getList()[1]);
40         int w = f.Size.Width * message.Length;
41         int h = f.Size.Height;
42         image = new uint[w, h];
43         d = new Dither(32, new Size(w, h), w * 4,
44                             0xff00, 0xff0000, 0xff000000, 0xff);
45         f.Render(scroll, image, image.GetLength(0) * 4);
46     }
47 
Draw()48     public void Draw()
49     {
50         int barCount = 6;
51         double t = (DateTime.Now - startTime).TotalMilliseconds;
52 
53         Clear();
54 
55         setColorAnsi(AnsiColor.WHITE, AnsiColor.BLACK);
56         for(int i = 0; i < barCount; i++)
57         {
58             double v = ((Math.Sin((t / 500.0)
59                           + (i / ((double)barCount))) + 1) / 2) * Size.Height;
60             Point p1 = new Point(0, (int)v);
61             Point p2 = new Point(Size.Width - 1, (int)v);
62 
63             setColorAnsi((uint)(i + 9), AnsiColor.BLACK);
64             /* drawLine is already clipped, we don't care about overflows */
65             drawLine(p1 + new Size(0, -2), p2 + new Size(0, -2), '-');
66             drawLine(p1 + new Size(0, -1), p2 + new Size(0, -1), '*');
67             drawLine(p1,                   p2,                   '#');
68             drawLine(p1 + new Size(0,  1), p2 + new Size(0,  1), '*');
69             drawLine(p1 + new Size(0,  2), p2 + new Size(0,  2), '-');
70         }
71 
72         int w = Size.Width;
73         int h = Size.Height;
74         int x = (int)(t / 10) % (12 * w);
75         int y = (int)(h * (2.0 + Math.Sin(t / 200.0)) / 4);
76         ditherBitmap(new Rectangle(- x, h / 2 - y, w * 12, y * 2), d, image);
77         ditherBitmap(new Rectangle(12 * w - x, h / 2 - y, w * 12, y * 2), d, image);
78 
79         setColorAnsi(AnsiColor.WHITE, AnsiColor.BLUE);
80         putStr(new Point(-30, -2) + Size, " -=[ Powered by libcaca ]=- ");
81         setColorAnsi(AnsiColor.WHITE, AnsiColor.BLACK);
82     }
83 }
84 
85 class DemoDisplay : Display
86 {
87     private DemoCanvas cv;
88 
DemoDisplay(DemoCanvas _cv)89     public DemoDisplay(DemoCanvas _cv) : base(_cv)
90     {
91         Title = "libcaca .NET Bindings test suite";
92         DisplayTime = 20000; // Refresh every 20 ms
93         cv = _cv;
94     }
95 
EventLoop()96     public void EventLoop()
97     {
98         Event ev;
99 
100         while((ev = getEvent(EventType.KEY_RELEASE, 10)).Type == 0)
101         {
102             cv.Draw();
103 
104             Refresh();
105         }
106 
107         if(ev.KeyCh > 0x20 && ev.KeyCh < 0x7f)
108             Console.WriteLine("Key pressed: {0}", ev.KeyUtf8);
109         else
110             Console.WriteLine("Key pressed: 0x{0:x}", ev.KeyCh);
111     }
112 }
113 
114 class Test
115 {
Main()116     public static void Main()
117     {
118         Console.WriteLine("libcaca {0} .NET test", Libcaca.getVersion());
119         Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>");
120 
121         /* Instanciate a caca canvas */
122         DemoCanvas cv = new DemoCanvas();
123 
124         /* We have a proper canvas, let's display it using Caca */
125         DemoDisplay dp = new DemoDisplay(cv);
126 
127         /* Random number. This is a static method,
128            not to be used with previous instance */
129         Console.WriteLine("A random number: {0}", Libcaca.Rand(0, 1337));
130 
131         dp.EventLoop();
132     }
133 }
134 
135