forked from intel/zephyr.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzjs_gfx.c
721 lines (646 loc) · 24.7 KB
/
zjs_gfx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
// Copyright (c) 2017-2018, Intel Corporation.
// This graphics library was inspired by the Adafruit GFX library
// https://github.com/adafruit/Adafruit-GFX-Library
// ZJS includes
#include "zjs_buffer.h"
#include "zjs_callbacks.h"
#include "zjs_error.h"
#include "zjs_gfx_font.h"
#include "zjs_util.h"
#define COLORBYTES 2 // Number of bytes needed to represent the color
#define MAXPIXELS 1000 * COLORBYTES
typedef struct gfx_handle {
u16_t screenW;
u16_t screenH;
jerry_value_t pixels;
zjs_buffer_t *pixelsPtr;
// Touched pixels values will hold a rectangle containing the buffer to draw
u16_t tpX0; // Touched pixels x start
u16_t tpX1; // Touched pixels x end
u16_t tpY0; // Touched pixels y start
u16_t tpY1; // Touched pixels y end
bool touched;
u8_t fillStyle[COLORBYTES];
jerry_value_t screenInitCB;
jerry_value_t drawDataCB;
jerry_value_t jsThis;
} gfx_handle_t;
typedef struct gfx_data {
int32_t coords[4];
u8_t color[COLORBYTES];
char *text;
jerry_size_t textSize;
u32_t size;
} gfx_data_t;
static jerry_value_t zjs_gfx_prototype;
// Whether we are in draw immediate mode. This mode takes far less RAM, but
// is much slower and shows the lines being drawn in real time vs all at once.
bool drawImmediate = true;
// Draw function depends on whether we are in immediate draw mode or not.
// It gets set by init
void (*zjs_gfx_draw_pixels)(int32_t, int32_t, u32_t, u32_t, u8_t[],
gfx_handle_t *);
static void zjs_gfx_callback_free(void *native)
{
// requires: handle is the native pointer we registered with
// jerry_set_object_native_handle
// effects: frees the gfx module
gfx_handle_t *handle = (gfx_handle_t *)native;
zjs_free(handle);
}
static const jerry_object_native_info_t gfx_type_info = {
.free_cb = zjs_gfx_callback_free
};
// Extracts the args from jerry values and puts them into the data struct
static void args_to_data(gfx_data_t *data, u32_t argc, const jerry_value_t argv[])
{
// Init the struct
for (u8_t i = 0; i < 4; i++)
data->coords[i] = 0;
data->size = 1;
data->textSize = 0;
if (argc < 7) {
for (u8_t i = 0; i < argc; i++) {
if (jerry_value_is_number(argv[i])) {
if (i < 4)
data->coords[i] = (int32_t)jerry_get_number_value(argv[i]);
else
data->size = (u32_t)jerry_get_number_value(argv[i]);
} else if (jerry_value_is_string(argv[i])) {
data->text = zjs_alloc_from_jstring(argv[i], &data->textSize);
if (!data->text) {
ERR_PRINT("GFX failed to copy text\n");
}
} else if (jerry_value_is_array(argv[i])) {
for (u8_t j = 0; j < COLORBYTES; j++) {
data->color[j] = (u8_t)jerry_get_number_value(
jerry_get_property_by_index(argv[i], j));
}
}
}
}
}
static void zjs_gfx_reset_touched_pixels(gfx_handle_t *gfxHandle)
{
gfxHandle->tpX0 = 0;
gfxHandle->tpX1 = 0;
gfxHandle->tpY0 = 0;
gfxHandle->tpY1 = 0;
gfxHandle->touched = false;
}
static void zjs_gfx_touch_pixels(int32_t x, int32_t y, u32_t w, u32_t h,
u8_t color[], gfx_handle_t *gfxHandle)
{
// Check that x and y aren't past the screen
if (x >= gfxHandle->screenW || y >= gfxHandle->screenH ||
(int32_t)(x + w) <= 0 || (int32_t)(y + h) <= 0) {
return;
}
if (x < 0) {
if (x + w <= gfxHandle->screenW) {
gfxHandle->tpX0 = 0;
gfxHandle->tpX1 = x + w - 1;
} else {
gfxHandle->tpX0 = 0;
gfxHandle->tpX1 = gfxHandle->screenW;
}
} else if (x < gfxHandle->screenW) {
if (x + w <= gfxHandle->screenW) {
gfxHandle->tpX0 = x;
gfxHandle->tpX1 = x + w - 1;
} else {
gfxHandle->tpX0 = x;
gfxHandle->tpX1 = gfxHandle->screenW;
}
}
if (y < 0) {
if (y + h <= gfxHandle->screenH) {
gfxHandle->tpY0 = 0;
gfxHandle->tpY1 = y + h - 1;
} else {
gfxHandle->tpY0 = 0;
gfxHandle->tpY1 = gfxHandle->screenH;
}
} else if (y < gfxHandle->screenH) {
if (y + h <= gfxHandle->screenH) {
gfxHandle->tpY0 = y;
gfxHandle->tpY1 = y + h - 1;
} else {
gfxHandle->tpY0 = y;
gfxHandle->tpY1 = gfxHandle->screenH;
}
}
if (!drawImmediate) {
// Update the pixel map
u16_t pixelsIndex = (x + gfxHandle->screenW * y) * COLORBYTES;
for (u16_t iY = y; iY < y + h; iY++) {
// Find the pixel's index in the array
pixelsIndex = (x + gfxHandle->screenW * iY) * COLORBYTES;
// Don't write past the memory
if (pixelsIndex > gfxHandle->pixelsPtr->bufsize) {
DBG_PRINT("Pixel index > bufsize!\n");
return;
}
for (u16_t iX = x; iX < x + w; iX++) {
// Each pixel can be several bytes, fill them in accordingly
for (u8_t cbyte = 0; cbyte < COLORBYTES; cbyte++) {
gfxHandle->pixelsPtr->buffer[pixelsIndex + cbyte] =
color[cbyte];
}
pixelsIndex += COLORBYTES;
}
}
}
gfxHandle->touched = true;
}
static jerry_value_t zjs_gfx_call_cb(u32_t x, u32_t y, u32_t w, u32_t h,
jerry_value_t data,
gfx_handle_t *gfxHandle)
{
jerry_value_t args[] = {
jerry_create_number(x),
jerry_create_number(y),
jerry_create_number(w),
jerry_create_number(h),
data
};
jerry_value_t ret = jerry_call_function(gfxHandle->drawDataCB,
gfxHandle->jsThis, args, 5);
if (jerry_value_is_error(ret)) {
ERR_PRINT("JS callback failed with %u..\n", (u32_t)ret);
return ret;
}
return ZJS_UNDEFINED;
}
static jerry_value_t zjs_gfx_flush(gfx_handle_t *gfxHandle)
{
if (!gfxHandle->touched) {
return ZJS_UNDEFINED;
}
u32_t tpW = gfxHandle->tpX1 - gfxHandle->tpX0 + 1;
u32_t tpH = gfxHandle->tpY1 - gfxHandle->tpY0 + 1;
u32_t pixels = tpW * tpH * COLORBYTES;
zjs_buffer_t *recBuf = NULL;
u16_t bufferIndex = 0;
u32_t origIndex = 0;
u8_t passes = 1; // Number of times the data buffer needs to be sent
jerry_value_t ret;
if (pixels > MAXPIXELS) {
u32_t bytesPerRow = tpW * COLORBYTES;
u32_t rowsPerBuf = MAXPIXELS / bytesPerRow;
passes = pixels / (bytesPerRow * rowsPerBuf);
// If passes has a remainder, add a pass
if (pixels % (bytesPerRow * rowsPerBuf) != 0) {
passes++;
}
pixels = bytesPerRow * rowsPerBuf;
}
// If there are a lot of passes, it will be faster to draw the whole buffer
// Not valid for draw immidiate mode
if (passes > 5 && !drawImmediate) {
ret = zjs_gfx_call_cb(0, 0, gfxHandle->screenW, gfxHandle->screenH,
gfxHandle->pixels, gfxHandle);
} else {
ZVAL recBufObj = zjs_buffer_create(pixels, &recBuf);
u32_t xStart = gfxHandle->tpX0;
u32_t yStart = gfxHandle->tpY0;
u32_t currX = xStart;
u32_t currY = yStart;
u32_t currW = 0;
u32_t currH = 0;
u16_t currPass = 0;
for (u16_t j = gfxHandle->tpY0; currPass < passes; j++) {
currH++;
currY++;
xStart = gfxHandle->tpX0;
for (u16_t i = gfxHandle->tpX0;
i <= gfxHandle->tpX1 && currPass < passes; i++) {
origIndex = (i + gfxHandle->screenW * j) * COLORBYTES;
// Fill the pixel
for (u8_t k = 0; k < COLORBYTES; k++) {
if (!drawImmediate) {
// Use cached pixel map
recBuf->buffer[bufferIndex + k] =
gfxHandle->pixelsPtr->buffer[origIndex + k];
} else {
// We are just drawing a solid rectangle, use fillStyle
recBuf->buffer[bufferIndex + k] =
gfxHandle->fillStyle[k];
}
}
bufferIndex += COLORBYTES;
// Width shouldn't be larger than touched pixel width
// This keeps track of widest point of the current buffer
if (currW < tpW) {
currW++;
}
// Send the buffer once its full or we are at the end
if (bufferIndex == recBuf->bufsize ||
currY > gfxHandle->tpY1 + 1) {
// If we didn't fill the last buffer completely, reset the
// size
if (bufferIndex < recBuf->bufsize) {
recBuf->bufsize = bufferIndex;
}
ret = zjs_gfx_call_cb(xStart, yStart, currW, currH,
recBufObj, gfxHandle);
if (jerry_value_is_error(ret)) {
zjs_gfx_reset_touched_pixels(gfxHandle);
return ret;
}
// Reset the buffer to fill from the beginning
bufferIndex = 0;
currW = 0;
currH = 0;
xStart = currX;
yStart = currY;
currPass++;
}
currX++;
}
currX = gfxHandle->tpX0;
}
}
zjs_gfx_reset_touched_pixels(gfxHandle);
return ZJS_UNDEFINED;
}
static void zjs_gfx_fill_rect_priv(int32_t x, int32_t y, u32_t w, u32_t h,
u8_t color[], gfx_handle_t *gfxHandle)
{
zjs_gfx_touch_pixels(x, y, w, h, color, gfxHandle);
for (u8_t cbyte = 0; cbyte < COLORBYTES; cbyte++) {
gfxHandle->fillStyle[cbyte] = color[cbyte];
}
zjs_gfx_flush(gfxHandle);
}
// Draws the rectangles needed to create the given char
static jerry_value_t zjs_gfx_draw_char_priv(u32_t x, u32_t y, char c,
u8_t color[], u32_t size,
gfx_handle_t *gfxHandle)
{
// To save size our font doesn't include the first 32 chars
u32_t asciiIndex = (u8_t)c - 32;
// Check that character is supported
if (asciiIndex < 0 || asciiIndex > 93) {
ERR_PRINT("GFX doesn't support '%c'\n", c);
asciiIndex = 31; // Set char to ?
}
u8_t fontBytes = font_data_descriptors[asciiIndex][0] * 2;
u16_t index = font_data_descriptors[asciiIndex][1];
zjs_buffer_t *charBuf = NULL;
ZVAL charBufObj = zjs_buffer_create(fontBytes, &charBuf);
if (charBuf) {
for (int i = 0; i < fontBytes; i++) {
charBuf->buffer[i] = font_data[index + i];
}
}
for (int i = 0; i < fontBytes; i += 2) {
u16_t line = (((u16_t)charBuf->buffer[i]) << 8) |
charBuf->buffer[i + 1];
for (int j = 0; j < 16; j++) {
if ((line >> j) & 1) {
int recX = x + (i / 2) * size;
int recY = y + j * size;
// Draw each bit
zjs_gfx_draw_pixels(recX, recY, size, size, color, gfxHandle);
}
}
}
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_gfx_flush_js)
{
// This function only does something when not in draw immediate mode
if (!drawImmediate) {
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
return zjs_gfx_flush(handle);
}
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_gfx_fill_rect)
{
// requires: Requires 5 arguments
// arg[0] - x coord of the top left.
// arg[1] - y coord of the top left.
// arg[2] - width.
// arg[3] - height.
// arg[4] - color array.
//
// effects: Draws a filled rectangle on the screen
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_NUMBER, Z_NUMBER, Z_ARRAY);
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
gfx_data_t argData;
args_to_data(&argData, argc, argv);
zjs_gfx_draw_pixels(argData.coords[0], argData.coords[1], argData.coords[2],
argData.coords[3], argData.color, handle);
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_gfx_draw_pixel)
{
// requires: Requires 3 arguments
// arg[0] - x coord of the top left.
// arg[1] - y coord of the top left.
// arg[2] - color array.
//
// effects: Draws a pixel on the screen
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_ARRAY);
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
gfx_data_t argData;
args_to_data(&argData, argc, argv);
zjs_gfx_draw_pixels(argData.coords[0], argData.coords[1], 1, 1,
argData.color, handle);
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_gfx_draw_line)
{
// requires: Requires 5 arguments
// arg[0] - x coord of the start.
// arg[1] - y coord of the start.
// arg[2] - x coord of the end.
// arg[3] - y coord of the end.
// arg[4] - color array.
// arg[5] - optional size of line.
//
// effects: Draws a line on the screen
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_NUMBER, Z_NUMBER, Z_ARRAY,
Z_OPTIONAL Z_NUMBER);
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
gfx_data_t argData;
args_to_data(&argData, argc, argv);
int xLen = argData.coords[2] > argData.coords[0]
? argData.coords[2] - argData.coords[0] + 1
: argData.coords[0] - argData.coords[2] + 1;
int yLen = argData.coords[3] > argData.coords[1]
? argData.coords[3] - argData.coords[1] + 1
: argData.coords[1] - argData.coords[3] + 1;
bool neg = false;
if (xLen <= yLen) {
// We always draw left to right, swap if argData.coords[0] is larger
if (argData.coords[0] > argData.coords[2]) {
int32_t tmp = argData.coords[0];
argData.coords[0] = argData.coords[2];
argData.coords[2] = tmp;
tmp = argData.coords[1];
argData.coords[1] = argData.coords[3];
argData.coords[3] = tmp;
}
// Line is going up
if (argData.coords[3] < argData.coords[1]) {
neg = true;
}
int32_t pos;
if (argData.coords[0] == argData.coords[2] &&
argData.coords[1] > argData.coords[3]) {
pos = argData.coords[3];
} else {
pos = argData.coords[1];
}
u32_t step = yLen / xLen;
u32_t trueStep = (yLen * 100) / xLen;
u32_t stepRemain = trueStep - (step * 100);
u32_t leftoverStep = 0;
u16_t currStep = step;
for (int32_t x = argData.coords[0]; x <= argData.coords[2]; x++) {
if (leftoverStep > 100) {
currStep = step + 1;
leftoverStep -= 100;
} else {
currStep = step;
}
zjs_gfx_draw_pixels(x, pos, argData.size, currStep, argData.color,
handle);
pos = neg == false ? pos + currStep : pos - currStep;
leftoverStep += stepRemain;
}
} else {
// We always draw left to right, swap if argData.coords[1] is larger
if (argData.coords[1] > argData.coords[3]) {
int32_t tmp = argData.coords[0];
argData.coords[0] = argData.coords[2];
argData.coords[2] = tmp;
tmp = argData.coords[1];
argData.coords[1] = argData.coords[3];
argData.coords[3] = tmp;
}
// Line is going up
if (argData.coords[2] < argData.coords[0])
neg = true;
int32_t pos;
if (argData.coords[1] == argData.coords[3] &&
argData.coords[0] > argData.coords[2]) {
pos = argData.coords[2];
} else {
pos = argData.coords[0];
}
u32_t step = xLen / yLen;
u32_t trueStep = (xLen * 100) / yLen;
u32_t stepRemain = trueStep - (step * 100);
u32_t leftoverStep = 0;
u16_t currStep = step;
for (int32_t y = argData.coords[1]; y <= argData.coords[3]; y++) {
if (leftoverStep > 100) {
currStep = step + 1;
leftoverStep -= 100;
} else {
currStep = step;
}
zjs_gfx_draw_pixels(pos, y, currStep, argData.size, argData.color,
handle);
pos = neg == false ? pos + currStep : pos - currStep;
leftoverStep += stepRemain;
}
}
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_gfx_draw_v_line)
{
// requires: Requires 4 arguments
// arg[0] - x coord of the start.
// arg[1] - y coord of the start.
// arg[2] - height of line.
// arg[3] - color array.
// arg[4] - optional size of line.
//
// effects: Draws a vertical line on the screen.
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_NUMBER, Z_ARRAY,
Z_OPTIONAL Z_NUMBER);
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
gfx_data_t argData;
args_to_data(&argData, argc, argv);
zjs_gfx_draw_pixels(argData.coords[0], argData.coords[1], argData.size,
argData.coords[2], argData.color, handle);
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_gfx_draw_h_line)
{
// requires: Requires 4 arguments
// arg[0] - x coord of the start.
// arg[1] - y coord of the start.
// arg[2] - width of line.
// arg[3] - color array.
// arg[4] - optional size of line.
//
// effects: Draws a horizontal line on the screen
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_NUMBER, Z_ARRAY,
Z_OPTIONAL Z_NUMBER);
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
gfx_data_t argData;
args_to_data(&argData, argc, argv);
zjs_gfx_draw_pixels(argData.coords[0], argData.coords[1], argData.coords[2],
argData.size, argData.color, handle);
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_gfx_draw_rect)
{
// requires: Requires 5 arguments
// arg[0] - x coord of the top left.
// arg[1] - y coord of the top left.
// arg[2] - width.
// arg[3] - height.
// arg[4] - color array.
// arg[5] - optional size of line.
//
// effects: Draws a rectangle on the screen
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_NUMBER, Z_NUMBER, Z_ARRAY,
Z_OPTIONAL Z_NUMBER);
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
gfx_data_t argData;
args_to_data(&argData, argc, argv);
zjs_gfx_draw_pixels(argData.coords[0], argData.coords[1], argData.coords[2],
argData.size, argData.color, handle);
zjs_gfx_draw_pixels(argData.coords[0],
argData.coords[1] + argData.coords[3] - argData.size,
argData.coords[2], argData.size, argData.color, handle);
zjs_gfx_draw_pixels(argData.coords[0], argData.coords[1], argData.size,
argData.coords[3], argData.color, handle);
zjs_gfx_draw_pixels(argData.coords[0] + argData.coords[2] - argData.size,
argData.coords[1], argData.size, argData.coords[3],
argData.color, handle);
return ZJS_UNDEFINED;
}
static ZJS_DECL_FUNC(zjs_gfx_draw_char)
{
// requires: Requires 4 arguments
// arg[0] - x coord of the top left.
// arg[1] - y coord of the top left.
// arg[2] - character.
// arg[3] - color array.
// arg[4] - optional size of character.
//
// effects: Draws a character on the screen
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_STRING, Z_ARRAY,
Z_OPTIONAL Z_NUMBER);
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
gfx_data_t argData;
args_to_data(&argData, argc, argv);
return zjs_gfx_draw_char_priv(argData.coords[0], argData.coords[1],
argData.text[0], argData.color, argData.size,
handle);
}
static ZJS_DECL_FUNC(zjs_gfx_draw_string)
{
// requires: Requires 4 arguments
// arg[0] - x coord of the top left.
// arg[1] - y coord of the top left.
// arg[2] - text.
// arg[3] - color array.
// arg[4] - optional size of character.
//
// effects: Draws a character on the screen
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_STRING, Z_ARRAY,
Z_OPTIONAL Z_NUMBER);
ZJS_GET_HANDLE(this, gfx_handle_t, handle, gfx_type_info);
gfx_data_t argData;
args_to_data(&argData, argc, argv);
jerry_value_t ret = ZJS_UNDEFINED;
u32_t x = argData.coords[0];
for (u8_t i = 0; i < argData.textSize; i++) {
// To save size our font doesn't include the first 32 chars
u32_t asciiIndex = (u8_t)argData.text[i] - 32;
// Check that character is supported
if (asciiIndex < 0 || asciiIndex > 93) {
ERR_PRINT("GFX doesn't support '%c'\n", argData.text[i]);
asciiIndex = 31; // Set char to ?
}
ret = zjs_gfx_draw_char_priv(x, argData.coords[1], argData.text[i],
argData.color, argData.size, handle);
if (jerry_value_is_error(ret)) {
return ret;
}
// Add a one for space
u8_t charWidth = font_data_descriptors[asciiIndex][0] + 1;
x = x + (charWidth * argData.size);
}
return ret;
}
static ZJS_DECL_FUNC(zjs_gfx_set_cb)
{
// requires: Requires 4 arguments
// arg[0] - The width of the screen.
// arg[1] - The height of the screen.
// arg[2] - The init callback to use.
// arg[3] - The drawRect callback to use.
// arg[4] - optional JS 'this' object
//
// effects: Initializes the GFX module
ZJS_VALIDATE_ARGS(Z_NUMBER, Z_NUMBER, Z_FUNCTION, Z_FUNCTION, Z_BOOL,
Z_OPTIONAL Z_OBJECT);
gfx_handle_t *handle = zjs_malloc(sizeof(gfx_handle_t));
if (!handle) {
return ZJS_ERROR("could not allocate handle\n");
}
handle->screenW = jerry_get_number_value(argv[0]);
handle->screenH = jerry_get_number_value(argv[1]);
handle->screenInitCB = jerry_acquire_value(argv[2]);
handle->drawDataCB = jerry_acquire_value(argv[3]);
drawImmediate = jerry_get_boolean_value(argv[4]);
if (argc > 5)
handle->jsThis = jerry_acquire_value(argv[5]);
else
handle->jsThis = jerry_create_undefined();
handle->pixelsPtr = NULL;
zjs_gfx_reset_touched_pixels(handle);
if (!drawImmediate) {
u32_t totalPixels = handle->screenW * handle->screenH * COLORBYTES;
handle->pixels = zjs_buffer_create(totalPixels, &handle->pixelsPtr);
zjs_gfx_draw_pixels = zjs_gfx_touch_pixels;
} else {
handle->pixels = 0;
handle->pixelsPtr = NULL;
zjs_gfx_draw_pixels = zjs_gfx_fill_rect_priv;
}
jerry_value_t gfx_obj = zjs_create_object();
jerry_set_prototype(gfx_obj, zjs_gfx_prototype);
jerry_set_object_native_pointer(gfx_obj, handle, &gfx_type_info);
jerry_call_function(handle->screenInitCB, handle->jsThis, NULL, 0);
return gfx_obj;
}
static void zjs_gfx_cleanup(void *native)
{
jerry_release_value(zjs_gfx_prototype);
}
static const jerry_object_native_info_t gfx_module_type_info = {
.free_cb = zjs_gfx_cleanup
};
static jerry_value_t zjs_gfx_init()
{
zjs_native_func_t proto[] = {
{ zjs_gfx_fill_rect, "fillRect" },
{ zjs_gfx_draw_pixel, "drawPixel" },
{ zjs_gfx_draw_line, "drawLine" },
{ zjs_gfx_draw_v_line, "drawVLine" },
{ zjs_gfx_draw_h_line, "drawHLine" },
{ zjs_gfx_draw_rect, "drawRect" },
{ zjs_gfx_draw_char, "drawChar" },
{ zjs_gfx_draw_string, "drawString" },
{ zjs_gfx_flush_js, "flush" },
{ NULL, NULL }
};
zjs_gfx_prototype = zjs_create_object();
zjs_obj_add_functions(zjs_gfx_prototype, proto);
jerry_value_t gfx_obj = zjs_create_object();
zjs_obj_add_function(gfx_obj, "init", zjs_gfx_set_cb);
jerry_set_object_native_pointer(gfx_obj, NULL, &gfx_module_type_info);
return gfx_obj;
}
JERRYX_NATIVE_MODULE(gfx, zjs_gfx_init)