Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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