Code: Select all
/' -- line rasterizer 2024 July 14 by dafhi
update: renamed mydraw -> myline
'/
' =======================================================================
sub _gfx_release( byref im as any ptr )
if imageinfo(im) = 0 then imagedestroy im
im = 0
end sub
'' image class
''
type imvars
dim as long w,h, bypp, pitch,rate
dim as any ptr pixels, im
dim as string driver_name
declare sub get_info( as any ptr = 0 )
declare destructor
end type
sub _get_screen( byref i as imvars )
_gfx_release i.im
ScreenInfo i.w, i.h, , i.bypp, i.pitch, i.rate, i.driver_name
i.pixels = screenptr
end sub
sub _get_image( byref i as imvars, im as any ptr )
if im<>i.im then _gfx_release i.im '' 2024 June 3
ImageInfo im, i.w, i.h, i.bypp, i.pitch, i.pixels
i.im = im
end sub
sub imvars.get_info( im as any ptr )
if im = 0 then _get_screen this: exit sub
_get_image this, im
end sub
destructor imvars
_gfx_release im
end destructor
' =======================================================================
dim shared as ulong ptr p32
dim shared as long wm, hm, pitchBy
#define sng as single '' reduced text
#define min( a, b) iif( (a)<(b), (a), (b) )
#define max( a, b) iif( (a)>(b), (a), (b) )
#macro sw( a, b, tmp )
tmp = a: a = b: b = tmp
#endmacro
' =======================================================================
namespace line2d '' rasterizer 2024 July 14 by dafhi
/'
example: screenres 800,600,32
line2d.render_target 0
sub myline( x sng, y sng, x1 sng, y1 sng, col as ulong)
using line2d
line2d_loopS
pset (*px, *py), col
line2d_loopE
end sub
myline rnd*800,rnd*600,rnd*800,rnd*600, rgb(255,255,255)
'/
dim as imvars im
sub render_target( _i as any ptr )
im.get_info _i
pitchBy = im.pitch \ 4 '' integer divide
p32 = im.pixels
wm = im.w - 1
hm = im.h - 1
end sub
function _no_intersect( byref x sng, byref y sng, byref x1 sng, byref y1 sng, slope sng, _max sng ) as long
static sng ptr xmin, xmax, ymin, ymax, xmin_y, xmax_y, ymin_x, ymax_x
static sng _min '' dummy variable atm
if x1 < x then
xmin = @x1: xmax = @x
xmin_y = @y1: xmax_y = @y
else
xmin = @x: xmax = @x1
xmin_y = @y: xmax_y = @y1
endif
dim as long retval = *xmin >= _max orelse *xmax < _min
if *xmin < _min then
*xmin_y += (_min - *xmin) * slope
*xmin = _min
endif
_max -= .0001
if *xmax > _max then
*xmax_y -= (*xmax - _max) * slope
*xmax = _max
endif
return retval
end function
sub _make_unrenderable( byref x sng, byref x1 sng )
x = 1
x1 = 0
end sub
type t_returns
sng x,y, _x, _y, x1, slope
end type
dim as t_returns ret
dim sng slope, s_temp, dx, dy
dim sng ptr px,py
sub _absdxdy_sorted( byref x sng, byref y sng, byref x1 sng, byref y1 sng, byref px sng, byref py sng, _im_w sng, _im_h sng )
if x > x1 then
sw( x, x1, s_temp )
sw( y, y1, s_temp )
endif
dx = x1 - x
dy = y1 - y: slope = dy / dx
if _no_intersect( x,y, x1,y1, slope, _im_w ) then _make_unrenderable ret._x, ret.x1: exit sub
if _no_intersect( y,x, y1,x1, dx/dy, _im_h ) then _make_unrenderable ret._x, ret.x1: exit sub
ret._x = x - .4999
ret._y = y - .4999
ret.x1 = x1 - .4999
ret.slope = slope
end sub
sub _common( byref x sng, byref y sng, byref x1 sng, byref y1 sng )
if abs(y1 - y) > abs(x1 - x) then
px = @ret.y: py = @ret.x
_absdxdy_sorted y, x, y1, x1, x,y, im.h, im.w
else
px = @ret.x: py = @ret.y
_absdxdy_sorted x, y, x1, y1, x,y, im.w, im.h
endif
end sub
''
#define line2d_loopS _common x, y, x1, y1: ret.x=ret._x: ret.y=ret._y: while ret.x < ret.x1: _
if *px > -0.5 andalso *py > -0.5 andalso _
*px < (wm+.5) andalso *py < (hm+.5) then
#define line2d_loopE endif: ret.y += ret.slope: ret.x += 1: wend
end namespace
' --------------------------
screenres 800,600, 32
line2d.render_target 0
sub mydraw( x sng, y sng, x1 sng, y1 sng, col as ulong)
using line2d
line2d_loopS
pset (*px, *py), col
line2d_loopE
end sub
mydraw rnd*800,rnd*600,rnd*800,rnd*600, rgba(255,255,255,50)
sleep