UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on December 11, 2018, 08:10:44 AM

Title: From Poke to Pointers (screen fill examples)
Post by: kevin on December 11, 2018, 08:10:44 AM
   From Poke to Pointers (screen fill examples)  (2018-12-11)



   Example #1 - Fill Screen using PokeInt

[pbcode]


      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



[/pbcode]



   Example #2 - Fill Screen using pointer

[pbcode]

      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

[/pbcode]



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

[pbcode]



      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




[/pbcode]




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

[pbcode]


      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

[/pbcode]
Title: Re: From Poke to Pointers (screen fill examples)
Post by: stevmjon on December 12, 2018, 07:33:38 PM
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
Title: Re: From Poke to Pointers (screen fill examples)
Post by: kevin on December 13, 2018, 11:29:11 PM
 
   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.