Hi hwnd,
On the setLength front, you should (unless I'm mistaken) always remember to setLength to 0 when you're finished with the array.
Another option you have though is to use a single array in the first place and use a function to generate an index when you access it using x,y,z. If you use your example dimensions, the function could look like this:-
Code:
function getIndex(x,y,z:integer):integer;
begin
result:=(z*256*256)+(y*256)+x;
end;
A faster, possibly more efficient variation on this could be:-
Code:
function getIndexFast(x,y,z:integer):integer;
begin
result:=(z shl 16)+(y shl 8)+x;
end;
Then you use a single dimensional array that is 256x256x8 elements in size.
The exact formula you use could vary to improve performance. For example, lets say you use the items inside three loops like this:-
Code:
for x:=0 to 255 do
begin
for y:=0 to 255 do
begin
for z:=0 to 7 do
begin
// Use item here
end;
end;
end;
Then, to improve performance you could do this:-
Code:
function getBaseIndex(x,y:integer):integer;
begin
result:=(x shl 11)+(y shl 3);
end;
...
var
baseIndex : integer;
...
for x:=0 to 255 do
begin
for y:=0 to 255 do
begin
baseIndex:=getBaseIndex(x,y);
for z:=0 to 7 do
begin
// Use item here
inc(baseIndex);
end;
end;
end;
Of course, if you just need to read through them in a particular order, then you can tailor the 'getIndex' function to cater for that and then simply read through from 0 to 524287. This approach of converting from separate indices to one big number can also be applied to using fully dynamic arrays based on pointers. A while ago I wrote an article that featured a class that represented a map layer. The follow up thread http://www.pascalgamedevelopment.com...hread.php?3640 provides some example code of how to do this if you're interested.
Hope this helps.
Bookmarks