123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- // Texture module
- // Copyright (C) 2015 Legimet + fgalliat (Xtase)
- //
- // This file is part of Duktape-nspire.
- //
- // Duktape-nspire is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Lesser General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // Duktape-nspire is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public License
- // along with Duktape-nspire. If not, see <http://www.gnu.org/licenses/>.
- #include "duktape.h"
- #include <limits.h>
- #include <stdbool.h>
- #include <libndls.h>
- // =================================================
- #include "framebuffer.h"
- // =================================================
- static duk_ret_t nsp_texture_constructor(duk_context *ctx) {
- int width = duk_require_int(ctx, 0);
- int height = duk_require_int(ctx, 1);
- if (width < 1 || height < 1) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "Width and height must be positive");
- duk_throw(ctx);
- }
- bool has_transparency;
- uint16_t transparent_color;
- if ((has_transparency = duk_is_number(ctx, 2))) {
- transparent_color = (uint16_t)duk_get_int(ctx, 2);
- }
- duk_push_this(ctx);
- duk_push_fixed_buffer(ctx, width * height * 2);
- duk_put_prop_string(ctx, -2, "bitmap");
- duk_push_int(ctx, width);
- duk_put_prop_string(ctx, -2, "width");
- duk_push_int(ctx, height);
- duk_put_prop_string(ctx, -2, "height");
- if (has_transparency) {
- duk_push_int(ctx, transparent_color);
- } else {
- duk_push_null(ctx);
- }
- duk_put_prop_string(ctx, -2, "transparentColor");
- return 0;
- }
- duk_ret_t nsp_texture_display(duk_context *ctx) {
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int width = duk_get_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int height = duk_get_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "transparentColor");
- if (width != 320 || height != 240 || !duk_is_null(ctx, -1)) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "must have dimensions 230x240 without transparency");
- duk_throw(ctx);
- }
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap;
- bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL || size != 320 * 240 * 2) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap buffer does not match with dimensions");
- duk_throw(ctx);
- }
- memcpy(SCREEN_BASE_ADDRESS, bitmap, 320 * 240 * 2);
- return 0;
- }
- duk_ret_t nsp_texture_fill(duk_context *ctx) {
- uint16_t color = duk_require_int(ctx, 0);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- for (size_t i = 0; i < size / 2; i++) {
- bitmap[i] = (uint16_t)color;
- }
- return 0;
- }
- duk_ret_t nsp_texture_get_pixel(duk_context *ctx) {
- int x = duk_require_int(ctx, 0);
- int y = duk_require_int(ctx, 1);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- if (0 <= x && x < w && 0 <= y && y < h && size >= (size_t)(w * h * 2)) {
- duk_push_int(ctx, bitmap[w * y + x]);
- return 1;
- } else {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "coordinates out of range");
- duk_throw(ctx);
- }
- }
- duk_ret_t nsp_texture_set_pixel(duk_context *ctx) {
- int x = duk_require_int(ctx, 0);
- int y = duk_require_int(ctx, 1);
- uint16_t color = duk_require_int(ctx, 2);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- if (0 <= x && x < w && 0 <= y && y < h && size >= (size_t)(w * h * 2)) {
- bitmap[w * y + x] = (uint16_t)color;
- return 0;
- } else {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "coordinates out of range");
- duk_throw(ctx);
- }
- }
- // ====== Xtase drawing routines ======
- duk_ret_t nsp_texture_draw_line(duk_context *ctx) {
- int x1 = duk_require_int(ctx, 0);
- int y1 = duk_require_int(ctx, 1);
- int x2 = duk_require_int(ctx, 2);
- int y2 = duk_require_int(ctx, 3);
- uint16_t color = duk_require_int(ctx, 4);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
- fb->width = w;
- fb->height = h;
- fb->fb_size = w * h;
- fb->fb = bitmap;
- drawLine(x1,y1,x2,y2,color,fb);
- free(fb);
- return 0;
- }
- duk_ret_t nsp_texture_draw_rect(duk_context *ctx) {
- int x = duk_require_int(ctx, 0);
- int y = duk_require_int(ctx, 1);
- int w_ = duk_require_int(ctx, 2);
- int h_ = duk_require_int(ctx, 3);
- uint16_t color = duk_require_int(ctx, 4);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
- fb->width = w;
- fb->height = h;
- fb->fb_size = w * h;
- fb->fb = bitmap;
- drawRect(x,y,w_,h_,color,fb);
- free(fb);
- return 0;
- }
- duk_ret_t nsp_texture_fill_rect(duk_context *ctx) {
- int x = duk_require_int(ctx, 0);
- int y = duk_require_int(ctx, 1);
- int w_ = duk_require_int(ctx, 2);
- int h_ = duk_require_int(ctx, 3);
- uint16_t color = duk_require_int(ctx, 4);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
- fb->width = w;
- fb->height = h;
- fb->fb_size = w * h;
- fb->fb = bitmap;
- fillRect(x,y,w_,h_,color,fb);
- free(fb);
- return 0;
- }
- int* getIntArray(duk_context *ctx, int stackIndex) {
- static int* result = NULL;
- if(duk_is_array(ctx, stackIndex)) {
- int resultLen = duk_get_length(ctx, stackIndex);
- result = (int*)malloc( resultLen * sizeof(int) );
- memset(result,0,resultLen);
- duk_enum(ctx, stackIndex, DUK_ENUM_ARRAY_INDICES_ONLY);
- // NOT stackIndex because waits enumIndex that is -1
- int idx=0;
- while (duk_next(ctx, -1, 1)) {
- // in JS/duktape toto[1] <=> toto["1"]
- // that's why keys are strings
- const char* k = duk_to_string(ctx, -2);
- int v = duk_to_int(ctx, -1);
- //printf("key=%s, value=%d\n", k, v);
- result[idx++] = v;
- duk_pop_2(ctx);
- }
- duk_pop(ctx); // duk_enum
- } else {
- printf("Found NO array\n");
- }
- return result;
- }
- duk_ret_t nsp_texture_draw_polyLines(duk_context *ctx) {
- duk_require_object_coercible(ctx, 0);
- int* xys = getIntArray(ctx,0);
- int nbPts = duk_require_int(ctx, 1);
- uint16_t color = duk_require_int(ctx, 2);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
- fb->width = w;
- fb->height = h;
- fb->fb_size = w * h;
- fb->fb = bitmap;
- drawPolyLines( xys, nbPts, color, fb );
- free(fb);
- return 0;
- }
- duk_ret_t nsp_texture_fill_polygon(duk_context *ctx) {
- duk_require_object_coercible(ctx, 0);
- int* xys = getIntArray(ctx,0);
- int nbPts = duk_require_int(ctx, 1);
- uint16_t color = duk_require_int(ctx, 2);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
- fb->width = w;
- fb->height = h;
- fb->fb_size = w * h;
- fb->fb = bitmap;
- fillPolygon( xys, nbPts, color, fb );
- free(fb);
- return 0;
- }
- duk_ret_t nsp_texture_draw_circle(duk_context *ctx) {
- int x = duk_require_int(ctx, 0);
- int y = duk_require_int(ctx, 1);
- int radius = duk_require_int(ctx, 2);
- uint16_t color = duk_require_int(ctx, 3);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
- fb->width = w;
- fb->height = h;
- fb->fb_size = w * h;
- fb->fb = bitmap;
- drawCircle(x,y,radius,color,fb);
- free(fb);
- return 0;
- }
- duk_ret_t nsp_texture_fill_circle(duk_context *ctx) {
- int x = duk_require_int(ctx, 0);
- int y = duk_require_int(ctx, 1);
- int radius = duk_require_int(ctx, 2);
- uint16_t color = duk_require_int(ctx, 3);
- duk_push_this(ctx);
- duk_get_prop_string(ctx, -1, "width");
- int w = duk_require_int(ctx, -1);
- duk_pop(ctx);
- duk_get_prop_string(ctx, -1, "height");
- int h = duk_require_int(ctx, -1);
- duk_pop(ctx);
- if (w <= 0 || h <= 0) {
- duk_push_error_object(ctx, DUK_ERR_RANGE_ERROR, "width and height must be positive");
- duk_throw(ctx);
- }
- duk_get_prop_string(ctx, -1, "bitmap");
- size_t size;
- uint16_t *bitmap = duk_get_buffer(ctx, -1, &size);
- if (bitmap == NULL) {
- duk_push_error_object(ctx, DUK_ERR_ERROR, "bitmap pointer is NULL");
- duk_throw(ctx);
- }
- FbDev* fb = (FbDev*)malloc( 1 * sizeof(FbDev) );
- fb->width = w;
- fb->height = h;
- fb->fb_size = w * h;
- fb->fb = bitmap;
- fillCircle(x,y,radius,color,fb);
- free(fb);
- return 0;
- }
- // ======/Xtase drawing routines/======
- static const duk_function_list_entry nsp_texture_methods[] = {
- {"display", nsp_texture_display, 0},
- {"fill", nsp_texture_fill, 1},
- {"getPx", nsp_texture_get_pixel, 2},
- {"setPx", nsp_texture_set_pixel, 3},
- // Xtase drawing routines
- {"drawLine", nsp_texture_draw_line, 5},
- {"drawRect", nsp_texture_draw_rect, 5},
- {"fillRect", nsp_texture_fill_rect, 5},
- {"drawPolyLines", nsp_texture_draw_polyLines, 3},
- {"fillPolygon", nsp_texture_fill_polygon, 3},
- {"drawCircle", nsp_texture_draw_circle, 4},
- {"fillCircle", nsp_texture_fill_circle, 4},
- {NULL, NULL, 0}
- };
- duk_ret_t dukopen_nsp_texture(duk_context *ctx) {
- duk_idx_t idx = duk_push_object(ctx);
- duk_idx_t stats_constr = duk_push_c_function(ctx, nsp_texture_constructor, DUK_VARARGS);
- duk_idx_t stats_prot = duk_push_object(ctx);
- duk_put_function_list(ctx, stats_prot, nsp_texture_methods);
- duk_put_prop_string(ctx, stats_constr, "prototype");
- duk_put_prop_string(ctx, idx, "Texture");
- return 1;
- }
|