-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathDraw.c
More file actions
16013 lines (15358 loc) · 537 KB
/
Draw.c
File metadata and controls
16013 lines (15358 loc) · 537 KB
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************************************************************
PicoMite MMBasic
Draw.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
/**
* @file Draw.c
* @author Geoff Graham, Peter Mather
* Thanks to ksinger for the ideas behind the SPRITE(B function
* @brief Source for Graphics MMBasic commands and functions
*/
/*
* @cond
* The following section will be excluded from the documentation.
*/
#include <stdarg.h>
#include <math.h>
#include "MMBasic_Includes.h"
#include "Hardware_Includes.h"
#include "hardware/spi.h"
#include "Memory.h"
#ifndef PICOMITEWEB
#include "pico/multicore.h"
extern mutex_t frameBufferMutex;
#endif
// Hidden-line helpers are only compiled for rp2350 builds.
#if defined(rp2350) && !defined(PICOMITEWEB)
static bool is_hiddenline_target(void)
{
if (WriteBuf == NULL)
return false;
if (WriteBuf == FrameBuf || WriteBuf == LayerBuf
#ifdef PICOMITEVGA
|| WriteBuf == DisplayBuf || WriteBuf == SecondFrame
#ifdef rp2350
|| WriteBuf == SecondLayer
#endif
#endif
)
return true;
return false;
}
static void hiddenline_raster_triangle(short x0, short y0, FLOAT3D iz0,
short x1, short y1, FLOAT3D iz1,
short x2, short y2, FLOAT3D iz2,
int minx, int miny, int maxx, int maxy,
FLOAT3D *zbuf, int bw)
{
int tri_minx = x0 < x1 ? x0 : x1;
tri_minx = tri_minx < x2 ? tri_minx : x2;
int tri_maxx = x0 > x1 ? x0 : x1;
tri_maxx = tri_maxx > x2 ? tri_maxx : x2;
int tri_miny = y0 < y1 ? y0 : y1;
tri_miny = tri_miny < y2 ? tri_miny : y2;
int tri_maxy = y0 > y1 ? y0 : y1;
tri_maxy = tri_maxy > y2 ? tri_maxy : y2;
int bbminx = tri_minx > minx ? tri_minx : minx;
int bbmaxx = tri_maxx < maxx ? tri_maxx : maxx;
int bbminy = tri_miny > miny ? tri_miny : miny;
int bbmaxy = tri_maxy < maxy ? tri_maxy : maxy;
int den_i = (y1 - y2) * (x0 - x2) + (x2 - x1) * (y0 - y2);
if (den_i == 0)
return;
FLOAT3D inv_den = 1.0 / (FLOAT3D)den_i;
FLOAT3D c0 = iz0 * inv_den;
FLOAT3D c1 = iz1 * inv_den;
FLOAT3D c2 = iz2 * inv_den;
// Edge-function coefficients: e(x,y) = A*x + B*y + C
int A0 = y1 - y2, B0 = x2 - x1, C0 = x1 * y2 - x2 * y1;
int A1 = y2 - y0, B1 = x0 - x2, C1 = x2 * y0 - x0 * y2;
int A2 = y0 - y1, B2 = x1 - x0, C2 = x0 * y1 - x1 * y0;
int sign = den_i > 0 ? 1 : -1;
for (int y = bbminy; y <= bbmaxy; y++)
{
int e0 = A0 * bbminx + B0 * y + C0;
int e1 = A1 * bbminx + B1 * y + C1;
int e2 = A2 * bbminx + B2 * y + C2;
for (int x = bbminx; x <= bbmaxx; x++)
{
if (e0 * sign >= 0 && e1 * sign >= 0 && e2 * sign >= 0)
{
FLOAT3D iz = ((FLOAT3D)e0 * c0) + ((FLOAT3D)e1 * c1) + ((FLOAT3D)e2 * c2);
int idx = (y - miny) * bw + (x - minx);
if (iz > zbuf[idx])
zbuf[idx] = iz;
}
e0 += A0;
e1 += A1;
e2 += A2;
}
}
}
static void hiddenline_draw_edge(short x0, short y0, FLOAT3D iz0,
short x1, short y1, FLOAT3D iz1,
int c,
int minx, int miny, int maxx, int maxy,
FLOAT3D *zbuf, int bw)
{
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int steps = dx > dy ? dx : dy;
if (steps == 0)
{
if (x0 >= minx && x0 <= maxx && y0 >= miny && y0 <= maxy)
{
int idx = (y0 - miny) * bw + (x0 - minx);
if (iz0 >= zbuf[idx] - 0.0005)
DrawPixel(x0, y0, c);
}
return;
}
FLOAT3D izstep = (iz1 - iz0) / (FLOAT3D)steps;
int sx = (x0 < x1) ? 1 : -1;
int sy = (y0 < y1) ? 1 : -1;
int err = dx - dy;
int x = x0;
int y = y0;
FLOAT3D izf = iz0;
for (int i = 0; i <= steps; i++)
{
if (x >= minx && x <= maxx && y >= miny && y <= maxy)
{
int idx = (y - miny) * bw + (x - minx);
if (izf >= zbuf[idx] - 0.0005)
DrawPixel(x, y, c);
}
int e2 = err << 1;
if (e2 > -dy)
{
err -= dy;
x += sx;
}
if (e2 < dx)
{
err += dx;
y += sy;
}
izf += izstep;
}
}
static FLOAT3D *hiddenline_zbuf_cache = NULL;
static int hiddenline_zbuf_capacity = 0;
static FLOAT3D *hiddenline_get_zbuf(int count)
{
if (count <= 0)
return NULL;
if (count > hiddenline_zbuf_capacity)
{
if (hiddenline_zbuf_cache != NULL)
FreeMemory((unsigned char *)hiddenline_zbuf_cache);
hiddenline_zbuf_cache = (FLOAT3D *)GetMemory(count * sizeof(FLOAT3D));
hiddenline_zbuf_capacity = count;
}
return hiddenline_zbuf_cache;
}
static void hiddenline_release_zbuf(void)
{
if (hiddenline_zbuf_cache != NULL)
{
FreeMemory((unsigned char *)hiddenline_zbuf_cache);
hiddenline_zbuf_cache = NULL;
hiddenline_zbuf_capacity = 0;
}
}
#endif
#ifdef PICOMITEWEB
#include "pico/cyw43_arch.h"
#endif
#if PICOMITERP2350
#include "VGA222.h"
#endif
#define LONG long
#define max(x, y) (((x) > (y)) ? (x) : (y))
#define min(x, y) (((x) < (y)) ? (x) : (y))
// Helper macros for strided array access (used for struct member arrays)
#define STRIDE_FLOAT(ptr, idx, stride) (*(MMFLOAT *)((char *)(ptr) + (idx) * (stride)))
#define STRIDE_INT(ptr, idx, stride) (*(long long int *)((char *)(ptr) + (idx) * (stride)))
// Maximum number of vertices for polygon fill operations
#define MAX_POLYGON_VERTICES 256
// Magic number to indicate sprite position is not in use
#define SPRITE_POS_INACTIVE 10000
// Magic number to indicate buffer is a triangle buffer (not rectangular)
#define TRIANGLE_BUFFER_MARKER 9999
void DrawFilledCircle(int x, int y, int radius, int r, int fill, int ints_per_line, uint32_t *br, MMFLOAT aspect, MMFLOAT aspect2);
void SaveTriangle(int bnbr, char *buff);
void RestoreTriangle(int bnbr, char *buff);
void ReadLine(int x1, int y1, int x2, int y2, char *buff);
void cmd_RestoreTriangle(unsigned char *p);
void polygon(unsigned char *p, int close);
void DrawCircleRingLineByLine(int x, int y, int r1, int r2, int c, MMFLOAT aspect, MMFLOAT aspect2);
typedef struct _BMPDECODER
{
LONG lWidth;
LONG lHeight;
LONG lImageOffset;
WORD wPaletteEntries;
BYTE bBitsPerPixel;
BYTE bHeaderType;
BYTE blBmMarkerFlag : 1;
BYTE blCompressionType : 3;
BYTE bNumOfPlanes : 3;
BYTE b16bit565flag : 1;
BYTE aPalette[256][3]; /* Each palette entry has RGB */
} BMPDECODER;
/***************************************************************************/
// define the fonts
#include "font1.h"
#include "Misc_12x20_LE.h"
#include "Hom_16x24_LE.h"
#include "Fnt_10x16.h"
#include "Inconsola.h"
#include "ArialNumFontPlus.h"
#include "Font_8x6.h"
#include "arial_bold.h"
#ifdef PICOMITEVGA
#ifndef HDMI
#include "Include.h"
#endif
#endif
#include "smallfont.h"
#include "font-8x10.h"
unsigned char *FontTable[FONT_TABLE_SIZE] = {(unsigned char *)font1,
(unsigned char *)Misc_12x20_LE,
#ifdef PICOMITEVGA
#ifdef HDMI
(unsigned char *)Hom_16x24_LE,
#else
(unsigned char *)arial_bold,
#endif
#else
(unsigned char *)Hom_16x24_LE,
#endif
(unsigned char *)Fnt_10x16,
(unsigned char *)Inconsola,
(unsigned char *)ArialNumFontPlus,
(unsigned char *)F_6x8_LE,
(unsigned char *)TinyFont,
(unsigned char *)font8x10,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL};
/***************************************************************************/
// the default function for DrawRectangle() and DrawBitmap()
short gui_font;
int gui_fcolour;
int gui_bcolour;
volatile short low_x = silly_low, high_y = silly_high, low_y = silly_low, high_x = silly_high;
int PrintPixelMode = 0;
short CurrentX = 0, CurrentY = 0; // the current default position for the next char to be written
short DisplayHRes, DisplayVRes; // the physical characteristics of the display
// Sprite buffer management - dynamically allocated in chunks of 3 sprites per 256-byte page
struct spritebuffer *spritebuff[MAXBLITBUF + 1] = {NULL};
// Chunk tracking: each chunk holds SPRITES_PER_CHUNK sprites
// We need (MAXBLITBUF + SPRITES_PER_CHUNK) / SPRITES_PER_CHUNK chunks max
#define SPRITE_CHUNK_COUNT ((MAXBLITBUF + SPRITES_PER_CHUNK) / SPRITES_PER_CHUNK)
static struct spritebuffer *sprite_chunks[SPRITE_CHUNK_COUNT] = {NULL};
static uint8_t sprite_chunk_used[SPRITE_CHUNK_COUNT] = {0}; // Count of sprites used in each chunk
struct blitbuffer blitbuff[MAXBLITBUF + 1] = {0};
char CMM1 = 0;
// the MMBasic programming characteristics of the display
// note that HRes == 0 is an indication that a display is not configured
short HRes = 0, VRes = 0;
short lastx, lasty;
const int CMM1map[16] = {BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, WHITE, MYRTLE, COBALT, MIDGREEN, CERULEAN, RUST, FUCHSIA, BROWN, LILAC};
int RGB121map[16];
// pointers to the drawing primitives
#ifndef PICOMITEWEB
struct D3D *struct3d[MAX3D + 1] = {NULL};
s_camera camera[MAXCAM + 1];
#endif
int layer_in_use[MAXLAYER + 1];
unsigned char LIFO[MAXBLITBUF];
unsigned char zeroLIFO[MAXBLITBUF];
uint8_t LIFOpointer = 0;
uint8_t zeroLIFOpointer = 0;
uint8_t sprites_in_use = 0;
char *COLLISIONInterrupt = NULL;
bool CollisionFound = false;
int sprite_which_collided = -1;
static bool hideall = 0;
uint8_t sprite_transparent = 0;
// Static object collision detection
struct stobject stobjects[MAXSTOBJECTS + 1] = {0};
char *STCollisionInterrupt = NULL;
bool STCollisionFound = false;
int sprite_hit_st = -1;
int st_which_collided = -1;
#ifdef PICOMITEVGA
#ifndef HDMI
uint32_t remap[256];
#else
uint32_t remap555[256];
uint32_t remap332[256];
uint16_t remap256[256];
#endif
short gui_font_width, gui_font_height;
int last_bcolour, last_fcolour;
volatile int CursorTimer = 0; // used to time the flashing cursor
extern volatile int QVgaScanLine;
bool mergedread = 0;
int ScreenSize = 0;
#else
extern int SSD1963data;
int map[16] = {0};
#ifdef PICOMITEWEB
#ifndef rp2350
short gui_font_width, gui_font_height;
int last_bcolour, last_fcolour;
volatile int CursorTimer = 0; // used to time the flashing cursor
int display_backlight; // the brightness of the backlight (1 to 100)
#else
#endif
extern int InvokingCtrl;
#else
extern int InvokingCtrl;
bool mergerunning = false;
volatile bool mergedone = false;
uint32_t mergetimer = 0;
#endif
#endif
void cmd_ReadTriangle(unsigned char *p);
void (*DrawRectangle)(int x1, int y1, int x2, int y2, int c) = (void (*)(int, int, int, int, int))DisplayNotSet;
void (*DrawBitmap)(int x1, int y1, int width, int height, int scale, int fc, int bc, unsigned char *bitmap) = (void (*)(int, int, int, int, int, int, int, unsigned char *))DisplayNotSet;
void (*ScrollLCD)(int lines) = (void (*)(int))DisplayNotSet;
void (*DrawBuffer)(int x1, int y1, int x2, int y2, unsigned char *c) = (void (*)(int, int, int, int, unsigned char *))DisplayNotSet;
void (*ReadBuffer)(int x1, int y1, int x2, int y2, unsigned char *c) = (void (*)(int, int, int, int, unsigned char *))DisplayNotSet;
void (*DrawBLITBuffer)(int x1, int y1, int x2, int y2, unsigned char *c) = (void (*)(int, int, int, int, unsigned char *))DisplayNotSet;
void (*ReadBLITBuffer)(int x1, int y1, int x2, int y2, unsigned char *c) = (void (*)(int, int, int, int, unsigned char *))DisplayNotSet;
void (*DrawBufferFast)(int x1, int y1, int x2, int y2, int blank, unsigned char *c) = (void (*)(int, int, int, int, int, unsigned char *))DisplayNotSet;
void (*ReadBufferFast)(int x1, int y1, int x2, int y2, unsigned char *c) = (void (*)(int, int, int, int, unsigned char *))DisplayNotSet;
void (*DrawPixel)(int x1, int y1, int c) = (void (*)(int, int, int))DisplayNotSet;
void DrawTriangle(int x0, int y0, int x1, int y1, int x2, int y2, int c, int fill);
// these are the GUI commands that are common to the MX170 and MX470 versions
// in the case of the MX170 this function is called directly by MMBasic when the GUI command is used
// in the case of the MX470 it is called by MX470GUI in GUI.c
const int colours[16] = {0x00, 0xFF, 0x4000, 0x40ff, 0x8000, 0x80ff, 0xff00, 0xffff, 0xff0000, 0xff00FF, 0xff4000, 0xff40ff, 0xff8000, 0xff80ff, 0xffff00, 0xffffff};
void MIPS16 initFonts(void)
{
FontTable[0] = (unsigned char *)font1;
FontTable[1] = (unsigned char *)Misc_12x20_LE;
#ifdef PICOMITEVGA
#ifdef HDMI
FontTable[2] = (unsigned char *)Hom_16x24_LE;
#else
FontTable[2] = (unsigned char *)arial_bold;
#endif
#else
FontTable[2] = (unsigned char *)Hom_16x24_LE;
#endif
FontTable[3] = (unsigned char *)Fnt_10x16;
FontTable[4] = (unsigned char *)Inconsola;
FontTable[5] = (unsigned char *)ArialNumFontPlus;
FontTable[6] = (unsigned char *)F_6x8_LE;
FontTable[7] = (unsigned char *)TinyFont;
FontTable[8] = (unsigned char *)font8x10;
FontTable[9] = NULL;
FontTable[10] = NULL;
FontTable[11] = NULL;
FontTable[12] = NULL;
FontTable[13] = NULL;
FontTable[14] = NULL;
FontTable[15] = NULL;
}
#if PICOMITERP2350
uint16_t __not_in_flash_func(RGB565)(uint32_t c)
{
return ((c >> 16) & 0b11111000) | ((c >> 13) & 0b00000111) | ((c << 3) & 0b1110000000000000) | ((c << 5) & 0b0001111100000000);
}
#endif
uint16_t __not_in_flash_func(RGB555)(uint32_t c)
{
return ((c & 0xf8) >> 3) | ((c & 0xf800) >> 6) | ((c & 0xf80000) >> 9);
}
uint8_t __not_in_flash_func(RGB332)(uint32_t c)
{
return (c >> 16 & 0xE0) | (c >> 11 & 0x1C) | (c >> 6 & 0x03);
}
/* @endcond */
void MIPS16 cmd_guiMX170(void)
{
unsigned char *p;
CheckDisplay(); // display a bitmap stored in an integer or string
if ((p = checkstring(cmdline, (unsigned char *)"BITMAP")))
{
int x, y, fc, bc, h, w, scale, t, bytes;
unsigned char *s;
MMFLOAT f;
long long int i64;
getcsargs(&p, 15);
if (!(argc & 1) || argc < 5)
StandardError(2);
// set the defaults
h = 8;
w = 8;
scale = 1;
bytes = 8;
fc = gui_fcolour;
bc = gui_bcolour;
x = getinteger(argv[0]);
y = getinteger(argv[2]);
// get the type of argument 3 (the bitmap) and its value (integer or string)
t = T_NOTYPE;
evaluate(argv[4], &f, &i64, &s, &t, true);
if (t & T_NBR)
SyntaxError();
else if (t & T_INT)
s = (unsigned char *)&i64;
else if (t & T_STR)
bytes = *s++;
if (argc > 5 && *argv[6])
w = getint(argv[6], 1, HRes);
if (argc > 7 && *argv[8])
h = getint(argv[8], 1, VRes);
if (argc > 9 && *argv[10])
scale = getint(argv[10], 1, 15);
if (argc > 11 && *argv[12])
fc = getint(argv[12], 0, WHITE);
if (argc == 15)
bc = getint(argv[14], -1, WHITE);
if (h * w > bytes * 8)
error("Not enough data");
DrawBitmap(x, y, w, h, scale, fc, bc, (unsigned char *)s);
if (Option.Refresh)
Display_Refresh();
return;
}
#ifndef PICOMITEVGA
#ifdef GUICONTROLS
if ((p = checkstring(cmdline, (unsigned char *)"BEEP")))
{
if (Option.TOUCH_Click == 0)
error("Click option not set");
ClickTimer = getint(p, 0, INT_MAX) + 1;
return;
}
#endif
if ((p = checkstring(cmdline, (unsigned char *)"RESET")))
{
if ((checkstring(p, (unsigned char *)"LCDPANEL")))
{
#ifdef PICOMITE
if (mergerunning)
{
multicore_fifo_push_blocking(0xFF);
busy_wait_ms(mergetimer + 200);
if (mergerunning)
{
SoftReset(SOFT_RESET);
}
}
#endif
InitDisplaySPI(true);
InitDisplayI2C(true);
if ((Option.TOUCH_CS || Option.TOUCH_IRQ) && !Option.TOUCH_CAP)
{
GetTouchValue(CMD_PENIRQ_ON); // send the controller the command to turn on PenIRQ
GetTouchAxis(CMD_MEASURE_X);
}
return;
}
}
if ((p = checkstring(cmdline, (unsigned char *)"CALIBRATE")))
{
int tlx, tly, trx, try, blx, bly, brx, bry, midy;
char *s;
if (Option.TOUCH_CS == 0 && Option.TOUCH_IRQ == 0)
error("Touch not configured");
if (*p && *p != '\'')
{ // if the calibration is provided on the command line
getcsargs(&p, 9);
if (argc != 9)
StandardError(2);
Option.TOUCH_SWAPXY = getinteger(argv[0]);
Option.TOUCH_XZERO = getinteger(argv[2]);
Option.TOUCH_YZERO = getinteger(argv[4]);
Option.TOUCH_XSCALE = getinteger(argv[6]) / 10000.0;
Option.TOUCH_YSCALE = getinteger(argv[8]) / 10000.0;
if (!CurrentLinePtr)
SaveOptions();
return;
}
else
{
if (CurrentLinePtr)
StandardError(10);
Option.TOUCH_SWAPXY = 0;
Option.TOUCH_XZERO = 0;
Option.TOUCH_YZERO = 0;
Option.TOUCH_XSCALE = 1.0f;
Option.TOUCH_YSCALE = 1.0f;
}
calibrate = 1;
GetCalibration(TARGET_OFFSET, TARGET_OFFSET, &tlx, &tly);
GetCalibration(HRes - TARGET_OFFSET, TARGET_OFFSET, &trx, &try);
if (abs(trx - tlx) < CAL_ERROR_MARGIN && abs(tly - try) < CAL_ERROR_MARGIN)
{
calibrate = 0;
error("Touch hardware failure %,%,%,%", tlx, trx, tly, try);
}
GetCalibration(TARGET_OFFSET, VRes - TARGET_OFFSET, &blx, &bly);
GetCalibration(HRes - TARGET_OFFSET, VRes - TARGET_OFFSET, &brx, &bry);
calibrate = 0;
midy = max(max(tly, try), max(bly, bry)) / 2;
Option.TOUCH_SWAPXY = ((tly < midy && try > midy) || (tly > midy && try < midy));
if (Option.TOUCH_SWAPXY)
{
swap(tlx, tly);
swap(trx, try);
swap(blx, bly);
swap(brx, bry);
}
Option.TOUCH_XSCALE = (MMFLOAT)(HRes - TARGET_OFFSET * 2) / (MMFLOAT)(trx - tlx);
Option.TOUCH_YSCALE = (MMFLOAT)(VRes - TARGET_OFFSET * 2) / (MMFLOAT)(bly - tly);
Option.TOUCH_XZERO = ((MMFLOAT)tlx - ((MMFLOAT)TARGET_OFFSET / Option.TOUCH_XSCALE));
Option.TOUCH_YZERO = ((MMFLOAT)tly - ((MMFLOAT)TARGET_OFFSET / Option.TOUCH_YSCALE));
SaveOptions();
brx = (HRes - TARGET_OFFSET) - ((brx - Option.TOUCH_XZERO) * Option.TOUCH_XSCALE);
bry = (VRes - TARGET_OFFSET) - ((bry - Option.TOUCH_YZERO) * Option.TOUCH_YSCALE);
if (abs(brx) > CAL_ERROR_MARGIN || abs(bry) > CAL_ERROR_MARGIN)
{
s = "Warning: Inaccurate calibration\r\n";
}
else
s = "Done. No errors\r\n";
CurrentX = CurrentY = 0;
MMPrintString(s);
strcpy((char *)inpbuf, "Deviation X = ");
IntToStr((char *)inpbuf + strlen((char *)inpbuf), brx, 10);
strcat((char *)inpbuf, ", Y = ");
IntToStr((char *)inpbuf + strlen((char *)inpbuf), bry, 10);
strcat((char *)inpbuf, " (pixels)\r\n");
MMPrintString((char *)inpbuf);
if (!Option.DISPLAY_CONSOLE)
{
GUIPrintString(0, 0, 0x11, JUSTIFY_LEFT, JUSTIFY_TOP, ORIENT_NORMAL, WHITE, BLACK, s);
GUIPrintString(0, 36, 0x11, JUSTIFY_LEFT, JUSTIFY_TOP, ORIENT_NORMAL, WHITE, BLACK, (char *)inpbuf);
}
return;
}
#endif
if ((p = checkstring(cmdline, (unsigned char *)"TEST")))
{
if ((checkstring(p, (unsigned char *)"LCDPANEL")))
{
int t, count = 0;
uint64_t start = time_us_64();
t = ((HRes > VRes) ? HRes : VRes) / 7;
while (getConsole() < '\r')
{
routinechecks();
#ifdef PICOMITEWEB
{
if (startupcomplete)
ProcessWeb(1);
}
#endif
DrawCircle(rand() % HRes, rand() % VRes, (rand() % t) + t / 5, 1, 1, rgb((rand() % 8) * 256 / 8, (rand() % 8) * 256 / 8, (rand() % 8) * 256 / 8), 1);
count++;
#ifdef PICOMITEVGA
#ifdef HDMI
while (v_scanline != 0)
{
}
#else
while (QVgaScanLine != 0)
{
}
#endif
#endif
}
ClearScreen(gui_bcolour);
PFlt(((MMFLOAT)count) / (((MMFLOAT)(time_us_64() - start)) / 1000000.0));
MMPrintString(" Circles per Second");
return;
}
#ifndef PICOMITEVGA
if ((checkstring(p, (unsigned char *)"TOUCH")))
{
int x, y;
ClearScreen(gui_bcolour);
while (getConsole() < '\r')
{
x = GetTouch(GET_X_AXIS);
y = GetTouch(GET_Y_AXIS);
if (x != TOUCH_ERROR && y != TOUCH_ERROR)
DrawBox(x - 1, y - 1, x + 1, y + 1, 0, WHITE, WHITE);
}
ClearScreen(gui_bcolour);
return;
}
#endif
}
StandardError(36);
}
/*
* @cond
* The following section will be excluded from the documentation.
*/
static inline void getargaddress(unsigned char *p, long long int **ip, MMFLOAT **fp, int *n, int *stride)
{
unsigned char *ptr = NULL;
*fp = NULL;
*ip = NULL;
if (stride)
*stride = sizeof(MMFLOAT); // Default stride for normal arrays (8 bytes)
char pp[STRINGSIZE] = {0};
strcpy(pp, (char *)p);
if (!isnamestart(pp[0]))
{ // found a literal
*n = 1;
return;
}
ptr = findvar((unsigned char *)pp, V_FIND | V_EMPTY_OK | V_NOFIND_NULL);
if (ptr && g_vartbl[g_VarIndex].type & (T_NBR | T_INT))
{
if (g_vartbl[g_VarIndex].dims[0] <= 0)
{ // simple variable
*n = 1;
return;
}
else
{ // array or array element
if (*n == 0)
*n = g_vartbl[g_VarIndex].dims[0] + 1 - g_OptionBase;
else
*n = (g_vartbl[g_VarIndex].dims[0] + 1 - g_OptionBase) < *n ? (g_vartbl[g_VarIndex].dims[0] + 1 - g_OptionBase) : *n;
skipspace(p);
do
{
p++;
} while (isnamechar(*p));
if (*p == '!' || *p == '%')
p++;
if (*p == '(')
{
p++;
skipspace(p);
if (*p != ')')
{ // array element
*n = 1;
return;
}
}
}
if (g_vartbl[g_VarIndex].dims[1] != 0)
StandardError(6);
if (g_vartbl[g_VarIndex].type & T_NBR)
*fp = (MMFLOAT *)ptr;
else
*ip = (long long int *)ptr;
}
#ifdef STRUCTENABLED
// Check if this is a struct member access (g_StructMemberType set by findvar)
else if (ptr && (g_vartbl[g_VarIndex].type & T_STRUCT) && g_StructMemberType != 0)
{
// Caller must handle stride for struct member arrays
if (stride == NULL)
StandardError(47);
// Check if this is an array element access (e.g., boxes(i%).x) vs whole array (boxes().x)
// We need to check if there's an index expression in the parentheses
unsigned char *pcheck = p;
skipspace(pcheck);
// Skip past variable name
while (isnamechar(*pcheck))
pcheck++;
if (*pcheck == '!' || *pcheck == '%' || *pcheck == '$')
pcheck++;
skipspace(pcheck);
if (*pcheck == '(')
{
pcheck++;
skipspace(pcheck);
if (*pcheck != ')')
{
// There's an index expression - this is a single element access
*n = 1;
return;
}
}
// This is a struct array with member access like points().x
int struct_type = (int)g_vartbl[g_VarIndex].size;
int struct_size = g_structtbl[struct_type]->total_size;
// Get member type from g_StructMemberType (set by findvar/ResolveStructMember)
int member_type = g_StructMemberType;
// Check member type is numeric
if (!(member_type & (T_NBR | T_INT)))
{
*n = 1; // Not a numeric member, treat as single value
return;
}
// Calculate number of elements from array dimensions
if (*n == 0)
*n = g_vartbl[g_VarIndex].dims[0] + 1 - g_OptionBase;
else
*n = (g_vartbl[g_VarIndex].dims[0] + 1 - g_OptionBase) < *n ? (g_vartbl[g_VarIndex].dims[0] + 1 - g_OptionBase) : *n;
// Check for 2D arrays (not supported)
if (g_vartbl[g_VarIndex].dims[1] != 0)
StandardError(6);
// Set stride to structure size
*stride = struct_size;
// Set the appropriate pointer based on member type
if (member_type & T_NBR)
*fp = (MMFLOAT *)ptr;
else
*ip = (long long int *)ptr;
}
#endif
else
{
*n = 1; // may be a function call
}
}
/****************************************************************************************************
General purpose drawing routines
****************************************************************************************************/
int rgb(int r, int g, int b)
{
return RGB(r, g, b);
}
void getcoord(char *p, int *x, int *y)
{
unsigned char *tp, *ttp;
char b[STRINGSIZE];
char savechar;
tp = getclosebracket((unsigned char *)p);
savechar = *tp;
*tp = 0; // remove the closing brackets
strcpy(b, p); // copy the coordinates to the temp buffer
*tp = savechar; // put back the closing bracket
ttp = (unsigned char *)b + 1;
// kludge (todo: fix this)
{
getcsargs(&ttp, 3); // this is a macro and must be the first executable stmt in a block
if (argc != 3)
SyntaxError();
*x = getinteger(argv[0]);
*y = getinteger(argv[2]);
}
}
int getColour(char *c, int minus)
{
int colour;
if (CMM1)
{
colour = getint((unsigned char *)c, (minus ? -1 : 0), 15);
if (colour >= 0)
colour = CMM1map[colour];
}
else
colour = getint((unsigned char *)c, (minus ? -1 : 0), 0xFFFFFFF);
return colour;
}
#ifndef PICOMITEVGA
void DrawPixelNormal(int x, int y, int c)
{
DrawRectangle(x, y, x, y, c);
}
#endif
void ClearScreen(int c)
{
if (!Option.DISPLAY_TYPE)
return;
#if PICOMITERP2350
if (ScrollLCD == ScrollLCDMEM332)
{
if (Option.DISPLAY_TYPE >= SSD1963_5_12BUFF)
{
ScrollStart = 0;
}
else
{
multicore_fifo_push_blocking(7);
multicore_fifo_push_blocking((uint32_t)0);
ScrollStart = 0;
}
}
#endif
#ifdef PICOMITEVGA
if (DISPLAY_TYPE == SCREENMODE1 && WriteBuf == DisplayBuf)
{
DrawRectangle(0, 0, HRes - 1, VRes - 1, 0);
#ifdef HDMI
memset((void *)WriteBuf, 0, ScreenSize);
if (FullColour)
{
uint16_t bcolour = RGB555(c);
for (int x = 0; x < X_TILE; x++)
{
for (int y = 0; y < Y_TILE; y++)
{
tilefcols[y * X_TILE + x] = RGB555(gui_fcolour);
tilebcols[y * X_TILE + x] = bcolour;
}
}
}
else
{
uint8_t bcolour = RGB332(c);
for (int x = 0; x < X_TILE; x++)
{
for (int y = 0; y < Y_TILE; y++)
{
tilefcols_w[y * X_TILE + x] = RGB332(gui_fcolour);
tilebcols_w[y * X_TILE + x] = bcolour;
}
}
}
CurrentX = CurrentY = 0;
#else
memset((void *)WriteBuf, 0, ScreenSize);
for (int x = 0; x < X_TILE; x++)
{
for (int y = 0; y < Y_TILE; y++)
{
tilefcols[y * X_TILE + x] = RGB121pack(gui_fcolour);
tilebcols[y * X_TILE + x] = RGB121pack(c);
}
}
#endif
}
else
DrawRectangle(0, 0, HRes - 1, VRes - 1, c);
#else
DrawRectangle(0, 0, HRes - 1, VRes - 1, c);
#endif
}
void DrawBuffered(int xti, int yti, int c, int complete)
{
static unsigned char pos = 0;
static unsigned char movex, movey, movec;
static short xtilast[8];
static short ytilast[8];
static int clast[8];
xtilast[pos] = xti;
ytilast[pos] = yti;
clast[pos] = c;
if (complete == 1)
{
if (pos == 1)
{
DrawPixel(xtilast[0], ytilast[0], clast[0]);
}
else
{
DrawLine(xtilast[0], ytilast[0], xtilast[pos - 1], ytilast[pos - 1], 1, clast[0]);
}
pos = 0;
}
else
{
if (pos == 0)
{
movex = movey = movec = 1;
pos += 1;
}
else
{
if (xti == xtilast[0] && abs(yti - ytilast[pos - 1]) == 1)
movex = 0;
else
movex = 1;
if (yti == ytilast[0] && abs(xti - xtilast[pos - 1]) == 1)
movey = 0;
else
movey = 1;
if (c == clast[0])
movec = 0;
else
movec = 1;
if (movec == 0 && (movex == 0 || movey == 0) && pos < 6)
pos += 1;
else
{
if (pos == 1)
{
DrawPixel(xtilast[0], ytilast[0], clast[0]);
}
else
{
DrawLine(xtilast[0], ytilast[0], xtilast[pos - 1], ytilast[pos - 1], 1, clast[0]);
}
movex = movey = movec = 1;
xtilast[0] = xti;
ytilast[0] = yti;
clast[0] = c;
pos = 1;
}
}
}
}
/**************************************************************************************************
Draw a line on a the video output
x1, y1 - the start coordinate
x2, y2 - the end coordinate
w - the width of the line (ignored for diagional lines)
c - the colour to use
***************************************************************************************************/
#define abs(a) (((a) > 0) ? (a) : -(a))
int SizeLine(int x1, int y1, int x2, int y2)
{
int n = 0;
if (y1 == y2)
{
return abs(x1 - x2) + 1;
}
if (x1 == x2)
{
return abs(y1 - y2) + 1;
}
int dx, dy, sx, sy, err, e2;
dx = abs(x2 - x1);
sx = x1 < x2 ? 1 : -1;
dy = -abs(y2 - y1);
sy = y1 < y2 ? 1 : -1;
err = dx + dy;
while (1)