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

586 regels
22 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. namespace Cucul
  19. {
  20. /* Static libcucul stuff that does not fit in any object */
  21. public static class Libcucul
  22. {
  23. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  24. SuppressUnmanagedCodeSecurity]
  25. private static extern int cucul_rand(int min, int max);
  26. public static int Rand(int min, int max)
  27. {
  28. return cucul_rand(min, max);
  29. }
  30. public const int BLACK = 0x00,
  31. BLUE = 0x01,
  32. GREEN = 0x02,
  33. CYAN = 0x03,
  34. RED = 0x04,
  35. MAGENTA = 0x05,
  36. BROWN = 0x06,
  37. LIGHTGRAY = 0x07,
  38. DARKGRAY = 0x08,
  39. LIGHTBLUE = 0x09,
  40. LIGHTGREEN = 0x0a,
  41. LIGHTCYAN = 0x0b,
  42. LIGHTRED = 0x0c,
  43. LIGHTMAGENTA = 0x0d,
  44. YELLOW = 0x0e,
  45. WHITE = 0x0f,
  46. DEFAULT = 0x10,
  47. TRANSPARENT = 0x20;
  48. public const int BOLD = 0x01,
  49. ITALICS = 0x02,
  50. UNDERLINE = 0x04,
  51. BLINK = 0x08;
  52. }
  53. public unsafe class CuculCanvas : IDisposable
  54. {
  55. public readonly IntPtr _cv;
  56. /* libcucul basic functions */
  57. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  58. SuppressUnmanagedCodeSecurity]
  59. private static extern IntPtr cucul_create_canvas(int w, int h);
  60. public CuculCanvas()
  61. {
  62. _cv = cucul_create_canvas(0, 0);
  63. }
  64. public CuculCanvas(int w, int h)
  65. {
  66. _cv = cucul_create_canvas(w, h);
  67. }
  68. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  69. SuppressUnmanagedCodeSecurity]
  70. private static extern int cucul_free_canvas(IntPtr cv);
  71. public void Dispose()
  72. {
  73. cucul_free_canvas(_cv);
  74. GC.SuppressFinalize(this);
  75. }
  76. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  77. SuppressUnmanagedCodeSecurity]
  78. private static extern int cucul_set_canvas_size(IntPtr cv,
  79. int w, int h);
  80. public void setSize(int w, int h)
  81. {
  82. cucul_set_canvas_size(_cv, w, h);
  83. }
  84. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  85. SuppressUnmanagedCodeSecurity]
  86. private static extern int cucul_get_canvas_width(IntPtr cv);
  87. public int width
  88. {
  89. get { return cucul_get_canvas_width(_cv); }
  90. set { cucul_set_canvas_size(_cv, value,
  91. cucul_get_canvas_height(_cv)); }
  92. }
  93. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  94. SuppressUnmanagedCodeSecurity]
  95. private static extern int cucul_get_canvas_height(IntPtr cv);
  96. public int height
  97. {
  98. get { return cucul_get_canvas_height(_cv); }
  99. set { cucul_set_canvas_size(_cv, cucul_get_canvas_width(_cv),
  100. value); }
  101. }
  102. /* canvas drawing */
  103. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  104. SuppressUnmanagedCodeSecurity]
  105. private static extern int cucul_gotoxy(IntPtr cv, int x, int y);
  106. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  107. SuppressUnmanagedCodeSecurity]
  108. private static extern int cucul_get_cursor_x(IntPtr cv);
  109. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  110. SuppressUnmanagedCodeSecurity]
  111. private static extern int cucul_get_cursor_y(IntPtr cv);
  112. public int cursorX
  113. {
  114. get { return cucul_get_cursor_x(_cv); }
  115. set { cucul_gotoxy(_cv, value, cucul_get_cursor_y(_cv)); }
  116. }
  117. public int cursorY
  118. {
  119. get { return cucul_get_cursor_y(_cv); }
  120. set { cucul_gotoxy(_cv, cucul_get_cursor_x(_cv), value); }
  121. }
  122. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  123. SuppressUnmanagedCodeSecurity]
  124. private static extern int cucul_put_char(IntPtr cv,
  125. int x, int y, int c);
  126. public int putChar(int x, int y, int c)
  127. {
  128. return cucul_put_char(_cv, x, y, c);
  129. }
  130. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  131. SuppressUnmanagedCodeSecurity]
  132. private static extern int cucul_get_char(IntPtr cv, int x, int y);
  133. public int getChar(int x, int y)
  134. {
  135. return cucul_get_char(_cv, x, y);
  136. }
  137. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  138. SuppressUnmanagedCodeSecurity]
  139. private static extern int cucul_put_str(IntPtr cv,
  140. int x, int y, string c);
  141. public int putStr(int x, int y, string c)
  142. {
  143. return cucul_put_str(_cv, x, y, c);
  144. }
  145. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  146. SuppressUnmanagedCodeSecurity]
  147. private static extern int cucul_get_attr(IntPtr cv, int x, int y);
  148. public int getAttr(int x, int y)
  149. {
  150. return cucul_get_attr(_cv, x, y);
  151. }
  152. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  153. SuppressUnmanagedCodeSecurity]
  154. private static extern int cucul_set_attr(IntPtr cv, int a);
  155. public int setAttr(int a)
  156. {
  157. return cucul_set_attr(_cv, a);
  158. }
  159. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  160. SuppressUnmanagedCodeSecurity]
  161. private static extern int cucul_put_attr(IntPtr cv,
  162. int x, int y, int a);
  163. public int putAttr(int x, int y, int a)
  164. {
  165. return cucul_put_attr(_cv, x, y, a);
  166. }
  167. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  168. SuppressUnmanagedCodeSecurity]
  169. private static extern int cucul_set_color_ansi(IntPtr cv,
  170. byte fg, byte bg);
  171. public int setColorAnsi(int fg, int bg)
  172. {
  173. return cucul_set_color_ansi(_cv, (byte)fg, (byte)bg);
  174. }
  175. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  176. SuppressUnmanagedCodeSecurity]
  177. private static extern int cucul_set_color_argb(IntPtr cv,
  178. int fg, int bg);
  179. public int setColorArgb(int fg, int bg)
  180. {
  181. return cucul_set_color_argb(_cv, fg, bg);
  182. }
  183. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  184. SuppressUnmanagedCodeSecurity]
  185. private static extern int cucul_clear_canvas(IntPtr cv);
  186. public int Clear()
  187. {
  188. return cucul_clear_canvas(_cv);
  189. }
  190. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  191. SuppressUnmanagedCodeSecurity]
  192. private static extern int cucul_set_canvas_handle(IntPtr cv,
  193. int x, int y);
  194. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  195. SuppressUnmanagedCodeSecurity]
  196. private static extern int cucul_get_canvas_handle_x(IntPtr cv);
  197. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  198. SuppressUnmanagedCodeSecurity]
  199. private static extern int cucul_get_canvas_handle_y(IntPtr cv);
  200. public int handleX
  201. {
  202. get { return cucul_get_canvas_handle_x(_cv); }
  203. set { cucul_set_canvas_handle(_cv, value,
  204. cucul_get_canvas_handle_y(_cv)); }
  205. }
  206. public int handleY
  207. {
  208. get { return cucul_get_canvas_handle_y(_cv); }
  209. set { cucul_set_canvas_handle(_cv, cucul_get_canvas_handle_x(_cv),
  210. value); }
  211. }
  212. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  213. SuppressUnmanagedCodeSecurity]
  214. private static extern int cucul_blit(IntPtr cv, int x, int y,
  215. IntPtr cv1, IntPtr cv2);
  216. public int Blit(int x, int y, CuculCanvas canvas)
  217. {
  218. return cucul_blit(_cv, x, y, canvas._cv, IntPtr.Zero);
  219. }
  220. public int Blit(int x, int y, CuculCanvas cv, CuculCanvas mask)
  221. {
  222. return cucul_blit(_cv, x, y, cv._cv, mask._cv);
  223. }
  224. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  225. SuppressUnmanagedCodeSecurity]
  226. private static extern int cucul_set_canvas_boundaries(IntPtr cv,
  227. int x, int y,
  228. int h, int w);
  229. public int setBoundaries(int x, int y, int h, int w)
  230. {
  231. return cucul_set_canvas_boundaries(_cv, x, y, h, w);
  232. }
  233. /* canvas transformation */
  234. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  235. SuppressUnmanagedCodeSecurity]
  236. private static extern int cucul_invert(IntPtr cv);
  237. public int Invert()
  238. {
  239. return cucul_invert(_cv);
  240. }
  241. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  242. SuppressUnmanagedCodeSecurity]
  243. private static extern int cucul_flip(IntPtr cv);
  244. public int Flip()
  245. {
  246. return cucul_flip(_cv);
  247. }
  248. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  249. SuppressUnmanagedCodeSecurity]
  250. private static extern int cucul_flop(IntPtr cv);
  251. public int Flop()
  252. {
  253. return cucul_flop(_cv);
  254. }
  255. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  256. SuppressUnmanagedCodeSecurity]
  257. private static extern int cucul_rotate_180(IntPtr cv);
  258. public int Rotate180()
  259. {
  260. return cucul_rotate_180(_cv);
  261. }
  262. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  263. SuppressUnmanagedCodeSecurity]
  264. private static extern int cucul_rotate_left(IntPtr cv);
  265. public int RotateLeft()
  266. {
  267. return cucul_rotate_left(_cv);
  268. }
  269. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  270. SuppressUnmanagedCodeSecurity]
  271. private static extern int cucul_rotate_right(IntPtr cv);
  272. public int RotateRight()
  273. {
  274. return cucul_rotate_right(_cv);
  275. }
  276. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  277. SuppressUnmanagedCodeSecurity]
  278. private static extern int cucul_stretch_left(IntPtr cv);
  279. public int StretchLeft()
  280. {
  281. return cucul_stretch_left(_cv);
  282. }
  283. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  284. SuppressUnmanagedCodeSecurity]
  285. private static extern int cucul_stretch_right(IntPtr cv);
  286. public int StretchRight()
  287. {
  288. return cucul_stretch_right(_cv);
  289. }
  290. /* primitives drawing */
  291. /* FIXME: highly incomplete */
  292. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  293. SuppressUnmanagedCodeSecurity]
  294. private static extern int cucul_draw_line(IntPtr cv, int x1, int y1, int x2, int y2, int c);
  295. public int drawLine(int x1, int y1, int x2, int y2, int c)
  296. {
  297. return cucul_draw_line(_cv, x1, y1, x2, y2, c);
  298. }
  299. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  300. SuppressUnmanagedCodeSecurity]
  301. private static extern int cucul_draw_polyline(IntPtr cv, int[] x, int[] y, int n, IntPtr c);
  302. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  303. SuppressUnmanagedCodeSecurity]
  304. private static extern int cucul_draw_thin_line(IntPtr cv, int x1, int y1, int x2, int y2);
  305. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  306. SuppressUnmanagedCodeSecurity]
  307. private static extern int cucul_draw_thin_polyline(IntPtr cv, int[] x, int[] y, int n);
  308. /* frame handling */
  309. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  310. SuppressUnmanagedCodeSecurity]
  311. private static extern int cucul_get_frame_count(IntPtr cv);
  312. public int getFrameCount()
  313. {
  314. return cucul_get_frame_count(_cv);
  315. }
  316. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  317. SuppressUnmanagedCodeSecurity]
  318. private static extern int cucul_set_frame(IntPtr cv, int f);
  319. public int setFrame(int f)
  320. {
  321. return cucul_set_frame(_cv, f);
  322. }
  323. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  324. SuppressUnmanagedCodeSecurity]
  325. private static extern string cucul_get_frame_name(IntPtr cv);
  326. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  327. SuppressUnmanagedCodeSecurity]
  328. private static extern int cucul_set_frame_name(IntPtr cv, string n);
  329. public string frameName
  330. {
  331. get { return cucul_get_frame_name(_cv); }
  332. set { cucul_set_frame_name(_cv, value); }
  333. }
  334. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  335. SuppressUnmanagedCodeSecurity]
  336. private static extern int cucul_create_frame(IntPtr cv, int f);
  337. public int createFrame(int f)
  338. {
  339. return cucul_create_frame(_cv, f);
  340. }
  341. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  342. SuppressUnmanagedCodeSecurity]
  343. private static extern int cucul_free_frame(IntPtr cv, int f);
  344. public int freeFrame(int f)
  345. {
  346. return cucul_free_frame(_cv, f);
  347. }
  348. /* bitmap dithering */
  349. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  350. SuppressUnmanagedCodeSecurity]
  351. private static extern int cucul_dither_bitmap(IntPtr c, int x, int y,
  352. int w, int h,
  353. IntPtr d, IntPtr data);
  354. public int ditherBitmap(int x, int y, int w, int h, CuculDither d,
  355. object data)
  356. {
  357. GCHandle gch = GCHandle.Alloc(data);
  358. int ret = cucul_dither_bitmap(_cv, x, y, w, h, d._dither,
  359. gch.AddrOfPinnedObject());
  360. gch.Free();
  361. return ret;
  362. }
  363. }
  364. public unsafe class CuculAttr
  365. {
  366. private int _attr;
  367. public CuculAttr(int attr)
  368. {
  369. attr = _attr;
  370. }
  371. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  372. SuppressUnmanagedCodeSecurity]
  373. private static extern byte cucul_attr_to_ansi(Int32 a);
  374. public byte toAnsi()
  375. {
  376. return cucul_attr_to_ansi(_attr);
  377. }
  378. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  379. SuppressUnmanagedCodeSecurity]
  380. private static extern byte cucul_attr_to_ansi_fg(Int32 a);
  381. public byte toAnsiFg()
  382. {
  383. return cucul_attr_to_ansi_fg(_attr);
  384. }
  385. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  386. SuppressUnmanagedCodeSecurity]
  387. private static extern byte cucul_attr_to_ansi_bg(Int32 a);
  388. public byte toAnsiBg()
  389. {
  390. return cucul_attr_to_ansi_bg(_attr);
  391. }
  392. }
  393. public unsafe class CuculDither : IDisposable
  394. {
  395. public readonly IntPtr _dither;
  396. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  397. SuppressUnmanagedCodeSecurity]
  398. private static extern IntPtr cucul_create_dither(int bpp, int w,
  399. int h, int pitch,
  400. ulong rmask,
  401. ulong gmask,
  402. ulong bmask,
  403. ulong amask);
  404. public CuculDither(int bpp, int w,int h, int pitch,
  405. uint rmask, uint gmask, uint bmask, uint amask)
  406. {
  407. _dither = cucul_create_dither(bpp, w, h, pitch,
  408. rmask, gmask, bmask, amask);
  409. }
  410. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  411. SuppressUnmanagedCodeSecurity]
  412. private static extern int cucul_free_dither(IntPtr d);
  413. public void Dispose()
  414. {
  415. cucul_free_dither(_dither);
  416. GC.SuppressFinalize(this);
  417. }
  418. /* TODO: fix this shit */
  419. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  420. SuppressUnmanagedCodeSecurity]
  421. private static extern int cucul_set_dither_palette(IntPtr d,
  422. int[] r, int[] g,
  423. int[] b, int[] a);
  424. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  425. SuppressUnmanagedCodeSecurity]
  426. private static extern int cucul_set_dither_brightness(IntPtr d, float b);
  427. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  428. SuppressUnmanagedCodeSecurity]
  429. private static extern int cucul_set_dither_gamma(IntPtr d, float g);
  430. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  431. SuppressUnmanagedCodeSecurity]
  432. private static extern int cucul_set_dither_contrast(IntPtr d, float c);
  433. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  434. SuppressUnmanagedCodeSecurity]
  435. private static extern int cucul_set_dither_invert(IntPtr d, int i);
  436. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  437. SuppressUnmanagedCodeSecurity]
  438. private static extern int cucul_set_dither_antialias(IntPtr d, string s);
  439. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  440. SuppressUnmanagedCodeSecurity]
  441. private static extern string[] cucul_get_dither_antialias_list(IntPtr d);
  442. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  443. SuppressUnmanagedCodeSecurity]
  444. private static extern int cucul_set_dither_color(IntPtr d, string s);
  445. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  446. SuppressUnmanagedCodeSecurity]
  447. private static extern string[] cucul_get_dither_color_list(IntPtr d);
  448. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  449. SuppressUnmanagedCodeSecurity]
  450. private static extern int cucul_set_dither_charset(IntPtr d, string s);
  451. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  452. SuppressUnmanagedCodeSecurity]
  453. private static extern string[] cucul_get_dither_charset_list(IntPtr d);
  454. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  455. SuppressUnmanagedCodeSecurity]
  456. private static extern int cucul_set_dither_mode(IntPtr d, string s);
  457. [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl),
  458. SuppressUnmanagedCodeSecurity]
  459. private static extern string[] cucul_get_dither_mode_list(IntPtr d);
  460. public int setBrightness(float b)
  461. {
  462. return cucul_set_dither_brightness(_dither, b);
  463. }
  464. public int setGamma(float g)
  465. {
  466. return cucul_set_dither_gamma(_dither, g);
  467. }
  468. public int setContrast(float c)
  469. {
  470. return cucul_set_dither_contrast(_dither, c);
  471. }
  472. public int setInvert(int i)
  473. {
  474. return cucul_set_dither_invert(_dither, i);
  475. }
  476. public int setAntialias(string s)
  477. {
  478. return cucul_set_dither_antialias(_dither, s);
  479. }
  480. public int setColor(string s)
  481. {
  482. return cucul_set_dither_color(_dither, s);
  483. }
  484. public int setCharset(string s)
  485. {
  486. return cucul_set_dither_charset(_dither, s);
  487. }
  488. public int setMode(string s)
  489. {
  490. return cucul_set_dither_mode(_dither, s);
  491. }
  492. /* <FIXME> */
  493. public string[] getAntialiasList()
  494. {
  495. return cucul_get_dither_antialias_list(_dither);
  496. }
  497. public string[] getColorList()
  498. {
  499. return cucul_get_dither_color_list(_dither);
  500. }
  501. public string[] getCharsetList()
  502. {
  503. return cucul_get_dither_charset_list(_dither);
  504. }
  505. public string[] getModeList()
  506. {
  507. return cucul_get_dither_mode_list(_dither);
  508. }
  509. /* </FIXME> */
  510. }
  511. }