You have a scope problem in the RenderbitmapArray function. ImageID is local to the function. When you first call the function, TheImage is copied into ImageID, which gets deleted as you expect. A new image is created and its ID stored in ImageID, but since ImageID is local to the function, the ID is lost when you exit. The next time through, TheImage is copied into ImageID again, but this is the old ID, not the one created in the last loop. If you pass ImageID back from the function, it will work. Or, since TheImage is declared Global anyway, you could just use that instead.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//SetSyncRate( 0 , 1 )
//SetSyncRate( 10 , 1 )
SetClearColor(64,32,48)
SetVirtualResolution( 1024, 768 )
SetWindowSize( 1024, 768, 0, 1 )
GOSUB IntializeTestVars
GOTO GameLoop
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
GameLoop:
DO
IF GetPointerPressed()
//Perlin2D( TheArray , MapSizeX , MapSizeY )
Noise2D( TheArray , MapSizeX , MapSizeY )
TheImage = RenderBitmapArray( TheArray , TheImage , TheSprite, MapSizeX , MapSizeY )
ENDIF
Print("FPS: " + str(ScreenFPS(),3) + " Vert: " + str(GetVerticesProcessed()) + " Tri: " + str(GetPolygonsDrawn()) + " vRAM: " + str(GetImageMemoryUsage(),2) + "MB")
Print("")
Print(TheImage)
Print(TheSprite)
Sync()
LOOP
END
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
IntializeTestVars:
#Constant MapSizeX = 256
#Constant MapSizeY = 256
GLOBAL TheArray AS INTEGER[MapSizeX,MapSizeY]
GLOBAL TheImage AS INTEGER : TheImage = CreateRenderImage( MapSizeX, MapSizeY, 0, 0 )
GLOBAL TheSprite AS INTEGER : TheSprite = CreateSprite( TheImage )
SetSpritePositionByOffset( TheSprite, GetVirtualWidth()*0.5, GetVirtualHeight()*0.5 )
RETURN
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
FUNCTION Noise2D( Array REF AS INTEGER[][] , SizeX AS INTEGER , SizeY AS INTEGER )
FOR y = 0 TO SizeY-1
FOR x = 0 TO SizeX-1
Array[x,y] = Random(0,255)
NEXT x
NEXT y
ENDFUNCTION
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
FUNCTION RenderBitmapArray(Array REF AS INTEGER[][],
ImageID AS INTEGER,
SpriteID AS INTEGER,
SizeX AS INTEGER,
SizeY AS INTEGER)
rMem AS INTEGER : rMem = CreateMemblockFromImage( ImageID )
FOR y = 0 TO SizeY-1
OffsetY = (y*SizeX*4) // Optimization
FOR x = 0 TO SizeX-1
Offset = 12+(x*4)+OffsetY // Optimization
SetMemblockByte( rMem, Offset , Array[x,y] )
SetMemblockByte( rMem, Offset+1, Array[x,y] )
SetMemblockByte( rMem, Offset+2, Array[x,y] )
SetMemblockByte( rMem, Offset+3, 255 )
NEXT x
NEXT y
IF ImageID THEN DeleteImage(ImageID) : ImageID = 0
ImageID = CreateImageFromMemblock( rMem ) : DeleteMemblock(rMem)
SetSpriteImage(SpriteID,ImageID)
ENDFUNCTION ImageID
// 4 bytes Width, 4 bytes Height, 4 bytes Depth,
// 4 bytes per pixel Red,Green,Blue,Alpha
// Pixel Order: top-left corner, proceeds left-to-right then top-to-bottom
// wwww hhhh dddd rgba rgba rgba ....