gfx pointer arithmetic

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
dafhi
Posts: 1335
Joined: Jun 04, 2005 9:51

gfx pointer arithmetic

Postby dafhi » Oct 18, 2011 16:51

in all her quirky glory.

[Updated] for readability

Code: Select all

Dim As Integer        SCR_W = 480, SCR_H = 360, bpp = 32
Dim As Integer        wid,hgt,bypp,pitch

ScreenRes SCR_W,SCR_H,bpp
ScreenInfo wid,hgt,bpp,bypp,pitch

Dim As Integer        widm = wid - 1
Dim As Integer        hgtm = hgt - 1
Dim As Integer        pitchm = pitch - bypp

Dim As UInteger       horizRGB = RGB(255,255,0)
Dim As Integer        offY = (hgtm - 20) * pitch
Dim As Any Ptr        botleft = ScreenPtr + offY

Dim As UInteger       vertRGB = RGB(0,255,0)
Dim As Integer        offX = 10 * bypp
Dim As Any ptr        topleft = ScreenPtr + pitch * hgtm

For dest_x As UInteger Ptr = botleft To botleft + pitchm Step 1
   *dest_x = horizRGB
Next

For dest_y As UInteger Ptr = ScreenPtr + offX To topleft + offX Step wid
   *dest_y = vertRGB
Next

Sleep
Last edited by dafhi on Jan 30, 2012 1:58, edited 1 time in total.
dodicat
Posts: 6390
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: gfx pointer arithmetic

Postby dodicat » Oct 20, 2011 0:09

Hi dafhi
The pointer arithmetic is fast, but easy to get a crash, I see that you have avoided that possibility.

Here's a Bressenham type line macro using pointer arithmetic, I've tried to make it crash proof, also, according to the help file, pointer stuff is supposed to work only within a screenlock/unlock loop.
Decide the colouring by the z value of the line, Bressenham lines being 3D in essence.

Code: Select all

screen 19,32

#macro _line(xf,yf,zf,xs,ys,zs,colour)
 scope
dim as integer xres=any,yres=any
 screeninfo xres,yres
 dim as uinteger pointer pixel
 dim as integer pitch=4*xres
 dim as any pointer row=screenptr
 Dim As Uinteger xpos=any,ypos=any
dim as single x1=xf,y1=yf,z=zf,nx=xs-xf,ny=ys-yf,nz=zs-zf
var length =sqr(nx*nx+ny*ny+nz*nz)
nx=nx/length:ny=ny/length:nz=nz/length
for i as integer=0 to length
 x1=x1+nx:y1=y1+ny:z=z+nz
 if (x1>=0)*(x1<xres)*(y1>=0)*(y1<yres) then
 ypos=pitch*(int(y1))
 xpos=ypos+4*(int(x1))
 pixel=row+xpos
 *pixel=colour
 end if
next i
end scope
#endmacro

screenlock
'fill
for y as integer=0 to 600
_line(0,y,0,800,y,255,rgb(255,255-z,255-z))
next y
'cross
for x as integer=-50 to 50
_line(x,0,0,800+x,600,255,rgb(z,z,255-z))
_line(x,600,0,800+x,0,255,rgb(z,255-z,z))
next x

screenunlock
sleep
dafhi
Posts: 1335
Joined: Jun 04, 2005 9:51

Postby dafhi » Oct 20, 2011 1:01

small code size is quite impressive. looking forward to seeing more of your opengl stuff

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests