Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 _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, int c);
  131. public int putChar(Point p, int c)
  132. {
  133. return cucul_put_char(_c_cv, p.X, p.Y, c);
  134. }
  135. public int putChar(int x, int y, int 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 int cucul_get_char(IntPtr cv, int x, int y);
  142. public int getChar(Point p)
  143. {
  144. return cucul_get_char(_c_cv, p.X, p.Y);
  145. }
  146. public int 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, int a);
  176. public int setAttr(int 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, int a);
  184. public int putAttr(Point p, int a)
  185. {
  186. return cucul_put_attr(_c_cv, p.X, p.Y, a);
  187. }
  188. public int putAttr(int x, int y, int 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(int fg, int 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. int fg, int bg);
  204. public int setColorArgb(int fg, int 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 int _attr;
  599. public CuculAttr(int attr)
  600. {
  601. _attr = attr;
  602. }
  603. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  604. SuppressUnmanagedCodeSecurity]
  605. private static extern byte cucul_attr_to_ansi(Int32 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(Int32 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(Int32 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. ulong rmask,
  633. ulong gmask,
  634. ulong bmask,
  635. ulong 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. int[] r, int[] g,
  655. int[] b, int[] 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, int 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(), buf.Length);
  760. }
  761. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  762. SuppressUnmanagedCodeSecurity]
  763. private static extern int cucul_free_font(IntPtr d);
  764. public void Dispose()
  765. {
  766. cucul_free_font(_font);
  767. _gch.Free();
  768. GC.SuppressFinalize(this);
  769. }
  770. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  771. SuppressUnmanagedCodeSecurity]
  772. private static extern IntPtr cucul_get_font_list();
  773. public static string[] getList()
  774. {
  775. IntPtr l = cucul_get_font_list();
  776. int size;
  777. for(size = 0; true; size++)
  778. if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero)
  779. break;
  780. string[] ret = new string[size];
  781. for(int i = 0; i < size; i++)
  782. {
  783. IntPtr s = Marshal.ReadIntPtr(l, IntPtr.Size * i);
  784. ret[i] = Marshal.PtrToStringAnsi(s);
  785. }
  786. return ret;
  787. }
  788. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  789. SuppressUnmanagedCodeSecurity]
  790. private static extern int cucul_get_font_width(IntPtr font);
  791. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  792. SuppressUnmanagedCodeSecurity]
  793. private static extern int cucul_get_font_height(IntPtr font);
  794. public Size Size
  795. {
  796. get { return new Size(cucul_get_font_width(_font),
  797. cucul_get_font_height(_font)); }
  798. }
  799. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  800. SuppressUnmanagedCodeSecurity]
  801. private static extern IntPtr cucul_get_font_blocks(IntPtr font);
  802. public int[,] getBlocks()
  803. {
  804. IntPtr l = cucul_get_font_blocks(_font);
  805. int size;
  806. for(size = 1; true; size += 2)
  807. if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero)
  808. break;
  809. int[,] ret = new int[size,2];
  810. for(int i = 0; i < size; i++)
  811. {
  812. ret[i,0] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2);
  813. ret[i,1] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2 + 1);
  814. }
  815. return ret;
  816. }
  817. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  818. SuppressUnmanagedCodeSecurity]
  819. private static extern int cucul_render_canvas(IntPtr cv, IntPtr f,
  820. IntPtr buf, int w, int h,
  821. int pitch);
  822. public int Render(CuculCanvas cv, uint[,] buf, int pitch)
  823. {
  824. GCHandle gch = GCHandle.Alloc(buf, GCHandleType.Pinned);
  825. int ret = cucul_render_canvas(cv._c_cv, _font,
  826. gch.AddrOfPinnedObject(),
  827. buf.GetLength(0), buf.GetLength(1),
  828. pitch);
  829. gch.Free();
  830. return ret;
  831. }
  832. }
  833. }