GEN... commands to allocate IDs without needing to be used

Previous topic - Next topic

FutureCow

Currently GEN... commands don't allocate numbers until the previous one has been used

eg.
Code (glbasic) Select
LOCAL sprite1,sprite2,sprite3
sprite1 = GENSPRITE();
GRABSPRITE sprite1,0,0,5,5
sprite2 = GENSPRITE();
sprite3 = GENSPRITE();
GRABSPRITE sprite2,0,0,5,5
GRABSPRITE sprite3,0,0,5,5


Will result in both sprite2 and sprite3 having a sprite ID of 1.
I'd like to request that the ID is allocated without having to be used.

Kitty Hello

GENSPRITE ries to find a free slot. If you're not using it, GENSPRITE will return the very same slot again. Use GenSprite and directly follow a GRAB/LOADSPRITE.

FutureCow

Does it reuse the spot if you clear the sprite with 'LOADSPRITE "",[number]' ?

If only there was a DELETESPRITE command....  :whistle:

It would have really helped my current board game project if it allocated the slot without using it. I have to do a lot of grabsprites in a loop every frame (as there's no command to draw part of a sprite (I'll beg if it will help get me a command to do that!!!  =D )). It was cutting down my coding immensely using GENSPRITE until I realised it wasn't preallocating the sprite IDs.

I'll be using hard coded numbers again for this project. It's a great command though!!!

Hemlos

Me and someone else were playing with an idea like this a few months ago...
We both made a function, which was almost exactly like GENSPRITE, you can probably just code that yourself.
Its actually how gensprite idea was started.

Hmm...LoadImage..i forget?

LOADImageID
DELETEImageID
etc
Bing ChatGpt is pretty smart :O

Quentin


Hemlos

Quote from: Quentin on 2009-Oct-13
LOADSPRITE "", num% = DELETESPRITE num% :)

ya, but thats not what he needs.

he needs a function set that stores and manipulates ID lists.
Bing ChatGpt is pretty smart :O

Hemlos

Code: (Written by Moru) [Select]
GLOBAL gImgFoo = LOADIMAGE( "gImgFoo.png" )
DRAWSPRITE gImgFoo , 100 , 100
SHOWSCREEN
MOUSEWAIT
END

FUNCTION LOADIMAGE: File$ //returns ID
    STATIC id%
    INC id, 1
    LOADSPRITE File$, id
    RETURN id
ENDFUNCTION
Bing ChatGpt is pretty smart :O

FutureCow

Thanks Hemlos, I might be able to use something like that. I'll have a think about it and see if I can make it work.

It would be good to know if gensprite reuses "deleted" sprites though. I might have to do some testing tomorrow.

Hemlos

Code (glbasic) Select
It would be good to know if gensprite reuses "deleted" sprites though. I might have to do some testing tomorrow.

Reusing an identifying value after clearing the memory of the image...

Lets have an example here...
Take a tile map, imagine a matrix field of identifiers, which were loaded using GENSPRITE:

id0 id1 id2 id3
id4 id5 id6 id7
id8 id9 idA idB
idC idD idE idF

If you want to free the allocated memory of some random image, like quentin said, all you need to do is:

LOADSPRITE "", id5

The value stored in id5 is irrelevant, because the memory allocated to the value is insignificantly small.
The only thing that matters is the image is removed from memory here.


Furthermore, if you want to reuse this identifier, its value already waiting to be used, the matrix isnt affected if you retain the value of the id.

//reuse an identifier...no need for gensprite again:
LOADSPRITE "newimage.png", id5
Bing ChatGpt is pretty smart :O

Kitty Hello

I really don't understand all the Hoff.
A free sprite is one that has width and height = 0. Thus, if you want to free/delete a sprite make it: GRABSPRITE id, 0,0,0,0 or whatever.
The GENSPRITE will pick a sprite slot that has no sprite loaded to prevent overwriting.

Hemlos

KH
He wanted to pre-allocate id's with some new command.
He just needs to create an array i think.
See, he tried to do this code below, and found sprite2 and sprite3 both equal a value of 1, which is supposed to do that.
Code (glbasic) Select
LOCAL sprite1,sprite2,sprite3
sprite1 = GENSPRITE();
GRABSPRITE sprite1,0,0,5,5
sprite2 = GENSPRITE();
sprite3 = GENSPRITE();
GRABSPRITE sprite2,0,0,5,5
GRABSPRITE sprite3,0,0,5,5

PRINT sprite2+" "+sprite3,100,100
SHOWSCREEN
MOUSEWAIT


I think he wants to create an array of unused id's
You might be able to do something like this..
Code (glbasic) Select

dim array[100]
for i=0 to 99
id = gensprite()
grabsprite id,0,0,1,1
array[i]=id
next

now all ids in this 100 slot array, are ready.

Futurecow, this is something you will need to play with...i dont know if this is suitable for you or not, goodluck!
TTYL
Bing ChatGpt is pretty smart :O

Moru

Quote from: FutureCow on 2009-Oct-13
I have to do a lot of grabsprites in a loop every frame (as there's no command to draw part of a sprite (I'll beg if it will help get me a command to do that!!!  =D )).

Do you want to draw only part of a sprite? Have you tried using POLYVECTOR?
With it you can draw only part of a sprite, just use the tx, ty coordinates to describe the area you want to use as texture for the points in the square you draw. Just remember you have to put all corners on the screen anti-clockwise.

To draw the upper left corner of a sprite that is 128x128 full size:
Code (glbasic) Select
LOADSPRITE "Blocks.bmp", 1 // 128x128 Bitmap (or any other size bigger than 64x64

// Size of the block on screen
x = 0
y = 0
w = 64
h = 64
// This is the texture coordinates we draw from
tx = 0
ty = 0
tw = 64
th = 64
STARTPOLY 1 // here you set the sprite ID to draw with
POLYVECTOR   x,   y, tx,    ty,    RGB(255, 255, 255)
POLYVECTOR   x, y+h, tx,    ty+th, RGB (255, 255, 255)
POLYVECTOR x+w, y+h, tx+tw, ty+th, RGB(255, 255, 255)
POLYVECTOR x+w,   y, tx+tw, ty,    RGB(  0, 255,   0) // This makes the upper right corner a bit green-tinted.
ENDPOLY


Sorry if this isn't what you want, I'm tired :-)

Hemlos

Quote from: FutureCow on 2009-Oct-13
I have to do a lot of grabsprites in a loop every frame (as there's no command to draw part of a sprite (I'll beg if it will help get me a command to do that!!!  =D )).

Ill reitterate on morus reply here..

As  moru says, POLYVECTOR, you will need to manipulate the texture coordinates.
POLYVECTOR makes triangles from vertex coordinates.
The vertex coordinates are relative to the "point of insertion".
The vertex coordinates also contain texture positions.
The texture positions directly translate to pixel position in a 2d image.

If you load an image into morus example code, you will render the upper left quadrant of the 128x128 image, into a 64x64 size polyvector (polyvectors can be color masked at each vertex for cool special color effects).
Shown in this image below is the quad being drawn from, into the poly.

[attachment deleted by admin]
Bing ChatGpt is pretty smart :O

FutureCow

Hi all!
This is getting a lot more complicated than it needs to be! (though thanks everyone for your replies!!!!!!!) To sort through all the confusion.

Firstly Hemlos has suggested the same solution I'm currently using - store the image IDs in an array, loop over it and do a GENSPRITE/GRABSPRITE loop,0,0,1,1 to create them. That solves my original problem (my 2 year old had had me up almost all the previous night and my brain wasn't working. I felt really stupid when that easy solution finally popped into my mind...  =D)
(No, I didn't delete any message, they're all still there. I was referring to the loadsprite comment in that post.)

Secondly was just a question :- If you deallocate a sprite with loadsprite "",number - does the next GENSPRITE reuse that sprite's ID? I'll do a test shortly to find out, I just didn't have access to GLBasic yesterday when I asked the question to test it myself.
I should've mentioned on my post that I realised you could unload a sprite with "loadsprite "", number" (though thanks for the post Quentin!!!) - I'd just like to see a dedicated command for it as it's counter-intuitive to use a "load" command to delete something.

Moru : Thanks for that. I haven't tried using POLYVECTOR before, but I'm writing one now.

FutureCow