BlitzScript3D

Blitz3D Forums/Blitz3D Programming/BlitzScript3D

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Multi-Dimensional Arrays Are Coming! I've work out the math to convert multiple indexes into a linear index for use with Blitz Arrays (ie: mytype.alien[100]). The code below pertains to the BlitzScript3D Engine but, may be useful for others.

This code snippet, illustrates the formula that will compute the offset into memory for an array with any number of dimensions:
Graphics 1024,768
n=Input("Number of Array Dimensions: ")
ascii= 96
element$=Chr(ascii+loop+1)
dimensions$="a"
For loop = 1 To n
	Print "Dim Array("+dimensions+")"
	Print "linear_index=base_index+"+String("(",loop)+"a_index)" + expressions$ + "*element_size_in_bytes"
	Print 
	element$=Chr(ascii+loop+1) 
	expression$="*"+element$+"_size+"+element$+"_index)"
	expressions$=expressions$+expression$
	dimensions$=dimensions$+","+element$
Next
Application of the formula in the BlitzScript3D Engine.
;bscvm Array
;supports up to 4 dimensions using Row Major Ordering Formula 
; @ webster.cs.ucr.edu/AoA/Windows/HTML/Arraysa2.html
;There is a generic formula that will compute the offset into memory For an array 
;with any number of dimensions, however, you'll rarely use more than four. 


Global blitzscriptVar[65535] ;a single dimension array

Type bscvmArray
	Field name$
	Field i_size
	Field j_size
	Field k_size
	Field l_size
	Field head
	Field tail
End Type

Function bscvmArrayCreate.bscvmArray(i,j=0,k=0,l=0)
	this.bscvmArray = New bscvmArray
	this\i_size=i+1
	this\j_size=j+1
	this\k_size=k+1
	this\l_size=l+1
	this\head=0
	this\tail=this\head + this\i_size * this\j_size * this\k_size * this\l_size
	Return this
End Function

Function bscvmArrayGet(this.bscvmArray,i_index,j_index=0,k_index=0,l_index=0)
	Return blitzscriptVar[this\head+(((i_index * this\j_size+j_index) * this\k_size+k_index) * this\l_size+l_index)]
End Function

Function bscvmArraySet(this.bscvmArray,i_index,j_index=0,k_index=0,l_index=0,value$=0)
	blitzscriptVar[this\head+(((i_index * this\j_size+j_index) * this\k_size+k_index) * this\l_size+l_index)]=value
End Function

Function bscvmArrayLinearIndexGet(this.bscvmArray,i_index,j_index=0,k_index=0,l_index=0)
	Return this\head+(((i_index * this\j_size+j_index) * this\k_size+k_index) * this\l_size+l_index)
End Function

Function bscvmArrayTest()

	this.bscvmArray=bscvmArrayCreate(3,3,3,3)
	
	For i =  0 To 3
	For j =  0 To 3
	For k =  0 To 3
	For l =  0 To 3
		
	DebugLog(i+","+j+","+k+","+l+"= "+m+" /  "+bscvmArrayLinearIndexGet(this,i,j,k,l))
	m=m+1
	
	Next
	Next
	Next
	Next
	DebugLog "tail="+this\tail
	
End Function

bscvmArrayTest()