Suggestion - HANDLE

Previous topic - Next topic

PeeJay

Coming from Blitz, I got very used to not having to remember long strings of numbers as to where I had loaded a particular sprite or sound effect, as they were assigned handles instead. As an example, we may have had something like:-
Code (glbasic) Select
Global piano
piano=LoadImage("piano.png")
DrawImage piano,0,0
Obviously, adding this as it stands would mean massive rewrites of code, and upsetting people who are used to the current method, so could I suggest a compromise? What about adding a HANDLE command? Perhaps GL could then just allocate numbers to all the handles and substitute at compile time? This may end something like this:-
Code (glbasic) Select
HANDLE piano
LOADSPRITE "piano.png",piano
DRAWSPRITE piano,0,0
Any thoughts?
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

Schranz0r

German but useful ;)

You can help me a little bit :P

http://www.glbasic.com/forum/viewtopic.php?id=1376
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

PeeJay

That's cool Schranz0r, but the addition of a HANDLE command would mean not having to worry about an extra type, and could be used for animated strips, sounds, streams, and probably other things too, giving a lot more flexibility
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

PeeJay

The thing is, I am not trying to turn GL into Blitz (or I may as well just carry on using Blitz!) but rather looking at the best features of Blitz and suggesting incorporating them into GL
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

Thunor

I use something similar to the above :-
Code (glbasic) Select
GLOBAL IMG_MOUSE_POINTER$ = "mouse-pointer.png"
GLOBAL IMG_MOUSE_POINTER_HANDLE = 0
GLOBAL IMG_MOUSE_POINTER_SHADOW$ = "mouse-pointer-shadow.png"
GLOBAL IMG_MOUSE_POINTER_SHADOW_HANDLE = 5
GLOBAL IMG_SPLASH$ = "splash.png"
GLOBAL IMG_SPLASH_HANDLE = 10
GLOBAL image_dir$ = "./data/images/"
...
...
FUNCTION load_media:
LOADSPRITE image_dir$ + IMG_MOUSE_POINTER$, IMG_MOUSE_POINTER_HANDLE
LOADSPRITE image_dir$ + IMG_MOUSE_POINTER_SHADOW$, IMG_MOUSE_POINTER_SHADOW_HANDLE
LOADSPRITE image_dir$ + IMG_SPLASH$, IMG_SPLASH_HANDLE
ENDFUNCTION
I'm using global variables as constants/defines. That's why I made them uppercase.

PeeJay

Which is fine, but declaring global variables all over the place uses up memory - not a big problem on the average desktop machine, but much more critical on portables
www.peejays-remakes.co.uk
For games, remakes, and GL Basic Tutorials
Artificial Intelligence is no match for Natural Stupidity

AndyH

Hey Peejay

I know where you're coming from, I guess it's more an object way of thinking.  I also guess it may require some sizeable changes to the way GLB works.

In the mean time, have you tried Schranz0r's useful code snippet.

Would allow you to do this:


GLOBAL myPlayer = LoadImage("Player.png")

LoadImage goes off to crawl the list of sprites created to find an empty one, then it loads it into it and returns the sprite Id.  You store that in myPlayer var for example, which is not a million miles away from how BMax and other object orientated languages would do it, only you have an Id to a sprite rather than a pointer to an object in memory.

I've expanding this idea for my use, I was adding in support for LoadAnim too, which is what sparked those posts about needing the ZOOM and ROTATE commands for ANIM's.  I've created functions for SETROTATION, SETSCALE and SETALPHA and also SETHANDLE, MIDHANDLEIMAGE and other handy functions.  Just now I'm stuck with ANIM's not yet having the functionality I need for rotate and scale, so it's either wait for them or use POLYVECTOR or (shudder) GRABSPRITE to put everything into it's own sprite and use a manager (like LOADIMAGE above) to also handle my own ANIM 'objects' and never go near Sprite Id's directly.

Thunor

You could very simply wrap LOADSPRITE :-
Code (glbasic) Select
GLOBAL IMAGE_HANDLE_CAT = MYLOADSPRITE("image_cat.png")
GLOBAL IMAGE_HANDLE_DOG = MYLOADSPRITE("image_dog.png")


FUNCTION MYLOADSPRITE: filename$
STATIC image_handle
INC image_handle, 1
LOADSPRITE filename$, image_handle
RETURN image_handle
ENDFUNCTION
...which is what Schranz0r's doing without types.
[EDIT]
Actually this is so much more efficient than what I was doing above I'm implementing it too :)

Schranz0r

Yepp STATIC, you can do that with all Mediafiles..
Nice!
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard