Following the discussion about unloading sprites, with the following test program :
LOCAL a%
a%=GENSPRITE()
DEBUG "Using sprite : "+a%+"\n"
LOADSPRITE "sprite1.png",a%
a%=GENSPRITE()
DEBUG "Next sprite : "+a%+"\n"
LOADSPRITE "",a%
a%=GENSPRITE()
DEBUG "Using sprite : "+a%+"\n"
I get the following output :
QuoteUsing sprite : 0
Next sprite : 1
Using sprite : 1
Which would mean the using LOADSPRITE "" didn't unload the sprite. Fair enough, perhaps an invalid filename is needed, so I changed the program to :
QuoteLOCAL a%
a%=GENSPRITE()
DEBUG "Using sprite : "+a%+"\n"
LOADSPRITE "sprite1.png",a%
a%=GENSPRITE()
DEBUG "Next sprite : "+a%+"\n"
LOADSPRITE "ifthisfileexiststhensomeonereallyneedstogetout.pmg",a%
a%=GENSPRITE()
DEBUG "Using sprite : "+a%+"\n"
But the results are the same :
QuoteUsing sprite : 0
Next sprite : 1
Using sprite : 1
I would expect the second Using sprite text to show 0 as the sprite is unloaded (or at least should be :) )
I thought GENSPRITE will return always the same id unless a real sprite is loaded. So the behavior is correct imho.
a = GENSPRITE() --> a = 0
LOADSPRITE "test.png", a
a = GENSPRITE() --> a = 1
b = GENSPRITE() --> b = 1 too
LOADSPRITE "test2.png", b
c = GENSPRITE() --> c = 2
Why should a sprite unloaded, which was never loaded in your example?
It was loaded and then unloaded
The sprite ID should be re-used with deleted sprites.
Sprite 0 is loaded and then deleted, so the next available sprite should be 0.
No, I don't think so
LOCAL a%
a%=GENSPRITE() // here a% = 0
DEBUG "Using sprite : "+a%+"\n"
LOADSPRITE "sprite1.png",a% // here a is used
a%=GENSPRITE() // so here a is the next free number 1
DEBUG "Next sprite : "+a%+"\n"
LOADSPRITE "",a% // here a is 1, not 0, so id = 0 isn't unloaded
a%=GENSPRITE() // and so a is still 1 here
DEBUG "Using sprite : "+a%+"\n"
from my point of view this behavior is absolutely correct.
GENSPRITE, like all the others should be using the first free slot available, and not just the next one along.
Two queries here :
- Does LOADSPRITE "",0/LOADSPRITE "x",0 actually unload the sprite (as documented http://www.glbasic.com/xmlhelp.php?lang=en&id=8&action=view (http://www.glbasic.com/xmlhelp.php?lang=en&id=8&action=view))
- Do the GEN commands find the first free slot ?
If using an invalid/non-existing filename for unloading sprites doesn't work then it might be time for a new command to do so.
Whilst just using the next index for the handle is quite to porgram, its not efficient as you could be potentially wasting free slots
Yes, it does :), but keep in mind, that GENSPRITE will increase the id only, if a sprite was really loaded. Otherwise this id is still unused. If a sprite is unloaded, this id is available for the next call of GENSPRITE
first% = GENSPRITE()
LOADSPRITE "example.png", first
second% = GENSPRITE()
LOADSPRITE "font.png", second
PRINT "1st ID: " + first, 0, 0
PRINT "2nd ID: " + second, 0, 20
PRINT "free frist sprite now ...", 0, 50
LOADSPRITE "", first
third% = GENSPRITE()
PRINT "new free slot is now " + third, 0, 80
SHOWSCREEN
KEYWAIT
Which is why unloading a sprite should return that ID when GENSPRITE is used.
At times I need to init a sprite number vars before knowing what it will be used for later in run-time. Something like this won't work as it doesn't increment without an action like LOADSPRITE and a counter works alternatively.
first$=GENSPRITE()
second$=GENSPRITE()
third$=GENSPRITE()
Indeed it doesn't which is correct
GENSPRITE simply searches for an index that has a sprite of size 0x0 - nothing more, nothing less.
Does it work then? LOADSPRITE "", id should set the GETSPRITESIZE id, x,y, to 0,0.
Quote from: Ocean on 2011-Jan-24
the original question was, whether such id's would be reused the next time GENSPRITE() is called.
... and it was answered. It is reused as you can see in the example above. I can't see the problem
I'll also check the sprite size after a LOADSPRITE "".
Yes, it is working correctly :) I had made an error in the original code :blink:
so does that mean it is loadpsprite"" or loadsprite"Blank"? or do both work.
Just wondered if I need to change my code....er and advice :)
Both work
ok thx
You can also delete sprites with GRABSPRITE id, 0,0,0,0
With this test code, the iPad had sound, PC didn't.
LOADSOUND "Media/crowd.wav", 1, 1
PLAYSOUND(1, 0.0, 1.0)
LOADSOUND "", 1, 1 // also tried LOADSOUND " ", 1, 1 and LOADSOUND "blank", 1, 1 - sound still played on the iPad
MOUSEWAIT
smells like a bug in the sound engine.