News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

combine two words to make an array?

Started by stevmjon, January 26, 2012, 01:05:41 AM

Previous topic - Next topic

stevmjon

call me dumbo, but i forgot how to do this.

been searching the forums, but can't find the example program i saw a while ago with this in it.

eg. need "anim" + "01" to become an array anim01(x,x)

  thanks for any help,  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#1
 You can't do that.

What are you trying to do ?

 

 Here's a bit of example that creates a manager of sorts that uses NAMES (Strings) rather than an numeric reference like an INDEX or pointer.

PlayBASIC Code: [Select]
   Type tAnimationSystem
Name$
NameLC$
NameHash
Handle
FrameCount
CurrentFrame
EndType

Dim Animations(0) as tAnimationSystem


// -----------------------------------------------------
// Make some junk empty animations
// -----------------------------------------------------

for lp=0 to 10
Anim_Tommy =NewAnimation("Tommy"+str$(lp))
next

// -----------------------------------------------------
// Make animation and fill with of some frames
// -----------------------------------------------------
Anim_Bill =NewAnimation("Bill")
For angle =0 to 180 step 15
Cls
Circle 50,50,50*sin(angle),true
ThisIMage=getFreeImage()
GetImage ThisImage,0,0,100,100
AddAnimationFrame("BILL",ThisImage)
next


// -----------------------------------------------------
// Run a bit if a demo
// -----------------------------------------------------

Setfps 30
Do
Cls $102030

DrawAnimationFrame("Bill",MouseX(),MouseY())

print Fps()
Sync
loop






// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// >> New Animation <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


Function NewAnimation(Name$)

; check if this animation already exists ? but anyway
; it if does,it just dumps an error to the debug console

Index=FindAnimationNameIndex(Name$)
if Index=0

; Get a free posiiotn in the array for this resource
index =GetFreeCell(Animations())

; make the lower case version of the Name ID$
NameLC$=Lower$(Name$)

; Store the name as is/ in lower case and make a search hash out of it
Animations(Index).name$ =Name$
Animations(Index).nameLC$ =NameLC$
Animations(index).NameHash =zMakeNameHash(NameLC$)

; create a 1D integer array to hold the frames in this animation
Animations(index).Handle =zMakeFrameArray()

; the number of frames in this animation
Animations(Index).frameCount =0

; current frame in animation set
Animations(Index).Currentframe=0

else
#print "Error - Animation Name ["+Name$+"] ALready Exists "
endif

EndFunction Index




// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// >> Draw Animation Frame <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Function DrawAnimationFrame(Name$,Xpos,Ypos)
Index=FindAnimationNameIndex(Name$)
if Index
FrameCount=Animations(index).frameCount

if FrameCount

CurrentFrame=Animations(index).CurrentFRame

MakeArray LocalFrames()
LocalFrames()=Animations(index).Handle

DrawImage LocalFrames(CurrentFrame),Xpos,Ypos,True

Animations(index).CurrentFRame=Mod(CurrentFRame+1,FrameCount)


endif

endif
EndFunction



// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// >> Add Animation Frame <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Function AddAnimationFrame(Name$,ImageIndex)
Index=FindAnimationNameIndex(Name$)
if Index

MakeArray LocalFrames()
LocalFrames()=Animations(index).Handle

FrameCount=Animations(index).frameCount

if FrameCount+1=>GetArrayElements(localFrames())
redim localFrames(FrameCount+128)
endif

localFrames(FrameCount)=IMageIndex
Animations(index).frameCount=FrameCount+1

Login required to view complete source code


  The trouble with such an approach, is that looking up the "NAME" and constantly converting back to a reference can be costly when put under heavy stress.  There are ways around that, but really bypassing the bottle neck is better if you can.

  A simple modification would be to offer ways of accessing the material via Name or via it's reference, so you'd look up where the material is initially by name,  then access it directly.    Using names is very useful though, in particular when externalizing our data from the program.    So a character description might refer to an animation, or a movement path by name.    Which is uses in the Thesius XIII demo that comes with PB..  

  This one is a slight tweak to remove the Name to ID look ups
PlayBASIC Code: [Select]
   Type tAnimationSystem
Name$
NameLC$
NameHash
Handle
FrameCount
CurrentFrame
EndType

Dim Animations(0) as tAnimationSystem


// -----------------------------------------------------
// Make some junk empty animations
// -----------------------------------------------------

for lp=0 to 10
Anim_Tommy =NewAnimation("Tommy"+str$(lp))
next

// -----------------------------------------------------
// Make animation and fill with of some frames
// -----------------------------------------------------
Anim_Bill =NewAnimation("Bill")
For angle =0 to 180 step 15
Cls
Circle 50,50,50*sin(angle),true
ThisIMage=getFreeImage()
GetImage ThisImage,0,0,100,100
AddAnimationFrame(Anim_Bill,ThisImage)
next


// -----------------------------------------------------
// Run a bit if a demo
// -----------------------------------------------------

Setfps 30
Do
Cls $102030

DrawAnimationFrame(Anim_Bill,MouseX(),MouseY())

print Fps()
Sync
loop






// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// >> New Animation <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------


Function NewAnimation(Name$)

; check if this animation already exists ? but anyway
; it if does,it just dumps an error to the debug console

Index=FindAnimationNameIndex(Name$)
if Index=0

; Get a free posiiotn in the array for this resource
index =GetFreeCell(Animations())

; make the lower case version of the Name ID$
NameLC$=Lower$(Name$)

; Store the name as is/ in lower case and make a search hash out of it
Animations(Index).name$ =Name$
Animations(Index).nameLC$ =NameLC$
Animations(index).NameHash =zMakeNameHash(NameLC$)

; create a 1D integer array to hold the frames in this animation
Animations(index).Handle =zMakeFrameArray()

; the number of frames in this animation
Animations(Index).frameCount =0

; current frame in animation set
Animations(Index).Currentframe=0

else
#print "Error - Animation Name ["+Name$+"] ALready Exists "
endif

EndFunction Index




// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// >> Draw Animation Frame <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Function DrawAnimationFrame(Index,Xpos,Ypos)
if LegalAnimationIndex(index)
FrameCount=Animations(index).frameCount

if FrameCount

CurrentFrame=Animations(index).CurrentFRame

MakeArray LocalFrames()
LocalFrames()=Animations(index).Handle

DrawImage LocalFrames(CurrentFrame),Xpos,Ypos,True

Animations(index).CurrentFRame=Mod(CurrentFRame+1,FrameCount)


endif

endif
EndFunction



// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// >> Add Animation Frame <<
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------

Function AddAnimationFrame(Index,ImageIndex)
if LegalAnimationIndex(index)

MakeArray LocalFrames()
LocalFrames()=Animations(index).Handle

FrameCount=Animations(index).frameCount

if FrameCount+1=>GetArrayElements(localFrames())
redim localFrames(FrameCount+128)
endif

localFrames(FrameCount)=IMageIndex
Animations(index).frameCount=FrameCount+1

else
Login required to view complete source code



 or you can do something like this, which i suspect is what you're thinking of..   Which is method of using an array stub to access data from another array.  Here we're declaring a bunch of arrays and then processing them all via a for/next loop.  

PlayBASIC Code: [Select]
   Dim Anim1(10)
Dim Anim2(20)
Dim Anim3(30)
Dim Anim4(40)
Dim Anim5(50)

For lp=GetArray(Anim1()) to GetArray(Anim5())

MakeArray ReferenceArray()
SetArray ReferenceArray(),lp

print GetarrayElements(ReferenceArray())

next

Sync
waitkey










stevmjon

#2
QuoteWhat are you trying to do ?

basically, i want to create seperate arrays automatically for storing sets of image numbers.
i want to keep the 'anim_' but change the '01' , '02' , '03' , '04' etc as i need them.

> beetle walk :   anim_01(20)
> beetle turn :    anim_02(6)
> mine throw :   anim_03(10)
> explosion    :   anim_04(30)

i have been doing this manually at the moment. i don't want to do this. i want the program to automatically create each array as it needs it, so i don't have to keep adding them manually in the code here and there in the correct place.

i used to do this automatically in my older code using a big array. i allowed max of 30 frames, and set an array to store all of it. eg. anim_frames(x,30). i kept the frame numbers in the 2nd dimension, and just re-dimmed the 1st dimension to add more characters. but, this wasted a lot of array space. eg. some animations would use 10 elements out of possible 30 elements (so 20 elements will never be used/filled). i could use this again, but i thought it wasted unused space in the big array.

thanks for your reply, i will read through your examples.

 stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#3
 I'd probably use something similar to the STRING NAME approach posted above.   So the animation data can live externally to the program code and more importantly characters can refer to animations by name in their external declarations.   None of this stuff really needs to be built into the program per-say.

So the animation declarations might just be a text file, set out in the structure with some keywords.  Text is ideal for this as it lets us add/edit animation data, without changing a single line of program code.  

Ie.


; --------------------------------------------
; Animation Declarations
; --------------------------------------------

beetle walk
{
  FileName="Beetle Walk"
  FileType="png"
  Frames=16
  Speed = 45
}




Now the game engine loads the text into memory, runs through it grabbing the data initializing the material in the internal animation array.   It's generally best to keep the data in such structures as minimal as possible.  So the program, constructs say the actual file name of the material for example.  

Characters can  be defined much the same way, but for animations or perhaps sounds etc, we use the material name.  This way if items share a material, we don't need to change the items if a change to that materials is required, we change the material.    Anyway, so the character loader reads the data in, then looks up the materials it uses by name.  Beyond this point, we refer to the material by it's media index/pointer rather than name.  So the naming stuff is purely for human convenience at design time.


Example,  Entity Example (Loading Game Levels)


stevmjon

thanks kev.  i haven't done a lot with loading & saving files. now i have more options.

  stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin


well, rather than store this data inside the program...  save yourself some heartache and write a little no thrills editor to set the properties of your character data say.   Then just include the  loader routine in your game.