Author Topic: Using pointers for sprites.  (Read 4239 times)

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Using pointers for sprites.
« 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, ...".

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Using pointers for sprites.
« Reply #1 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
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Using pointers for sprites.
« Reply #2 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?

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Re: Using pointers for sprites.
« Reply #3 on: 2012-Nov-28 »
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.

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3356
  • Integrated Brain
    • View Profile
Re: Using pointers for sprites.
« Reply #4 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

MrTAToad

  • Guest
Re: Using pointers for sprites.
« Reply #5 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!)...


Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Using pointers for sprites.
« Reply #6 on: 2012-Nov-28 »
Well I'll try this on some of the codes I have and see if it works.

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Using pointers for sprites.
« Reply #7 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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Using pointers for sprites.
« Reply #8 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.
« Last Edit: 2012-Nov-28 by Lord_TNK »

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3356
  • Integrated Brain
    • View Profile
Re: Using pointers for sprites.
« Reply #9 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:

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Using pointers for sprites.
« Reply #10 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.

Offline siatek

  • Mr. Drawsprite
  • **
  • Posts: 58
    • View Profile
Re: Using pointers for sprites.
« Reply #11 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 ;) )
glbasic roxx !!!

Offline Lord_TNK

  • Mr. Drawsprite
  • **
  • Posts: 54
    • View Profile
Re: Using pointers for sprites.
« Reply #12 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.

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3356
  • Integrated Brain
    • View Profile
Re: Using pointers for sprites.
« Reply #13 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:

MrTAToad

  • Guest
Re: Using pointers for sprites.
« Reply #14 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.