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 2090 2007-11-28 07:15:07Z 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 | |
---|
17 | using System; |
---|
18 | using System.Drawing; |
---|
19 | using System.Runtime.InteropServices; |
---|
20 | |
---|
21 | using Cucul; |
---|
22 | using Caca; |
---|
23 | |
---|
24 | class DemoCanvas : CuculCanvas |
---|
25 | { |
---|
26 | private uint[,] image; |
---|
27 | |
---|
28 | private DateTime startTime; |
---|
29 | private CuculDither d; |
---|
30 | private CuculCanvas scroll; |
---|
31 | |
---|
32 | public DemoCanvas() |
---|
33 | { |
---|
34 | startTime = DateTime.Now; |
---|
35 | |
---|
36 | string message = " --- POWERED BY LIBCACA --- OLDSCHOOL TEXT EFFECTS ARE 100% PURE WIN"; |
---|
37 | |
---|
38 | scroll = new CuculCanvas(new Size(message.Length, 1)); |
---|
39 | scroll.setColorAnsi(Libcucul.WHITE, Libcucul.TRANSPARENT); |
---|
40 | scroll.putStr(new Point(0, 0), message); |
---|
41 | |
---|
42 | CuculFont f = new CuculFont(CuculFont.getList()[1]); |
---|
43 | int w = f.Size.Width * message.Length; |
---|
44 | int h = f.Size.Height; |
---|
45 | image = new uint[w, h]; |
---|
46 | d = new CuculDither(32, new Size(w, h), w * 4, |
---|
47 | 0xff00, 0xff0000, 0xff000000, 0xff); |
---|
48 | f.Render(scroll, image, image.GetLength(0) * 4); |
---|
49 | } |
---|
50 | |
---|
51 | public void Draw() |
---|
52 | { |
---|
53 | int barCount = 6; |
---|
54 | double t = (DateTime.Now - startTime).TotalMilliseconds; |
---|
55 | |
---|
56 | Clear(); |
---|
57 | |
---|
58 | setColorAnsi(Libcucul.WHITE, Libcucul.BLACK); |
---|
59 | for(int i = 0; i < barCount; i++) |
---|
60 | { |
---|
61 | double v = ((Math.Sin((t / 500.0) |
---|
62 | + (i / ((double)barCount))) + 1) / 2) * Size.Height; |
---|
63 | Point p1 = new Point(0, (int)v); |
---|
64 | Point p2 = new Point(Size.Width - 1, (int)v); |
---|
65 | |
---|
66 | setColorAnsi(i + 9, Libcucul.BLACK); |
---|
67 | /* drawLine is already clipped, we don't care about overflows */ |
---|
68 | drawLine(p1 + new Size(0, -2), p2 + new Size(0, -2), '-'); |
---|
69 | drawLine(p1 + new Size(0, -1), p2 + new Size(0, -1), '*'); |
---|
70 | drawLine(p1, p2, '#'); |
---|
71 | drawLine(p1 + new Size(0, 1), p2 + new Size(0, 1), '*'); |
---|
72 | drawLine(p1 + new Size(0, 2), p2 + new Size(0, 2), '-'); |
---|
73 | } |
---|
74 | |
---|
75 | int w = Size.Width; |
---|
76 | int h = Size.Height; |
---|
77 | int x = (int)(t / 10) % (12 * w); |
---|
78 | int y = (int)(h * (2.0 + Math.Sin(t / 200.0)) / 4); |
---|
79 | ditherBitmap(new Rectangle(- x, h / 2 - y, w * 12, y * 2), d, image); |
---|
80 | ditherBitmap(new Rectangle(12 * w - x, h / 2 - y, w * 12, y * 2), d, image); |
---|
81 | |
---|
82 | setColorAnsi(Libcucul.WHITE, Libcucul.BLUE); |
---|
83 | putStr(new Point(-30, -2) + Size, " -=[ Powered by libcaca ]=- "); |
---|
84 | setColorAnsi(Libcucul.WHITE, Libcucul.BLACK); |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | class DemoDisplay : CacaDisplay |
---|
89 | { |
---|
90 | private DemoCanvas cv; |
---|
91 | |
---|
92 | public DemoDisplay(DemoCanvas _cv) : base(_cv) |
---|
93 | { |
---|
94 | Title = "libcaca .NET Bindings test suite"; |
---|
95 | DisplayTime = 20000; // Refresh every 20 ms |
---|
96 | cv = _cv; |
---|
97 | } |
---|
98 | |
---|
99 | public void EventLoop() |
---|
100 | { |
---|
101 | CacaEvent ev; |
---|
102 | |
---|
103 | while((ev = getEvent(CacaEventType.KEY_RELEASE, 10)).Type == 0) |
---|
104 | { |
---|
105 | cv.Draw(); |
---|
106 | |
---|
107 | Refresh(); |
---|
108 | } |
---|
109 | |
---|
110 | if(ev.KeyCh > 0x20 && ev.KeyCh < 0x7f) |
---|
111 | Console.WriteLine("Key pressed: {0}", ev.KeyUtf8); |
---|
112 | else |
---|
113 | Console.WriteLine("Key pressed: 0x{0:x}", ev.KeyCh); |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | class Test |
---|
118 | { |
---|
119 | public static void Main() |
---|
120 | { |
---|
121 | Console.WriteLine("libcaca {0} .NET test", Libcaca.getVersion()); |
---|
122 | Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>"); |
---|
123 | |
---|
124 | /* Instanciate a cucul canvas */ |
---|
125 | DemoCanvas cv = new DemoCanvas(); |
---|
126 | |
---|
127 | /* We have a proper canvas, let's display it using Caca */ |
---|
128 | DemoDisplay dp = new DemoDisplay(cv); |
---|
129 | |
---|
130 | /* Random number. This is a static method, |
---|
131 | not to be used with previous instance */ |
---|
132 | Console.WriteLine("A random number: {0}", Libcucul.Rand(0, 1337)); |
---|
133 | |
---|
134 | dp.EventLoop(); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|