gfx pointer arithmetic

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
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

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

Last edited by dafhi on Jan 30, 2012 1:58, edited 1 time in total.
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)
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)
for i as integer=0 to length
 if (x1>=0)*(x1<xres)*(y1>=0)*(y1<yres) then
 end if
next i
end scope

for y as integer=0 to 600
next y
for x as integer=-50 to 50
next x

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