Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

825 lignes
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. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  291. SuppressUnmanagedCodeSecurity]
  292. private static extern int cucul_draw_line(IntPtr cv, int x1, int y1,
  293. int x2, int y2, uint c);
  294. public int drawLine(Point p1, Point p2, uint c)
  295. {
  296. return cucul_draw_line(_cv, p1.X, p1.Y, p2.X, p2.Y, c);
  297. }
  298. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  299. SuppressUnmanagedCodeSecurity]
  300. private static extern int cucul_draw_polyline(IntPtr cv, int[] x,
  301. int[] y, int n, uint c);
  302. public int drawPolyline(Point[] lp, uint c)
  303. {
  304. int[] lx = new int[lp.Length];
  305. int[] ly = new int[lp.Length];
  306. for(int i = 0; i < lp.Length; i++)
  307. {
  308. lx[i] = lp[i].X;
  309. ly[i] = lp[i].Y;
  310. }
  311. return cucul_draw_polyline(_cv, lx, ly, lp.Length - 1, c);
  312. }
  313. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  314. SuppressUnmanagedCodeSecurity]
  315. private static extern int cucul_draw_thin_line(IntPtr cv, int x1,
  316. int y1, int x2, int y2);
  317. public int drawThinLine(Point p1, Point p2)
  318. {
  319. return cucul_draw_thin_line(_cv, p1.X, p1.Y, p2.X, p2.Y);
  320. }
  321. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  322. SuppressUnmanagedCodeSecurity]
  323. private static extern int cucul_draw_thin_polyline(IntPtr cv, int[] x,
  324. int[] y, int n);
  325. public int drawThinPolyline(Point[] lp)
  326. {
  327. int[] lx = new int[lp.Length];
  328. int[] ly = new int[lp.Length];
  329. for(int i = 0; i < lp.Length; i++)
  330. {
  331. lx[i] = lp[i].X;
  332. ly[i] = lp[i].Y;
  333. }
  334. return cucul_draw_thin_polyline(_cv, lx, ly, lp.Length - 1);
  335. }
  336. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  337. SuppressUnmanagedCodeSecurity]
  338. private static extern int cucul_draw_circle(IntPtr cv, int x, int y,
  339. int r, uint c);
  340. public int drawCircle(Point p1, int r, uint c)
  341. {
  342. return cucul_draw_circle(_cv, p1.X, p1.Y, r, c);
  343. }
  344. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  345. SuppressUnmanagedCodeSecurity]
  346. private static extern int cucul_draw_ellipse(IntPtr cv, int x, int y,
  347. int a, int b, uint c);
  348. public int drawEllipse(Point p1, int a, int b, uint c)
  349. {
  350. return cucul_draw_ellipse(_cv, p1.X, p1.Y, a, b, c);
  351. }
  352. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  353. SuppressUnmanagedCodeSecurity]
  354. private static extern int cucul_draw_thin_ellipse(IntPtr cv,
  355. int x, int y,
  356. int a, int b);
  357. public int drawThinEllipse(Point p1, int a, int b)
  358. {
  359. return cucul_draw_thin_ellipse(_cv, p1.X, p1.Y, a, b);
  360. }
  361. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  362. SuppressUnmanagedCodeSecurity]
  363. private static extern int cucul_fill_ellipse(IntPtr cv, int x, int y,
  364. int a, int b, uint c);
  365. public int fillEllipse(Point p1, int a, int b, uint c)
  366. {
  367. return cucul_fill_ellipse(_cv, p1.X, p1.Y, a, b, c);
  368. }
  369. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  370. SuppressUnmanagedCodeSecurity]
  371. private static extern int cucul_draw_box(IntPtr cv, int x, int y,
  372. int w, int h, uint c);
  373. public int drawBox(Rectangle r, uint c)
  374. {
  375. return cucul_draw_box(_cv, r.X, r.Y, r.Width, r.Height, c);
  376. }
  377. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  378. SuppressUnmanagedCodeSecurity]
  379. private static extern int cucul_draw_thin_box(IntPtr cv, int x, int y,
  380. int w, int h);
  381. public int drawThinBox(Rectangle r)
  382. {
  383. return cucul_draw_thin_box(_cv, r.X, r.Y, r.Width, r.Height);
  384. }
  385. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  386. SuppressUnmanagedCodeSecurity]
  387. private static extern int cucul_draw_cp437_box(IntPtr cv, int x, int y,
  388. int w, int h);
  389. public int drawCp437Box(Rectangle r)
  390. {
  391. return cucul_draw_cp437_box(_cv, r.X, r.Y, r.Width, r.Height);
  392. }
  393. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  394. SuppressUnmanagedCodeSecurity]
  395. private static extern int cucul_fill_box(IntPtr cv, int x, int y,
  396. int w, int h, uint c);
  397. public int fillBox(Rectangle r, uint c)
  398. {
  399. return cucul_fill_box(_cv, r.X, r.Y, r.Width, r.Height, c);
  400. }
  401. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  402. SuppressUnmanagedCodeSecurity]
  403. private static extern int cucul_draw_triangle(IntPtr cv, int x1,
  404. int y1, int x2, int y2,
  405. int x3, int y3, uint c);
  406. public int drawTriangle(Point p1, Point p2, Point p3, uint c)
  407. {
  408. return cucul_draw_triangle(_cv, p1.X, p1.Y, p2.X, p2.Y,
  409. p3.X, p3.Y, c);
  410. }
  411. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  412. SuppressUnmanagedCodeSecurity]
  413. private static extern int cucul_draw_thin_triangle(IntPtr cv,
  414. int x1, int y1,
  415. int x2, int y2,
  416. int x3, int y3);
  417. public int drawThinTriangle(Point p1, Point p2, Point p3)
  418. {
  419. return cucul_draw_thin_triangle(_cv, p1.X, p1.Y, p2.X, p2.Y,
  420. p3.X, p3.Y);
  421. }
  422. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  423. SuppressUnmanagedCodeSecurity]
  424. private static extern int cucul_fill_triangle(IntPtr cv, int x1,
  425. int y1, int x2, int y2,
  426. int x3, int y3, uint c);
  427. public int fillTriangle(Point p1, Point p2, Point p3, uint c)
  428. {
  429. return cucul_fill_triangle(_cv, p1.X, p1.Y, p2.X, p2.Y,
  430. p3.X, p3.Y, c);
  431. }
  432. /* frame handling */
  433. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  434. SuppressUnmanagedCodeSecurity]
  435. private static extern int cucul_get_frame_count(IntPtr cv);
  436. public int getFrameCount()
  437. {
  438. return cucul_get_frame_count(_cv);
  439. }
  440. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  441. SuppressUnmanagedCodeSecurity]
  442. private static extern int cucul_set_frame(IntPtr cv, int f);
  443. public int setFrame(int f)
  444. {
  445. return cucul_set_frame(_cv, f);
  446. }
  447. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  448. SuppressUnmanagedCodeSecurity]
  449. private static extern string cucul_get_frame_name(IntPtr cv);
  450. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  451. SuppressUnmanagedCodeSecurity]
  452. private static extern int cucul_set_frame_name(IntPtr cv, string n);
  453. public string FrameName
  454. {
  455. get { return cucul_get_frame_name(_cv); }
  456. set { cucul_set_frame_name(_cv, value); }
  457. }
  458. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  459. SuppressUnmanagedCodeSecurity]
  460. private static extern int cucul_create_frame(IntPtr cv, int f);
  461. public int createFrame(int f)
  462. {
  463. return cucul_create_frame(_cv, f);
  464. }
  465. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  466. SuppressUnmanagedCodeSecurity]
  467. private static extern int cucul_free_frame(IntPtr cv, int f);
  468. public int freeFrame(int f)
  469. {
  470. return cucul_free_frame(_cv, f);
  471. }
  472. /* bitmap dithering */
  473. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  474. SuppressUnmanagedCodeSecurity]
  475. private static extern int cucul_dither_bitmap(IntPtr c, int x, int y,
  476. int w, int h,
  477. IntPtr d, IntPtr data);
  478. public int ditherBitmap(Rectangle r, CuculDither d, object data)
  479. {
  480. GCHandle gch = GCHandle.Alloc(data, GCHandleType.Pinned);
  481. int ret = cucul_dither_bitmap(_cv, r.X, r.Y, r.Width, r.Height,
  482. d._dither, gch.AddrOfPinnedObject());
  483. gch.Free();
  484. return ret;
  485. }
  486. }
  487. public class CuculAttr
  488. {
  489. private int _attr;
  490. public CuculAttr(int attr)
  491. {
  492. _attr = attr;
  493. }
  494. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  495. SuppressUnmanagedCodeSecurity]
  496. private static extern byte cucul_attr_to_ansi(Int32 a);
  497. public byte toAnsi()
  498. {
  499. return cucul_attr_to_ansi(_attr);
  500. }
  501. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  502. SuppressUnmanagedCodeSecurity]
  503. private static extern byte cucul_attr_to_ansi_fg(Int32 a);
  504. public byte toAnsiFg()
  505. {
  506. return cucul_attr_to_ansi_fg(_attr);
  507. }
  508. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  509. SuppressUnmanagedCodeSecurity]
  510. private static extern byte cucul_attr_to_ansi_bg(Int32 a);
  511. public byte toAnsiBg()
  512. {
  513. return cucul_attr_to_ansi_bg(_attr);
  514. }
  515. }
  516. public class CuculDither : IDisposable
  517. {
  518. public readonly IntPtr _dither;
  519. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  520. SuppressUnmanagedCodeSecurity]
  521. private static extern IntPtr cucul_create_dither(int bpp, int w,
  522. int h, int pitch,
  523. ulong rmask,
  524. ulong gmask,
  525. ulong bmask,
  526. ulong amask);
  527. public CuculDither(int bpp, Size s, int pitch,
  528. uint rmask, uint gmask, uint bmask, uint amask)
  529. {
  530. _dither = cucul_create_dither(bpp, s.Width, s.Height, pitch,
  531. rmask, gmask, bmask, amask);
  532. }
  533. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  534. SuppressUnmanagedCodeSecurity]
  535. private static extern int cucul_free_dither(IntPtr d);
  536. public void Dispose()
  537. {
  538. cucul_free_dither(_dither);
  539. GC.SuppressFinalize(this);
  540. }
  541. /* TODO: fix this shit */
  542. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  543. SuppressUnmanagedCodeSecurity]
  544. private static extern int cucul_set_dither_palette(IntPtr d,
  545. int[] r, int[] g,
  546. int[] b, int[] a);
  547. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  548. SuppressUnmanagedCodeSecurity]
  549. private static extern int cucul_set_dither_brightness(IntPtr d, float b);
  550. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  551. SuppressUnmanagedCodeSecurity]
  552. private static extern int cucul_set_dither_gamma(IntPtr d, float g);
  553. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  554. SuppressUnmanagedCodeSecurity]
  555. private static extern int cucul_set_dither_contrast(IntPtr d, float c);
  556. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  557. SuppressUnmanagedCodeSecurity]
  558. private static extern int cucul_set_dither_invert(IntPtr d, int i);
  559. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  560. SuppressUnmanagedCodeSecurity]
  561. private static extern int cucul_set_dither_antialias(IntPtr d, string s);
  562. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  563. SuppressUnmanagedCodeSecurity]
  564. private static extern string[] cucul_get_dither_antialias_list(IntPtr d);
  565. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  566. SuppressUnmanagedCodeSecurity]
  567. private static extern int cucul_set_dither_color(IntPtr d, string s);
  568. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  569. SuppressUnmanagedCodeSecurity]
  570. private static extern string[] cucul_get_dither_color_list(IntPtr d);
  571. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  572. SuppressUnmanagedCodeSecurity]
  573. private static extern int cucul_set_dither_charset(IntPtr d, string s);
  574. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  575. SuppressUnmanagedCodeSecurity]
  576. private static extern string[] cucul_get_dither_charset_list(IntPtr d);
  577. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  578. SuppressUnmanagedCodeSecurity]
  579. private static extern int cucul_set_dither_mode(IntPtr d, string s);
  580. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  581. SuppressUnmanagedCodeSecurity]
  582. private static extern string[] cucul_get_dither_mode_list(IntPtr d);
  583. public int setBrightness(float b)
  584. {
  585. return cucul_set_dither_brightness(_dither, b);
  586. }
  587. public int setGamma(float g)
  588. {
  589. return cucul_set_dither_gamma(_dither, g);
  590. }
  591. public int setContrast(float c)
  592. {
  593. return cucul_set_dither_contrast(_dither, c);
  594. }
  595. public int setInvert(int i)
  596. {
  597. return cucul_set_dither_invert(_dither, i);
  598. }
  599. public int setAntialias(string s)
  600. {
  601. return cucul_set_dither_antialias(_dither, s);
  602. }
  603. public int setColor(string s)
  604. {
  605. return cucul_set_dither_color(_dither, s);
  606. }
  607. public int setCharset(string s)
  608. {
  609. return cucul_set_dither_charset(_dither, s);
  610. }
  611. public int setMode(string s)
  612. {
  613. return cucul_set_dither_mode(_dither, s);
  614. }
  615. /* <FIXME> */
  616. public string[] getAntialiasList()
  617. {
  618. return cucul_get_dither_antialias_list(_dither);
  619. }
  620. public string[] getColorList()
  621. {
  622. return cucul_get_dither_color_list(_dither);
  623. }
  624. public string[] getCharsetList()
  625. {
  626. return cucul_get_dither_charset_list(_dither);
  627. }
  628. public string[] getModeList()
  629. {
  630. return cucul_get_dither_mode_list(_dither);
  631. }
  632. /* </FIXME> */
  633. }
  634. public class CuculFont : IDisposable
  635. {
  636. private IntPtr _font;
  637. private GCHandle _gch;
  638. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  639. SuppressUnmanagedCodeSecurity]
  640. private static extern IntPtr cucul_load_font(IntPtr data, int len);
  641. public CuculFont(string s)
  642. {
  643. IntPtr name = Marshal.StringToHGlobalAnsi(s);
  644. _font = cucul_load_font(name, 0);
  645. Marshal.FreeHGlobal(name);
  646. }
  647. public CuculFont(byte[] buf)
  648. {
  649. GCHandle _gch = GCHandle.Alloc(buf, GCHandleType.Pinned);
  650. _font = cucul_load_font(_gch.AddrOfPinnedObject(), buf.Length);
  651. }
  652. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  653. SuppressUnmanagedCodeSecurity]
  654. private static extern int cucul_free_font(IntPtr d);
  655. public void Dispose()
  656. {
  657. cucul_free_font(_font);
  658. _gch.Free();
  659. GC.SuppressFinalize(this);
  660. }
  661. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  662. SuppressUnmanagedCodeSecurity]
  663. private static extern IntPtr cucul_get_font_list();
  664. public static string[] getList()
  665. {
  666. IntPtr l = cucul_get_font_list();
  667. int size;
  668. for(size = 0; true; size++)
  669. if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero)
  670. break;
  671. string[] ret = new string[size];
  672. for(int i = 0; i < size; i++)
  673. {
  674. IntPtr s = Marshal.ReadIntPtr(l, IntPtr.Size * i);
  675. ret[i] = Marshal.PtrToStringAnsi(s);
  676. }
  677. return ret;
  678. }
  679. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  680. SuppressUnmanagedCodeSecurity]
  681. private static extern int cucul_get_font_width(IntPtr font);
  682. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  683. SuppressUnmanagedCodeSecurity]
  684. private static extern int cucul_get_font_height(IntPtr font);
  685. public Size Size
  686. {
  687. get { return new Size(cucul_get_font_width(_font),
  688. cucul_get_font_height(_font)); }
  689. }
  690. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  691. SuppressUnmanagedCodeSecurity]
  692. private static extern IntPtr cucul_get_font_blocks(IntPtr font);
  693. public int[,] getBlocks()
  694. {
  695. IntPtr l = cucul_get_font_blocks(_font);
  696. int size;
  697. for(size = 1; true; size += 2)
  698. if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero)
  699. break;
  700. int[,] ret = new int[size,2];
  701. for(int i = 0; i < size; i++)
  702. {
  703. ret[i,0] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2);
  704. ret[i,1] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2 + 1);
  705. }
  706. return ret;
  707. }
  708. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  709. SuppressUnmanagedCodeSecurity]
  710. private static extern int cucul_render_canvas(IntPtr cv, IntPtr f,
  711. IntPtr buf, int w, int h,
  712. int pitch);
  713. public int Render(CuculCanvas cv, uint[,] buf, int pitch)
  714. {
  715. GCHandle gch = GCHandle.Alloc(buf, GCHandleType.Pinned);
  716. int ret = cucul_render_canvas(cv._cv, _font,
  717. gch.AddrOfPinnedObject(),
  718. buf.GetLength(0), buf.GetLength(1),
  719. pitch);
  720. gch.Free();
  721. return ret;
  722. }
  723. }
  724. }