lib_glue.c 8.6 KB

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