Code: Select all
/' -- clip rect namespace - 2024 Feb 9 - by dafhi
a namespaced clip rect for 2d graphics
updates
demo and comments
renames
project: low level 2d clipper -> clip rect
set -> setrc
custom_blit -> custom_draw
'/
type imvars '' helper
declare sub get_info( byref p as any ptr = 0 )
as long w
as long h
as long pitch,rate
as long bypp,bpp
as any ptr pixels, im
as string driver_name
end type
sub imvars.get_info( byref p as any ptr )
if p = 0 then
ScreenInfo w,h, bpp, bypp, pitch, rate, driver_name
pixels = screenptr
else
ImageInfo p, w, h, bypp, pitch, pixels
endif
im = p
end sub
#define min( a, b) iif( (a)<(b), (a), (b) )
#define max( a, b) iif( (a)>(b), (a), (b) )
namespace clp_rec
'' internals
dim as imvars buf ' primary hard limit (w,h)
dim as short x0, x1 ' secondary hard limit
dim as short y0, y1
type rect field = 2 ' alignment
dim as short x0, x1
dim as short y0, y1
end type
dim as rect rc ' helper
/' = example =
clp_rec.acquire_buf 0 '' app window (0) or fb image
clp_rec.set_clp 5,5, 50, 40 '' (optional)
sub custom_blit( x as short, y as short, w as short, h as short )
using clp_rec '' helper
setrc rc, x, y, w, h ''
for y as long = rc.y0 to rc.y1
for x as long = rc.x0 to rc.x1
..
'/
sub setrc( byref r as rect, x as short, y as short, w as short, h as short )
r.x0 = max( x0, x )
r.y0 = max( y0, y )
r.x1 = min( x1, x+w-1 )
r.y1 = min( y1, y+h-1 )
end sub
sub set_clp( _
xx as short, yy as short, _
ww as short = 100, hh as short = 100 )
x0 = max(0,xx)
y0 = max(0,yy)
x1 = min(xx + ww-1, buf.w-1)
y1 = min(yy + hh-1, buf.h-1)
setrc rc, xx, yy, ww, hh
end sub
sub acquire_buf( p as any ptr )
buf.get_info p
if buf.h > 0 then set_clp 0,0, buf.w,buf.h
end sub
end namespace ' --- clp_rec
' -- Main
'
sub custom_draw( _
x as short = 0, y as short = 0, _
ww as short = 100, hh as short = 100, _color as ulong = -1 )
using clp_rec ' helper
setrc rc, x, y, ww, hh '
for y as long = rc.y0 to rc.y1
for x as long = rc.x0 to rc.x1
pset (x,y), _color
next
next
end sub
const w = 800
const h = 600
screenres w, h, 32
clp_rec.acquire_buf 0 '' app window (0) or fb image
clp_rec.set_clp 20, 20, 200, 200
for i as long = 1 to 1500
var size = 12
var x = (20-size) + (200+size)*rnd
var y = (20-size) + (200+size)*rnd
custom_draw x,y, size, size, rnd * culng(-1)
next
sleep