Free brushes

Blitz3D Forums/Blitz3D Programming/Free brushes

I wrote a program that allows people to edit textures.
However, when a brush is saved under the same name, and then reloaded, the old version is loaded.
It seems that when you reload a new instance of the same brush, it returns the previous loaded brush.
A way around this is to free the old brush, and then free any object that is using this brush. However, this is not a suitable solution for my program.
I have some code here that demonstrates this:
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()

;setup camera
camera = CreateCamera()
MoveEntity camera, 0, 0, -10
;setup cube
cube = CreateCube()
;create image
image = CreateImage(64, 64)


;----------------------
;1. Create First Image (ovals)
;----------------------
Cls
Oval 0, 0, 64, 64
GrabImage image, 0, 0
SaveImage image, "test1.bmp"

;----------------------
;2. Apply Image
;----------------------
tex = LoadBrush("test1.bmp")
PaintMesh cube, tex


;		;----------------------
;		;3. Remove 1st brush from memory
;		;----------------------
;		;remove cube, because it contains brush
;		FreeEntity cube 
;		cube = CreateCube()
;		;remove brush itself
;		FreeBrush tex
		
;----------------------
;4. Create Second Image (rectangles)
;----------------------
Cls
Rect 0, 0, 64, 64, 0
Rect 8, 8, 48, 48, 0
GrabImage image, 0, 0
SaveImage image, "test1.bmp"

;----------------------
;5. Apply 2nd Image
;----------------------
brush = LoadBrush("test1.bmp")
PaintMesh cube, brush

;----------------------
;6. Test
;----------------------
RenderWorld()
Flip

;cleanup
DeleteFile "test1.bmp"

WaitKey()

End

It creates a first brush (with ovals), and then it creates a second one (with squares). However, only the first brush is shown.
I commented out the quick fix. When I CopyEntity() or CopyMesh() the original mesh, the brush still remains resident.
Does anybody know a way to force the reloading of the texture ?

Ow .. it seems that with using LoadImage, you can work around this:
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()

;setup camera
camera = CreateCamera()
MoveEntity camera, 0, 0, -10
;setup cube
cube = CreateCube()
;create image
image = CreateImage(64, 64)


;----------------------
;1. Create First Image (ovals)
;----------------------
Cls
Oval 0, 0, 64, 64
GrabImage image, 0, 0
SaveImage image, "test1.bmp"

;----------------------
;2. Apply Image
;----------------------
tex = iLoadBrush("test1.bmp")
PaintMesh cube, tex

;		;----------------------
;		;3. Remove 1st brush from memory
;		;----------------------
;		;remove cube, because it contains brush
;		FreeEntity cube 
;		cube = CreateCube()
;		;remove brush itself
;		FreeBrush tex
		
;----------------------
;4. Create Second Image (rectangles)
;----------------------
Cls
Rect 0, 0, 64, 64, 0
Rect 8, 8, 48, 48, 0
GrabImage image, 0, 0
SaveImage image, "test1.bmp"

;----------------------
;5. Apply 2nd Image
;----------------------
brush = iLoadBrush("test1.bmp")
;PaintMesh cube, brush

;----------------------
;6. Test
;----------------------
RenderWorld()
Flip

;cleanup
DeleteFile "test1.bmp"

WaitKey()

End

;-----------------------------------------------------------------------------------------------------
;												iLoadBrush
;-----------------------------------------------------------------------------------------------------
;uses LoadImage to update brush
Function iLoadBrush(file$)

	brush = LoadBrush(file$)
	img = LoadImage(file$)
	tex = LoadTexture(file$)
	ww = TextureWidth(tex)
	hh = TextureHeight(tex)
	ResizeImage img, ww, hh
	CopyRect 0, 0, ImageWidth(img), ImageHeight(img), 0, 0, ImageBuffer(img), TextureBuffer(tex)
	BrushTexture brush, tex
	FreeTexture tex
	FreeImage img
	Return brush
	 
End Function


It's Blitz3D's internal automatic memory usage optimation: when you load a texture twice, it will use only one copy of it in VRAM and create pseudo instances for every further handle, AS LONG AS it has the same flags and everything. As you suggested, there are some workarounds, like creating a texture using CreateTexture and then CopyRect a LoadImage-ed image to the texture buffer. Alpha and/or Mask information will have to be recreated with this method, there are some examples in the code archive (eg: optimize alpha/mask). Also, unfort. this won't work with DDS textures.
An other workaround is to save or copy the texture using a new, individual name. This temporary file may then be deleted instantly.