Using pointers for sprites.

Previous topic - Next topic

Lord_TNK

Not sure if there is a different term for this (as C language uses "pointer" for giving alternate names to variables), but does GLBasic allow assigning an alternate name for images loaded? So far, I have to use number strings for every sprite and animation loaded, and that could get very confusing fast. I'd prefer a code to be able to use something like "DRAWSPRITE yellow_rat, ..." rather than "DRAWSPRITE 374, ...".

kanonet

Of cause you can do this.

Lets say your code looks like this:
Code (glbasic) Select
LOADSPRITE "image.png", 0
DRAWSPRITE 0, 0,0


Then you can replace it with this:
Code (glbasic) Select
LOCAL img% = 0
LOADSPRITE "image.png", img
DRAWSPRITE img, 0,0


Or even do it this way:
Code (glbasic) Select
LOCAL img% = GENSPRITE()
LOADSPRITE "image.png", img
DRAWSPRITE img, 0,0
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Lord_TNK

Quote from: kanonet on 2012-Nov-28
Of cause you can do this.

Lets say your code looks like this:
Code (glbasic) Select
LOADSPRITE "image.png", 0
DRAWSPRITE 0, 0,0


Then you can replace it with this:
Code (glbasic) Select
LOCAL img% = 0
LOADSPRITE "image.png", img
DRAWSPRITE img, 0,0


Or even do it this way:
Code (glbasic) Select
LOCAL img% = GENSPRITE()
LOADSPRITE "image.png", img
DRAWSPRITE img, 0,0

Cool. Firstly, can I do that as a GLOBAL command as well as LOCAL? Second, what does the "%" mean in this case?

spicypixel

You can use GLOBAL and the % is to set an integer so that you have 0 and not 0.0 ;)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

mentalthink

but use % or # I think not it's needed , ins ' it?¿...

I think GLbasic turns Integers and float variables automatically (GLbasic knows what  each variable it's), at least I never declare the variables whit the symbols... but I'm not sure 100%... if I say it's true

MrTAToad

Variables defined without % (or $) are floating-point values.  In most cases it's not a problem (and dont forget that in earlier versions of GLBasic floats and strings were the only variable types allowed!)...


Lord_TNK

Well I'll try this on some of the codes I have and see if it works.

kanonet

Like MrTAToad said if you dont declare it with % it will be float. Everything works fine with floats, but Integers are way faster and uses less memory. So i prefer to use integers as often as possible.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Lord_TNK

#8
Tried it as a GLOBAL command, and it works. Thanks, everyone!

Also, is that code how pointers work in general on GLBasic, or are there different ways for different things? I'll try that out in the code.

mentalthink

Well but this it's a news for me... I always think GLbasic make something internal... I never put it for laziness...

Thanks every day learn a bit more here!!!. THKS  :good:

Lord_TNK

Quote from: mentalthink on 2012-Nov-28
Well but this it's a news for me... I always think GLbasic make something internal... I never put it for laziness...

Thanks every day learn a bit more here!!!. THKS  :good:
Writing good code cannot work if you are lazy.

And the issue here is readability, not less effort (since the names would often take more to type than numbers). It's a lot easier, when looking at code, to understand a name for all loaded sprites than a number for them.

siatek

Im using global values for sprites (sprName). Im thinking about some kind of textures manager but im using only few atlas textures so im to lazy to ;) to make it :)

about the floats in modern hardware i think that there is no speed advantage of use integeres ? am i wrong ?  (and we have a lot of memory right ;) )
glbasic roxx !!!

Lord_TNK

Quote from: siatek on 2012-Nov-29
Im using global values for sprites (sprName). Im thinking about some kind of textures manager but im using only few atlas textures so im to lazy to ;) to make it :)

about the floats in modern hardware i think that there is no speed advantage of use integeres ? am i wrong ?  (and we have a lot of memory right ;) )
But there apparently is a worry when calculating them, since binary often throws them off.

mentalthink

I use GLBasic form 3 or 4 years ago every day, and I use like siatek global names for sprites... something like
global spr_Explosion_01

Never , but never I have a trouble whit nothing, variables, sounds, sprites, textures , objects 3d, nothing... but if the integers require minus memory, I think not it's a hard work pres ALT and the Number 5 in the Keyboard  :P(Spanish Keyboard), and don't waste a lot of time for make this , if it's more correct better than don't make nothing...

But sure I now don't change my thousands of lines of my code...  :D , for the next...  :whistle:

MrTAToad

You can get rounding errors with floating point values with certainly values because the binary value cant be expressed exactly (1/3 causing a problem for example).  This a common problem with all languages.