GLBasic forum

Main forum => FAQ => Topic started by: Lord_TNK on 2012-Nov-28

Title: Using pointers for sprites.
Post by: Lord_TNK on 2012-Nov-28
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, ...".
Title: Re: Using pointers for sprites.
Post by: 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
Title: Re: Using pointers for sprites.
Post by: Lord_TNK on 2012-Nov-28
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?
Title: Re: Using pointers for sprites.
Post by: spicypixel on 2012-Nov-28
You can use GLOBAL and the % is to set an integer so that you have 0 and not 0.0 ;)
Title: Re: Using pointers for sprites.
Post by: mentalthink on 2012-Nov-28
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
Title: Re: Using pointers for sprites.
Post by: MrTAToad on 2012-Nov-28
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!)...

Title: Re: Using pointers for sprites.
Post by: Lord_TNK on 2012-Nov-28
Well I'll try this on some of the codes I have and see if it works.
Title: Re: Using pointers for sprites.
Post by: kanonet on 2012-Nov-28
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.
Title: Re: Using pointers for sprites.
Post by: Lord_TNK on 2012-Nov-28
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.
Title: Re: Using pointers for sprites.
Post by: 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:
Title: Re: Using pointers for sprites.
Post by: Lord_TNK on 2012-Nov-28
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.
Title: Re: Using pointers for sprites.
Post by: 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 ;) )
Title: Re: Using pointers for sprites.
Post by: Lord_TNK on 2012-Nov-29
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.
Title: Re: Using pointers for sprites.
Post by: mentalthink on 2012-Nov-29
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:
Title: Re: Using pointers for sprites.
Post by: MrTAToad on 2012-Nov-29
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.
Title: Re: Using pointers for sprites.
Post by: kanonet on 2012-Nov-29
Quote from: siatek on 2012-Nov-29about 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 ;) )
Why waste memory? And integers are still faster than floats, they are just way easier to calculate. And they are not always fully accurate. And you should avoid variable type conversation at all, cause they are slow too. Many commands require you to input integers (array want integers as index too!), like DRAWSPRITE does too. If you input a float in a function that wants integers, every float variable will get converted from float to integer every single time you call it. - Obviously this is a huge waste of speed. If you are able to, you should always use integers and be sure that you select the right type of variable for you needs and check what your functions need as input.

Btw. you should also prefer LOCAL over GLOBAL. 1st this avoids var name collisions in different parts of you project (keep it encapsulated!) and LOCALs are a tiny bit faster than GLOBALs too.
Title: Re: Using pointers for sprites.
Post by: fuzzy70 on 2012-Nov-29
What would be handy is more data types, for example we have commands to read BYTE/UBYTE/WORD/UWORD etc from files but as soon as you read a byte into a integer it jumps from being an 8bit value to a 32bit one.

However, how it could me implemented is another matter & may just cause confusion or problems as we have $/%/# used already so extra symbols would have to be used. I get round it by using AND/OR & ASL/ASR although have no idea what performance loss it takes (might be very small).

With regards to memory mobile devices are (on general) way behind desktop pc's with the amount of available ram, some android devices only having 256mb/512mb & with all the running processes like messaging etc whats left can be pretty small.

Lee
Title: Re: Using pointers for sprites.
Post by: kanonet on 2012-Nov-29
At the moment we have 4 native GLB data types: integer (%), float (#), string ($) and 64-bit integer (called int64, no short symbol), + custom Types.

But you can also use C data types, like:
Code (glbasic) Select
LOCAL a AS char
So you can already save memory in GLB. But keep in mind that you may lose speed if you use them to calc or input in functions with different datatypes, cause you would force type conversations.
Title: Re: Using pointers for sprites.
Post by: Lord_TNK on 2012-Nov-29
Quote from: kanonet on 2012-Nov-29
Quote from: siatek on 2012-Nov-29about 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 ;) )
Why waste memory? And integers are still faster than floats, they are just way easier to calculate. And they are not always fully accurate. And you should avoid variable type conversation at all, cause they are slow too. Many commands require you to input integers (array want integers as index too!), like DRAWSPRITE does too. If you input a float in a function that wants integers, every float variable will get converted from float to integer every single time you call it. - Obviously this is a huge waste of speed. If you are able to, you should always use integers and be sure that you select the right type of variable for you needs and check what your functions need as input.

Btw. you should also prefer LOCAL over GLOBAL. 1st this avoids var name collisions in different parts of you project (keep it encapsulated!) and LOCALs are a tiny bit faster than GLOBALs too.

1. I would keep pointers different anyway, since it wouldn't make sense to give different bitmaps the same names regardless.

2. I think bitmaps that are to be used for most of the game would best be given pointers globally, while sprites in certain situations could be local (such as enemies just for certain areas.