Drawing Order in 2D Tile Editor

BlitzPlus Forums/BlitzPlus Programming/Drawing Order in 2D Tile Editor

The second option would be the way to go for the linked list method I outlined above.

The problem with conditional level checking as shown by Skn[ac] in the code above is that you end up checking all the tiles multiple times...this isn't bad when you have a small over all number of them...but say you have 10,000 (a 100 by 100 array)...and 17 of them are on the first layer, 45 are on the second, 9,000 on the third, and 54 on the fourth...you end up checking all 10,000 tiles 4 times per update...and thus in the first layer alone with only 17 tiles to draw, means you are wasteing time checking the remaining 9983 tiles...sure this is pretty fast, but you are wasteing time on checking instead of processing other things (like AI, etc..)

However there are simple ways to speed even that up...bounds checking...for each layer you would record the minimum and maximum array locations that include the specific layer in question...

for example if the map is 100 tiles wide by 100 tiles tall you could have something like this for each layer at map load:

Layer1_min_X = 100
Layer1_min_Y = 100
Layer1_max_X = 0
Layer1_max_y = 0

then run through the tiles in the array...if the tile is on the particular layer then check if the array X/Y location is within those bounds:

For X = 0 to 100
For Y = 0 to 100

;obviously do this for each layer
If map.tile(x,y)\layer = 1 then
If X < level1_min_X then level1_min_x = x
If y < level1_min_y then level1_min_y = y
If X > level1_max_X then level1_max_x = x
If y > level1_max_y then level1_max_y = y
end if

next
next


then when you draw, you can do it as Skn[ac] showed except for each layer you set up the for/next loops to start at minX/Y to maxX/Y...something like:

For X = Level1_min_X to Level1_max_X
For Y = Level1)min_Y to Level1_max_Y

if map.tile(x,y)\layer = 1 then draw tile

next
next

this will only have you checking for the correct layer in an area of the map that is certain to have tiles in it...meaning you arn't wasteing time checking for the correct layer in potentialy larger areas of the map that won't have tiles to draw right now...

However the linked list method is more efficent in that it directly points to the tiles that need to be drawn.