From 66b06d6c40489a307f70dcc205d12e0869c6e886 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 30 Oct 2024 18:17:16 +0100 Subject: [PATCH] added gfx2.safe_vertical_line, gfx2.safe_rect, gfx2.safe_fillrect for completeness --- compiler/res/prog8lib/cx16/gfx2.p8 | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/compiler/res/prog8lib/cx16/gfx2.p8 b/compiler/res/prog8lib/cx16/gfx2.p8 index bb567566c..6cf739afc 100644 --- a/compiler/res/prog8lib/cx16/gfx2.p8 +++ b/compiler/res/prog8lib/cx16/gfx2.p8 @@ -128,6 +128,18 @@ gfx2 { vertical_line(xx+rwidth-1, yy+1, rheight-2, color) } + sub safe_rect(uword xx, uword yy, uword rwidth, uword rheight, ubyte color) { + ; does bounds checking and clipping + safe_horizontal_line(xx, yy, rwidth, color) + if rheight==1 + return + safe_horizontal_line(xx, yy+rheight-1, rwidth, color) + safe_vertical_line(xx, yy+1, rheight-2, color) + if rwidth==1 + return + safe_vertical_line(xx+rwidth-1, yy+1, rheight-2, color) + } + sub fillrect(uword xx, uword yy, uword rwidth, uword rheight, ubyte color) { ; Draw a filled rectangle of the given size and color. ; To fill the whole screen, use clear_screen(color) instead - it is much faster. @@ -139,6 +151,34 @@ gfx2 { } } + sub safe_fillrect(uword xx, uword yy, uword rwidth, uword rheight, ubyte color) { + ; Draw a filled rectangle of the given size and color. + ; To fill the whole screen, use clear_screen(color) instead - it is much faster. + ; This safe version does bounds checking and clipping. + if xx>=width or yy>=width + return + if msb(xx)&$80!=0 { + rwidth += xx + xx = 0 + } + if xx>=width + return + if xx+rwidth>width + rwidth = width-xx + if rwidth>width + return + + if yy + rheight > height + rheight = height-yy + if rheight>height + return + + repeat rheight { + horizontal_line(xx, yy, rwidth, color) + yy++ + } + } + sub horizontal_line(uword xx, uword yy, uword length, ubyte color) { if length==0 return @@ -285,6 +325,24 @@ gfx2 { } + sub safe_vertical_line(uword xx, uword yy, uword lheight, ubyte color) { + ; does bounds checking and clipping + if msb(yy)&$80!=0 or yy>=height + return + if msb(xx)&$80!=0 or xx>=width + return + if msb(yy)&$80!=0 { + lheight += yy + yy = 0 + } + if yy+lheight>height + lheight = height-yy + if lheight>height + return + + vertical_line(xx, yy, lheight, color) + } + sub line(uword @zp x1, uword @zp y1, uword @zp x2, uword @zp y2, ubyte color) { ; Bresenham algorithm. ; This code special-cases various quadrant loops to allow simple ++ and -- operations.