Changeset 2047


Ignore:
Timestamp:
11/24/07 20:56:33 (5 years ago)
Author:
sam
Message:
  • Rewrote the C# test in a more object-oriented way.
Location:
libcaca/trunk/csharp
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/csharp/Caca.cs

    r2046 r2047  
    8383 
    8484 
    85   public unsafe class Display : IDisposable 
     85  public unsafe class CacaDisplay : IDisposable 
    8686    { 
    8787    [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] 
     
    117117    IntPtr _dp; 
    118118 
    119     public Display(CuculCanvas cv) 
     119    public CacaDisplay(CuculCanvas cv) 
    120120    { 
    121121        _cv = cv._cv; 
  • libcaca/trunk/csharp/test.cs

    r2046 r2047  
    1919using Caca; 
    2020 
    21 class Test { 
     21class DemoCanvas : CuculCanvas 
     22{ 
     23    private DateTime startTime; 
    2224 
     25    public DemoCanvas() 
     26    { 
     27        startTime = DateTime.Now; 
     28    } 
    2329 
     30    public void Draw() 
     31    { 
     32        int barCount = 6; 
     33        double t = (DateTime.Now - startTime).TotalMilliseconds; 
    2434 
    25         public static void Main() { 
    26         int barCount = 6; 
     35        Clear(); 
     36 
     37        setColorAnsi(Libcucul.WHITE, Libcucul.BLACK); 
     38        for(int i = 0; i < barCount; i++) 
     39        { 
     40            double v = ((Math.Sin((t / 500.0) 
     41                          + (i / ((double)barCount))) + 1) / 2) * height; 
     42            int y = (int)v; 
     43 
     44            setColorAnsi(i + 9, Libcucul.BLACK); 
     45            /* drawLine is already clipped, we don't care about overflows */ 
     46            drawLine(0, y - 2, width, y - 2, '-'); 
     47            drawLine(0, y - 1, width, y - 1, '*'); 
     48            drawLine(0, y,     width, y,     '#'); 
     49            drawLine(0, y + 1, width, y + 1, '*'); 
     50            drawLine(0, y + 2, width, y + 2, '-'); 
     51        } 
     52 
     53        setColorAnsi(Libcucul.WHITE, Libcucul.BLUE); 
     54        putStr(width - 30, height - 2, " -=[ Powered by libcaca ]=- "); 
     55        setColorAnsi(Libcucul.WHITE, Libcucul.BLACK); 
     56    } 
     57} 
     58 
     59class DemoDisplay : CacaDisplay 
     60{ 
     61    private Event e; 
     62    private DemoCanvas cv; 
     63 
     64    public DemoDisplay(DemoCanvas _cv) : base(_cv) 
     65    { 
     66        setDisplayTime(20000); // Refresh every 20 ms 
     67        setDisplayTitle("libcaca .NET Bindings test suite"); 
     68        cv = _cv; 
     69        e = new Event(); 
     70    } 
     71 
     72    public void EventLoop() 
     73    { 
     74        while(getEvent(Event.type.KEY_RELEASE, e, 10) == 0) 
     75        { 
     76            cv.Draw(); 
     77 
     78            Refresh(); 
     79        } 
     80    } 
     81} 
     82 
     83class Test 
     84{ 
     85    public static void Main() 
     86    { 
    2787        Console.WriteLine("libcaca .NET test"); 
    28                 Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>"); 
     88        Console.WriteLine("(c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>"); 
    2989 
    3090        /* Instanciate a cucul canvas */ 
    31         CuculCanvas cv = new CuculCanvas(); 
     91        DemoCanvas cv = new DemoCanvas(); 
    3292 
     93        /* We have a proper canvas, let's display it using Caca */ 
     94        DemoDisplay dp = new DemoDisplay(cv); 
    3395 
    3496        /* Random number. This is a static method, 
     
    3698        Console.WriteLine("A random number : {0}", Libcucul.Rand(0, 1337)); 
    3799 
    38  
    39  
    40         /* We have a proper canvas, let's display it using Caca */ 
    41         Display dp = new Display(cv); 
    42         dp.setDisplayTime(20000); // Refresh every 20 ms 
    43  
    44         dp.setDisplayTitle("libcaca .NET Bindings test suite"); 
    45  
    46         double v; 
    47         Int32 y = 0; 
    48         Event e = new Event(); 
    49         int i; 
    50  
    51         DateTime startTime = DateTime.Now; 
    52         while(dp.getEvent(Event.type.KEY_RELEASE, e, 10) == 0) 
    53           { 
    54             TimeSpan curTime = DateTime.Now - startTime; 
    55             double t = curTime.TotalMilliseconds; 
    56             cv.setColorAnsi(Libcucul.WHITE, Libcucul.BLACK); 
    57             for(i=0; i<barCount;i++) 
    58              { 
    59                 v = ((Math.Sin((t/500.0)+(i/((double)barCount)))+1)/2)*cv.height; 
    60                 y = (Int32) v; 
    61  
    62  
    63  
    64                 cv.setColorAnsi(i+9, Libcucul.BLACK); 
    65                 /* drawLine is already clipped, we don't care about overflows */ 
    66                 cv.drawLine(0, y-2, cv.width, y-2, '-'); 
    67                 cv.drawLine(0, y-1, cv.width, y-1, '*'); 
    68                 cv.drawLine(0, y, cv.width, y, '#'); 
    69                 cv.drawLine(0, y+1, cv.width, y+1, '*'); 
    70                 cv.drawLine(0, y+2, cv.width, y+2, '-'); 
    71              } 
    72  
    73              cv.setColorAnsi(Libcucul.WHITE, Libcucul.BLUE); 
    74              cv.putStr(cv.width - 30,cv.height - 2," -=[ Powered by libcaca ]=- "); 
    75              cv.setColorAnsi(Libcucul.WHITE, Libcucul.BLACK); 
    76  
    77  
    78             dp.Refresh(); 
    79             cv.Clear(); 
    80  
    81           } 
     100        dp.EventLoop(); 
    82101 
    83102        /* Force deletion of our instances for fun */ 
    84103        dp.Dispose(); 
    85104        cv.Dispose(); 
    86         } 
     105    } 
    87106 
    88107} 
Note: See TracChangeset for help on using the changeset viewer.