Original Post
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:
Any help is appreciated; Thanks!
// 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; }
};