CopyImage problem

Blitz3D Forums/Blitz3D Programming/CopyImage problem

CakeMonitor(Posted 2006) [#1]
Firstly, I know what people like to say about using CopyImage and ResizeImage on the fly instead of preprocessing it all and sticking the results into an array, but this particular re-size might occur once in any 5 minute window of play, could re-size the image to any one of about 200 different sizes and the game runs at about 3% to 4% CPU usage on my system... so I don't want to hear anything about that. Not today. :p

However, I wondered if anyone could shed some light on this strange problem I have so I can get rid of my dubious work-around. Take a look at the following code. The important function it the one called ResizeBat, which (ignoring the first two lines) simply copies a base image over an existing one and then re-sizes it. You can see I've commented out the normal Blitz command CopyImage and added a call to my own function: myCopyImage (listed below).


Function ResizeBat(num, ammount#)
	batScale(num) = ammount#
	If batScale(num) <=0 Then batScale(num) = 0.1

;	bat(num) = CopyImage (bat(0))
	myCopyImage(bat(0), bat(num))

	ResizeImage bat(num), 128 * batScale(num), 20
End Function


Function myCopyImage(source, dest)
	ResizeImage dest, ImageWidth(source), ImageHeight(source)
	oldbuffer = GraphicsBuffer()
	SetBuffer ImageBuffer(dest)
	DrawBlock source, 0, 0
	SetBuffer oldbuffer
End Function


If I use neither of the copy functions, the re-size works as expected.
If I use the Blitz copy function, neither the copy nor the re-size takes place.
If I use my copy function both the copy and the re-size work fine.
If I use both copy functions (pointless, I know, but I was just testing stuff out by this time) no copying or resizing takes place.


Bizarre!
Any ideas?


tonyg(Posted 2006) [#2]
I know these aren't functions but it's a simpler method as I no longer believe the 'pre-rotate' method is necessary...
Graphics 640,480,16

SetBuffer BackBuffer()
AutoMidHandle True 
alien = LoadImage("ship.png")

Const leftk = 203, rightk = 205


While Not KeyHit(1)
	Cls
		
	If KeyDown(leftk)
			a = a -6
	ElseIf KeyDown(rightk)
			a = a+6
	EndIf 
	
	alien1 = CopyImage(alien)
	If KeyHit(200) ResizeImage alien,100,100
	If KeyHit(208) ResizeImage alien,32,32
	RotateImage alien1,a
		
	DrawImage alien1,320,240
	
	FreeImage alien1
	Flip
Wend : End  



CakeMonitor(Posted 2006) [#3]
thanks for this tony, I've tried it on my system and it all works fine. So why does this 'CopyImage : ResizeImage' work fine and mine not work?


tonyg(Posted 2006) [#4]
You might want to show the smallest possible example where this isn't working as expected.


CakeMonitor(Posted 2006) [#5]
This doesn't work: (no copy or re-size takes place)
	bat(num) = CopyImage (bat(0))
	ResizeImage bat(num), 128 * batScale(num), 20


This does work: (both the copy and the re-size take place)
	myCopyImage(bat(0), bat(num))
	ResizeImage bat(num), 128 * batScale(num), 20
the *only* difference between the two is I'm calling my own copy image function instead of the Blitz one.




Also, FYI:

This doesn't work: (neither of the copies or the re-size takes place)
	bat(num) = CopyImage (bat(0))
	myCopyImage(bat(0), bat(num))
	ResizeImage bat(num), 128 * batScale(num), 20


This does work: (but I need the copy to happen as well as the re-size)
	ResizeImage bat(num), 128 * batScale(num), 20


Basically as soon as I use Blitz's CopyImage(), the image related commands in that particular function, no longer work. The game continues to play fine with no pauses, hic-ups or errors (except the image is nopt re-sized!)


DJWoodgate(Posted 2006) [#6]
In its original form you are creating a new image and not freeing the previous one stored at bat(num). On the other hand your modified copy function does not create a new image. If as you say a resize only happens every five minutes or so of gameplay then it is not going to cause major problems, not straight away anyway.

However the fact that in its original form you are creating a new image and trying to store its handle in the bat array is I fear terribly significant and you still have failed to provide us with one single very important and absolutely vital piece of information... The datatype of the bat array.


CakeMonitor(Posted 2006) [#7]
Um, integer. no?

it's defined as Dim bat(2), and it's used to store image handles... which are integers, right?


DJWoodgate(Posted 2006) [#8]
Hah. Well that blows my theory out of the water then. Can you reproduce this in a simple program you can post here? And you had better state which version you are using just incase it turns out to be a bug.

Edit.
I have just tried it and it works fine here with v1.90.




CakeMonitor(Posted 2006) [#9]
duh!

got it now, thanks!

It was because I was using a sprite library (that I wrote, so *really* I should have spotted what was going on!), and the library uses a different variable (SpriteItem\image) to store the integer handle to the image in question.

I've been happily using it for months without altering images in-game, just creating them at startup and destroying them when the program ends.

of course, FreeImage bat(num) : bat(num) = CopyImage bat(0) was assigning a new value to bat(num), but not updating SpriteItem\image. My own function worked because it was copying the new image into the original, simply resizing the the original image to match the new size, all the while keeping its original handle.

It's just sloppy coding... I originally thought this project would be very small and simple and wouldn't require my sprite library functions, after a bit of development I decided I did want the library and tried to include it so some sections are using lists of my sprite type and other sections are using array handles to the sprite items. Bad coder. Bad. <slaps self on face>


Damien Sturdy(Posted 2006) [#10]
I know this will get annoying if we do it for everyone who posts who's using a FreeImage command... but what version of Bltiz are you currently using in this project?


CakeMonitor(Posted 2006) [#11]
version 1.90

I'm gonna do the 1.94 update later this weekend.

if that presents any new problems I'll come back and mention it here, but assume for now that it's all working with v1.94 too.


big10p(Posted 2006) [#12]
Do not update to 1.94 - it's bugged. 1.91 is the latest stable version.


Blitzplotter(Posted 2006) [#13]
Mmmm - bugged ? I've updated recently to 1.94 to allow for the use of the excellent Protean with no great problems.


big10p(Posted 2006) [#14]
Yep, 1.94 has got memory leak problems.

Why does Protean not work with earlier versions, then?