SetImageOverride ( file$,bank,width,height[,bytes_per_pixel] )

Parameters

file$ - the image file that worlds and resources name; it need not exist

bank - the pixels: width*height*bytes_per_pixel bytes, row by row from the top

width - the image's width, in pixels

height - the image's height, in pixels

bytes_per_pixel (optional) - 4 (default) for red, green, blue and alpha bytes, or 1 for one grey byte a pixel, as region maps use

Description

Has worlds and resources loaded from now on use pixels from a bank in place of an image file, and returns True.

It is how an editor shows pictures it has not saved yet. The World Editor's Paint tool keeps a terrain's painted blend map in memory and sets it as the override, so the preview it rebuilds shows the painted ground; Save writes the file with ExportTerrainLayers. Its Paint regions tool does the same with region maps, one grey byte a pixel, and ExportTerrainRegions. A world's texture assets, materials, terrain layers and blend maps that name the file all use the pixels.

The pixels are copied when you call it; change the bank and call it again to update the override. The file need not exist yet: a world or resource may name it, and checks that referenced files exist count it as present while the override stands. Textures already loaded keep their pixels, and LoadTexture and LoadImage still read the file itself.

ClearImageOverride ends an override; ImageOverridden says whether a file has one. Heightmaps have their own: SetHeightmapOverride.


See also: ClearImageOverride, ImageOverridden, SetHeightmapOverride, ExportTerrainLayers, PaintTerrainLayer.

Example

; SetImageOverride Example
; ------------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,32,40,-30
RotateEntity camera,40,0,0
CameraRange camera,0.1,400

light=CreateLight(1)
RotateEntity light,50,-30,0

; Loads the hills' terrain definition into a terrain of its own, or 0
Function LoadHills(document)
    library=LoadResourceDocumentAsync(document)
    If library=0 Then Return 0
    While Not WorldReady(library)
        If WorldLoadState(library)=WORLD_LOAD_FAILED Then
            FreeWorld library
            Return 0
        EndIf
        UpdateAsyncLoads 2
    Wend
    hills=CreateTerrainResource(FindWorldAsset(library,"resource"))
    FreeWorld library
    Return hills
End Function

; A blend map in memory, 64x64, 4 bytes a pixel: red, green, blue and alpha
; are the weights of the grass and rock layers (and two unused ones)
blend=CreateBank(64*64*4)
Function Stripes(blend,width)
    For y=0 To 63
        For x=0 To 63
            offset=(y*64+x)*4
            rock=((x/width) Mod 2)=1
            PokeByte blend,offset,255*(1-rock)
            PokeByte blend,offset+1,255*rock
            PokeByte blend,offset+2,0
            PokeByte blend,offset+3,0
        Next
    Next
End Function
Stripes blend,8

; Loads of this file, which does not exist, use the bank's pixels instead
file$="demo_blend.png"
SetImageOverride file,blend,64,64

; A terrain definition naming the file as its blend map, kept in memory
media$="../../../samples/terrain-resources/"
document=CreateResourceDocument("terrain-definition","demo_hills.terrain")
text$="{'format':'blitz3d.terrain','version':1,'id':'demo.hills','assets':["
text=text+"{'id':'height','kind':'heightmap','source':'"+media+"hills.b3height'},"
text=text+"{'id':'grass','kind':'texture','source':'"+media+"grass.png'},"
text=text+"{'id':'rock','kind':'texture','source':'"+media+"rock.png'},"
text=text+"{'id':'blend','kind':'texture','source':'"+file+"','options':{'flags':3}}],"
text=text+"'definition':{'heightmap':'height','dimensions':[64,14,64],'shading':true,"
text=text+"'material':{'mode':'layers','layers':[{'texture':'grass','uvScale':[0.25,0.25]},"
text=text+"{'texture':'rock','uvScale':[0.25,0.25]}],'splatMap':'blend'}}}"
If ReplaceResourceDocumentSource(document,Replace(text,"'",Chr(34)),ResourceDocumentRevision(document))=0 Then RuntimeError ResourceDocumentError()
hills=LoadHills(document)
If hills=0 Then RuntimeError ResourceDocumentError()
message$="The hills' blend map came from a bank, not a file"
width=8

While Not KeyDown(1)

    ; Space paints the stripes narrower, stands in for the file again, and reloads
    If KeyHit(57) Then
        width=width/2
        If width<1 Then width=16
        Stripes blend,width
        SetImageOverride file,blend,64,64
        FreeEntity hills
        hills=LoadHills(document)
        message="Reloaded with stripes "+width+" pixels wide"
    EndIf

    ; C clears the override: now the file is missing and loading fails
    If KeyHit(46) Then
        ClearImageOverride file
        reloaded=LoadHills(document)
        If reloaded=0 Then
            message="Without the override: "+Left(ResourceDocumentError(),60)
        Else
            FreeEntity reloaded
        EndIf
    EndIf

    ; Arrow keys move the camera
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    RenderWorld

    Text 0,0,"Arrow keys: move camera   Space: new stripes and reload   C: clear override   Esc: exit"
    Text 0,20,"ImageOverridden("+Chr(34)+file+Chr(34)+") = "+ImageOverridden(file)
    Text 0,40,message

    Flip

Wend

ClearImageOverride
FreeResourceDocument document
FreeBank blend

End

Index