GLBasic forum

Main forum => FAQ => Topic started by: spicypixel on 2011-Jun-07

Title: Obvious error but I can't see it =)
Post by: spicypixel on 2011-Jun-07
Can someone tell me how I can loop properly through the second array type. I'ts gonna be an obvious syntax error but I just can't see it lol.

Code (glbasic) Select

// Type declaration
TYPE Sprite
     SpriteX
     SpriteY
     SpriteFrame
ENDTYPE
GLOBAL working_sprite[] AS Sprite
GLOBAL failing_sprite[] AS Sprite

// tile array data storage
DIM tiles[8][8]

// sprite type data storage
DIM working_sprite[64]
DIM failing_sprite[8][8]

// Build the Array and Sprites for the Type
spritecount = 0
FOR y = 0 TO 7
FOR x = 0 TO 7

tiles[x][y] = RND(7)

working_sprite[spritecount].SpriteX = (x * 10)
working_sprite[spritecount].SpriteY = (y * 10)
working_sprite[spritecount].SpriteFrame = tiles[x][y]
spritecount = spritecount + 1

failing_sprite[x][y].SpriteX = (x * 10)
failing_sprite[x][y].SpriteY = (y * 10)
failing_sprite[x][y].SpriteFrame = tiles[x][y]

NEXT
NEXT

// Display Tile Info
FOREACH spr IN working_sprite[]
PRINT spr.SpriteFrame, spr.SpriteX , spr.SpriteY
NEXT

FOREACH spr IN failing_sprite[]
PRINT spr.SpriteFrame, spr.SpriteX + 100, spr.SpriteY
NEXT

SHOWSCREEN
MOUSEWAIT

Title: Re: Obvious error but I can't see it =)
Post by: Kitty Hello on 2011-Jun-07
foreach only works for 1D arrays. You mean failing_sprite, right?
Title: Re: Obvious error but I can't see it =)
Post by: spicypixel on 2011-Jun-07
Yeh that's exactly what I mean. Guess I will need to change the way I do it ;) Cheers Gernot!
Title: Re: Obvious error but I can't see it =)
Post by: bigsofty on 2011-Jun-07
Not very neat but something like...

Code (glbasic) Select
LOCAL x%,y%
FOR x = 0 TO BOUNDS(failing_sprite[],0)-1
FOR y = 0 TO BOUNDS(failing_sprite[],1)-1
PRINT failing_sprite[x][y].SpriteFrame, failing_sprite[x][y].SpriteX + 100, failing_sprite[x][y].SpriteY
NEXT
NEXT
Title: Re: Obvious error but I can't see it =)
Post by: spicypixel on 2011-Jun-07
Quote from: bigsofty on 2011-Jun-07
Not very neat but something like...

Code (glbasic) Select
LOCAL x%,y%
FOR x = 0 TO BOUNDS(failing_sprite[],0)-1
FOR y = 0 TO BOUNDS(failing_sprite[],1)-1
PRINT failing_sprite[x][y].SpriteFrame, failing_sprite[x][y].SpriteX + 100, failing_sprite[x][y].SpriteY
NEXT
NEXT


It's exactly what Ive written :D
Title: Re: Obvious error but I can't see it =)
Post by: Slydog on 2011-Jun-07
Or, you could have 'failing_sprite' have only one dimension, like 'working_sprite' does.
If for some reason you need to know the x/y for the 'failing_sprite' DIFFERENT than the ones defined in TYPE Sprite, you could add another TYPE variable.

I don't know how you are using it, so this may not be doable.
Title: Re: Obvious error but I can't see it =)
Post by: matchy on 2011-Jun-07
It seems to me that there is more to syntax in the example code which doesn't seem right to me. :S Generally, sprites (as in a bitmap) don't have x & y plots and are usually reused unlike objects like enemies that can draw the same sprite in a loop.

So the types should be like this example:
Code (glbasic) Select

TYPE sprite as sprites
num
width
height
file_name$
ENDTYPE

TYPE enemy as enemies
x
y
width
height
sprite_num
frame
frames
speed
angle
ENDTYPE
Title: Re: Obvious error but I can't see it =)
Post by: Slydog on 2011-Jun-07
I agree, a sprite (as used by GLB) doesn't have a location.  Calling it a 'bitmap' would be more accurate.
And, an enemy DOES have a location, and a bitmap, so your TYPES make sense.

Personally I would love to embed the sprite object directly into the enemy type like below.
But since each enemy sprite is a COPY of 'sp_enemy' in this example (and not a reference to) it could become memory or resource expensive if your sprite type expands (bad example maybe, but my fonts/gui had this problem)

Code (glbasic) Select
TYPE TSprite
num
width
height
file_name$
ENDTYPE

TYPE TEnemy
x
y
width
height
sprite AS TSprite
frame
frames
speed
angle
ENDTYPE

GLOBAL enemy[100] AS TEnemy
GLOBAL sp_enemy AS TSprite
enemy[0].sprite = sp_enemy
enemy[1].sprite = sp_enemy
Title: Re: Obvious error but I can't see it =)
Post by: matchy on 2011-Jun-07
You are right about the memory. All you need is the sprite num to reference index (pointer) to. No need to duplicate the whole sprite.

(Not tested)
Code (glbasic) Select

TYPE TSprite
num
width
height
file_name$
ENDTYPE

TYPE TEnemy
x
y
width
height
sprite
frame
frames
speed
angle
ENDTYPE

TYPE TBitmap
green // enemy names
blue
red
boss
dino
tank
ENDTYPE

GLOBAL sprite[] as TSprite
GLOBAL enemy[] AS TEnemy
GLOBAL Bitmap as TBitmap

Bitmap.red = add_sprite("red.png")
Bitmap.green = add_sprite("green.png")
Bitmap.tank = add_sprite("tank.png")

DIM enemy[5]

// these can be reset at anytime.
enemy[0].sprite_num = Bitmap.red
enemy[1].sprite_num = Bitmap.green
enemy[2].sprite_num = Bitmap.tank
enemy[3].sprite_num = Bitmap.green
enemy[4].sprite_num = Bitmap.green

FUNCTION add_sprite: file_name$
LOCAL sprites = BOUNDS(sprite[], 0)
REDIM sprite[sprites + 1]
sprite[sprites].id = sprites
sprite[sprites].file_name$ = file_name$
sprite[sprites].num = GENSPRITE()
IF DOESFILEEXIST(sprite[sprites].file_name$)
LOADSPRITE sprite[sprites].file_name$, sprite[sprites].num
GETSPRITESIZE sprite[sprites].num, sprite[sprites].width, sprite[sprites].height
ELSE
debug_print("ERROR: Sprite not found: " + sprite[sprites].file_name$)
ENDIF
RETURN sprites
ENDFUNCTION