lib_glue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. // Key glue data
  36. #define GLUE_KEY(x) &KEY_NSPIRE_ ## x,
  37. static const t_key const * key_array[] = {
  38. #include "n2dlib_keys.xmacro"
  39. };
  40. #undef GLUE_KEY
  41. #define GLUE_KEY(x) "NK_"#x ,
  42. static const char * key_array_names[] = {
  43. #include "n2dlib_keys.xmacro"
  44. };
  45. #undef GLUE_KEY
  46. // Utilities
  47. SQInteger n2d_itofix (HSQUIRRELVM v)
  48. {
  49. int i;
  50. sq_getinteger(v, 2, &i);
  51. sq_pushinteger(v, itofix(i));
  52. return 1;
  53. }
  54. SQInteger n2d_fixtoi (HSQUIRRELVM v)
  55. {
  56. Fixed f;
  57. sq_getinteger(v, 2, &f);
  58. sq_pushinteger(v, fixtoi(f));
  59. return 1;
  60. }
  61. SQInteger n2d_fixdiv (HSQUIRRELVM v)
  62. {
  63. Fixed x, y;
  64. sq_getinteger(v, 2, &x);
  65. sq_getinteger(v, 3, &y);
  66. sq_pushinteger(v, fixdiv(x, y));
  67. }
  68. SQInteger n2d_fixmul (HSQUIRRELVM v)
  69. {
  70. Fixed x, y;
  71. sq_getinteger(v, 2, &x);
  72. sq_getinteger(v, 3, &y);
  73. sq_pushinteger(v, fixmul(x, y));
  74. return 1;
  75. }
  76. SQInteger n2d_fixcos (HSQUIRRELVM v)
  77. {
  78. Fixed angle;
  79. sq_getinteger(v, 2, &angle);
  80. sq_pushinteger(v, fixcos(angle));
  81. return 1;
  82. }
  83. SQInteger n2d_fixsin (HSQUIRRELVM v)
  84. {
  85. Fixed angle;
  86. sq_getinteger(v, 2, &angle);
  87. sq_pushinteger(v, fixsin(angle));
  88. return 1;
  89. }
  90. // Drawing routines
  91. SQInteger n2d_initBuffering (HSQUIRRELVM v)
  92. {
  93. initBuffering();
  94. return 0;
  95. }
  96. SQInteger n2d_updateScreen (HSQUIRRELVM v)
  97. {
  98. updateScreen();
  99. return 0;
  100. }
  101. SQInteger n2d_deinitBuffering (HSQUIRRELVM v)
  102. {
  103. deinitBuffering();
  104. return 0;
  105. }
  106. SQInteger n2d_clearBuffer (HSQUIRRELVM v)
  107. {
  108. unsigned int color;
  109. sq_getinteger(v, 2, &color);
  110. clearBuffer((unsigned short) color);
  111. return 0;
  112. }
  113. SQInteger n2d_clearBufferB (HSQUIRRELVM v)
  114. {
  115. clearBufferB();
  116. return 0;
  117. }
  118. SQInteger n2d_clearBufferW (HSQUIRRELVM v)
  119. {
  120. clearBufferW();
  121. return 0;
  122. }
  123. SQInteger n2d_getPixel (HSQUIRRELVM v)
  124. {
  125. SQUserPointer image;
  126. unsigned int x, y;
  127. sqstd_getblob(v, 2, &image);
  128. sq_getinteger(v, 3, &x);
  129. sq_getinteger(v, 4, &y);
  130. unsigned int value = getPixel((unsigned short*) image, x, y);
  131. sq_pushinteger(v, value);
  132. return 1;
  133. }
  134. SQInteger n2d_setPixelUnsafe (HSQUIRRELVM v)
  135. {
  136. unsigned int x, y, color;
  137. sq_getinteger(v, 2, &x);
  138. sq_getinteger(v, 3, &y);
  139. sq_getinteger(v, 4, &color);
  140. setPixelUnsafe(x, y, (unsigned short)color);
  141. return 0;
  142. }
  143. SQInteger n2d_setPixel (HSQUIRRELVM v)
  144. {
  145. unsigned int x, y, color;
  146. sq_getinteger(v, 2, &x);
  147. sq_getinteger(v, 3, &y);
  148. sq_getinteger(v, 4, &color);
  149. setPixel(x, y, (unsigned short)color);
  150. return 0;
  151. }
  152. SQInteger n2d_setPixelRGB (HSQUIRRELVM v)
  153. {
  154. unsigned int x, y, r, g, b;
  155. sq_getinteger(v, 2, &x);
  156. sq_getinteger(v, 3, &y);
  157. sq_getinteger(v, 4, &r);
  158. sq_getinteger(v, 5, &g);
  159. sq_getinteger(v, 6, &b);
  160. setPixelRGB(x, y, (unsigned char)r, (unsigned char)g, (unsigned char)b);
  161. return 0;
  162. }
  163. SQInteger n2d_drawHLine (HSQUIRRELVM v)
  164. {
  165. int y, x1, x2;
  166. unsigned int color;
  167. sq_getinteger(v, 2, &y);
  168. sq_getinteger(v, 3, &x1);
  169. sq_getinteger(v, 4, &x2);
  170. sq_getinteger(v, 5, &color);
  171. drawHLine(y, x1, x2, color);
  172. return 0;
  173. }
  174. SQInteger n2d_drawVLine (HSQUIRRELVM v)
  175. {
  176. int x, y1, y2;
  177. unsigned int color;
  178. sq_getinteger(v, 2, &x);
  179. sq_getinteger(v, 3, &y1);
  180. sq_getinteger(v, 4, &y2);
  181. sq_getinteger(v, 5, &color);
  182. drawVLine(x, y1, y2, color);
  183. return 0;
  184. }
  185. SQInteger n2d_fillRect (HSQUIRRELVM v)
  186. {
  187. int x, y, w, h, color;
  188. sq_getinteger(v, 2, &x);
  189. sq_getinteger(v, 3, &y);
  190. sq_getinteger(v, 4, &w);
  191. sq_getinteger(v, 5, &h);
  192. sq_getinteger(v, 6, &color);
  193. fillRect(x, y, w, h, color);
  194. }
  195. SQInteger n2d_drawSprite (HSQUIRRELVM v)
  196. {
  197. SQUserPointer image;
  198. unsigned x, y, flash;
  199. unsigned int flash_color;
  200. sqstd_getblob(v, 2, &image);
  201. sq_getinteger(v, 3, &x);
  202. sq_getinteger(v, 4, &y);
  203. sq_getinteger(v, 5, &flash);
  204. sq_getinteger(v, 6, &flash_color);
  205. drawSprite((const unsigned short*)image, x, y, flash, (unsigned short) flash_color);
  206. return 0;
  207. }
  208. SQInteger n2d_drawSpritePart (HSQUIRRELVM v)
  209. {
  210. SQUserPointer image;
  211. unsigned x, y, flash;
  212. unsigned xp, yp, wp, hp;
  213. unsigned int flash_color;
  214. sqstd_getblob(v, 2, &image);
  215. sq_getinteger(v, 3, &x);
  216. sq_getinteger(v, 4, &y);
  217. sq_getinteger(v, 5, &xp);
  218. sq_getinteger(v, 6, &yp);
  219. sq_getinteger(v, 7, &wp);
  220. sq_getinteger(v, 8, &hp);
  221. sq_getinteger(v, 9, &flash);
  222. sq_getinteger(v, 10, &flash_color);
  223. Rect r = {x, y, wp, hp};
  224. drawSpritePart((const unsigned short*)image, x, y, &r, flash, (unsigned short) flash_color);
  225. return 0;
  226. }
  227. SQInteger n2d_drawSpriteScaled (HSQUIRRELVM v)
  228. {
  229. SQUserPointer image;
  230. int xs, ys, ws, hs, flash;
  231. unsigned int flash_color;
  232. sqstd_getblob(v, 2, &image);
  233. sq_getinteger(v, 3, &xs);
  234. sq_getinteger(v, 4, &ys);
  235. sq_getinteger(v, 5, &hs);
  236. sq_getinteger(v, 6, &ws);
  237. sq_getinteger(v, 7, &flash);
  238. sq_getinteger(v, 8, &flash_color);
  239. Rect r = {xs, ys, ws, hs};
  240. drawSpriteScaled((const unsigned short*)image, &r, flash, (unsigned short) flash_color);
  241. return 0;
  242. }
  243. SQInteger n2d_drawSpriteRotated (HSQUIRRELVM v)
  244. {
  245. SQUserPointer image;
  246. int xsr, ysr, wsr, hsr;
  247. int xrc, yrc, wrc, hrc;
  248. int angle, flash;
  249. unsigned int flash_color;
  250. sqstd_getblob(v, 2, &image);
  251. sq_getinteger(v, 3, &xsr);
  252. sq_getinteger(v, 4, &ysr);
  253. sq_getinteger(v, 5, &wsr);
  254. sq_getinteger(v, 6, &hsr);
  255. sq_getinteger(v, 7, &xrc);
  256. sq_getinteger(v, 8, &yrc);
  257. sq_getinteger(v, 9, &wrc);
  258. sq_getinteger(v, 10, &hrc);
  259. sq_getinteger(v, 11, &angle);
  260. sq_getinteger(v, 12, &flash);
  261. sq_getinteger(v, 13, &flash_color);
  262. Rect sr = {xsr, ysr, wsr, hsr};
  263. Rect rc = {xrc, yrc, wrc, hrc};
  264. drawSpriteRotated((const unsigned short*)image, &sr, &rc, (Fixed)angle, flash, flash_color);
  265. }
  266. SQInteger n2d_drawLine (HSQUIRRELVM v)
  267. {
  268. int x1, x2, y1, y2;
  269. unsigned int color;
  270. sq_getinteger(v, 2, &x1);
  271. sq_getinteger(v, 3, &y1);
  272. sq_getinteger(v, 4, &x2);
  273. sq_getinteger(v, 5, &y2);
  274. sq_getinteger(v, 6, &color);
  275. drawLine(x1, y1, x2, y2, color);
  276. return 0;
  277. }
  278. SQInteger n2d_fillCircle (HSQUIRRELVM v)
  279. {
  280. int x, y, r, color;
  281. sq_getinteger(v, 2, &x);
  282. sq_getinteger(v, 3, &y);
  283. sq_getinteger(v, 4, &r);
  284. sq_getinteger(v, 5, &color);
  285. fillCircle(x, y, r, color);
  286. }
  287. SQInteger n2d_fillEllipse (HSQUIRRELVM v)
  288. {
  289. int x, y, r, R, color;
  290. sq_getinteger(v, 2, &x);
  291. sq_getinteger(v, 3, &y);
  292. sq_getinteger(v, 4, &r);
  293. sq_getinteger(v, 5, &R);
  294. sq_getinteger(v, 6, &color);
  295. fillEllipse(x, y, r, R, color);
  296. }
  297. SQInteger n2d_drawString (HSQUIRRELVM v)
  298. {
  299. int x, y, margin;
  300. SQChar* str;
  301. unsigned int fc, olc;
  302. sq_getinteger(v, 2, &x);
  303. sq_getinteger(v, 3, &y);
  304. sq_getinteger(v, 4, &margin);
  305. sq_getstring(v, 5, (const SQChar**)&str);
  306. sq_getinteger(v, 6, &fc);
  307. sq_getinteger(v, 7, &olc);
  308. drawString(&x, &y, margin, (char*)str, fc, olc);
  309. // pushing new X/Y values.
  310. sq_newarray(v, 2);
  311. sq_pushinteger(v, x);
  312. sq_set(v, -2);
  313. sq_pushinteger(v, y);
  314. sq_set(v, -2);
  315. return 1;
  316. }
  317. SQInteger n2d_drawDecimal (HSQUIRRELVM v)
  318. {
  319. int x, y, n;
  320. unsigned int fc, olc;
  321. sq_getinteger(v, 2, &x);
  322. sq_getinteger(v, 3, &y);
  323. sq_getinteger(v, 4, &n);
  324. sq_getinteger(v, 5, &fc);
  325. sq_getinteger(v, 6, &olc);
  326. drawDecimal(&x, &y, n, (unsigned short)fc, (unsigned short)olc);
  327. // pushing new X/Y values.
  328. sq_newarray(v, 2);
  329. sq_pushinteger(v, x);
  330. sq_set(v, -2);
  331. sq_pushinteger(v, y);
  332. sq_set(v, -2);
  333. return 1;
  334. }
  335. SQInteger n2d_drawChar (HSQUIRRELVM v)
  336. {
  337. int x, y, margin, ch;
  338. unsigned int fc, olc;
  339. sq_getinteger(v, 2, &x);
  340. sq_getinteger(v, 3, &y);
  341. sq_getinteger(v, 4, &margin);
  342. sq_getinteger(v, 5, &ch);
  343. sq_getinteger(v, 6, &fc);
  344. sq_getinteger(v, 7, &olc);
  345. drawChar(&x, &y, margin, ch, fc, olc);
  346. // pushing new X/Y values.
  347. sq_newarray(v, 2);
  348. sq_pushinteger(v, x);
  349. sq_set(v, -2);
  350. sq_pushinteger(v, y);
  351. sq_set(v, -2);
  352. return 1;
  353. }
  354. SQInteger n2d_numberWidth (HSQUIRRELVM v)
  355. {
  356. int n;
  357. sq_getinteger(v, 2);
  358. sq_pushinteger(v, numberWidth(n));
  359. return 1;
  360. }
  361. SQInteger n2d_stringWidth (HSQUIRRELVM v)
  362. {
  363. SQChar* str;
  364. sq_getstring(v, 2, (const SQChar**)&str);
  365. sq_pushinteger(v, stringWidth((const char*)str));
  366. return 1;
  367. }
  368. SQInteger n2d_getKeyPressed(HSQUIRRELVM v)
  369. {
  370. t_key collect;
  371. int result = get_key_pressed(&collect);
  372. if(result)
  373. {
  374. sq_newtable(v);
  375. sq_pushstring(v, "row", -1);
  376. sq_pushinteger(v, collect.row);
  377. sq_newslot(v, -3, SQFalse);
  378. sq_pushstring(v, "col", -1);
  379. sq_pushinteger(v, collect.col);
  380. sq_newslot(v, -3, SQFalse);
  381. sq_pushstring(v, "tpad_row", -1);
  382. sq_pushinteger(v, collect.tpad_row);
  383. sq_newslot(v, -3, SQFalse);
  384. sq_pushstring(v, "tpad_col", -1);
  385. sq_pushinteger(v, collect.tpad_col);
  386. sq_newslot(v, -3, SQFalse);
  387. sq_pushstring(v, "tpad_arrow", -1);
  388. sq_pushinteger(v, collect.tpad_arrow);
  389. sq_newslot(v, -3, SQFalse);
  390. }
  391. else
  392. {
  393. sq_newtable(v);
  394. sq_pushstring(v, "row", -1);
  395. sq_pushinteger(v, _KEY_DUMMY_ROW);
  396. sq_newslot(v, -3, SQFalse);
  397. sq_pushstring(v, "col", -1);
  398. sq_pushinteger(v, _KEY_DUMMY_COL);
  399. sq_newslot(v, -3, SQFalse);
  400. sq_pushstring(v, "tpad_row", -1);
  401. sq_pushinteger(v, _KEY_DUMMY_ROW);
  402. sq_newslot(v, -3, SQFalse);
  403. sq_pushstring(v, "tpad_col", -1);
  404. sq_pushinteger(v,_KEY_DUMMY_COL);
  405. sq_newslot(v, -3, SQFalse);
  406. sq_pushstring(v, "tpad_arrow", -1);
  407. sq_pushinteger(v, TPAD_ARROW_NONE);
  408. sq_newslot(v, -3, SQFalse);
  409. }
  410. return 1;
  411. }
  412. SQInteger n2d_isKey(HSQUIRRELVM v)
  413. {
  414. t_key collect;
  415. int arrow;
  416. int key_index;
  417. sq_getinteger(v, 3, &key_index);
  418. sq_pushstring(v, "row", -1);
  419. sq_get(v, 2);
  420. sq_getinteger(v, -1, &collect.row);
  421. sq_pushstring(v, "col", -1);
  422. sq_get(v, 2);
  423. sq_getinteger(v, -1, &collect.col);
  424. sq_pushstring(v, "tpad_row", -1);
  425. sq_get(v, 2);
  426. sq_getinteger(v, -1, &collect.tpad_row);
  427. sq_pushstring(v, "tpad_col", -1);
  428. sq_get(v, 2);
  429. sq_getinteger(v, -1, &collect.tpad_col);
  430. sq_pushstring(v, "tpad_arrow", -1);
  431. sq_get(v, 2);
  432. sq_getinteger(v, -1, &arrow);
  433. collect.tpad_arrow = arrow;
  434. if((key_index>=0) && (key_index < sizeof(key_array)/sizeof(t_key*)))
  435. {
  436. t_key* ref = key_array[key_index];
  437. sq_pushbool(v, isKey(collect, *ref));
  438. }
  439. else
  440. sq_pushbool(v, false);
  441. return 1;
  442. }
  443. #define _DECL_GLOBALIO_FUNC(name,nparams,typecheck) {_SC(#name),n2d_##name,nparams,typecheck}
  444. static const SQRegFunction n2d_funcs[]={
  445. _DECL_GLOBALIO_FUNC(itofix,2,_SC(".i")),
  446. _DECL_GLOBALIO_FUNC(fixtoi,2,_SC(".i")),
  447. _DECL_GLOBALIO_FUNC(fixdiv,2,_SC(".ii")),
  448. _DECL_GLOBALIO_FUNC(fixmul,2,_SC(".ii")),
  449. _DECL_GLOBALIO_FUNC(fixsin,2,_SC(".i")),
  450. _DECL_GLOBALIO_FUNC(fixcos,2,_SC(".i")),
  451. _DECL_GLOBALIO_FUNC(initBuffering,1,_SC(".")),
  452. _DECL_GLOBALIO_FUNC(deinitBuffering,1,_SC(".")),
  453. _DECL_GLOBALIO_FUNC(updateScreen,1,_SC(".")),
  454. _DECL_GLOBALIO_FUNC(clearBuffer,2,_SC(".i")),
  455. _DECL_GLOBALIO_FUNC(clearBufferB,1,_SC(".")),
  456. _DECL_GLOBALIO_FUNC(clearBufferW,1,_SC(".")),
  457. _DECL_GLOBALIO_FUNC(getPixel,4,_SC(".sii")),
  458. _DECL_GLOBALIO_FUNC(setPixelUnsafe,4,_SC(".iii")),
  459. _DECL_GLOBALIO_FUNC(setPixel,4,_SC(".iii")),
  460. _DECL_GLOBALIO_FUNC(setPixelRGB,6,_SC(".iiiii")),
  461. _DECL_GLOBALIO_FUNC(drawHLine,5,_SC(".iiii")),
  462. _DECL_GLOBALIO_FUNC(drawVLine,5,_SC(".iiii")),
  463. _DECL_GLOBALIO_FUNC(fillRect,6,_SC(".iiiii")),
  464. _DECL_GLOBALIO_FUNC(drawSprite,6,_SC(".xiiii")),
  465. _DECL_GLOBALIO_FUNC(drawSpritePart,10,_SC(".xiiiiiiii")),
  466. _DECL_GLOBALIO_FUNC(drawSpriteScaled,8,_SC(".xiiiiiiii")),
  467. _DECL_GLOBALIO_FUNC(drawSpriteRotated,13,_SC(".xiiiiiiiiiii")),
  468. _DECL_GLOBALIO_FUNC(drawLine,6,_SC(".iiiii")),
  469. _DECL_GLOBALIO_FUNC(fillRect,6,_SC(".iiiii")),
  470. _DECL_GLOBALIO_FUNC(fillCircle,5,_SC(".iiii")),
  471. _DECL_GLOBALIO_FUNC(fillEllipse,5,_SC(".iiiii")),
  472. _DECL_GLOBALIO_FUNC(drawString,7,_SC(".iiisii")),
  473. _DECL_GLOBALIO_FUNC(drawDecimal,6,_SC(".iiiii")),
  474. _DECL_GLOBALIO_FUNC(drawChar,7,_SC(".iiiiiii")),
  475. _DECL_GLOBALIO_FUNC(numberWidth,2,_SC(".i")),
  476. _DECL_GLOBALIO_FUNC(stringWidth,2,_SC(".s")),
  477. _DECL_GLOBALIO_FUNC(getKeyPressed,1,_SC(".")),
  478. _DECL_GLOBALIO_FUNC(isKey,3,_SC(".ti")),
  479. {NULL,(SQFUNCTION)0,0,NULL}
  480. };
  481. SQRESULT register_keys (HSQUIRRELVM v)
  482. {
  483. SQInteger top = sq_gettop(v);
  484. SQObject enumeration_table;
  485. // Get const table
  486. sq_pushconsttable(v);
  487. // push new enum's name
  488. sq_pushstring(v,"n2dk",-1);
  489. // Create enum tale
  490. sq_newtable(v);
  491. // For each value, push it in the enum.
  492. for(int i = 0; i < sizeof(key_array)/sizeof(t_key*); i++)
  493. {
  494. // table.i = index;
  495. sq_pushstring(v, (SQChar*)key_array_names[i], -1);
  496. sq_pushinteger(v, i);
  497. sq_newslot(v, -3, false);
  498. }
  499. // Adding it to the const table
  500. sq_newslot(v, -3, SQFalse);
  501. // Popping the const table.
  502. sq_settop(v,top);
  503. }
  504. SQRESULT register_n2dlib (HSQUIRRELVM v)
  505. {
  506. register_keys(v);
  507. register_lib(v, _SC("n2d"), n2d_funcs);
  508. }
  509. #undef _DECL_GLOBALIO_FUNC