nsp_texture.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // Texture module
  2. // Copyright (C) 2015 Legimet + fgalliat (Xtase)
  3. //
  4. // This file is part of Duktape-nspire.
  5. //
  6. // Duktape-nspire is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Lesser General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // Duktape-nspire is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Lesser General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Lesser General Public License
  17. // along with Duktape-nspire. If not, see <http://www.gnu.org/licenses/>.
  18. #include "duktape.h"
  19. #include <limits.h>
  20. #include <stdbool.h>
  21. #include <libndls.h>
  22. // =================================================
  23. #include "framebuffer.h"
  24. // =================================================
  25. static duk_ret_t nsp_texture_constructor(duk_context *ctx) {
  26. int width = duk_require_int(ctx, 0);
  27. int height = duk_require_int(ctx, 1);
  28. if (width < 1 || height < 1) {
  29. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "Width and height must be positive");
  30. duk_throw(ctx);
  31. }
  32. bool has_transparency;
  33. uint16_t transparent_color;
  34. if ((has_transparency = duk_is_number(ctx, 2))) {
  35. transparent_color = (uint16_t)duk_get_int(ctx, 2);
  36. }
  37. duk_push_this(ctx);
  38. duk_push_fixed_buffer(ctx, width * height * 2);
  39. duk_put_prop_string(ctx, -2, "bitmap");
  40. duk_push_int(ctx, width);
  41. duk_put_prop_string(ctx, -2, "width");
  42. duk_push_int(ctx, height);
  43. duk_put_prop_string(ctx, -2, "height");
  44. if (has_transparency) {
  45. duk_push_int(ctx, transparent_color);
  46. } else {
  47. duk_push_null(ctx);
  48. }
  49. duk_put_prop_string(ctx, -2, "transparentColor");
  50. return 0;
  51. }
  52. duk_ret_t nsp_texture_display(duk_context *ctx) {
  53. duk_push_this(ctx);
  54. duk_get_prop_string(ctx, -1, "width");
  55. int width = duk_get_int(ctx, -1);
  56. duk_pop(ctx);
  57. duk_get_prop_string(ctx, -1, "height");
  58. int height = duk_get_int(ctx, -1);
  59. duk_pop(ctx);
  60. duk_get_prop_string(ctx, -1, "transparentColor");
  61. if (width != 320 || height != 240 || !duk_is_null(ctx, -1)) {
  62. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "must have dimensions 230x240 without transparency");
  63. duk_throw(ctx);
  64. }
  65. duk_pop(ctx);
  66. duk_get_prop_string(ctx, -1, "bitmap");
  67. size_t size;
  68. uint16_t *bitmap;
  69. bitmap = duk_get_buffer(ctx, -1, &size);
  70. if (bitmap == NULL || size != 320 * 240 * 2) {
  71. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap buffer does not match with dimensions");
  72. duk_throw(ctx);
  73. }
  74. memcpy(SCREEN_BASE_ADDRESS, bitmap, 320 * 240 * 2);
  75. return 0;
  76. }
  77. duk_ret_t nsp_texture_fill(duk_context *ctx) {
  78. uint16_t color = duk_require_int(ctx, 0);
  79. duk_push_this(ctx);
  80. duk_get_prop_string(ctx, -1, "bitmap");
  81. size_t size;
  82. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  83. if (bitmap == NULL) {
  84. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  85. duk_throw(ctx);
  86. }
  87. for (size_t i = 0; i < size / 2; i++) {
  88. bitmap[i] = (uint16_t)color;
  89. }
  90. return 0;
  91. }
  92. duk_ret_t nsp_texture_get_pixel(duk_context *ctx) {
  93. int x = duk_require_int(ctx, 0);
  94. int y = duk_require_int(ctx, 1);
  95. duk_push_this(ctx);
  96. duk_get_prop_string(ctx, -1, "width");
  97. int w = duk_require_int(ctx, -1);
  98. duk_pop(ctx);
  99. duk_get_prop_string(ctx, -1, "height");
  100. int h = duk_require_int(ctx, -1);
  101. duk_pop(ctx);
  102. if (w <= 0 || h <= 0) {
  103. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  104. duk_throw(ctx);
  105. }
  106. duk_get_prop_string(ctx, -1, "bitmap");
  107. size_t size;
  108. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  109. if (bitmap == NULL) {
  110. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  111. duk_throw(ctx);
  112. }
  113. if (0 <= x && x < w && 0 <= y && y < h && size >= (size_t)(w * h * 2)) {
  114. duk_push_int(ctx, bitmap[w * y + x]);
  115. return 1;
  116. } else {
  117. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "coordinates out of range");
  118. duk_throw(ctx);
  119. }
  120. }
  121. duk_ret_t nsp_texture_set_pixel(duk_context *ctx) {
  122. int x = duk_require_int(ctx, 0);
  123. int y = duk_require_int(ctx, 1);
  124. uint16_t color = duk_require_int(ctx, 2);
  125. duk_push_this(ctx);
  126. duk_get_prop_string(ctx, -1, "width");
  127. int w = duk_require_int(ctx, -1);
  128. duk_pop(ctx);
  129. duk_get_prop_string(ctx, -1, "height");
  130. int h = duk_require_int(ctx, -1);
  131. duk_pop(ctx);
  132. if (w <= 0 || h <= 0) {
  133. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  134. duk_throw(ctx);
  135. }
  136. duk_get_prop_string(ctx, -1, "bitmap");
  137. size_t size;
  138. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  139. if (bitmap == NULL) {
  140. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  141. duk_throw(ctx);
  142. }
  143. if (0 <= x && x < w && 0 <= y && y < h && size >= (size_t)(w * h * 2)) {
  144. bitmap[w * y + x] = (uint16_t)color;
  145. return 0;
  146. } else {
  147. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "coordinates out of range");
  148. duk_throw(ctx);
  149. }
  150. }
  151. // ====== Xtase drawing routines ======
  152. duk_ret_t nsp_texture_draw_line(duk_context *ctx) {
  153. int x1 = duk_require_int(ctx, 0);
  154. int y1 = duk_require_int(ctx, 1);
  155. int x2 = duk_require_int(ctx, 2);
  156. int y2 = duk_require_int(ctx, 3);
  157. uint16_t color = duk_require_int(ctx, 4);
  158. duk_push_this(ctx);
  159. duk_get_prop_string(ctx, -1, "width");
  160. int w = duk_require_int(ctx, -1);
  161. duk_pop(ctx);
  162. duk_get_prop_string(ctx, -1, "height");
  163. int h = duk_require_int(ctx, -1);
  164. duk_pop(ctx);
  165. if (w <= 0 || h <= 0) {
  166. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  167. duk_throw(ctx);
  168. }
  169. duk_get_prop_string(ctx, -1, "bitmap");
  170. size_t size;
  171. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  172. if (bitmap == NULL) {
  173. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  174. duk_throw(ctx);
  175. }
  176. FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
  177. fb->width = w;
  178. fb->height = h;
  179. fb->fb_size = w * h;
  180. fb->fb = bitmap;
  181. drawLine(x1,y1,x2,y2,color,fb);
  182. free(fb);
  183. return 0;
  184. }
  185. duk_ret_t nsp_texture_draw_rect(duk_context *ctx) {
  186. int x = duk_require_int(ctx, 0);
  187. int y = duk_require_int(ctx, 1);
  188. int w_ = duk_require_int(ctx, 2);
  189. int h_ = duk_require_int(ctx, 3);
  190. uint16_t color = duk_require_int(ctx, 4);
  191. duk_push_this(ctx);
  192. duk_get_prop_string(ctx, -1, "width");
  193. int w = duk_require_int(ctx, -1);
  194. duk_pop(ctx);
  195. duk_get_prop_string(ctx, -1, "height");
  196. int h = duk_require_int(ctx, -1);
  197. duk_pop(ctx);
  198. if (w <= 0 || h <= 0) {
  199. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  200. duk_throw(ctx);
  201. }
  202. duk_get_prop_string(ctx, -1, "bitmap");
  203. size_t size;
  204. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  205. if (bitmap == NULL) {
  206. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  207. duk_throw(ctx);
  208. }
  209. FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
  210. fb->width = w;
  211. fb->height = h;
  212. fb->fb_size = w * h;
  213. fb->fb = bitmap;
  214. drawRect(x,y,w_,h_,color,fb);
  215. free(fb);
  216. return 0;
  217. }
  218. duk_ret_t nsp_texture_fill_rect(duk_context *ctx) {
  219. int x = duk_require_int(ctx, 0);
  220. int y = duk_require_int(ctx, 1);
  221. int w_ = duk_require_int(ctx, 2);
  222. int h_ = duk_require_int(ctx, 3);
  223. uint16_t color = duk_require_int(ctx, 4);
  224. duk_push_this(ctx);
  225. duk_get_prop_string(ctx, -1, "width");
  226. int w = duk_require_int(ctx, -1);
  227. duk_pop(ctx);
  228. duk_get_prop_string(ctx, -1, "height");
  229. int h = duk_require_int(ctx, -1);
  230. duk_pop(ctx);
  231. if (w <= 0 || h <= 0) {
  232. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  233. duk_throw(ctx);
  234. }
  235. duk_get_prop_string(ctx, -1, "bitmap");
  236. size_t size;
  237. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  238. if (bitmap == NULL) {
  239. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  240. duk_throw(ctx);
  241. }
  242. FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
  243. fb->width = w;
  244. fb->height = h;
  245. fb->fb_size = w * h;
  246. fb->fb = bitmap;
  247. fillRect(x,y,w_,h_,color,fb);
  248. free(fb);
  249. return 0;
  250. }
  251. int* getIntArray(duk_context *ctx, int stackIndex) {
  252. static int* result = NULL;
  253. if(duk_is_array(ctx, stackIndex)) {
  254. int resultLen = duk_get_length(ctx, stackIndex);
  255. result = (int*)malloc( resultLen * sizeof(int) );
  256. memset(result,0,resultLen);
  257. duk_enum(ctx, stackIndex, DUK_ENUM_ARRAY_INDICES_ONLY);
  258. // NOT stackIndex because waits enumIndex that is -1
  259. int idx=0;
  260. while (duk_next(ctx, -1, 1)) {
  261. // in JS/duktape toto[1] <=> toto["1"]
  262. // that's why keys are strings
  263. const char* k = duk_to_string(ctx, -2);
  264. int v = duk_to_int(ctx, -1);
  265. //printf("key=%s, value=%d\n", k, v);
  266. result[idx++] = v;
  267. duk_pop_2(ctx);
  268. }
  269. duk_pop(ctx); // duk_enum
  270. } else {
  271. printf("Found NO array\n");
  272. }
  273. return result;
  274. }
  275. duk_ret_t nsp_texture_draw_polyLines(duk_context *ctx) {
  276. duk_require_object_coercible(ctx, 0);
  277. int* xys = getIntArray(ctx,0);
  278. int nbPts = duk_require_int(ctx, 1);
  279. uint16_t color = duk_require_int(ctx, 2);
  280. duk_push_this(ctx);
  281. duk_get_prop_string(ctx, -1, "width");
  282. int w = duk_require_int(ctx, -1);
  283. duk_pop(ctx);
  284. duk_get_prop_string(ctx, -1, "height");
  285. int h = duk_require_int(ctx, -1);
  286. duk_pop(ctx);
  287. if (w <= 0 || h <= 0) {
  288. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  289. duk_throw(ctx);
  290. }
  291. duk_get_prop_string(ctx, -1, "bitmap");
  292. size_t size;
  293. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  294. if (bitmap == NULL) {
  295. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  296. duk_throw(ctx);
  297. }
  298. FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
  299. fb->width = w;
  300. fb->height = h;
  301. fb->fb_size = w * h;
  302. fb->fb = bitmap;
  303. drawPolyLines( xys, nbPts, color, fb );
  304. free(fb);
  305. return 0;
  306. }
  307. duk_ret_t nsp_texture_fill_polygon(duk_context *ctx) {
  308. duk_require_object_coercible(ctx, 0);
  309. int* xys = getIntArray(ctx,0);
  310. int nbPts = duk_require_int(ctx, 1);
  311. uint16_t color = duk_require_int(ctx, 2);
  312. duk_push_this(ctx);
  313. duk_get_prop_string(ctx, -1, "width");
  314. int w = duk_require_int(ctx, -1);
  315. duk_pop(ctx);
  316. duk_get_prop_string(ctx, -1, "height");
  317. int h = duk_require_int(ctx, -1);
  318. duk_pop(ctx);
  319. if (w <= 0 || h <= 0) {
  320. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  321. duk_throw(ctx);
  322. }
  323. duk_get_prop_string(ctx, -1, "bitmap");
  324. size_t size;
  325. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  326. if (bitmap == NULL) {
  327. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  328. duk_throw(ctx);
  329. }
  330. FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
  331. fb->width = w;
  332. fb->height = h;
  333. fb->fb_size = w * h;
  334. fb->fb = bitmap;
  335. fillPolygon( xys, nbPts, color, fb );
  336. free(fb);
  337. return 0;
  338. }
  339. duk_ret_t nsp_texture_draw_circle(duk_context *ctx) {
  340. int x = duk_require_int(ctx, 0);
  341. int y = duk_require_int(ctx, 1);
  342. int radius = duk_require_int(ctx, 2);
  343. uint16_t color = duk_require_int(ctx, 3);
  344. duk_push_this(ctx);
  345. duk_get_prop_string(ctx, -1, "width");
  346. int w = duk_require_int(ctx, -1);
  347. duk_pop(ctx);
  348. duk_get_prop_string(ctx, -1, "height");
  349. int h = duk_require_int(ctx, -1);
  350. duk_pop(ctx);
  351. if (w <= 0 || h <= 0) {
  352. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  353. duk_throw(ctx);
  354. }
  355. duk_get_prop_string(ctx, -1, "bitmap");
  356. size_t size;
  357. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  358. if (bitmap == NULL) {
  359. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  360. duk_throw(ctx);
  361. }
  362. FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
  363. fb->width = w;
  364. fb->height = h;
  365. fb->fb_size = w * h;
  366. fb->fb = bitmap;
  367. drawCircle(x,y,radius,color,fb);
  368. free(fb);
  369. return 0;
  370. }
  371. duk_ret_t nsp_texture_fill_circle(duk_context *ctx) {
  372. int x = duk_require_int(ctx, 0);
  373. int y = duk_require_int(ctx, 1);
  374. int radius = duk_require_int(ctx, 2);
  375. uint16_t color = duk_require_int(ctx, 3);
  376. duk_push_this(ctx);
  377. duk_get_prop_string(ctx, -1, "width");
  378. int w = duk_require_int(ctx, -1);
  379. duk_pop(ctx);
  380. duk_get_prop_string(ctx, -1, "height");
  381. int h = duk_require_int(ctx, -1);
  382. duk_pop(ctx);
  383. if (w <= 0 || h <= 0) {
  384. duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
  385. duk_throw(ctx);
  386. }
  387. duk_get_prop_string(ctx, -1, "bitmap");
  388. size_t size;
  389. uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
  390. if (bitmap == NULL) {
  391. duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
  392. duk_throw(ctx);
  393. }
  394. FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
  395. fb->width = w;
  396. fb->height = h;
  397. fb->fb_size = w * h;
  398. fb->fb = bitmap;
  399. fillCircle(x,y,radius,color,fb);
  400. free(fb);
  401. return 0;
  402. }
  403. // ======/Xtase drawing routines/======
  404. static const duk_function_list_entry nsp_texture_methods[] = {
  405. {"display", nsp_texture_display, 0},
  406. {"fill", nsp_texture_fill, 1},
  407. {"getPx", nsp_texture_get_pixel, 2},
  408. {"setPx", nsp_texture_set_pixel, 3},
  409. // Xtase drawing routines
  410. {"drawLine", nsp_texture_draw_line, 5},
  411. {"drawRect", nsp_texture_draw_rect, 5},
  412. {"fillRect", nsp_texture_fill_rect, 5},
  413. {"drawPolyLines", nsp_texture_draw_polyLines, 3},
  414. {"fillPolygon", nsp_texture_fill_polygon, 3},
  415. {"drawCircle", nsp_texture_draw_circle, 4},
  416. {"fillCircle", nsp_texture_fill_circle, 4},
  417. {NULL, NULL, 0}
  418. };
  419. duk_ret_t dukopen_nsp_texture(duk_context *ctx) {
  420. duk_idx_t idx = duk_push_object(ctx);
  421. duk_idx_t stats_constr = duk_push_c_function(ctx, nsp_texture_constructor, DUK_VARARGS);
  422. duk_idx_t stats_prot = duk_push_object(ctx);
  423. duk_put_function_list(ctx, stats_prot, nsp_texture_methods);
  424. duk_put_prop_string(ctx, stats_constr, "prototype");
  425. duk_put_prop_string(ctx, idx, "Texture");
  426. return 1;
  427. }