lib_glue.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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, &ws);
  177. sq_getinteger(v, 6, &hs);
  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_fillCircle (HSQUIRRELVM v)
  208. {
  209. int x, y, r, color;
  210. sq_getinteger(v, 2, &x);
  211. sq_getinteger(v, 3, &y);
  212. sq_getinteger(v, 4, &r);
  213. sq_getinteger(v, 5, &color);
  214. fillCircle(x, y, r, color);
  215. }
  216. SQInteger n2d_fillEllipse (HSQUIRRELVM v)
  217. {
  218. int x, y, r, R, color;
  219. sq_getinteger(v, 2, &x);
  220. sq_getinteger(v, 3, &y);
  221. sq_getinteger(v, 4, &r);
  222. sq_getinteger(v, 5, &R);
  223. sq_getinteger(v, 6, &color);
  224. fillEllipse(x, y, r, R, color);
  225. }
  226. SQInteger n2d_drawString (HSQUIRRELVM v)
  227. {
  228. int x, y, margin;
  229. SQChar* str;
  230. unsigned int fc, olc;
  231. sq_getinteger(v, 2, &x);
  232. sq_getinteger(v, 3, &y);
  233. sq_getinteger(v, 4, &margin);
  234. sq_getstring(v, 5, &str);
  235. sq_getinteger(v, 6, &fc);
  236. sq_getinteger(v, 7, &olc);
  237. drawString(&x, &y, margin, (char*)str, fc, olc);
  238. // pushing new X/Y values.
  239. sq_newarray(v, 2);
  240. sq_pushinteger(v, x);
  241. sq_set(v, -2);
  242. sq_pushinteger(v, y);
  243. sq_set(v, -2);
  244. return 1;
  245. }
  246. SQInteger n2d_drawDecimal (HSQUIRRELVM v)
  247. {
  248. int x, y, n;
  249. unsigned int fc, olc;
  250. sq_getinteger(v, 2, &x);
  251. sq_getinteger(v, 3, &y);
  252. sq_getinteger(v, 4, &n);
  253. sq_getinteger(v, 5, &fc);
  254. sq_getinteger(v, 6, &olc);
  255. drawDecimal(&x, &y, n, (unsigned short)fc, (unsigned short)olc);
  256. // pushing new X/Y values.
  257. sq_newarray(v, 2);
  258. sq_pushinteger(v, x);
  259. sq_set(v, -2);
  260. sq_pushinteger(v, y);
  261. sq_set(v, -2);
  262. return 1;
  263. }
  264. SQInteger n2d_drawChar (HSQUIRRELVM v)
  265. {
  266. int x, y, margin, ch;
  267. unsigned int fc, olc;
  268. sq_getinteger(v, 2, &x);
  269. sq_getinteger(v, 3, &y);
  270. sq_getinteger(v, 4, &margin);
  271. sq_getstring(v, 5, &ch);
  272. sq_getinteger(v, 6, &fc);
  273. sq_getinteger(v, 7, &olc);
  274. drawChar(&x, &y, margin, ch, fc, olc);
  275. // pushing new X/Y values.
  276. sq_newarray(v, 2);
  277. sq_pushinteger(v, x);
  278. sq_set(v, -2);
  279. sq_pushinteger(v, y);
  280. sq_set(v, -2);
  281. return 1;
  282. }
  283. #define _DECL_GLOBALIO_FUNC(name,nparams,typecheck) {_SC(#name),n2d_##name,nparams,typecheck}
  284. static const SQRegFunction n2d_funcs[]={
  285. _DECL_GLOBALIO_FUNC(initBuffering,1,_SC(".")),
  286. _DECL_GLOBALIO_FUNC(deinitBuffering,1,_SC(".")),
  287. _DECL_GLOBALIO_FUNC(updateScreen,1,_SC(".")),
  288. _DECL_GLOBALIO_FUNC(clearBuffer,2,_SC(".i")),
  289. _DECL_GLOBALIO_FUNC(clearBufferB,1,_SC(".")),
  290. _DECL_GLOBALIO_FUNC(clearBufferW,1,_SC(".")),
  291. _DECL_GLOBALIO_FUNC(getPixel,4,_SC(".sii")),
  292. _DECL_GLOBALIO_FUNC(setPixelUnsafe,4,_SC(".iii")),
  293. _DECL_GLOBALIO_FUNC(setPixel,4,_SC(".iii")),
  294. _DECL_GLOBALIO_FUNC(setPixelRGB,6,_SC(".iiiii")),
  295. _DECL_GLOBALIO_FUNC(drawHLine,4,_SC(".iii")),
  296. _DECL_GLOBALIO_FUNC(fillRect,5,_SC(".iiii")),
  297. _DECL_GLOBALIO_FUNC(drawSprite,6,_SC(".xiiii")),
  298. _DECL_GLOBALIO_FUNC(drawSpritePart,10,_SC(".xiiiiiiii")),
  299. _DECL_GLOBALIO_FUNC(drawSpriteScaled,8,_SC(".xiiiiiiii")),
  300. _DECL_GLOBALIO_FUNC(drawSpriteRotated,13,_SC(".xiiiiiiiiiii")),
  301. _DECL_GLOBALIO_FUNC(fillRect,5,_SC(".iiii")),
  302. _DECL_GLOBALIO_FUNC(fillCircle,5,_SC(".iiii")),
  303. _DECL_GLOBALIO_FUNC(fillEllipse,5,_SC(".iiiii")),
  304. _DECL_GLOBALIO_FUNC(drawString,7,_SC(".iiisii")),
  305. _DECL_GLOBALIO_FUNC(drawDecimal,5,_SC(".iiiii")),
  306. _DECL_GLOBALIO_FUNC(drawChar,7,_SC(".iiiiiii")),
  307. {NULL,(SQFUNCTION)0,0,NULL}
  308. };
  309. SQRESULT register_n2dlib (HSQUIRRELVM v)
  310. {
  311. return register_lib(v, _SC("n2d"), n2d_funcs);
  312. }
  313. #undef _DECL_GLOBALIO_FUNC