#### Archived

This topic is now archived and is closed to further replies.

# Render Stack Theory

This topic is 5506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm currently trying to do some sort of render stack/order for a 2D game I'm developing. The only way I can think of to do this is terrebly slow: (I have a sort-value(z-value) from 1-100.) This is my current code:
for i = 1 to 100
for u = 1 to object.ubound
if object.zValue = i then
Render
end if
next u
next i

This nested loop will be very slow. How can this be done in another, better way? I dont know if I can use DX Z-Value somehow. but this is a 2d game and i use the DXSprite interface to draw everything. [edited by - lexor on November 18, 2002 8:48:17 AM]

##### Share on other sites
GRATULATIONS - you got yourself the worst possible solution. Really.

First, why not use Direct3d? 2d Orthoginal projection, order the quadrs by z-order and hammer them out. Yes, you can use z-value in this case.

Second, in the given sample - you can optimise this in two ways:
(a) pre-order the objects. Frankly, they wont get reordered all the time, or?
(b) at least ptut them into "slices" (bavkground, objects etc.) - way less stuff to do.
(c.1) i nthe inner look, colelct the lowest number ABOVE the current one, then advance i to this value (if you an - looks like visual basic here).

DO everything yo ucan to get rid of the STUPID (sorry) nested loop.

Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

##### Share on other sites
Thanks for the replay!
Ahhh... Worst possible situation .. not funny.

Problem is that the character must be able to walk both in front and behind objects. All object are frozen (like trees houses ect.) but that damn character needs to walk wherever he wants.

The game I'm writing here is like the old Monkey Island, Sam and Max, Larry. (from Lucas Arts).
Back then, everything was 2d but the 3d feel was there, and this was much helped by the ability to walk both in front and behind objects.

This is why I would need some sort of Z-Order.
Maybe its possible to just update the character Zpos each frame and leave the rest to the loading-screen .. I donno ..Thanks for your help!

[edited by - lexor on November 18, 2002 9:30:40 AM]

##### Share on other sites
lexor:

If you''re just drawing rectangular sprites, you should either use the Zbuffer and specify z coordinates, or sort your array of sprites by depth before rendering them.

Insertion sort is a good sorting algorithm to use when you only have a few elements moving around in the sorted array.

Thona:
Dude, could you have possibly re-written that post to sound like a bit less of a jerk? That was out of line.

##### Share on other sites
i just finished one of these that i did using layers, as thona sort of hinted at. it worked good enough that it didn''t require dx/ogl - just plain old gdi did the trick.

what i did was assign each "object" to a layer. background had one, water and ground was next, then water edges, then trees, then bushes, etc. the player layer was between the upper/lower part of the trees, houses, etc., so that he walked behind the object if his y value was less than the objects bottom, otherwise he walked in front. then the game score etc as the highest layer.

then i just rendered in a loop, lowest layer to highest, left to right, top to bottom.

##### Share on other sites
and thona, i''m sure the real MS MVPs would appreciate you not soiling their hard earned reputation with posts such as above.

• ### Forum Statistics

• Total Topics
628714
• Total Posts
2984353

• 23
• 11
• 10
• 13
• 14