2D z-buffer sorting (2D in D3D9 C#)

Started by
2 comments, last by deadimp 18 years, 4 months ago
I'm creating a tile-based RPG with Direct3D9 and C# using the Sprite interface and have size 32x48 sprites and 32x32 tiles. I would like to draw the sprites based off their y value (bottom draws first) so I can support overlapping sprites. Until I heard of the z-buffer sorting method, I manually compared all sprites and sorted them which is tedious. I enabled the z-buffer and passed the y value as the z value in my draw function and noticed that the buffer only handles values from 0 to 1. I also noticed the higher the z value, the further it's drawn, which is the opposite of what I need. I heard that transparency wouldn't work if I sorted my sprites in this way (not sure if it's true). If anyone knows how to solve this, or a better way to accomplish this, I'm all ears. Thanks.
Advertisement
Hi

I'm not using z buffer sorting. You can achieve same effect drawing further tiles first and closest tiles last. Same for sprites.
This you can get with two for loops. However, disabling z-buffer in directx you get noticable speed improvements. Just try to disable z-buffering in any D3D application and look at fps counter.

....int tilex = 48, tiley =12; // size in pixel of our tile// tiles visible onscreen#define	MAP_DRAW_WIDTH	(19)#define MAP_DRAW_HEIGHT	(50)//tile coordinate on mapint tMapDrawX = 0;int tMapDrawY = 0;for(int y = 0; y<MAP_DRAW_HEIGHT;y++){		 for(int x = 0; x<=MAP_DRAW_WIDTH;x++)  {// POINT g_ptDrawOffset is scroll offset of tile 0,0int tMapX = g_ptDrawOffset.x+tMapDrawX+x; //draw coordinates int tMapY = g_ptDrawOffset.y+tMapDrawY+x;int index = 0;index = (tMapY * m_Width) + tMapX; // MAPDATA[index] store values of tilesint draw_xpos, draw_ypos;if(CHECKEVEN(y)) draw_xpos = g_ptScrollOffset.x+x*xoffset;else draw_xpos = g_ptScrollOffset.x+x*xoffset-(xoffset/2);			draw_ypos = g_ptScrollOffset.y+y*yoffset;// custom iso draw funcionBlitIso(m_pDevice, draw_xpos,draw_ypos,48,24,0xffffffff);}if(CHECKEVEN(tMapDrawX+tMapDrawY)) tMapDrawX--;else tMapDrawY++;}and....static inline bool CHECKEVEN(int num){	if(num/2 == (float)num/2.0f)		return true;	return false;}


Hope this helps...
thats not a good idea,
z buffering is inefficent and the blending wont work right with zbuffering alone
Wait... Comparing/sorting out sprites isn't a tedious task, if you know what to use.
All I did was simply use STL's "sort()" method, and created an operator overload for the "graphical" objects. (However, I used STL's "vector" also)
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

This topic is closed to new replies.

Advertisement