Browse Source

* Use System.Drawing.Rectangle etc. for cucul-sharp and caca-sharp arguments,

allowing for much cleaner code (at the expense of an extra dependency).
tags/v0.99.beta14
Sam Hocevar sam 17 years ago
parent
commit
1c03b72337
4 changed files with 94 additions and 113 deletions
  1. +20
    -34
      csharp/Caca.cs
  2. +52
    -61
      csharp/Cucul.cs
  3. +6
    -5
      csharp/Makefile.am
  4. +16
    -13
      csharp/test.cs

+ 20
- 34
csharp/Caca.cs View File

@@ -16,6 +16,7 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Drawing;

using Cucul;

@@ -112,18 +113,18 @@ namespace Caca
public class CacaEvent : IDisposable
{
public IntPtr cevent;
private IntPtr utf8;
private IntPtr _utf8;

public CacaEvent()
{
cevent = Marshal.AllocHGlobal(32);
utf8 = Marshal.AllocHGlobal(8);
_utf8 = Marshal.AllocHGlobal(8);
}

public void Dispose()
{
Marshal.FreeHGlobal(cevent);
Marshal.FreeHGlobal(utf8);
Marshal.FreeHGlobal(_utf8);
GC.SuppressFinalize(this);
}

@@ -154,13 +155,13 @@ namespace Caca
[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_event_key_utf8(IntPtr ev,
IntPtr utf8);
IntPtr _utf8);
public string keyUtf8
{
get
{
caca_get_event_key_utf8(cevent, utf8);
return Marshal.PtrToStringAnsi(utf8);
caca_get_event_key_utf8(cevent, _utf8);
return Marshal.PtrToStringAnsi(_utf8);
}
}

@@ -175,33 +176,25 @@ namespace Caca
[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_event_mouse_x(IntPtr ev);
public int mouseX
{
get { return caca_get_event_mouse_x(cevent); }
}

[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_event_mouse_y(IntPtr ev);
public int mouseY
public Point mousePos
{
get { return caca_get_event_mouse_y(cevent); }
get { return new Point(caca_get_event_mouse_x(cevent),
caca_get_event_mouse_y(cevent)); }
}

[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_event_resize_width(IntPtr ev);
public int resizeWidth
{
get { return caca_get_event_resize_width(cevent); }
}

[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_event_resize_height(IntPtr ev);
public int resizeHeight
public Size resizeSize
{
get { return caca_get_event_resize_height(cevent); }
get { return new Size(caca_get_event_resize_width(cevent),
caca_get_event_resize_height(cevent)); }
}
}

@@ -263,17 +256,13 @@ namespace Caca
[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_display_width(IntPtr dp);
public int width
{
get { return caca_get_display_width(_dp); }
}

[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_display_height(IntPtr dp);
public int height
public Size Size
{
get { return caca_get_display_height(_dp); }
get { return new Size(caca_get_display_width(_dp),
caca_get_display_height(_dp)); }
}

[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
@@ -295,17 +284,13 @@ namespace Caca
[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_mouse_x(IntPtr k);
public int mouseX
{
get { return caca_get_mouse_x(_dp); }
}

[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int caca_get_mouse_y(IntPtr k);
public int mouseY
public Point mousePos
{
get { return caca_get_mouse_y(_dp); }
get { return new Point(caca_get_mouse_x(_dp),
caca_get_mouse_y(_dp)); }
}

[DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
@@ -317,3 +302,4 @@ namespace Caca
}
}
}


+ 52
- 61
csharp/Cucul.cs View File

@@ -16,6 +16,7 @@
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Drawing;

namespace Cucul
{
@@ -77,9 +78,9 @@ namespace Cucul
_cv = cucul_create_canvas(0, 0);
}

public CuculCanvas(int w, int h)
public CuculCanvas(Size s)
{
_cv = cucul_create_canvas(w, h);
_cv = cucul_create_canvas(s.Width, s.Height);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
@@ -95,29 +96,29 @@ namespace Cucul
SuppressUnmanagedCodeSecurity]
private static extern int cucul_set_canvas_size(IntPtr cv,
int w, int h);
public void setSize(int w, int h)
public void setSize(Size s)
{
cucul_set_canvas_size(_cv, w, h);
cucul_set_canvas_size(_cv, s.Width, s.Height);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_get_canvas_width(IntPtr cv);
public int width
{
get { return cucul_get_canvas_width(_cv); }
set { cucul_set_canvas_size(_cv, value,
cucul_get_canvas_height(_cv)); }
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_get_canvas_height(IntPtr cv);
public int height
public Size Size
{
get { return cucul_get_canvas_height(_cv); }
set { cucul_set_canvas_size(_cv, cucul_get_canvas_width(_cv),
value); }
get { return new Size(cucul_get_canvas_width(_cv),
cucul_get_canvas_height(_cv)); }
set { cucul_set_canvas_size(_cv, value.Width, value.Height); }
}

public Rectangle Rectangle
{
get { return new Rectangle(0, 0, cucul_get_canvas_width(_cv),
cucul_get_canvas_height(_cv)); }
set { cucul_set_canvas_size(_cv, value.Width, value.Height); }
}

/* canvas drawing */
@@ -131,50 +132,45 @@ namespace Cucul
[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_get_cursor_y(IntPtr cv);
public int cursorX
public Point Cursor
{
get { return cucul_get_cursor_x(_cv); }
set { cucul_gotoxy(_cv, value, cucul_get_cursor_y(_cv)); }
}

public int cursorY
{
get { return cucul_get_cursor_y(_cv); }
set { cucul_gotoxy(_cv, cucul_get_cursor_x(_cv), value); }
get { return new Point(cucul_get_cursor_x(_cv),
cucul_get_cursor_y(_cv)); }
set { cucul_gotoxy(_cv, value.X, value.Y); }
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_put_char(IntPtr cv,
int x, int y, int c);
public int putChar(int x, int y, int c)
public int putChar(Point p, int c)
{
return cucul_put_char(_cv, x, y, c);
return cucul_put_char(_cv, p.X, p.Y, c);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_get_char(IntPtr cv, int x, int y);
public int getChar(int x, int y)
public int getChar(Point p)
{
return cucul_get_char(_cv, x, y);
return cucul_get_char(_cv, p.X, p.Y);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_put_str(IntPtr cv,
int x, int y, string c);
public int putStr(int x, int y, string c)
public int putStr(Point p, string c)
{
return cucul_put_str(_cv, x, y, c);
return cucul_put_str(_cv, p.X, p.Y, c);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_get_attr(IntPtr cv, int x, int y);
public int getAttr(int x, int y)
public int getAttr(Point p)
{
return cucul_get_attr(_cv, x, y);
return cucul_get_attr(_cv, p.X, p.Y);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
@@ -189,9 +185,9 @@ namespace Cucul
SuppressUnmanagedCodeSecurity]
private static extern int cucul_put_attr(IntPtr cv,
int x, int y, int a);
public int putAttr(int x, int y, int a)
public int putAttr(Point p, int a)
{
return cucul_put_attr(_cv, x, y, a);
return cucul_put_attr(_cv, p.X, p.Y, a);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
@@ -230,42 +226,36 @@ namespace Cucul
[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_get_canvas_handle_y(IntPtr cv);
public int handleX
{
get { return cucul_get_canvas_handle_x(_cv); }
set { cucul_set_canvas_handle(_cv, value,
cucul_get_canvas_handle_y(_cv)); }
}

public int handleY
public Point Handle
{
get { return cucul_get_canvas_handle_y(_cv); }
set { cucul_set_canvas_handle(_cv, cucul_get_canvas_handle_x(_cv),
value); }
get { return new Point(cucul_get_canvas_handle_x(_cv),
cucul_get_canvas_handle_y(_cv)); }
set { cucul_set_canvas_handle(_cv, value.X, value.Y); }
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_blit(IntPtr cv, int x, int y,
IntPtr cv1, IntPtr cv2);
public int Blit(int x, int y, CuculCanvas canvas)
public int Blit(Point p, CuculCanvas canvas)
{
return cucul_blit(_cv, x, y, canvas._cv, IntPtr.Zero);
return cucul_blit(_cv, p.X, p.Y, canvas._cv, IntPtr.Zero);
}

public int Blit(int x, int y, CuculCanvas cv, CuculCanvas mask)
public int Blit(Point p, CuculCanvas cv, CuculCanvas mask)
{
return cucul_blit(_cv, x, y, cv._cv, mask._cv);
return cucul_blit(_cv, p.X, p.Y, cv._cv, mask._cv);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_set_canvas_boundaries(IntPtr cv,
int x, int y,
int h, int w);
public int setBoundaries(int x, int y, int h, int w)
int h, int w);
public int setBoundaries(Rectangle r)
{
return cucul_set_canvas_boundaries(_cv, x, y, h, w);
return cucul_set_canvas_boundaries(_cv, r.X, r.Y,
r.Width, r.Height);
}

/* canvas transformation */
@@ -339,11 +329,13 @@ namespace Cucul

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_draw_line(IntPtr cv, int x1, int y1, int x2, int y2, int c);
public int drawLine(int x1, int y1, int x2, int y2, int c)
private static extern int cucul_draw_line(IntPtr cv, int x1, int y1,
int x2, int y2, int c);
public int drawLine(Point p1, Point p2, int c)
{
return cucul_draw_line(_cv, x1, y1, x2, y2, c);
return cucul_draw_line(_cv, p1.X, p1.Y, p2.X, p2.Y, c);
}

[DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
SuppressUnmanagedCodeSecurity]
private static extern int cucul_draw_polyline(IntPtr cv, int[] x, int[] y, int n, IntPtr c);
@@ -407,12 +399,11 @@ namespace Cucul
private static extern int cucul_dither_bitmap(IntPtr c, int x, int y,
int w, int h,
IntPtr d, IntPtr data);
public int ditherBitmap(int x, int y, int w, int h, CuculDither d,
object data)
public int ditherBitmap(Rectangle r, CuculDither d, object data)
{
GCHandle gch = GCHandle.Alloc(data, GCHandleType.Pinned);
int ret = cucul_dither_bitmap(_cv, x, y, w, h, d._dither,
gch.AddrOfPinnedObject());
int ret = cucul_dither_bitmap(_cv, r.X, r.Y, r.Width, r.Height,
d._dither, gch.AddrOfPinnedObject());
gch.Free();
return ret;
}
@@ -464,10 +455,10 @@ namespace Cucul
ulong gmask,
ulong bmask,
ulong amask);
public CuculDither(int bpp, int w,int h, int pitch,
public CuculDither(int bpp, Size s, int pitch,
uint rmask, uint gmask, uint bmask, uint amask)
{
_dither = cucul_create_dither(bpp, w, h, pitch,
_dither = cucul_create_dither(bpp, s.Width, s.Height, pitch,
rmask, gmask, bmask, amask);
}



+ 6
- 5
csharp/Makefile.am View File

@@ -17,15 +17,16 @@ EXTRA_DIST = $(cucul_sources) cucul-sharp.dll.config \
test.cs

cucul-sharp.dll: $(cucul_sources)
gmcs $(cucul_sources) -out:$@ -target:library -unsafe
gmcs -unsafe $(cucul_sources) -out:$@ -target:library \
-r:System.Drawing.dll

caca-sharp.dll: $(caca_sources) cucul-sharp.dll
gmcs $(caca_sources) -out:$@ -target:library \
-r:./cucul-sharp.dll -lib:./ -unsafe
gmcs -unsafe $(caca_sources) -out:$@ -target:library -lib:./ \
-r:System.Drawing.dll -r:./cucul-sharp.dll

test.exe: test.cs caca-sharp.dll cucul-sharp.dll
gmcs test.cs -out:$@ \
-r:./cucul-sharp.dll -r:./caca-sharp.dll -lib:./ -unsafe
gmcs test.cs -out:$@ -lib:./ \
-r:System.Drawing.dll -r:./cucul-sharp.dll -r:./caca-sharp.dll

clean-local:
rm -f *.exe *.dll


+ 16
- 13
csharp/test.cs View File

@@ -15,6 +15,7 @@


using System;
using System.Drawing;
using System.Runtime.InteropServices;

using Cucul;
@@ -22,7 +23,7 @@ using Caca;

class DemoCanvas : CuculCanvas
{
private uint[,] table;
private uint[,] image;

private DateTime startTime;
private CuculDither d;
@@ -31,8 +32,9 @@ class DemoCanvas : CuculCanvas
{
startTime = DateTime.Now;

table = new uint[16,16];
d = new CuculDither(32, 16, 16, 16 * 4, 0xff0000, 0xff00, 0xff, 0x0);
image = new uint[16,16];
d = new CuculDither(32, new Size(16, 16), 16 * 4,
0xff0000, 0xff00, 0xff, 0x0);
}

public void Draw()
@@ -55,30 +57,31 @@ class DemoCanvas : CuculCanvas
if(x2 < 0) x2 = 0;
if(y2 < 0) y2 = 0;

table[x,y] = (uint)((x2 + y2) << 16)
image[x,y] = (uint)((x2 + y2) << 16)
| (uint)(x2 << 8)
| (uint)(y2);
}
ditherBitmap(0, 0, width, height, d, table);
ditherBitmap(Rectangle, d, image);

setColorAnsi(Libcucul.WHITE, Libcucul.BLACK);
for(int i = 0; i < barCount; i++)
{
double v = ((Math.Sin((t / 500.0)
+ (i / ((double)barCount))) + 1) / 2) * height;
int y = (int)v;
+ (i / ((double)barCount))) + 1) / 2) * Size.Height;
Point p1 = new Point(0, (int)v);
Point p2 = new Point(Size.Width - 1, (int)v);

setColorAnsi(i + 9, Libcucul.BLACK);
/* drawLine is already clipped, we don't care about overflows */
drawLine(0, y - 2, width, y - 2, '-');
drawLine(0, y - 1, width, y - 1, '*');
drawLine(0, y, width, y, '#');
drawLine(0, y + 1, width, y + 1, '*');
drawLine(0, y + 2, width, y + 2, '-');
drawLine(p1 + new Size(0, -2), p2 + new Size(0, -2), '-');
drawLine(p1 + new Size(0, -1), p2 + new Size(0, -1), '*');
drawLine(p1, p2, '#');
drawLine(p1 + new Size(0, 1), p2 + new Size(0, 1), '*');
drawLine(p1 + new Size(0, 2), p2 + new Size(0, 2), '-');
}

setColorAnsi(Libcucul.WHITE, Libcucul.BLUE);
putStr(width - 30, height - 2, " -=[ Powered by libcaca ]=- ");
putStr(new Point(-30, -2) + Size, " -=[ Powered by libcaca ]=- ");
setColorAnsi(Libcucul.WHITE, Libcucul.BLACK);
}
}


Loading…
Cancel
Save