forked from SuperIlu/DOjS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfx.c
751 lines (650 loc) · 18 KB
/
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/*
MIT License
Copyright (c) 2019 Andre Seidelt <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <allegro.h>
#include <dirent.h>
#include <dpmi.h>
#include <errno.h>
#include <mujs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include "DOjS.h"
#include "bitmap.h"
#include "color.h"
#include "funcs.h"
#include "gfx.h"
#include "util.h"
/************
** structs **
************/
//! polygon data as needed by the drawing functions.
typedef struct poly_array {
int len; //!< number of entries
int *data; //!< a dynamic array: int[][2]
} poly_array_t;
//! arc coordinates
typedef struct _arc_return {
int16_t startX; //!< start point X
int16_t startY; //!< start point Y
int16_t endX; //!< end point X
int16_t endY; //!< end point Y
int16_t centerX; //!< center point X
int16_t centerY; //!< center point Y
} arc_return_t;
/*********************
** static variables **
*********************/
/**
* @brief info structure of the current arc.
*/
static arc_return_t arcReturn;
/**
* @brief radius of the current custom primitive.
*/
static int customRadius;
/*********************
** static functions **
*********************/
/**
* @brief free a polygon array[][2].
*
* @param array the array to free.
*/
static void f_freeArray(poly_array_t *array) {
if (array) {
free(array->data);
free(array);
}
}
/**
* @brief allocate an array[len][2] for the given number of points.
*
* @param len the number of points.
*
* @return poly_array_t* or NULL if out of memory.
*/
static poly_array_t *f_allocArray(int len) {
poly_array_t *array = malloc(sizeof(poly_array_t));
if (!array) {
return NULL;
}
array->len = len;
array->data = malloc(sizeof(int) * len * 2);
if (!array->data) {
f_freeArray(array);
return NULL;
}
return array;
}
/**
* @brief convert JS-array to C array for polygon functions.
*
* @param J the JS context.
* @param idx index of th JS-array on the stack.
*
* @return poly_array_t* or NULL if out of memory.
*/
static poly_array_t *f_convertArray(js_State *J, int idx) {
if (!js_isarray(J, idx)) {
JS_ENOARR(J);
return NULL;
} else {
int len = js_getlength(J, idx);
poly_array_t *array = f_allocArray(len);
if (!array) {
return NULL;
}
for (int i = 0; i < len; i++) {
js_getindex(J, idx, i);
{
int pointlen = js_getlength(J, -1);
if (pointlen < 2) {
js_error(J, "Points must have two values");
f_freeArray(array);
return NULL;
}
js_getindex(J, -1, 0);
int x = js_toint16(J, -1);
js_pop(J, 1);
js_getindex(J, -1, 1);
int y = js_toint16(J, -1);
js_pop(J, 1);
array->data[i * 2 + 0] = x;
array->data[i * 2 + 1] = y;
}
js_pop(J, 1);
}
return array;
}
}
/**
* @brief get name of screen mode.
* GetScreenMode():string
*
* @param J
*/
static void f_GetScreenMode(js_State *J) { js_pushnumber(J, get_color_depth()); }
/**
* @brief get the width of the drawing area.
* SizeX():number
*
* @param J the JS context.
*/
static void f_SizeX(js_State *J) { js_pushnumber(J, DOjS.current_bm->w); }
/**
* @brief get the height of the drawing area.
* SizeY():number
*
* @param J the JS context.
*/
static void f_SizeY(js_State *J) { js_pushnumber(J, DOjS.current_bm->h); }
/**
* @brief create object with the coordinates of the last arc drawn.
*
* res={"centerX":50,"centerY":50,"endX":71,"endY":29,"startX":80,"startY":50}
*
* @param J the JS context.
*/
static void f_arcReturn(js_State *J, arc_return_t *art) {
js_newobject(J);
{
js_pushnumber(J, art->startX);
js_setproperty(J, -2, "startX");
js_pushnumber(J, art->startY);
js_setproperty(J, -2, "startY");
js_pushnumber(J, art->endX);
js_setproperty(J, -2, "endX");
js_pushnumber(J, art->endY);
js_setproperty(J, -2, "endY");
js_pushnumber(J, art->centerX);
js_setproperty(J, -2, "centerX");
js_pushnumber(J, art->centerY);
js_setproperty(J, -2, "centerY");
}
}
/**
* @brief clear the screen with given color.
* ClearScreen(c:Color)
*
* @param J the JS context.
*/
static void f_ClearScreen(js_State *J) {
int color = js_toint32(J, 1);
clear_to_color(DOjS.current_bm, color);
}
/**
* @brief draw a plot.
*
* Plot(x:number, y:number, c:Color)
*
* @param J the JS context.
*/
static void f_Plot(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
int color = js_toint32(J, 3);
putpixel(DOjS.current_bm, x, y, color);
}
/**
* @brief draw a line.
*
* Line(x1:number, y1:number, x2:number, y2:number, c:Color)
*
* @param J the JS context.
*/
static void f_Line(js_State *J) {
int x1 = js_toint16(J, 1);
int y1 = js_toint16(J, 2);
int x2 = js_toint16(J, 3);
int y2 = js_toint16(J, 4);
int color = js_toint32(J, 5);
line(DOjS.current_bm, x1, y1, x2, y2, color);
}
/**
* @brief custom function to draw 'larger pixels'.
*
* @param bmp destination BITMAP.
* @param x pixel coordinate.
* @param y pixel coordinate.
* @param d color.
*/
static void f_customPixel(BITMAP *bmp, int x, int y, int d) { circlefill(bmp, x, y, customRadius, d); }
/**
* @brief draw a line with variable thinkness.
*
* CustomLine(x1:number, y1:number, x2:number, y2:number, w:number, c:Color)
*
* @param J the JS context.
*/
static void f_CustomLine(js_State *J) {
int x1 = js_toint16(J, 1);
int y1 = js_toint16(J, 2);
int x2 = js_toint16(J, 3);
int y2 = js_toint16(J, 4);
int w = js_toint16(J, 5);
int color = js_toint32(J, 6);
if (w % 2) {
customRadius = w / 2 + 1;
} else {
customRadius = w / 2;
}
do_line(DOjS.current_bm, x1, y1, x2, y2, color, f_customPixel);
}
/**
* @brief draw a box.
*
* Box(x1:number, y1:number, x2:number, y2:number, c:Color)
*
* @param J the JS context.
*/
static void f_Box(js_State *J) {
int x1 = js_toint16(J, 1);
int y1 = js_toint16(J, 2);
int x2 = js_toint16(J, 3);
int y2 = js_toint16(J, 4);
int color = js_toint32(J, 5);
rect(DOjS.current_bm, x1, y1, x2, y2, color);
}
/**
* @brief draw a circle.
*
* Circle(x1:number, y1:number, r:number, c:Color)
*
* @param J the JS context.
*/
static void f_Circle(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
int r = js_toint16(J, 3);
int color = js_toint32(J, 4);
circle(DOjS.current_bm, x, y, r, color);
}
/**
* @brief draw a circle with variable thinkness.
*
* CustomCircle(x1:number, y1:number, r:number, w:number, c:Color)
*
* @param J the JS context.
*/
static void f_CustomCircle(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
int r = js_toint16(J, 3);
int w = js_toint16(J, 4);
int color = js_toint32(J, 5);
if (w % 2) {
customRadius = w / 2 + 1;
} else {
customRadius = w / 2;
}
do_circle(DOjS.current_bm, x, y, r, color, f_customPixel);
}
/**
* @brief draw an ellipse.
*
* Ellipse(xc:number, yc:number, xa:number, ya:number, c:Color)
*
* @param J the JS context.
*/
static void f_Ellipse(js_State *J) {
int xc = js_toint16(J, 1);
int yc = js_toint16(J, 2);
int xa = js_toint16(J, 3);
int ya = js_toint16(J, 4);
int color = js_toint32(J, 5);
ellipse(DOjS.current_bm, xc, yc, xa, ya, color);
}
/**
* @brief draw an ellipse with variable thinkness.
*
* CustomEllipse(xc:number, yc:number, xa:number, ya:number, c:Color)
*
* @param J the JS context.
*/
static void f_CustomEllipse(js_State *J) {
int xc = js_toint16(J, 1);
int yc = js_toint16(J, 2);
int xa = js_toint16(J, 3);
int ya = js_toint16(J, 4);
int w = js_toint16(J, 5);
int color = js_toint32(J, 6);
if (w % 2) {
customRadius = w / 2 + 1;
} else {
customRadius = w / 2;
}
do_ellipse(DOjS.current_bm, xc, yc, xa, ya, color, f_customPixel);
}
/**
* @brief record the coordinates while drawing pixels.
*
* @param bmp destination BITMAP.
* @param x pixel coordinate.
* @param y pixel coordinate.
* @param d color.
*/
static void f_recordingPixel(BITMAP *bmp, int x, int y, int d) {
arcReturn.endX = x;
arcReturn.endY = y;
if (arcReturn.startX == -1) {
arcReturn.startX = x;
arcReturn.startY = y;
}
putpixel(bmp, x, y, d);
}
/**
* @brief record the coordinates while drawing custom pixels.
*
* @param bmp destination BITMAP.
* @param x pixel coordinate.
* @param y pixel coordinate.
* @param d color.
*/
static void f_recordingCustomPixel(BITMAP *bmp, int x, int y, int d) {
arcReturn.endX = x;
arcReturn.endY = y;
if (arcReturn.startX == -1) {
arcReturn.startX = x;
arcReturn.startY = y;
}
circlefill(bmp, x, y, customRadius, d);
}
/**
* @brief Draw a circle arc.
* CircleArc(
* x:number,
* y:number,
* r:number,
* start:number,
* end:number,
* c:Color):{"centerX":XXX,"centerY":XXX,"endX":XXX,"endY":XXX,"startX":XXX,"startY":XXX}
*
* @param J the JS context.
*/
static void f_CircleArc(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
int r = js_toint16(J, 3);
double start = js_tonumber(J, 4);
double end = js_tonumber(J, 5);
int color = js_toint32(J, 6);
arcReturn.startX = arcReturn.startY = -1;
arcReturn.centerX = x;
arcReturn.centerY = y;
do_arc(DOjS.current_bm, x, y, ftofix(start), ftofix(end), r, color, f_recordingPixel);
f_arcReturn(J, &arcReturn);
}
/**
* @brief Draw a circle arc with variable thinkness.
* CustomCircleArc(
* x:number,
* y:number,
* r:number,
* start:number,
* end:number,
* w:number,
* c:Color):{"centerX":XXX,"centerY":XXX,"endX":XXX,"endY":XXX,"startX":XXX,"startY":XXX}
*
* @param J the JS context.
*/
static void f_CustomCircleArc(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
int r = js_toint16(J, 3);
double start = js_tonumber(J, 4);
double end = js_tonumber(J, 5);
int w = js_toint16(J, 6);
int color = js_toint32(J, 7);
if (w % 2) {
customRadius = w / 2 + 1;
} else {
customRadius = w / 2;
}
arcReturn.startX = arcReturn.startY = -1;
arcReturn.centerX = x;
arcReturn.centerY = y;
do_arc(DOjS.current_bm, x, y, ftofix(start), ftofix(end), r, color, f_recordingCustomPixel);
f_arcReturn(J, &arcReturn);
}
/**
* @brief draw a filled box.
*
* FilledBox(x1:number, y1:number, x2:number, y2:number, c:Color)
*
* @param J the JS context.
*/
static void f_FilledBox(js_State *J) {
int x1 = js_toint16(J, 1);
int y1 = js_toint16(J, 2);
int x2 = js_toint16(J, 3);
int y2 = js_toint16(J, 4);
int color = js_toint32(J, 5);
rectfill(DOjS.current_bm, x1, y1, x2, y2, color);
}
/**
* @brief draw a filled circle.
*
* FilledCircle(x1:number, y1:number, r:number, c:Color)
*
* @param J the JS context.
*/
static void f_FilledCircle(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
int r = js_toint16(J, 3);
int color = js_toint32(J, 4);
circlefill(DOjS.current_bm, x, y, r, color);
}
/**
* @brief draw an ellipse.
*
* FilledEllipse(xc:number, yc:number, xa:number, ya:number, c:Color)
*
* @param J the JS context.
*/
static void f_FilledEllipse(js_State *J) {
int xc = js_toint16(J, 1);
int yc = js_toint16(J, 2);
int xa = js_toint16(J, 3);
int ya = js_toint16(J, 4);
int color = js_toint32(J, 5);
ellipsefill(DOjS.current_bm, xc, yc, xa, ya, color);
}
/**
* @brief do a flood fill.
* FloodFill(x:number, y:number, c:Color)
*
* @param J the JS context.
*/
static void f_FloodFill(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
int color = js_toint32(J, 3);
floodfill(DOjS.current_bm, x, y, color);
}
/**
* @brief draw a polyline.
* PolyLine([[x1, x2], [..], [xN, yN]], c:Color)
*
* @param J the JS context.
*/
static void f_FilledPolygon(js_State *J) {
poly_array_t *array = f_convertArray(J, 1);
int color = js_toint32(J, 2);
polygon(DOjS.current_bm, array->len, array->data, color);
f_freeArray(array);
}
/**
* @brief draw a text with the default font.
* TextXY(x:number, y:number, text:string, fg:Color, bg:Color)
*
* @param J the JS context.
*/
static void f_TextXY(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
const char *str = js_tostring(J, 3);
int fg = js_toint32(J, 4);
int bg = js_toint32(J, 5);
textout_ex(DOjS.current_bm, font, (char *)str, x, y, fg, bg);
}
/**
* @brief save current screen to file.
* SaveBmpImage(fname:string)
*
* @param J the JS context.
*/
static void f_SaveBmpImage(js_State *J) {
const char *fname = js_tostring(J, 1);
PALETTE pal;
get_palette(pal);
if (save_bmp(fname, DOjS.current_bm, (const struct RGB *)&pal) != 0) {
js_error(J, "Can't save screen to BMP file '%s': %s", fname, allegro_error);
}
}
/**
* @brief save current screen to file.
* SaveBmpImage(fname:string)
*
* @param J the JS context.
*/
static void f_SavePcxImage(js_State *J) {
const char *fname = js_tostring(J, 1);
PALETTE pal;
get_palette(pal);
if (save_pcx(fname, DOjS.current_bm, (const struct RGB *)&pal) != 0) {
js_error(J, "Can't save screen to PCX file '%s': %s", fname, allegro_error);
}
}
/**
* @brief save current screen to file.
* SaveBmpImage(fname:string)
*
* @param J the JS context.
*/
static void f_SaveTgaImage(js_State *J) {
const char *fname = js_tostring(J, 1);
PALETTE pal;
get_palette(pal);
if (save_tga(fname, DOjS.current_bm, (const struct RGB *)&pal) != 0) {
js_error(J, "Can't save screen to TGA file '%s': %s", fname, allegro_error);
}
}
/**
* @brief get the color of an on-screen pixel.
* GetPixel(x, y):Color
*
* @param J the JS context.
*/
static void f_GetPixel(js_State *J) {
int x = js_toint16(J, 1);
int y = js_toint16(J, 2);
js_pushnumber(J, getpixel(DOjS.current_bm, x, y) | 0xFE000000);
}
/**
* @brief enable/disable transparency.
* TransparencyEnabled(boolean)
*
* @param J the JS context.
*/
static void f_TransparencyEnabled(js_State *J) {
DOjS.transparency_available = js_toboolean(J, 1);
update_transparency();
}
/**
* @brief set the current rendering destination.
* SetRenderBitmap(bm:Bitmap)
*
* @param J the JS context.
*/
static void f_SetRenderBitmap(js_State *J) {
if (js_isundefined(J, 1) || js_isnull(J, 1)) {
DOjS.current_bm = DOjS.render_bm;
DEBUG("Restoring render_bm\n");
} else {
BITMAP *bm = js_touserdata(J, 1, TAG_BITMAP);
DOjS.current_bm = bm;
DEBUGF("Setting 0x%p\n", bm);
}
}
/**
* @brief draw 32bit ARGB array to screen.
* DrawArray(dat:number[], x:number, y:number, width:number, height:number)
*
* @param J the JS context.
*/
static void f_DrawArray(js_State *J) {
int x = js_tonumber(J, 2);
int y = js_tonumber(J, 3);
int w = js_tonumber(J, 4);
int h = js_tonumber(J, 5);
int arr_len = js_getlength(J, 1);
int len = arr_len < w * h ? arr_len : w * h;
for (int i = 0; i < len; i++) {
js_getindex(J, 1, i);
putpixel(DOjS.current_bm, x + (i % w), y + (i / h), js_toint32(J, -1));
js_pop(J, 1);
}
}
/***********************
** exported functions **
***********************/
/**
* @brief initialize grx subsystem.
*
* @param J VM state.
*/
void init_gfx(js_State *J) {
DEBUGF("%s\n", __PRETTY_FUNCTION__);
// define some global properties
js_pushglobal(J);
js_setglobal(J, "global");
// define global functions
FUNCDEF(J, f_SetRenderBitmap, "SetRenderBitmap", 1);
FUNCDEF(J, f_GetScreenMode, "GetScreenMode", 0);
FUNCDEF(J, f_SizeX, "SizeX", 0);
FUNCDEF(J, f_SizeY, "SizeY", 0);
FUNCDEF(J, f_ClearScreen, "ClearScreen", 1);
FUNCDEF(J, f_TextXY, "TextXY", 5);
FUNCDEF(J, f_Plot, "Plot", 3);
FUNCDEF(J, f_Line, "Line", 5);
FUNCDEF(J, f_CustomLine, "CustomLine", 6);
FUNCDEF(J, f_Box, "Box", 5);
FUNCDEF(J, f_Circle, "Circle", 4);
FUNCDEF(J, f_CustomCircle, "CustomCircle", 5);
FUNCDEF(J, f_Ellipse, "Ellipse", 5);
FUNCDEF(J, f_CustomEllipse, "CustomEllipse", 6);
FUNCDEF(J, f_CircleArc, "CircleArc", 7);
FUNCDEF(J, f_CustomCircleArc, "CustomCircleArc", 8);
FUNCDEF(J, f_FilledBox, "FilledBox", 5);
FUNCDEF(J, f_FilledCircle, "FilledCircle", 4);
FUNCDEF(J, f_FilledEllipse, "FilledEllipse", 5);
FUNCDEF(J, f_FilledPolygon, "FilledPolygon", 2);
FUNCDEF(J, f_FloodFill, "FloodFill", 4);
FUNCDEF(J, f_SaveBmpImage, "SaveBmpImage", 1);
FUNCDEF(J, f_SavePcxImage, "SavePcxImage", 1);
FUNCDEF(J, f_SaveTgaImage, "SaveTgaImage", 1);
FUNCDEF(J, f_GetPixel, "GetPixel", 2);
FUNCDEF(J, f_DrawArray, "DrawArray", 5);
FUNCDEF(J, f_TransparencyEnabled, "TransparencyEnabled", 2);
DEBUGF("%s DONE\n", __PRETTY_FUNCTION__);
}