Main Menu

Render Array As Image

Started by kevin, December 07, 2018, 10:42:20 AM

Previous topic - Next topic

kevin

 Render Array As Image


    This is example of how you can use an Integer array as an image substitute, then copy the array to a temp image and draw it with mask colour to the display or wherever


PlayBASIC Code: [Select]
      LoadFont "Arial",1,32


Width= 800
Height=600

Dim ScreenArray(Width*Height)

Do
cls 255

WidthByHeightMinus1 =Width*Height-1
For lp=0 to rnd(50)

Index1 = floor(Rnd#(WidthByHeightMinus1))

Index2 = ClipRange(Index1+100,0 ,WidthByHeightMinus1)

For Index = Index1 To index2
ScreenArray(index) = rndrgb()
next
next


if (frames and $ff)=0

ClearArray ScreenArray(),0
Endif

RenderScreen(Width,Height)
; print frames
frames++


Print "Render Array as Image"
sync
loop



Function RenderScreen(Width,Height)

// This has no clipping

local ThisSurface= GetSurface()


static Strip32BitImage
if Strip32BitImage=0

Strip32BitImage= GetFreeimage()
CreateFxImageEx Strip32BitImage,Width,Height, 32
endif


local SrcPtr = GetArrayPtr(ScreenArray(),true)
local DestPtr = GetImagePtr(Strip32BitImage)


WidthBy4 = Width*4

For Ylp=0 to Height-1

CopyMemory SrcPtr,DestPtr,WidthBy4

SrcPtr += (WidthBy4)
DestPtr += (WidthBy4)

next

rendertoimage ThisSurface
lockbuffer
drawimage Strip32BitImage,0,0,true
unlockbuffer

EndFunction










    Video


  This is just a quick video of a source code that shows the programmer how they might render an array as if it was an image.  to do this we copy the array to a temp image then draw that image onto the destination surface.