142 строки
4.1 KiB

  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$
  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. using System;
  16. using System.Drawing;
  17. using System.Runtime.InteropServices;
  18. using Cucul;
  19. using Caca;
  20. class DemoCanvas : CuculCanvas
  21. {
  22. private uint[,] image;
  23. private DateTime startTime;
  24. private CuculDither d;
  25. private CuculFont f;
  26. public DemoCanvas()
  27. {
  28. startTime = DateTime.Now;
  29. image = new uint[16,16];
  30. d = new CuculDither(32, new Size(16, 16), 16 * 4,
  31. 0xff0000, 0xff00, 0xff, 0x0);
  32. f = new CuculFont(CuculFont.getList()[0]);
  33. f.getBlocks();
  34. }
  35. public void Draw()
  36. {
  37. int barCount = 6;
  38. double t = (DateTime.Now - startTime).TotalMilliseconds;
  39. Clear();
  40. double cos = Math.Cos(t / 500.0);
  41. double sin = Math.Sin(t / 500.0);
  42. for(int y = 0; y < 16; y++)
  43. for(int x = 0; x < 16; x++)
  44. {
  45. double xt = (double)(x - 8);
  46. double yt = (double)(y - 8);
  47. int x2 = (int)(xt * cos + yt * sin + 8.0);
  48. int y2 = (int)(xt * sin - yt * cos + 8.0);
  49. if(x2 < 0) x2 = 0;
  50. if(y2 < 0) y2 = 0;
  51. image[x,y] = (uint)((x2 + y2) << 16)
  52. | (uint)(x2 << 8)
  53. | (uint)(y2);
  54. }
  55. ditherBitmap(Rectangle, d, image);
  56. setColorAnsi(Libcucul.WHITE, Libcucul.BLACK);
  57. for(int i = 0; i < barCount; i++)
  58. {
  59. double v = ((Math.Sin((t / 500.0)
  60. + (i / ((double)barCount))) + 1) / 2) * Size.Height;
  61. Point p1 = new Point(0, (int)v);
  62. Point p2 = new Point(Size.Width - 1, (int)v);
  63. setColorAnsi(i + 9, Libcucul.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. setColorAnsi(Libcucul.WHITE, Libcucul.BLUE);
  72. putStr(new Point(-30, -2) + Size, " -=[ Powered by libcaca ]=- ");
  73. setColorAnsi(Libcucul.WHITE, Libcucul.BLACK);
  74. }
  75. }
  76. class DemoDisplay : CacaDisplay
  77. {
  78. private DemoCanvas cv;
  79. public DemoDisplay(DemoCanvas _cv) : base(_cv)
  80. {
  81. displayTime = 20000; // Refresh every 20 ms
  82. title = "libcaca .NET Bindings test suite";
  83. cv = _cv;
  84. }
  85. public void EventLoop()
  86. {
  87. CacaEvent ev;
  88. while((ev = getEvent(CacaEventType.KEY_RELEASE, 10)).type == 0)
  89. {
  90. cv.Draw();
  91. Refresh();
  92. }
  93. if(ev.keyCh > 0x20 && ev.keyCh < 0x7f)
  94. Console.WriteLine("Key pressed: {0}", ev.keyUtf8);
  95. else
  96. Console.WriteLine("Key pressed: 0x{0:x}", ev.keyCh);
  97. }
  98. }
  99. class Test
  100. {
  101. public static void Main()
  102. {
  103. Console.WriteLine("libcaca {0} .NET test", Libcaca.getVersion());
  104. Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>");
  105. /* Instanciate a cucul canvas */
  106. DemoCanvas cv = new DemoCanvas();
  107. /* We have a proper canvas, let's display it using Caca */
  108. DemoDisplay dp = new DemoDisplay(cv);
  109. /* Random number. This is a static method,
  110. not to be used with previous instance */
  111. Console.WriteLine("A random number: {0}", Libcucul.Rand(0, 1337));
  112. dp.EventLoop();
  113. }
  114. }