lib_glue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include <squirrel.h>
  2. #include <sqstdblob.h>
  3. #include "n2DLib/n2DLib.h"
  4. SQInteger register_global_func(HSQUIRRELVM v,SQFUNCTION f,const char *fname)
  5. {
  6. sq_pushroottable(v);
  7. sq_pushstring(v,fname,-1);
  8. sq_newclosure(v,f,0); //create a new function
  9. sq_newslot(v,-3, SQFalse);
  10. sq_pop(v,1); //pops the root table
  11. return 0;
  12. }
  13. SQRESULT register_lib(HSQUIRRELVM v, const SQChar *lib_name, const SQRegFunction *reg)
  14. {
  15. SQInteger top = sq_gettop(v);
  16. sq_pushstring(v,lib_name,-1);
  17. sq_newtable(v);
  18. int i = 0;
  19. while(reg[i].name != NULL) {
  20. const SQRegFunction* fun = &reg[i];
  21. sq_pushstring(v, fun->name, -1);
  22. sq_newclosure(v, fun->f, 0);
  23. sq_setparamscheck(v, fun->nparamscheck, fun->typemask);
  24. sq_setnativeclosurename(v, -1, fun->name);
  25. sq_newslot(v, -3, SQFalse);
  26. i++;
  27. }
  28. sq_newslot(v, -3, SQFalse);
  29. sq_settop(v,top);
  30. return SQ_ERROR;
  31. }
  32. //////////////////
  33. // n2DLib glue. //
  34. //////////////////
  35. // Utilities
  36. SQInteger n2d_itofix (HSQUIRRELVM v)
  37. {
  38. int i;
  39. sq_getinteger(v, 2, &i);
  40. sq_pushinteger(v, itofix(i));
  41. return 1;
  42. }
  43. SQInteger n2d_fixtoi (HSQUIRRELVM v)
  44. {
  45. Fixed f;
  46. sq_getinteger(v, 2, &f);
  47. sq_pushinteger(v, fixtoi(f));
  48. return 1;
  49. }
  50. SQInteger n2d_fixdiv (HSQUIRRELVM v)
  51. {
  52. Fixed x, y;
  53. sq_getinteger(v, 2, &x);
  54. sq_getinteger(v, 3, &y);
  55. sq_pushinteger(v, fixdiv(x, y));
  56. }
  57. SQInteger n2d_fixmul (HSQUIRRELVM v)
  58. {
  59. Fixed x, y;
  60. sq_getinteger(v, 2, &x);
  61. sq_getinteger(v, 3, &y);
  62. sq_pushinteger(v, fixmul(x, y));
  63. return 1;
  64. }
  65. SQInteger n2d_fixcos (HSQUIRRELVM v)
  66. {
  67. Fixed angle;
  68. sq_getinteger(v, 2, &angle);
  69. sq_pushinteger(v, fixcos(angle));
  70. return 1;
  71. }
  72. SQInteger n2d_fixsin (HSQUIRRELVM v)
  73. {
  74. Fixed angle;
  75. sq_getinteger(v, 2, &angle);
  76. sq_pushinteger(v, fixsin(angle));
  77. return 1;
  78. }
  79. // Drawing routines
  80. SQInteger n2d_initBuffering (HSQUIRRELVM v)
  81. {
  82. initBuffering();
  83. return 0;
  84. }
  85. SQInteger n2d_updateScreen (HSQUIRRELVM v)
  86. {
  87. updateScreen();
  88. return 0;
  89. }
  90. SQInteger n2d_deinitBuffering (HSQUIRRELVM v)
  91. {
  92. deinitBuffering();
  93. return 0;
  94. }
  95. SQInteger n2d_clearBuffer (HSQUIRRELVM v)
  96. {
  97. unsigned int color;
  98. sq_getinteger(v, 2, &color);
  99. clearBuffer((unsigned short) color);
  100. return 0;
  101. }
  102. SQInteger n2d_clearBufferB (HSQUIRRELVM v)
  103. {
  104. clearBufferB();
  105. return 0;
  106. }
  107. SQInteger n2d_clearBufferW (HSQUIRRELVM v)
  108. {
  109. clearBufferW();
  110. return 0;
  111. }
  112. SQInteger n2d_getPixel (HSQUIRRELVM v)
  113. {
  114. SQUserPointer image;
  115. unsigned int x, y;
  116. sqstd_getblob(v, 2, &image);
  117. sq_getinteger(v, 3, &x);
  118. sq_getinteger(v, 4, &y);
  119. unsigned int value = getPixel((unsigned short*) image, x, y);
  120. sq_pushinteger(v, value);
  121. return 1;
  122. }
  123. SQInteger n2d_setPixelUnsafe (HSQUIRRELVM v)
  124. {
  125. unsigned int x, y, color;
  126. sq_getinteger(v, 2, &x);
  127. sq_getinteger(v, 3, &y);
  128. sq_getinteger(v, 4, &color);
  129. setPixelUnsafe(x, y, (unsigned short)color);
  130. return 0;
  131. }
  132. SQInteger n2d_setPixel (HSQUIRRELVM v)
  133. {
  134. unsigned int x, y, color;
  135. sq_getinteger(v, 2, &x);
  136. sq_getinteger(v, 3, &y);
  137. sq_getinteger(v, 4, &color);
  138. setPixel(x, y, (unsigned short)color);
  139. return 0;
  140. }
  141. SQInteger n2d_setPixelRGB (HSQUIRRELVM v)
  142. {
  143. unsigned int x, y, r, g, b;
  144. sq_getinteger(v, 2, &x);
  145. sq_getinteger(v, 3, &y);
  146. sq_getinteger(v, 4, &r);
  147. sq_getinteger(v, 5, &g);
  148. sq_getinteger(v, 6, &b);
  149. setPixelRGB(x, y, (unsigned char)r, (unsigned char)g, (unsigned char)b);
  150. return 0;
  151. }
  152. SQInteger n2d_drawHLine (HSQUIRRELVM v)
  153. {
  154. int y, x1, x2;
  155. unsigned int color;
  156. sq_getinteger(v, 2, &y);
  157. sq_getinteger(v, 3, &x1);
  158. sq_getinteger(v, 4, &x2);
  159. sq_getinteger(v, 5, &color);
  160. drawHLine(y, x1, x2, color);
  161. return 0;
  162. }
  163. SQInteger n2d_drawVLine (HSQUIRRELVM v)
  164. {
  165. int x, y1, y2;
  166. unsigned int color;
  167. sq_getinteger(v, 2, &x);
  168. sq_getinteger(v, 3, &y1);
  169. sq_getinteger(v, 4, &y2);
  170. sq_getinteger(v, 5, &color);
  171. drawVLine(x, y1, y2, color);
  172. return 0;
  173. }
  174. SQInteger n2d_fillRect (HSQUIRRELVM v)
  175. {
  176. int x, y, w, h, color;
  177. sq_getinteger(v, 2, &x);
  178. sq_getinteger(v, 3, &y);
  179. sq_getinteger(v, 4, &w);
  180. sq_getinteger(v, 5, &h);
  181. sq_getinteger(v, 6, &color);
  182. fillRect(x, y, w, h, color);
  183. }
  184. SQInteger n2d_drawSprite (HSQUIRRELVM v)
  185. {
  186. SQUserPointer image;
  187. unsigned x, y, flash;
  188. unsigned int flash_color;
  189. sqstd_getblob(v, 2, &image);
  190. sq_getinteger(v, 3, &x);
  191. sq_getinteger(v, 4, &y);
  192. sq_getinteger(v, 5, &flash);
  193. sq_getinteger(v, 6, &flash_color);
  194. drawSprite((const unsigned short*)image, x, y, flash, (unsigned short) flash_color);
  195. return 0;
  196. }
  197. SQInteger n2d_drawSpritePart (HSQUIRRELVM v)
  198. {
  199. SQUserPointer image;
  200. unsigned x, y, flash;
  201. unsigned xp, yp, wp, hp;
  202. unsigned int flash_color;
  203. sqstd_getblob(v, 2, &image);
  204. sq_getinteger(v, 3, &x);
  205. sq_getinteger(v, 4, &y);
  206. sq_getinteger(v, 5, &xp);
  207. sq_getinteger(v, 6, &yp);
  208. sq_getinteger(v, 7, &wp);
  209. sq_getinteger(v, 8, &hp);
  210. sq_getinteger(v, 9, &flash);
  211. sq_getinteger(v, 10, &flash_color);
  212. Rect r = {x, y, wp, hp};
  213. drawSpritePart((const unsigned short*)image, x, y, &r, flash, (unsigned short) flash_color);
  214. return 0;
  215. }
  216. SQInteger n2d_drawSpriteScaled (HSQUIRRELVM v)
  217. {
  218. SQUserPointer image;
  219. int xs, ys, ws, hs, flash;
  220. unsigned int flash_color;
  221. sqstd_getblob(v, 2, &image);
  222. sq_getinteger(v, 3, &xs);
  223. sq_getinteger(v, 4, &ys);
  224. sq_getinteger(v, 5, &hs);
  225. sq_getinteger(v, 6, &ws);
  226. sq_getinteger(v, 7, &flash);
  227. sq_getinteger(v, 8, &flash_color);
  228. Rect r = {xs, ys, ws, hs};
  229. drawSpriteScaled((const unsigned short*)image, &r, flash, (unsigned short) flash_color);
  230. return 0;
  231. }
  232. SQInteger n2d_drawSpriteRotated (HSQUIRRELVM v)
  233. {
  234. SQUserPointer image;
  235. int xsr, ysr, wsr, hsr;
  236. int xrc, yrc, wrc, hrc;
  237. int angle, flash;
  238. unsigned int flash_color;
  239. sqstd_getblob(v, 2, &image);
  240. sq_getinteger(v, 3, &xsr);
  241. sq_getinteger(v, 4, &ysr);
  242. sq_getinteger(v, 5, &wsr);
  243. sq_getinteger(v, 6, &hsr);
  244. sq_getinteger(v, 7, &xrc);
  245. sq_getinteger(v, 8, &yrc);
  246. sq_getinteger(v, 9, &wrc);
  247. sq_getinteger(v, 10, &hrc);
  248. sq_getinteger(v, 11, &angle);
  249. sq_getinteger(v, 12, &flash);
  250. sq_getinteger(v, 13, &flash_color);
  251. Rect sr = {xsr, ysr, wsr, hsr};
  252. Rect rc = {xrc, yrc, wrc, hrc};
  253. drawSpriteRotated((const unsigned short*)image, &sr, &rc, (Fixed)angle, flash, flash_color);
  254. }
  255. SQInteger n2d_drawLine (HSQUIRRELVM v)
  256. {
  257. int x1, x2, y1, y2;
  258. unsigned int color;
  259. sq_getinteger(v, 2, &x1);
  260. sq_getinteger(v, 3, &y1);
  261. sq_getinteger(v, 4, &x2);
  262. sq_getinteger(v, 5, &y2);
  263. sq_getinteger(v, 6, &color);
  264. drawLine(x1, y1, x2, y2, color);
  265. return 0;
  266. }
  267. SQInteger n2d_fillCircle (HSQUIRRELVM v)
  268. {
  269. int x, y, r, color;
  270. sq_getinteger(v, 2, &x);
  271. sq_getinteger(v, 3, &y);
  272. sq_getinteger(v, 4, &r);
  273. sq_getinteger(v, 5, &color);
  274. fillCircle(x, y, r, color);
  275. }
  276. SQInteger n2d_fillEllipse (HSQUIRRELVM v)
  277. {
  278. int x, y, r, R, color;
  279. sq_getinteger(v, 2, &x);
  280. sq_getinteger(v, 3, &y);
  281. sq_getinteger(v, 4, &r);
  282. sq_getinteger(v, 5, &R);
  283. sq_getinteger(v, 6, &color);
  284. fillEllipse(x, y, r, R, color);
  285. }
  286. SQInteger n2d_drawString (HSQUIRRELVM v)
  287. {
  288. int x, y, margin;
  289. SQChar* str;
  290. unsigned int fc, olc;
  291. sq_getinteger(v, 2, &x);
  292. sq_getinteger(v, 3, &y);
  293. sq_getinteger(v, 4, &margin);
  294. sq_getstring(v, 5, (const SQChar**)&str);
  295. sq_getinteger(v, 6, &fc);
  296. sq_getinteger(v, 7, &olc);
  297. drawString(&x, &y, margin, (char*)str, fc, olc);
  298. // pushing new X/Y values.
  299. sq_newarray(v, 2);
  300. sq_pushinteger(v, x);
  301. sq_set(v, -2);
  302. sq_pushinteger(v, y);
  303. sq_set(v, -2);
  304. return 1;
  305. }
  306. SQInteger n2d_drawDecimal (HSQUIRRELVM v)
  307. {
  308. int x, y, n;
  309. unsigned int fc, olc;
  310. sq_getinteger(v, 2, &x);
  311. sq_getinteger(v, 3, &y);
  312. sq_getinteger(v, 4, &n);
  313. sq_getinteger(v, 5, &fc);
  314. sq_getinteger(v, 6, &olc);
  315. drawDecimal(&x, &y, n, (unsigned short)fc, (unsigned short)olc);
  316. // pushing new X/Y values.
  317. sq_newarray(v, 2);
  318. sq_pushinteger(v, x);
  319. sq_set(v, -2);
  320. sq_pushinteger(v, y);
  321. sq_set(v, -2);
  322. return 1;
  323. }
  324. SQInteger n2d_drawChar (HSQUIRRELVM v)
  325. {
  326. int x, y, margin, ch;
  327. unsigned int fc, olc;
  328. sq_getinteger(v, 2, &x);
  329. sq_getinteger(v, 3, &y);
  330. sq_getinteger(v, 4, &margin);
  331. sq_getinteger(v, 5, &ch);
  332. sq_getinteger(v, 6, &fc);
  333. sq_getinteger(v, 7, &olc);
  334. drawChar(&x, &y, margin, ch, fc, olc);
  335. // pushing new X/Y values.
  336. sq_newarray(v, 2);
  337. sq_pushinteger(v, x);
  338. sq_set(v, -2);
  339. sq_pushinteger(v, y);
  340. sq_set(v, -2);
  341. return 1;
  342. }
  343. SQInteger n2d_numberWidth (HSQUIRRELVM v)
  344. {
  345. int n;
  346. sq_getinteger(v, 2);
  347. sq_pushinteger(v, numberWidth(n));
  348. return 1;
  349. }
  350. SQInteger n2d_stringWidth (HSQUIRRELVM v)
  351. {
  352. SQChar* str;
  353. sq_getstring(v, 2, (const SQChar**)&str);
  354. sq_pushinteger(v, stringWidth((const char*)str));
  355. return 1;
  356. }
  357. #define _DECL_GLOBALIO_FUNC(name,nparams,typecheck) {_SC(#name),n2d_##name,nparams,typecheck}
  358. static const SQRegFunction n2d_funcs[]={
  359. _DECL_GLOBALIO_FUNC(itofix,2,_SC(".i")),
  360. _DECL_GLOBALIO_FUNC(fixtoi,2,_SC(".i")),
  361. _DECL_GLOBALIO_FUNC(fixdiv,2,_SC(".ii")),
  362. _DECL_GLOBALIO_FUNC(fixmul,2,_SC(".ii")),
  363. _DECL_GLOBALIO_FUNC(fixsin,2,_SC(".i")),
  364. _DECL_GLOBALIO_FUNC(fixcos,2,_SC(".i")),
  365. _DECL_GLOBALIO_FUNC(initBuffering,1,_SC(".")),
  366. _DECL_GLOBALIO_FUNC(deinitBuffering,1,_SC(".")),
  367. _DECL_GLOBALIO_FUNC(updateScreen,1,_SC(".")),
  368. _DECL_GLOBALIO_FUNC(clearBuffer,2,_SC(".i")),
  369. _DECL_GLOBALIO_FUNC(clearBufferB,1,_SC(".")),
  370. _DECL_GLOBALIO_FUNC(clearBufferW,1,_SC(".")),
  371. _DECL_GLOBALIO_FUNC(getPixel,4,_SC(".sii")),
  372. _DECL_GLOBALIO_FUNC(setPixelUnsafe,4,_SC(".iii")),
  373. _DECL_GLOBALIO_FUNC(setPixel,4,_SC(".iii")),
  374. _DECL_GLOBALIO_FUNC(setPixelRGB,6,_SC(".iiiii")),
  375. _DECL_GLOBALIO_FUNC(drawHLine,5,_SC(".iiii")),
  376. _DECL_GLOBALIO_FUNC(drawVLine,5,_SC(".iiii")),
  377. _DECL_GLOBALIO_FUNC(fillRect,6,_SC(".iiiii")),
  378. _DECL_GLOBALIO_FUNC(drawSprite,6,_SC(".xiiii")),
  379. _DECL_GLOBALIO_FUNC(drawSpritePart,10,_SC(".xiiiiiiii")),
  380. _DECL_GLOBALIO_FUNC(drawSpriteScaled,8,_SC(".xiiiiiiii")),
  381. _DECL_GLOBALIO_FUNC(drawSpriteRotated,13,_SC(".xiiiiiiiiiii")),
  382. _DECL_GLOBALIO_FUNC(drawLine,6,_SC(".iiiii")),
  383. _DECL_GLOBALIO_FUNC(fillRect,6,_SC(".iiiii")),
  384. _DECL_GLOBALIO_FUNC(fillCircle,5,_SC(".iiii")),
  385. _DECL_GLOBALIO_FUNC(fillEllipse,5,_SC(".iiiii")),
  386. _DECL_GLOBALIO_FUNC(drawString,7,_SC(".iiisii")),
  387. _DECL_GLOBALIO_FUNC(drawDecimal,6,_SC(".iiiii")),
  388. _DECL_GLOBALIO_FUNC(drawChar,7,_SC(".iiiiiii")),
  389. _DECL_GLOBALIO_FUNC(numberWidth,2,_SC(".i")),
  390. _DECL_GLOBALIO_FUNC(stringWidth,2,_SC(".s")),
  391. {NULL,(SQFUNCTION)0,0,NULL}
  392. };
  393. SQRESULT register_n2dlib (HSQUIRRELVM v)
  394. {
  395. return register_lib(v, _SC("n2d"), n2d_funcs);
  396. }
  397. #undef _DECL_GLOBALIO_FUNC