GLBasic forum

Main forum => Bug Reports => Topic started by: ampos on 2011-Sep-20

Title: gensprite()
Post by: ampos on 2011-Sep-20
If you write

Code (glbasic) Select
function test:
   local sp1=gensprite()
   local sp2=gensprite()
   loadsprite "sp1.png",sp1
   loadsprite "sp2.png",sp2
endfuntion


gensprite() always returns the same number for both gensprite.

If you want it to work you have to do:

Code (glbasic) Select
function test:
   local sp1=gensprite()
   loadsprite "sp1.png",sp1
   local sp2=gensprote()
   loadsprite "sp2.png",sp2
endfunction


Is this a "feature" or a bug?

(I like to declare all variables at the start...)
Title: Re: gensprite()
Post by: Minion on 2011-Sep-20
Just my take on this, but using gensprite would pick the first available "slot", if you dont fill that slot then the next time you use gensprite it would return the same index, until that spot is filled.
Title: Re: gensprite()
Post by: Slydog on 2011-Sep-20
Just like Minion said.

I use a custom function (as suggested by someone? (Gernot?)) that steps through all sprites until it finds one without any dimensions. 
I think maybe GENSPRITE doesn't reuse unloaded sprite IDs?  (If it does, I can't see why I would use this function!)
Can't remember how to free a sprite, maybe 'LOADSPRITE "", sprite_id'?

Code (glbasic) Select
// Find free sprite slot
FUNCTION Sprite_GetNextId%:
LOCAL id% = 1
LOCAL sx%, sy%
GETSPRITESIZE id, sx, sy
WHILE sx>0 OR sy>0
INC id
IF id > 30000 THEN RETURN 1 // If something goes wrong, overwrite sprite id 1. Could return '-1' instead, but would need to check value when returned
GETSPRITESIZE id, sx, sy
WEND
RETURN id
ENDFUNCTION

Title: Re: gensprite()
Post by: Kitty Hello on 2011-Sep-20
GENSPRITE does exaclty what you did in your function. Just in reverse order - it tries to find the largest free number available (so it's less possible to mess up older code). Same for GENSOUND, GENFONT.
Title: Re: gensprite()
Post by: spacefractal on 2011-Oct-03
except there is really some bug in GENSPRITE() i found today.

If I remove a image with LOADSPRITE "", then GENSPRITE can break and can pickup a number, there is allready in use (etc its replaced a image which its should not do that).

So I think I need that nice function above, and its works really nicely and workaround a unknown GENSPRITE() bug (as I not sure what it happens, but its after a LOADSPRITE "").
Title: Re: gensprite()
Post by: MrTAToad on 2011-Oct-03
Can't say I get that here.  However, a LOADSPRITE "" does generate the "error : No file" message in the status area
Title: Re: gensprite()
Post by: Kitty Hello on 2011-Oct-04
I removbed that error message for the next release.