GLBasic forum

Main forum => Bug Reports => Topic started by: MrTAToad on 2011-Jan-23

Title: Unloading sprites
Post by: MrTAToad on 2011-Jan-23
Following the discussion about unloading sprites, with the following test program :

Code (glbasic) Select
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 :) )
Title: Re: Unloading sprites
Post by: Quentin on 2011-Jan-23
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?
Title: Re: Unloading sprites
Post by: MrTAToad on 2011-Jan-23
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.
Title: Re: Unloading sprites
Post by: Quentin on 2011-Jan-23
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.
Title: Re: Unloading sprites
Post by: MrTAToad on 2011-Jan-23
GENSPRITE, like all the others should be using the first free slot available, and not just the next one along.

Two queries here :


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
Title: Re: Unloading sprites
Post by: Quentin on 2011-Jan-23
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

Code (glbasic) Select

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
Title: Re: Unloading sprites
Post by: MrTAToad on 2011-Jan-23
Which is why unloading a sprite should return that ID when GENSPRITE is used.
Title: Re: Unloading sprites
Post by: matchy on 2011-Jan-23
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.
Code (glbasic) Select

first$=GENSPRITE()
second$=GENSPRITE()
third$=GENSPRITE()


Title: Re: Unloading sprites
Post by: MrTAToad on 2011-Jan-24
Indeed it doesn't which is correct
Title: Re: Unloading sprites
Post by: Kitty Hello on 2011-Jan-24
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.
Title: Re: Unloading sprites
Post by: Quentin on 2011-Jan-24
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
Title: Re: Unloading sprites
Post by: MrTAToad on 2011-Jan-24
I'll also check the sprite size after a LOADSPRITE "".

Title: Re: Unloading sprites
Post by: MrTAToad on 2011-Jan-24
Yes, it is working correctly :)  I had made an error in the original code  :blink:
Title: Re: Unloading sprites
Post by: Leginus on 2011-Jan-24
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 :)
Title: Re: Unloading sprites
Post by: MrTAToad on 2011-Jan-24
Both work
Title: Re: Unloading sprites
Post by: Leginus on 2011-Jan-24
ok thx
Title: Re: Unloading sprites
Post by: Kitty Hello on 2011-Jan-25
You can also delete sprites with GRABSPRITE id, 0,0,0,0
Title: Re: Unloading sprites
Post by: Millerszone on 2011-Feb-03
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
Title: Re: Unloading sprites
Post by: Kitty Hello on 2011-Feb-04
smells like a bug in the sound engine.