News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

From Poke to Pointers (screen fill examples)

Started by kevin, December 11, 2018, 08:10:44 AM

Previous topic - Next topic

kevin

   From Poke to Pointers (screen fill examples)  (2018-12-11)



   Example #1 - Fill Screen using PokeInt

PlayBASIC Code: [Select]
      Do

frames++
ThisColour+=$10203

t=timer()
FillScreen1(ThisColour)
tt#+=Timer()-t


text 0,0,"Average Fill Time:"+str$(tt#/frames)

Sync


loop spacekey()
end





Function FillScreen1(ThisColour)
RendertoScreen
lockbuffer
Width =GetSurfaceWidth()
Height=GetSurfaceHeight()

Ptr = GetImagePtr(0)
Pitch = GetImagePitch(0)

ThisRGB=point(0,0)

for Ylp=0 to Height-1

RowAddress = Ptr + (Ylp * Pitch)
for Xlp=0 to Width-1
PokeInt RowAddress+(Xlp*4), ThisColour
next
next

unlockbuffer

EndFunction








   Example #2 - Fill Screen using pointer

PlayBASIC Code: [Select]
      Do

frames++
ThisColour+=$10203

t=timer()
FillScreen2(ThisColour)
tt#+=Timer()-t


text 0,0,"Average Fill Time:"+str$(tt#/frames)

Sync


loop spacekey()
end




Function FillScreen2(ThisColour)
RendertoScreen
lockbuffer
Width =GetSurfaceWidth()
Height=GetSurfaceHeight()

Ptr = GetImagePtr(0)
Pitch = GetImagePitch(0)

ThisRGB=point(0,0)

for Ylp=0 to Height-1

; define pointer that read/writes Integers
Dim RowPtr as Integer Pointer
; Comute the first pixel on this row
RowPtr = Ptr + (Ylp * Pitch)

for Xlp=0 to Width-1
; de reference (WRITE TO) this pointer, same as POKEINT
*RowPtr=ThisColour

; add 1 to this pointer. Since the pointer is an integer
; adding 1 is actually moving 4 bytes ahead to the next pixel
RowPtr++
next
next

unlockbuffer

EndFunction






   Example #3 - Fill Screen using typed pointer, with a static array field

PlayBASIC Code: [Select]
      Do

frames++
ThisColour+=$10203

t=timer()
FillScreen3(ThisColour)
tt#+=Timer()-t


text 0,0,"Average Fill Time:"+str$(tt#/frames)

Sync


loop spacekey()
end




Type tPixelRow
Pixels(10000)
EndType



Function FillScreen3(ThisColour)
RendertoScreen
lockbuffer
Width =GetSurfaceWidth()
Height=GetSurfaceHeight()

Ptr = GetImagePtr(0)
Pitch = GetImagePitch(0)

ThisRGB=point(0,0)

for Ylp=0 to Height-1

; define typed pointer. Writing to it is just like
; Peek / poke at this location with and offset
Dim Row as tPixelRow Pointer
; Comute the first pixel on this row
Row = Ptr + (Ylp * Pitch)

for Xlp=0 to Width-1
; Write to the address of this pointer with offset of the array
; index. Since the Pixels array is the first field in the type
; it'll have an offset of zero from where ever the pointer is
; pointing

; now since the array is 32bit, it's the same as going
; pokeint RowAddress+(Xlp*4), ThisColour
Row.Pixels(xlp)=ThisColour
next
next

unlockbuffer

EndFunction










   Example #4 - Fill Screen using typed pointer, with a static array field (UNROLLED)

PlayBASIC Code: [Select]
      Do

frames++
ThisColour+=$10203

t=timer()
FillScreen3_UNROLLED(ThisColour)
tt#+=Timer()-t


text 0,0,"Average Fill Time:"+str$(tt#/frames)

Sync


loop spacekey()
end




Type tPixelRow
Pixels(7)
EndType







Function FillScreen3_UNROLLED(ThisColour)
RendertoScreen
lockbuffer
Width =GetSurfaceWidth()
Height=GetSurfaceHeight()

Ptr = GetImagePtr(0)
Pitch = GetImagePitch(0)

ThisRGB=point(0,0)


; The Number of 8 pixel chunks in this width
Chunks = Width / 8

; the left over pixels on this row
LeftOverPixels = Width-( Chunks * 8 )

for Ylp=0 to Height-1

; define typed pointer. Writing to it is just like
; Peek / poke at this location with and offset
Dim Row as tPixelRow Pointer
; Comute the first pixel on this row
Row = Ptr + (Ylp * Pitch)



for Xlp=0 to Width-1 step 8


; write to this address - Add the offset of Pixels array
; and compute the 0 pixel
Row.Pixels(0)=ThisColour

; write to this address - Add the offset of Pixels array
; and compute the 1 pixel

Row.Pixels(1)=ThisColour
Row.Pixels(2)=ThisColour
Row.Pixels(3)=ThisColour
Row.Pixels(4)=ThisColour
Row.Pixels(5)=ThisColour
Row.Pixels(6)=ThisColour
Row.Pixels(7)=ThisColour

Row= Row + Sizeof(tPixelRow)
next


for Xlp=0 to leftOvers-1
Row.Pixels(xlp)= ThisColour
next

next

unlockbuffer

EndFunction




stevmjon

#1
comparing these examples makes pointers clearer to understand.

example #4 :
LeftOverPixels = Width - (Chunks*8)

* the multiply by 8 is being replaced with a smiley face
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

 
   ahh..  yeah,  i've corrected the code above.  It occurs because the custom tags are post processed as part of the broads theme, some any sequence of characters that just happens to be smiley can leak through into code blocks.