Unloading sprites

Previous topic - Next topic

MrTAToad

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 :) )

Quentin

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?

MrTAToad

#2
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.

Quentin

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.

MrTAToad

#4
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

Quentin

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

MrTAToad

Which is why unloading a sprite should return that ID when GENSPRITE is used.

matchy

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()



MrTAToad

Indeed it doesn't which is correct

Kitty Hello

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.

Quentin

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

MrTAToad

I'll also check the sprite size after a LOADSPRITE "".


MrTAToad

Yes, it is working correctly :)  I had made an error in the original code  :blink:

Leginus

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 :)

MrTAToad