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.
 
 
 
 
 
 

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