forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.common.spinh
798 lines (712 loc) · 29.1 KB
/
graphics.common.spinh
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
{
---------------------------------------------------------------------------------------------------
Filename: graphics.common.spinh
Description: Generic bitmap-oriented graphics rendering routines
Author: Jesse Burt
Started: May 19, 2019
Updated: Jan 27, 2024
Copyright (c) 2024 - See end of file for terms of use.
---------------------------------------------------------------------------------------------------
}
#include "terminal.common.spinh" ' for printf(), puts(), etc
VAR
{ putchar() function pointer }
long putchar
{ display buffer and dimensions }
long _ptr_drawbuffer
long _buff_sz
long _bytesperln
long _disp_width, _disp_height, _disp_xmax, _disp_ymax
{ current draw color }
long _fgcolor, _bgcolor
{ font parameters }
long _char_attrs
long _font_addr
word _charpx_xmax, _charpx_ymax
word _charpx_x, _charpx_y
byte _fnt_width, _fnt_height, _fnt_rot, _fnt_cmin, _fnt_cmax, _char_hspace, _char_vspace
byte _fnt_xscl, _fnt_yscl
byte _charcell_w, _charcell_h
byte _charcell_xmax, _charcell_ymax
PUB bgcolor(col)
' Set background color for subsequent drawing
_bgcolor := col
#ifndef GFX_DIRECT
PUB bitmap(ptr_bmap, xs, ys, bm_wid, bm_lns) | offs
' Copy a bitmap to the display buffer
' ptr_bmap: pointer to bitmap data
' (xs, ys): upper-left corner of bitmap
' bm_wid: width of bitmap, in pixels
' bm_lns: number of lines in bitmap
repeat
' calc offset from framebuffer and bitmap start,
' then copy the bitmap one line at a time,
' using the display's defined native word size
offs := ((ys++ * _bytesperln) + xs) ' increment Y start
MEMMV_NATIVE(_ptr_drawbuffer+offs <# _ptr_drawbuffer+_buff_sz, {
} ptr_bmap+offs, bm_wid)
while bm_lns-- ' loop if there're more lines
#endif
#ifndef GFX_DIRECT
PUB box(x0, y0, x1, y1, color, filled) | x, y
' Draw a box
' x0, y0: Start coordinates
' x1, y1: End coordinates
' color: box color
' filled: Flag to set whether to fill the box or not
case filled
FALSE:
repeat x from x0 to x1
plot(x, y0, color)
plot(x, y1, color)
repeat y from y0 to y1
plot(x0, y, color)
plot(x1, y, color)
TRUE:
#ifdef 1BPP
repeat y from y0 to y1
repeat x from x0 to x1
plot(x, y, color)
#else
if ( (x0 => 0) and (x0 =< _disp_xmax) and (y0 => 0) and ...
(y0 =< _disp_ymax) and (x1 => 0) and (x1 =< _disp_xmax) and ...
(y1 => 0) and (y1 =< _disp_ymax) )
x := ||(x1-x0)+1
if ( x1 < x0 )
repeat y from y0 to y1
memfill(x1, y, color, x)
else
repeat y from y0 to y1
memfill(x0, y, color, x)
else
return FALSE
#endif
#endif
PUB tx = putchar
PUB char = putchar
PUB putchar_0deg(ch) | p, gl_row, gl_col, last_glcol, last_glrow, char_wid, char_ht
' Low-level character rendering routine
' For font file definitions with these characteristics:
' * 0 degrees rotation (portrait, top-down)
' * each glyph word is a row of the glyph, e.g. for 5x8 'A':
' %00100000
' %01010000
' %10001000
' %10001000
' %11111000
' %10001000
' %10001000
' %00000000
' * bit order can be normal (MSBit in each glyph word is left edge)
' or reversed (MSBit in each glyph word is right edge; build with -DFNT_GLYPH_REV)
' * up to 8px wide
if ( (ch < _fnt_cmin) or (ch > _fnt_cmax) ) ' don't waste any time if the char is invalid
return
p := _font_addr + (ch * _fnt_height)
last_glcol := _fnt_width-1 ' last column of pixels in font
last_glrow := _fnt_height-1 ' definition
char_wid := _charcell_w * _fnt_xscl ' scaled character dimensions
char_ht := _charcell_h * _fnt_yscl '
if ( _char_attrs & TERMINAL )
{ process control characters, don't draw them }
case ch
LF:
{ line feed }
_charpx_y += char_ht ' goto next text row
if ( _charpx_y > _charpx_ymax )
_charpx_y -= char_ht ' scroll the display up a row if we try to go
#ifndef GFX_DIRECT
scroll_up_fs(char_ht) ' past the end
#endif
return
CR:
{ carriage return }
_charpx_x := 0 ' goto first text column
return
{ read a glyph in from the font table }
repeat gl_row from 0 to last_glrow
repeat gl_col from 0 to last_glcol
#ifdef FNT_GLYPH_REV
{ reverse byte-order glyphs (MSBit in each glyph word is right edge) }
if ( byte[p][gl_row] & (1 << gl_col) )
#else
{ normal byte-order glyphs (MSBit in each glyph word is left edge) }
if ( (byte[p][gl_row] >< 8 ) & (1 << gl_col) )
#endif
plot( _charpx_x + gl_col, ...
_charpx_y + gl_row, ...
_fgcolor )
else
{ draw the background color for unset pixels if the DRAWBG attribute is set
( see char_attr() )
otherwise, unset pixels will be ignored (effectively transparent) }
if ( _char_attrs & DRAWBG )
plot( _charpx_x + gl_col, ...
_charpx_y + gl_row, ...
_bgcolor )
_charpx_x += char_wid ' goto next column
#ifndef FNT_POS_NOGRID
if ( _charpx_x > _charpx_xmax )
_charpx_x := 0 ' goto the first column of the next row if we try
_charpx_y += char_ht ' to go past the right edge
if ( _charpx_y > _charpx_ymax )
_charpx_y -= char_ht ' scroll the display up a row if we try to go
# ifndef GFX_DIRECT
scroll_up_fs(char_ht) ' past the end
# endif
#endif
pub putchar32_0deg(ch) | bpr, lastbyte, p, last_glcol, last_glrow, char_wid, char_ht, rowbyte, bit, gl_row, gl_col
' Low-level character rendering routine
' For font file definitions with these characteristics:
' * 0 degrees rotation (portrait, top-down)
' * each glyph word is a row of the glyph, e.g. for 5x8 'A':
' %00100000
' %01010000
' %10001000
' %10001000
' %11111000
' %10001000
' %10001000
' %00000000
' * bit order can be normal (MSBit in each glyph word is left edge)
' or reversed (MSBit in each glyph word is right edge; build with -DFNT_GLYPH_REV)
' * up to 32px wide
if ( (ch < _fnt_cmin) or (ch > _fnt_cmax) ) ' don't waste any time if the char is invalid
return
bpr := _fnt_width/8 ' calc number of bytes per glyph row
lastbyte := bpr-1
p := _font_addr + ( (ch-_fnt_cmin) * (_fnt_height * bpr))
last_glcol := _fnt_width-1 ' last column of pixels in font
last_glrow := _fnt_height-1 ' definition
char_wid := _charcell_w * _fnt_xscl ' scaled character dimensions
char_ht := _charcell_h * _fnt_yscl '
if ( _char_attrs & TERMINAL )
{ process control characters, don't draw them }
case ch
LF:
{ line feed }
_charpx_y += char_ht ' goto next text row
if ( _charpx_y > _charpx_ymax )
_charpx_y -= char_ht ' scroll the display up a row if we try to go
#ifndef GFX_DIRECT
scroll_up_fs(char_ht) ' past the end
#endif
return
CR:
{ carriage return }
_charpx_x := 0 ' goto first text column
return
{ read a glyph in from the font table }
repeat gl_row from 0 to last_glrow ' for each glyph row
gl_col := 0
repeat rowbyte from 0 to lastbyte ' for each byte in the row
repeat bit from 0 to 7
#ifdef FNT_GLYPH_REV
{ reverse byte-order glyphs (MSBit in each byte of glyph data is right edge) }
if ( (byte[p][rowbyte] >< 8) & (1 << bit) )
#else
{ normal byte-order glyphs (MSBit in each byte of glyph data is left edge) }
if ( byte[p][rowbyte] & (1 << bit) )
#endif
plot( _charpx_x + gl_col, ...
_charpx_y + gl_row, ...
_fgcolor )
else
{ draw the background color for unset pixels if the DRAWBG attribute is set
( see char_attr() )
otherwise, unset pixels will be ignored (effectively transparent) }
if ( _char_attrs & DRAWBG )
plot( _charpx_x + gl_col, ...
_charpx_y + gl_row, ...
_bgcolor )
gl_col++
p += bpr ' point to next row of glyph data
_charpx_x += char_wid ' goto next column
#ifndef FNT_POS_NOGRID
if ( _charpx_x > _charpx_xmax )
_charpx_x := 0 ' goto the first column of the next row if we try
_charpx_y += char_ht ' to go past the right edge
if ( _charpx_y > _charpx_ymax )
_charpx_y -= char_ht ' scroll the display up a row if we try to go
# ifndef GFX_DIRECT
scroll_up_fs(char_ht) ' past the end
# endif
#endif
PUB putchar_90deg(ch) | glyph_col, glyph_row, xs, ys, xe, ye, last_glyph_col, last_glyph_row, glyph, char_ht, char_wid
' Low-level character rendering routine
' For font file definitions with these characteristics:
' * 90 degrees rotation (landscape)
' * each glyph word is a column of the glyph, e.g. for 5x8 'A':
' %01111100
' %00010010
' %00010001
' %00010010
' %01111100
' * byte order can be normal (leftmost bytes first)
' or reversed (rightmost bytes first)
if ( (ch < _fnt_cmin) or (ch > _fnt_cmax) ) ' don't waste any time if the char is invalid
return
last_glyph_col := _fnt_width-1 ' last column of pixels in font
last_glyph_row := _fnt_height-1 ' definition
char_wid := _charcell_w * _fnt_xscl ' scaled character dimensions
char_ht := _charcell_h * _fnt_yscl '
if ( _char_attrs & TERMINAL )
{ process control characters, don't draw them }
case ch
LF:
{ line feed }
_charpx_y += char_ht ' goto next text row
if ( _charpx_y > _charpx_ymax )
_charpx_y -= char_ht ' scroll the display up a row if we try to go
#ifndef GFX_DIRECT
scroll_up_fs(char_ht) ' past the end
#endif
return
CR:
{ carriage return }
_charpx_x := 0 ' goto first text column
return
{ read a glyph in from the font table }
ch *= _fnt_width
if ( (_fnt_xscl == 1) and (_fnt_yscl == 1) )
{ no scaling; render font 1:1 }
repeat glyph_col from 0 to last_glyph_col
#ifdef FNT_GLYPH_REV
{ reversed byte order fonts }
xs := _charpx_x + (_fnt_width-glyph_col)
#else
{ normal byte order fonts }
xs := _charpx_x + glyph_col
#endif
glyph := ch + glyph_col ' point to current glyph data
repeat glyph_row from 0 to last_glyph_row
if ( byte[_font_addr][glyph] & (1 << glyph_row) )
plot( xs, ...
_charpx_y + glyph_row, ...
_fgcolor )
else
if ( _char_attrs & DRAWBG )
{ actually draw the currently set background color (see bgcolor() ) }
{ NOTE: This incurs a significant speed penalty if enabled }
plot( xs, ...
_charpx_y + glyph_row, ...
_bgcolor )
else
{ scale font rendering up by an integer factor (x, y independently) }
{ use filled boxes to draw each pixel of the glyph }
repeat glyph_col from 0 to last_glyph_col
#ifdef FNT_GLYPH_REV
{ reversed byte order fonts }
xs := _charpx_x + (_fnt_width*_fnt_xscl)-(glyph_col * _fnt_xscl)
#else
{ normal byte order fonts }
xs := _charpx_x + (glyph_col * _fnt_xscl)
#endif
xe := (xs + _fnt_xscl)
glyph := ch + glyph_col ' point to current glyph data
repeat glyph_row from 0 to last_glyph_row
ys := _charpx_y + (glyph_row * _fnt_yscl)
ye := ys + _fnt_yscl
if ( byte[_font_addr][glyph] & (1 << glyph_row) )
box(xs, ys, xe, ye, _fgcolor, true)
else
if ( _char_attrs & DRAWBG )
box(xs, ys, xe, ye, _bgcolor, true)
_charpx_x += char_wid ' goto next column
#ifndef FNT_POS_NOGRID
if ( _charpx_x > _charpx_xmax )
_charpx_x := 0 ' goto the first column of the next row if we try
_charpx_y += char_ht ' to go past the right edge
if ( _charpx_y > _charpx_ymax )
_charpx_y -= char_ht ' scroll the display up a row if we try to go
# ifndef GFX_DIRECT
scroll_up_fs(char_ht) ' past the end
# endif
#endif
CON
{ character attributes }
DRAWBG = (1 << 0) ' draw background color
TERMINAL= (1 << 1) ' process control characters
PUB charattrs = char_attrs
PUB char_attrs(attrs)
' Set character attributes
_char_attrs := attrs
PUB circle(x0, y0, radius, color, filled) | x, y, err, cdx, cdy, cht
' Draw a circle
' x0, y0: Coordinates
' radius: Circle radius
' color: Color to draw circle
' filled: fill the circle (0: no, nonzero: yes)
case filled
FALSE:
x := radius - 1
y := 0
cdx := 1
cdy := 1
err := cdx - (radius << 1)
repeat while (x => y)
plot(x0 + x, y0 + y, color)
plot(x0 + y, y0 + x, color)
plot(x0 - y, y0 + x, color)
plot(x0 - x, y0 + y, color)
plot(x0 - x, y0 - y, color)
plot(x0 - y, y0 - x, color)
plot(x0 + y, y0 - x, color)
plot(x0 + x, y0 - y, color)
if (err =< 0)
y++
err += cdy
cdy += 2
if (err > 0)
x--
cdx += 2
err += cdx - (radius << 1)
other:
repeat x from -radius to radius
cht := ^^((radius * radius) - (x * x))
repeat y from -cht to cht-1
plot(x0 + x, y0 + y, color)
#ifndef GFX_DIRECT
PUB copy(sx, sy, ex, ey, dx, dy) | x, y, tmp
' Copy rectangular region at (sx, sy, ex, ey) to (dx, dy)
repeat y from sy to ey
repeat x from sx to ex
tmp := point(x, y)
plot((dx + x)-sx, (dy + y)-sy, tmp)
PUB cut(sx, sy, ex, ey, dx, dy) | x, y, tmp
' Copy rectangular region at (sx, sy, ex, ey) to (dx, dy)
' Subsequently clears original region (sx, sy, ex, ey) to background color
repeat y from sy to ey
repeat x from sx to ex
tmp := point(x, y)
plot((dx + x)-sx, (dy + y)-sy, tmp) ' Copy to destination region
plot(x, y, _bgcolor) ' Cut the original region
#endif
PUB fgcolor(col)
' Set foreground color of subsequent drawing operations
_fgcolor := col
PUB fontheight = font_height
PUB font_height{}: curr_ht
' Return the set font height
return _fnt_height
PUB fontscale = font_scl
PUB font_scl(x, y): curr_scl
' Set font rendering scale factor
' Valid values: 1..8
_fnt_xscl := 1 #> x <# 8
_fnt_yscl := 1 #> y <# 8
PUB fontsize = font_sz
PUB font_sz(w, h)
' Set expected dimensions of font glyphs, in pixels
' NOTE: This has a direct impact on the number of text columns and rows
' NOTE: font_spacing() should be called before calling this method
_fnt_width := w
_fnt_height := h
_fnt_xscl := 1 #> _fnt_xscl
_fnt_yscl := 1 #> _fnt_yscl
_charcell_w := (_fnt_width + _char_hspace) ' calc overall character cell
_charcell_h := (_fnt_height + _char_vspace)' dims (font size + spacing)
_charpx_xmax := (_disp_width - _charcell_w) ' calc maximum x, y character
_charpx_ymax := (_disp_height - _charcell_h)' pixel position
_charcell_xmax := (_disp_width / _charcell_w)
_charcell_ymax := (_disp_height / _charcell_h)
PUB fontspacing = font_spacing
PUB font_spacing(h, v)
' Set spacing between character cells (horizontally, vertically)
_char_hspace := h
_char_vspace := v
PUB fontwidth = font_width
PUB font_width{}: curr_wid
' Return the set font width
return _fnt_width
PUB get_address(): addr
' Get the currently set display buffer address
return _ptr_drawbuffer
#ifndef GFX_DIRECT
PUB line(x1, y1, x2, y2, c) | sx, sy, ddx, ddy, err, e2
' Draw line from x1, y1 to x2, y2, in color c
if (x1 == x2) or (y1 == y2)
if (x1 == x2) ' draw vertical line
repeat sy from y1 to y2
plot(x1, sy, c)
if (y1 == y2) ' draw horizontal line
repeat sx from x1 to x2
plot(sx, y1, c)
else ' draw diag. line (Bresenham)
ddx := ||(x2-x1)
ddy := ||(y2-y1)
err := ddx-ddy
sx := -1
if (x1 < x2)
sx := 1
sy := -1
if (y1 < y2)
sy := 1
repeat until ((x1 == x2) AND (y1 == y2))
plot(x1, y1, c)
e2 := err << 1
if e2 > -ddy
err -= ddy
x1 += sx
if e2 < ddx
err += ddx
y1 += sy
#endif
PUB position = pos_xy
PUB pos_xy(x, y)
' Set text draw position, in character-cell col and row
#ifdef FNT_POS_NOGRID
{ if set, position is set freehand - any pixel location on screen }
_charpx_x := x ' leave untouched; plot() will validate whether
_charpx_y := y ' any pixels are offscreen
#else
{ default: position is set in terms of character cell columns and rows }
x := 0 #> x <# text_cols()-1 ' clamp to min/max coordinates
y := 0 #> y <# text_rows()-1
_charpx_x := (x * _charcell_w) * _fnt_xscl ' calc pixel position:
_charpx_y := (y * _charcell_h) * _fnt_yscl ' char cell coords * font dims
#endif
PUB r5g6b5_rgb565(r, g, b): rgb565
' Return 16-bit color in RGB565 format, given red (5), green (6), blue (5) components
return ((r << 11) | (g << 5) | b)
PUB rgb222_rgb6(r, g, b)
' Return 6-bit color from discrete Red, Green, Blue color components
' Valid values:
' r, g, b: 0..3
' NOTE: The least-significant two bits are a requirement of the 6bpp VGA display driver
return ((((r <# 3) #> 0) << 6) | (((g <# 3) #> 0) << 4) | (((b <# 3) #> 0) << 2) | $3)
PUB rgbw8888_rgb32(r, g, b, w): rgb32
' Return 32-bit long from discrete Red, Green, Blue, White color components (values 0..255)
rgb32.byte[3] := r
rgb32.byte[2] := g
rgb32.byte[1] := b
rgb32.byte[0] := w
PUB rgbw8888_rgb32_brightness(r, g, b, w, level): rgb32
' Return 32-bit long from discrete Red, Green, Blue, White color components
' and clamp all color channels to maximum level or brightness
' Valid values:
' r, g, b, w: 0..255
' level: 0..100 (%)
if (level =< 0)
return 0
elseif (level => 255)
return RGBW8888_RGB32(r, g, b, w)
else
r := r * level / 255 ' Apply level to RGBW
g := g * level / 255
b := b * level / 255
w := w * level / 255
return RGBW8888_RGB32(r, g, b, w)
PUB rgb565_r5(rgb565): r5
' Isolate 5-bit red component of 16-bit RGB565 color
return ((rgb565 & $F800) >> 11)
PUB rgb565_g5(rgb565): g5
' Isolate 6-bit green component of 16-bit RGB565 color
return ((rgb565 & $07E0) >> 5)
PUB rgb565_b5(rgb565): b5
' Isolate 5-bit blue component of 16-bit RGB565 color
return (rgb565 & $001F)
PUB rgb565_r8(rgb565): r8
' Isolate red component of 16-bit RGB565 color and return value scaled to 8-bit range
return (((rgb565 & $F800) >> 11) * 527 + 23 ) >> 6
PUB rgb565_g8(rgb565): g8
' Isolate green component of 16-bit RGB565 color and return value scaled to 8-bit range
return (((rgb565 & $7E0) >> 5) * 259 + 33 ) >> 6
PUB rgb565_b8(rgb565): b8
' Isolate blue component of 16-bit RGB565 color and return value scaled to 8-bit range
return ((rgb565 & $1F) * 527 + 23 ) >> 6
#ifndef GFX_DIRECT
PUB scale(sx, sy, ex, ey, offsx, offsy, size) | x, y, dx, dy, in
' Scale a region of the display up by size
repeat y from sy to ey
repeat x from sx to ex
in := point(x, y)
dx := offsx + (x*size)-(sx*size)
dy := offsy + (y*size)-(sy*size)
box(dx, dy, dx + size, dy + size, in, TRUE)
#endif
#ifndef GFX_DIRECT
PUB scrolldown = scroll_down
PUB scroll_down(sx, sy, ex, ey) | scr_width, src, dest, x, y
' Scroll a region of the display down by 1 pixel
scr_width := ex-sx
repeat y from ey-1 to sy
#ifdef 1BPP
' use Copy() for monochrome display types, until a more efficient
' method can be devised
copy(sx, y, ex, y, sx, y+1)
#else
src := sx + (y * _disp_width) * BYTESPERPX
dest := sx + ((y+1) * _disp_width) * BYTESPERPX
MEMMV_NATIVE(_ptr_drawbuffer + dest, _ptr_drawbuffer + src, scr_width)
#endif
#ifndef GFX_DIRECT
PUB scroll_down_fs(px) | max_pg, y, x, fb_off
' Scroll down full screen (1bpp displays only)
' px: number of rows or lines to scroll
#ifdef 1BPP
px := px <# _disp_height ' clamp scroll length to display height
max_pg := (_disp_height / 8) ' get # of pages from set display height
repeat px
repeat y from max_pg to 0 ' bottom-up so it doesn't look strange
fb_off := (y * _disp_width)
repeat x from 0 to _disp_xmax
{ scroll down a page of display memory (lsh), then put the bottom-most pixel of the
previous page into the top-most pixel of this one }
byte[_ptr_drawbuffer][fb_off + x] <<= 1
if ( y > 0 )
{ don't try to copy pixels from before the first page }
byte[_ptr_drawbuffer][fb_off + x] |= (byte[_ptr_drawbuffer] ...
[((y-1) * _disp_width) + x ] >> 7) & 1
#else
repeat px
scroll_down(0, 0, _disp_xmax, _disp_ymax)
#endif
#endif
PUB scrollleft = scroll_left
PUB scroll_left(sx, sy, ex, ey) | scr_width, src, dest, y, yoffs
' Scroll a region of the display left by 1 pixel
scr_width := ex-sx
repeat y from sy to ey
#ifdef 1BPP
copy(sx, y, ex, y, sx-1, y)
#else
yoffs := (y * _disp_width)
src := (sx + yoffs) * BYTESPERPX
dest := ((0 #> (sx-1)) + yoffs) * BYTESPERPX
MEMMV_NATIVE(_ptr_drawbuffer+dest, _ptr_drawbuffer+src, scr_width)
#endif
PUB scrollright = scroll_right
PUB scroll_right(sx, sy, ex, ey) | scr_width, src, dest, y, yoffs
' Scroll a region of the display right by 1 pixel
scr_width := ex-sx
repeat y from sy to ey
#ifdef 1BPP
copy(sx, y, ex, y, sx+1, y)
#else
yoffs := (y * _disp_width)
src := (sx + yoffs) * BYTESPERPX
dest := (((sx+1) <# _disp_width) + yoffs) * BYTESPERPX
MEMMV_NATIVE(_ptr_drawbuffer + dest, _ptr_drawbuffer + src, scr_width)
#endif
PUB scrollup = scroll_up
PUB scroll_up(sx, sy, ex, ey) | scr_width, src, dest, x, y
' Scroll a region of the display up by 1 pixel
scr_width := ex-sx
repeat y from sy+1 to ey
#ifdef 1BPP
copy(sx, y, ex, y, sx, y-1)
#else
src := sx + (y * _disp_width) * BYTESPERPX
dest := sx + ((y-1) * _disp_width) * BYTESPERPX
MEMMV_NATIVE(_ptr_drawbuffer + dest, _ptr_drawbuffer + src, scr_width)
#endif
#endif
#ifndef GFX_DIRECT
PUB scroll_up_fs(px) | max_pg, y, x, fb_off
' Scroll up full screen
' px: number of rows or lines to scroll
#ifdef 1BPP
px := px <# _disp_height ' clamp scroll length to display height
max_pg := (_disp_height / 8) ' get # of pages from set display height
repeat px
repeat y from 0 to max_pg
fb_off := (y * _disp_width)
repeat x from 0 to _disp_xmax
{ scroll up a page of display memory (rsh), then put the top-most pixel of the next
page into the bottom-most pixel of this one }
byte[_ptr_drawbuffer][fb_off + x] >>= 1
if ( y < max_pg )
byte[_ptr_drawbuffer][fb_off + x] |= (byte[_ptr_drawbuffer] ...
[((y+1) * _disp_width) + x] & 1) << 7
#else
repeat px ' scroll up one text row
scrollup(0, 0, _disp_xmax, _disp_ymax)
#endif
#endif
PUB address = set_address
PUB set_address(addr=0)
' Set display buffer address
' addr: address of display buffer to use (or 0 to use the driver's internal buffer)
#ifndef GFX_DIRECT
{ only build this if targetting a buffered display; the framebuffer won't exist otherwise }
if ( addr == 0 )
{ use the buffer built-in to the display driver }
_ptr_drawbuffer := @_framebuffer
else
{ use an external buffer }
_ptr_drawbuffer := addr
#endif
PUB set_dims(w, h)
' Set display dimensions
' w: width of display (X-axis) in pixels
' h: height of display (Y-axis) in pixels
_disp_width := w
_disp_height := h
_disp_xmax := _disp_width - 1
_disp_ymax := _disp_height - 1
{ update the buffer size, and the bytes per line }
_buff_sz := (_disp_width * _disp_height) * BYTESPERPX
_bytesperln := _disp_width * BYTESPERPX
PUB set_font(ptr_fnt, ptr_setup)
' Setup the font and character routines
' ptr_fnt: pointer to the font glyph table (the font bitmaps)
' ptr_setup: pointer to 7-byte structure containing font characteristics:
' byte[ptr_setup][0]: font width
' byte[ptr_setup][1]: font height
' byte[ptr_setup][2]: font rotation
' byte[ptr_setup][3]: font first character
' byte[ptr_setup][4]: font last character
' byte[ptr_setup][5]: font space between chars (horiz.)
' byte[ptr_setup][6]: font space between chars (vert.)
font_addr(ptr_fnt)
bytemove(@_fnt_width, ptr_setup, 7)
{ set up size, spacing }
font_sz( byte[ptr_setup][0], byte[ptr_setup][1] )
{ check rotation }
if ( byte[ptr_setup][2] == 0 )
set_font_rotation_0deg()
elseif ( byte[ptr_setup][2] == 90 )
set_font_rotation_90deg()
PUB fontaddress = font_addr
PUB font_addr = set_font_addr
PUB set_font_addr(addr)
' Set address of font definition
_font_addr := addr
PUB set_font_char_range(cmin, cmax)
' Set range of ASCII values contained in font
_fnt_cmin := cmin
_fnt_cmax := cmax
PUB set_font_rotation_0deg()
' Set putchar function to 0-degree rotated type
putchar := @putchar_0deg
PUB set_font_rotation_90deg()
' Set putchar function to 90-degree rotated type
putchar := @putchar_90deg
PUB set_putchar(ptr)
' Set a custom putchar function
putchar := ptr
PUB textcols = text_cols
PUB text_cols{}: cols
' Returns number of displayable text columns, based on set display width, font width, and scale
return _charcell_xmax / _fnt_xscl
PUB textrows = text_rows
PUB text_rows{}: rows
' Returns number of displayable text rows, based on set display height, font height, and scale
return _charcell_ymax / _fnt_yscl
DAT
{
Copyright 2024 Jesse Burt
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.
}