Skip to main content
GameDev.net gamedev.net
🔒 Locked

sorting vectors

Started by Laroche Mar 23, 2003 at 6:17 PM 1 replies 250+ views
Original Post
Laroche
Laroche
I have a Renderer class which is suppose to sort objects and draw them by texture, but im not sure whether it is sorting properly or not due to the fact I''m not sure if this code is broken, or if the sorting code is broken. I''d really appreciate if someone would take a quick peek and tell me if I did anything specifically wrong:
  
// This is in CRenderer.cpp

void CRenderer::Render()
{
	Sort();
	
	bool NewBatch = TRUE;
	bool One = FALSE;
	CTexture * pTexture = 0;
	int Amount = 0;

	std::vector<CObject*> DrawList;
	DrawList.clear();
	
	CObject * pTemp = 0;

	for (int j = 0; j < LAYER; ++j)
	{
		if (DisplayVector[j].size() == 0)
			continue;

		if (DisplayVector[j].size() == 1)
		{
			One = TRUE;
		}
			
		for (std::vector<CObject*>::iterator i = DisplayVector[j].begin(); i != DisplayVector[j].end();
		i++)
		{
			if (One)
			{
				DrawList.push_back((*i));
				pTexture = (*i)->GetTexture();
				pTemp = (*i);
				NewBatch = FALSE;
			}
			
			if (NewBatch)
			{
				pTexture = (*i)->GetTexture();
				DrawList.push_back((*i));
				NewBatch = FALSE;
				One = FALSE;
				pTemp = (*i);
				Amount++;
			}

			if (pTemp->GetTexture()->GetID() == (*i)->GetTexture()->GetID() && !One)
			{
				DrawList.push_back((*i));
				pTemp = (*i);
				Amount++;
				continue;
			}
			
			for (std::vector<CObject*>::iterator i = DrawList.begin(); i != DrawList.end(); i++)
			{
				SpriteVert * pQuad = (*i)->GetSprite()->GetQuad();
				for (int i = 0; i < 6; ++i)
				{
					pVb->AddVertex(pQuad[i]);
				}
			}

			pVb->GetDevice()->SetTexture(0, NULL);
			pVb->GetDevice()->SetTexture(0, pTemp->GetTexture()->GetTexture());
			pVb->ForceDraw();
			pVb->GetDevice()->SetTexture(0, NULL);
			NewBatch = TRUE;
		}
	}
	
	DrawList.clear();
	Clear();
}

void CRenderer::Sort()
{
	for (int i = 0; i < LAYER; ++i)
	{
		std::sort(DisplayVector[i].begin(), DisplayVector[i].end(), less_mag());
	}
}

// This is less_mag() ( it''s modified and taken off of an SGI example)


	struct less_mag : public std::binary_function<CObject*, CObject*, bool> {
		bool operator()(CObject* x, CObject* y) { if (x->GetTexture()->GetID() < y->GetTexture()->GetID()) return 1; return 0; }
    };

  
Any help is appreciated; Thanks!
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
Zipster
Zipster
Sorting looks fine to me.
Laroche
Laroche
Hmm, fixed the problem.. I should be clearing the drawlist when I change to another list, instead of letting it rack up useless vertices.. Guess vector sorting had nothing to do with the problem Thnks anyways
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.