You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

826 lines
32 KiB

  1. /*
  2. * libcucul .NET bindings for libcucul
  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 library 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.Runtime.InteropServices;
  17. using System.Security;
  18. using System.Drawing;
  19. namespace Cucul
  20. {
  21. /* Static libcucul stuff that does not fit in any object */
  22. public static class Libcucul
  23. {
  24. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  25. SuppressUnmanagedCodeSecurity]
  26. private static extern int cucul_rand(int min, int max);
  27. public static int Rand(int min, int max)
  28. {
  29. return cucul_rand(min, max);
  30. }
  31. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  32. SuppressUnmanagedCodeSecurity]
  33. private static extern IntPtr cucul_get_version();
  34. public static string getVersion()
  35. {
  36. return Marshal.PtrToStringAnsi(cucul_get_version());
  37. }
  38. public const int BLACK = 0x00,
  39. BLUE = 0x01,
  40. GREEN = 0x02,
  41. CYAN = 0x03,
  42. RED = 0x04,
  43. MAGENTA = 0x05,
  44. BROWN = 0x06,
  45. LIGHTGRAY = 0x07,
  46. DARKGRAY = 0x08,
  47. LIGHTBLUE = 0x09,
  48. LIGHTGREEN = 0x0a,
  49. LIGHTCYAN = 0x0b,
  50. LIGHTRED = 0x0c,
  51. LIGHTMAGENTA = 0x0d,
  52. YELLOW = 0x0e,
  53. WHITE = 0x0f,
  54. DEFAULT = 0x10,
  55. TRANSPARENT = 0x20;
  56. public const int BOLD = 0x01,
  57. ITALICS = 0x02,
  58. UNDERLINE = 0x04,
  59. BLINK = 0x08;
  60. }
  61. public class CuculCanvas : IDisposable
  62. {
  63. public readonly IntPtr _cv;
  64. /* libcucul basic functions */
  65. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  66. SuppressUnmanagedCodeSecurity]
  67. private static extern IntPtr cucul_create_canvas(int w, int h);
  68. public CuculCanvas()
  69. {
  70. _cv = cucul_create_canvas(0, 0);
  71. }
  72. public CuculCanvas(Size s)
  73. {
  74. _cv = cucul_create_canvas(s.Width, s.Height);
  75. }
  76. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  77. SuppressUnmanagedCodeSecurity]
  78. private static extern int cucul_free_canvas(IntPtr cv);
  79. public void Dispose()
  80. {
  81. cucul_free_canvas(_cv);
  82. GC.SuppressFinalize(this);
  83. }
  84. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  85. SuppressUnmanagedCodeSecurity]
  86. private static extern int cucul_set_canvas_size(IntPtr cv,
  87. int w, int h);
  88. public void setSize(Size s)
  89. {
  90. cucul_set_canvas_size(_cv, s.Width, s.Height);
  91. }
  92. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  93. SuppressUnmanagedCodeSecurity]
  94. private static extern int cucul_get_canvas_width(IntPtr cv);
  95. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  96. SuppressUnmanagedCodeSecurity]
  97. private static extern int cucul_get_canvas_height(IntPtr cv);
  98. public Size Size
  99. {
  100. get { return new Size(cucul_get_canvas_width(_cv),
  101. cucul_get_canvas_height(_cv)); }
  102. set { cucul_set_canvas_size(_cv, value.Width, value.Height); }
  103. }
  104. public Rectangle Rectangle
  105. {
  106. get { return new Rectangle(0, 0, cucul_get_canvas_width(_cv),
  107. cucul_get_canvas_height(_cv)); }
  108. set { cucul_set_canvas_size(_cv, value.Width, value.Height); }
  109. }
  110. /* canvas drawing */
  111. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  112. SuppressUnmanagedCodeSecurity]
  113. private static extern int cucul_gotoxy(IntPtr cv, int x, int y);
  114. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  115. SuppressUnmanagedCodeSecurity]
  116. private static extern int cucul_get_cursor_x(IntPtr cv);
  117. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  118. SuppressUnmanagedCodeSecurity]
  119. private static extern int cucul_get_cursor_y(IntPtr cv);
  120. public Point Cursor
  121. {
  122. get { return new Point(cucul_get_cursor_x(_cv),
  123. cucul_get_cursor_y(_cv)); }
  124. set { cucul_gotoxy(_cv, value.X, value.Y); }
  125. }
  126. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  127. SuppressUnmanagedCodeSecurity]
  128. private static extern int cucul_put_char(IntPtr cv,
  129. int x, int y, int c);
  130. public int putChar(Point p, int c)
  131. {
  132. return cucul_put_char(_cv, p.X, p.Y, c);
  133. }
  134. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  135. SuppressUnmanagedCodeSecurity]
  136. private static extern int cucul_get_char(IntPtr cv, int x, int y);
  137. public int getChar(Point p)
  138. {
  139. return cucul_get_char(_cv, p.X, p.Y);
  140. }
  141. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  142. SuppressUnmanagedCodeSecurity]
  143. private static extern int cucul_put_str(IntPtr cv,
  144. int x, int y, string c);
  145. public int putStr(Point p, string c)
  146. {
  147. return cucul_put_str(_cv, p.X, p.Y, c);
  148. }
  149. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  150. SuppressUnmanagedCodeSecurity]
  151. private static extern int cucul_get_attr(IntPtr cv, int x, int y);
  152. public int getAttr(Point p)
  153. {
  154. return cucul_get_attr(_cv, p.X, p.Y);
  155. }
  156. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  157. SuppressUnmanagedCodeSecurity]
  158. private static extern int cucul_set_attr(IntPtr cv, int a);
  159. public int setAttr(int a)
  160. {
  161. return cucul_set_attr(_cv, a);
  162. }
  163. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  164. SuppressUnmanagedCodeSecurity]
  165. private static extern int cucul_put_attr(IntPtr cv,
  166. int x, int y, int a);
  167. public int putAttr(Point p, int a)
  168. {
  169. return cucul_put_attr(_cv, p.X, p.Y, a);
  170. }
  171. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  172. SuppressUnmanagedCodeSecurity]
  173. private static extern int cucul_set_color_ansi(IntPtr cv,
  174. byte fg, byte bg);
  175. public int setColorAnsi(int fg, int bg)
  176. {
  177. return cucul_set_color_ansi(_cv, (byte)fg, (byte)bg);
  178. }
  179. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  180. SuppressUnmanagedCodeSecurity]
  181. private static extern int cucul_set_color_argb(IntPtr cv,
  182. int fg, int bg);
  183. public int setColorArgb(int fg, int bg)
  184. {
  185. return cucul_set_color_argb(_cv, fg, bg);
  186. }
  187. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  188. SuppressUnmanagedCodeSecurity]
  189. private static extern int cucul_clear_canvas(IntPtr cv);
  190. public int Clear()
  191. {
  192. return cucul_clear_canvas(_cv);
  193. }
  194. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  195. SuppressUnmanagedCodeSecurity]
  196. private static extern int cucul_set_canvas_handle(IntPtr cv,
  197. int x, int y);
  198. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  199. SuppressUnmanagedCodeSecurity]
  200. private static extern int cucul_get_canvas_handle_x(IntPtr cv);
  201. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  202. SuppressUnmanagedCodeSecurity]
  203. private static extern int cucul_get_canvas_handle_y(IntPtr cv);
  204. public Point Handle
  205. {
  206. get { return new Point(cucul_get_canvas_handle_x(_cv),
  207. cucul_get_canvas_handle_y(_cv)); }
  208. set { cucul_set_canvas_handle(_cv, value.X, value.Y); }
  209. }
  210. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  211. SuppressUnmanagedCodeSecurity]
  212. private static extern int cucul_blit(IntPtr cv, int x, int y,
  213. IntPtr cv1, IntPtr cv2);
  214. public int Blit(Point p, CuculCanvas canvas)
  215. {
  216. return cucul_blit(_cv, p.X, p.Y, canvas._cv, IntPtr.Zero);
  217. }
  218. public int Blit(Point p, CuculCanvas cv, CuculCanvas mask)
  219. {
  220. return cucul_blit(_cv, p.X, p.Y, cv._cv, mask._cv);
  221. }
  222. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  223. SuppressUnmanagedCodeSecurity]
  224. private static extern int cucul_set_canvas_boundaries(IntPtr cv,
  225. int x, int y,
  226. int h, int w);
  227. public int setBoundaries(Rectangle r)
  228. {
  229. return cucul_set_canvas_boundaries(_cv, r.X, r.Y,
  230. r.Width, r.Height);
  231. }
  232. /* canvas transformation */
  233. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  234. SuppressUnmanagedCodeSecurity]
  235. private static extern int cucul_invert(IntPtr cv);
  236. public int Invert()
  237. {
  238. return cucul_invert(_cv);
  239. }
  240. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  241. SuppressUnmanagedCodeSecurity]
  242. private static extern int cucul_flip(IntPtr cv);
  243. public int Flip()
  244. {
  245. return cucul_flip(_cv);
  246. }
  247. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  248. SuppressUnmanagedCodeSecurity]
  249. private static extern int cucul_flop(IntPtr cv);
  250. public int Flop()
  251. {
  252. return cucul_flop(_cv);
  253. }
  254. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  255. SuppressUnmanagedCodeSecurity]
  256. private static extern int cucul_rotate_180(IntPtr cv);
  257. public int Rotate180()
  258. {
  259. return cucul_rotate_180(_cv);
  260. }
  261. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  262. SuppressUnmanagedCodeSecurity]
  263. private static extern int cucul_rotate_left(IntPtr cv);
  264. public int RotateLeft()
  265. {
  266. return cucul_rotate_left(_cv);
  267. }
  268. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  269. SuppressUnmanagedCodeSecurity]
  270. private static extern int cucul_rotate_right(IntPtr cv);
  271. public int RotateRight()
  272. {
  273. return cucul_rotate_right(_cv);
  274. }
  275. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  276. SuppressUnmanagedCodeSecurity]
  277. private static extern int cucul_stretch_left(IntPtr cv);
  278. public int StretchLeft()
  279. {
  280. return cucul_stretch_left(_cv);
  281. }
  282. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  283. SuppressUnmanagedCodeSecurity]
  284. private static extern int cucul_stretch_right(IntPtr cv);
  285. public int StretchRight()
  286. {
  287. return cucul_stretch_right(_cv);
  288. }
  289. /* primitives drawing */
  290. /* FIXME: highly incomplete */
  291. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  292. SuppressUnmanagedCodeSecurity]
  293. private static extern int cucul_draw_line(IntPtr cv, int x1, int y1,
  294. int x2, int y2, uint c);
  295. public int drawLine(Point p1, Point p2, uint c)
  296. {
  297. return cucul_draw_line(_cv, p1.X, p1.Y, p2.X, p2.Y, c);
  298. }
  299. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  300. SuppressUnmanagedCodeSecurity]
  301. private static extern int cucul_draw_polyline(IntPtr cv, int[] x,
  302. int[] y, int n, uint c);
  303. public int drawPolyline(Point[] lp, uint c)
  304. {
  305. int[] lx = new int[lp.Length];
  306. int[] ly = new int[lp.Length];
  307. for(int i = 0; i < lp.Length; i++)
  308. {
  309. lx[i] = lp[i].X;
  310. ly[i] = lp[i].Y;
  311. }
  312. return cucul_draw_polyline(_cv, lx, ly, lp.Length - 1, c);
  313. }
  314. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  315. SuppressUnmanagedCodeSecurity]
  316. private static extern int cucul_draw_thin_line(IntPtr cv, int x1,
  317. int y1, int x2, int y2);
  318. public int drawThinLine(Point p1, Point p2)
  319. {
  320. return cucul_draw_thin_line(_cv, p1.X, p1.Y, p2.X, p2.Y);
  321. }
  322. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  323. SuppressUnmanagedCodeSecurity]
  324. private static extern int cucul_draw_thin_polyline(IntPtr cv, int[] x,
  325. int[] y, int n);
  326. public int drawThinPolyline(Point[] lp)
  327. {
  328. int[] lx = new int[lp.Length];
  329. int[] ly = new int[lp.Length];
  330. for(int i = 0; i < lp.Length; i++)
  331. {
  332. lx[i] = lp[i].X;
  333. ly[i] = lp[i].Y;
  334. }
  335. return cucul_draw_thin_polyline(_cv, lx, ly, lp.Length - 1);
  336. }
  337. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  338. SuppressUnmanagedCodeSecurity]
  339. private static extern int cucul_draw_circle(IntPtr cv, int x, int y,
  340. int r, uint c);
  341. public int drawCircle(Point p1, int r, uint c)
  342. {
  343. return cucul_draw_circle(_cv, p1.X, p1.Y, r, c);
  344. }
  345. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  346. SuppressUnmanagedCodeSecurity]
  347. private static extern int cucul_draw_ellipse(IntPtr cv, int x, int y,
  348. int a, int b, uint c);
  349. public int drawEllipse(Point p1, int a, int b, uint c)
  350. {
  351. return cucul_draw_ellipse(_cv, p1.X, p1.Y, a, b, c);
  352. }
  353. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  354. SuppressUnmanagedCodeSecurity]
  355. private static extern int cucul_draw_thin_ellipse(IntPtr cv,
  356. int x, int y,
  357. int a, int b);
  358. public int drawThinEllipse(Point p1, int a, int b)
  359. {
  360. return cucul_draw_thin_ellipse(_cv, p1.X, p1.Y, a, b);
  361. }
  362. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  363. SuppressUnmanagedCodeSecurity]
  364. private static extern int cucul_fill_ellipse(IntPtr cv, int x, int y,
  365. int a, int b, uint c);
  366. public int fillEllipse(Point p1, int a, int b, uint c)
  367. {
  368. return cucul_fill_ellipse(_cv, p1.X, p1.Y, a, b, c);
  369. }
  370. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  371. SuppressUnmanagedCodeSecurity]
  372. private static extern int cucul_draw_box(IntPtr cv, int x, int y,
  373. int w, int h, uint c);
  374. public int drawBox(Rectangle r, uint c)
  375. {
  376. return cucul_draw_box(_cv, r.X, r.Y, r.Width, r.Height, c);
  377. }
  378. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  379. SuppressUnmanagedCodeSecurity]
  380. private static extern int cucul_draw_thin_box(IntPtr cv, int x, int y,
  381. int w, int h);
  382. public int drawThinBox(Rectangle r)
  383. {
  384. return cucul_draw_thin_box(_cv, r.X, r.Y, r.Width, r.Height);
  385. }
  386. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  387. SuppressUnmanagedCodeSecurity]
  388. private static extern int cucul_draw_cp437_box(IntPtr cv, int x, int y,
  389. int w, int h);
  390. public int drawCp437Box(Rectangle r)
  391. {
  392. return cucul_draw_cp437_box(_cv, r.X, r.Y, r.Width, r.Height);
  393. }
  394. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  395. SuppressUnmanagedCodeSecurity]
  396. private static extern int cucul_fill_box(IntPtr cv, int x, int y,
  397. int w, int h, uint c);
  398. public int fillBox(Rectangle r, uint c)
  399. {
  400. return cucul_fill_box(_cv, r.X, r.Y, r.Width, r.Height, c);
  401. }
  402. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  403. SuppressUnmanagedCodeSecurity]
  404. private static extern int cucul_draw_triangle(IntPtr cv, int x1,
  405. int y1, int x2, int y2,
  406. int x3, int y3, uint c);
  407. public int drawTriangle(Point p1, Point p2, Point p3, uint c)
  408. {
  409. return cucul_draw_triangle(_cv, p1.X, p1.Y, p2.X, p2.Y,
  410. p3.X, p3.Y, c);
  411. }
  412. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  413. SuppressUnmanagedCodeSecurity]
  414. private static extern int cucul_draw_thin_triangle(IntPtr cv,
  415. int x1, int y1,
  416. int x2, int y2,
  417. int x3, int y3);
  418. public int drawThinTriangle(Point p1, Point p2, Point p3)
  419. {
  420. return cucul_draw_thin_triangle(_cv, p1.X, p1.Y, p2.X, p2.Y,
  421. p3.X, p3.Y);
  422. }
  423. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  424. SuppressUnmanagedCodeSecurity]
  425. private static extern int cucul_fill_triangle(IntPtr cv, int x1,
  426. int y1, int x2, int y2,
  427. int x3, int y3, uint c);
  428. public int fillTriangle(Point p1, Point p2, Point p3, uint c)
  429. {
  430. return cucul_fill_triangle(_cv, p1.X, p1.Y, p2.X, p2.Y,
  431. p3.X, p3.Y, c);
  432. }
  433. /* frame handling */
  434. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  435. SuppressUnmanagedCodeSecurity]
  436. private static extern int cucul_get_frame_count(IntPtr cv);
  437. public int getFrameCount()
  438. {
  439. return cucul_get_frame_count(_cv);
  440. }
  441. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  442. SuppressUnmanagedCodeSecurity]
  443. private static extern int cucul_set_frame(IntPtr cv, int f);
  444. public int setFrame(int f)
  445. {
  446. return cucul_set_frame(_cv, f);
  447. }
  448. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  449. SuppressUnmanagedCodeSecurity]
  450. private static extern string cucul_get_frame_name(IntPtr cv);
  451. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  452. SuppressUnmanagedCodeSecurity]
  453. private static extern int cucul_set_frame_name(IntPtr cv, string n);
  454. public string frameName
  455. {
  456. get { return cucul_get_frame_name(_cv); }
  457. set { cucul_set_frame_name(_cv, value); }
  458. }
  459. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  460. SuppressUnmanagedCodeSecurity]
  461. private static extern int cucul_create_frame(IntPtr cv, int f);
  462. public int createFrame(int f)
  463. {
  464. return cucul_create_frame(_cv, f);
  465. }
  466. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  467. SuppressUnmanagedCodeSecurity]
  468. private static extern int cucul_free_frame(IntPtr cv, int f);
  469. public int freeFrame(int f)
  470. {
  471. return cucul_free_frame(_cv, f);
  472. }
  473. /* bitmap dithering */
  474. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  475. SuppressUnmanagedCodeSecurity]
  476. private static extern int cucul_dither_bitmap(IntPtr c, int x, int y,
  477. int w, int h,
  478. IntPtr d, IntPtr data);
  479. public int ditherBitmap(Rectangle r, CuculDither d, object data)
  480. {
  481. GCHandle gch = GCHandle.Alloc(data, GCHandleType.Pinned);
  482. int ret = cucul_dither_bitmap(_cv, r.X, r.Y, r.Width, r.Height,
  483. d._dither, gch.AddrOfPinnedObject());
  484. gch.Free();
  485. return ret;
  486. }
  487. }
  488. public class CuculAttr
  489. {
  490. private int _attr;
  491. public CuculAttr(int attr)
  492. {
  493. attr = _attr;
  494. }
  495. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  496. SuppressUnmanagedCodeSecurity]
  497. private static extern byte cucul_attr_to_ansi(Int32 a);
  498. public byte toAnsi()
  499. {
  500. return cucul_attr_to_ansi(_attr);
  501. }
  502. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  503. SuppressUnmanagedCodeSecurity]
  504. private static extern byte cucul_attr_to_ansi_fg(Int32 a);
  505. public byte toAnsiFg()
  506. {
  507. return cucul_attr_to_ansi_fg(_attr);
  508. }
  509. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  510. SuppressUnmanagedCodeSecurity]
  511. private static extern byte cucul_attr_to_ansi_bg(Int32 a);
  512. public byte toAnsiBg()
  513. {
  514. return cucul_attr_to_ansi_bg(_attr);
  515. }
  516. }
  517. public class CuculDither : IDisposable
  518. {
  519. public readonly IntPtr _dither;
  520. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  521. SuppressUnmanagedCodeSecurity]
  522. private static extern IntPtr cucul_create_dither(int bpp, int w,
  523. int h, int pitch,
  524. ulong rmask,
  525. ulong gmask,
  526. ulong bmask,
  527. ulong amask);
  528. public CuculDither(int bpp, Size s, int pitch,
  529. uint rmask, uint gmask, uint bmask, uint amask)
  530. {
  531. _dither = cucul_create_dither(bpp, s.Width, s.Height, pitch,
  532. rmask, gmask, bmask, amask);
  533. }
  534. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  535. SuppressUnmanagedCodeSecurity]
  536. private static extern int cucul_free_dither(IntPtr d);
  537. public void Dispose()
  538. {
  539. cucul_free_dither(_dither);
  540. GC.SuppressFinalize(this);
  541. }
  542. /* TODO: fix this shit */
  543. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  544. SuppressUnmanagedCodeSecurity]
  545. private static extern int cucul_set_dither_palette(IntPtr d,
  546. int[] r, int[] g,
  547. int[] b, int[] a);
  548. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  549. SuppressUnmanagedCodeSecurity]
  550. private static extern int cucul_set_dither_brightness(IntPtr d, float b);
  551. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  552. SuppressUnmanagedCodeSecurity]
  553. private static extern int cucul_set_dither_gamma(IntPtr d, float g);
  554. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  555. SuppressUnmanagedCodeSecurity]
  556. private static extern int cucul_set_dither_contrast(IntPtr d, float c);
  557. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  558. SuppressUnmanagedCodeSecurity]
  559. private static extern int cucul_set_dither_invert(IntPtr d, int i);
  560. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  561. SuppressUnmanagedCodeSecurity]
  562. private static extern int cucul_set_dither_antialias(IntPtr d, string s);
  563. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  564. SuppressUnmanagedCodeSecurity]
  565. private static extern string[] cucul_get_dither_antialias_list(IntPtr d);
  566. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  567. SuppressUnmanagedCodeSecurity]
  568. private static extern int cucul_set_dither_color(IntPtr d, string s);
  569. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  570. SuppressUnmanagedCodeSecurity]
  571. private static extern string[] cucul_get_dither_color_list(IntPtr d);
  572. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  573. SuppressUnmanagedCodeSecurity]
  574. private static extern int cucul_set_dither_charset(IntPtr d, string s);
  575. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  576. SuppressUnmanagedCodeSecurity]
  577. private static extern string[] cucul_get_dither_charset_list(IntPtr d);
  578. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  579. SuppressUnmanagedCodeSecurity]
  580. private static extern int cucul_set_dither_mode(IntPtr d, string s);
  581. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  582. SuppressUnmanagedCodeSecurity]
  583. private static extern string[] cucul_get_dither_mode_list(IntPtr d);
  584. public int setBrightness(float b)
  585. {
  586. return cucul_set_dither_brightness(_dither, b);
  587. }
  588. public int setGamma(float g)
  589. {
  590. return cucul_set_dither_gamma(_dither, g);
  591. }
  592. public int setContrast(float c)
  593. {
  594. return cucul_set_dither_contrast(_dither, c);
  595. }
  596. public int setInvert(int i)
  597. {
  598. return cucul_set_dither_invert(_dither, i);
  599. }
  600. public int setAntialias(string s)
  601. {
  602. return cucul_set_dither_antialias(_dither, s);
  603. }
  604. public int setColor(string s)
  605. {
  606. return cucul_set_dither_color(_dither, s);
  607. }
  608. public int setCharset(string s)
  609. {
  610. return cucul_set_dither_charset(_dither, s);
  611. }
  612. public int setMode(string s)
  613. {
  614. return cucul_set_dither_mode(_dither, s);
  615. }
  616. /* <FIXME> */
  617. public string[] getAntialiasList()
  618. {
  619. return cucul_get_dither_antialias_list(_dither);
  620. }
  621. public string[] getColorList()
  622. {
  623. return cucul_get_dither_color_list(_dither);
  624. }
  625. public string[] getCharsetList()
  626. {
  627. return cucul_get_dither_charset_list(_dither);
  628. }
  629. public string[] getModeList()
  630. {
  631. return cucul_get_dither_mode_list(_dither);
  632. }
  633. /* </FIXME> */
  634. }
  635. public class CuculFont : IDisposable
  636. {
  637. private IntPtr _font;
  638. private GCHandle _gch;
  639. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  640. SuppressUnmanagedCodeSecurity]
  641. private static extern IntPtr cucul_load_font(IntPtr data, int len);
  642. public CuculFont(string s)
  643. {
  644. IntPtr name = Marshal.StringToHGlobalAnsi(s);
  645. _font = cucul_load_font(name, 0);
  646. Marshal.FreeHGlobal(name);
  647. }
  648. public CuculFont(byte[] buf)
  649. {
  650. GCHandle _gch = GCHandle.Alloc(buf, GCHandleType.Pinned);
  651. _font = cucul_load_font(_gch.AddrOfPinnedObject(), buf.Length);
  652. }
  653. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  654. SuppressUnmanagedCodeSecurity]
  655. private static extern int cucul_free_font(IntPtr d);
  656. public void Dispose()
  657. {
  658. cucul_free_font(_font);
  659. _gch.Free();
  660. GC.SuppressFinalize(this);
  661. }
  662. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  663. SuppressUnmanagedCodeSecurity]
  664. private static extern IntPtr cucul_get_font_list();
  665. public static string[] getList()
  666. {
  667. IntPtr l = cucul_get_font_list();
  668. int size;
  669. for(size = 0; true; size++)
  670. if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero)
  671. break;
  672. string[] ret = new string[size];
  673. for(int i = 0; i < size; i++)
  674. {
  675. IntPtr s = Marshal.ReadIntPtr(l, IntPtr.Size * i);
  676. ret[i] = Marshal.PtrToStringAnsi(s);
  677. }
  678. return ret;
  679. }
  680. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  681. SuppressUnmanagedCodeSecurity]
  682. private static extern int cucul_get_font_width(IntPtr font);
  683. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  684. SuppressUnmanagedCodeSecurity]
  685. private static extern int cucul_get_font_height(IntPtr font);
  686. public Size Size
  687. {
  688. get { return new Size(cucul_get_font_width(_font),
  689. cucul_get_font_height(_font)); }
  690. }
  691. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  692. SuppressUnmanagedCodeSecurity]
  693. private static extern IntPtr cucul_get_font_blocks(IntPtr font);
  694. public int[,] getBlocks()
  695. {
  696. IntPtr l = cucul_get_font_blocks(_font);
  697. int size;
  698. for(size = 1; true; size += 2)
  699. if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero)
  700. break;
  701. int[,] ret = new int[size,2];
  702. for(int i = 0; i < size; i++)
  703. {
  704. ret[i,0] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2);
  705. ret[i,1] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2 + 1);
  706. }
  707. return ret;
  708. }
  709. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  710. SuppressUnmanagedCodeSecurity]
  711. private static extern int cucul_render_canvas(IntPtr cv, IntPtr f,
  712. IntPtr buf, int w, int h,
  713. int pitch);
  714. public int Render(CuculCanvas cv, uint[,] buf, int pitch)
  715. {
  716. GCHandle gch = GCHandle.Alloc(buf, GCHandleType.Pinned);
  717. int ret = cucul_render_canvas(cv._cv, _font,
  718. gch.AddrOfPinnedObject(),
  719. buf.GetLength(0), buf.GetLength(1),
  720. pitch);
  721. gch.Free();
  722. return ret;
  723. }
  724. }
  725. }