Jump to content
  • Advertisement
Sign in to follow this  
KingofNoobs

For Loop Mechanics

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

Could anyone tell me if there is anything wrong with initializing "reverse iterators" for reverse list iteration by moving to end() -1 and begin() -1 and then running my for loop in reverse. The code follows:

AtlasBuffers::iterator begin;
AtlasBuffers::iterator end;
if(mFromTop)
{
begin = --mAtlasBuffers.end();
end = --mAtlasBuffers.begin();
}
else
{
begin = mAtlasBuffers.begin();
end = mAtlasBuffers.end();
}
for(; begin != end;)
{
const Atlas * tempAtlas = begin->first;
VIAggregator tempVIAggregator = begin->second;
std::vector<SimpleVertex> tempVertexAggregator = tempVIAggregator.first;
std::vector<uint32_t> tempIndexAggregator = tempVIAggregator.second;

// Only update VRAM buffers if they have changed
if(mVertexBufferChanged)
{
ID3D11Buffer * vertexBuffer = 0;
CreateVertexBuffer(&vertexBuffer, tempVertexAggregator);
unsigned int stride = sizeof(SimpleVertex);
unsigned int offset = 0;
mContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
ReleaseCOM(vertexBuffer);
// Index buffer only changed if vertex buffer changed
if(mIndexBufferChanged)
{
ID3D11Buffer * indexBuffer = 0;
CreateIndexBuffer(&indexBuffer, tempIndexAggregator);
mContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
ReleaseCOM(indexBuffer);
}

}

// Only reset texture if it has changed.
if(tempAtlas != mLastAtlasUsed)
{
mContext->PSSetShaderResources( 0, 1, (ID3D11ShaderResourceView*const*) &(mAtlasBuffers.begin()->first->GetTexture()) );
mLastAtlasUsed = tempAtlas;
}
mContext->DrawIndexed( tempIndexAggregator.size(), 0, 0 );
if(mFromTop)
{
++begin;
}
else
{
--begin;
}

} // for
mFromTop = !mFromTop;

Thanks in advance.

-Dave Ottley Edited by KingofNoobs

Share this post


Link to post
Share on other sites
Advertisement
Steve,

My original intention was to get element rend(), however, the compiler complains because the variable is an iterator, not a reverse_iterator.

Share this post


Link to post
Share on other sites
Doing end()-1 isn't technically legal, so that would be a bad idea. I would refactor all your inner loop code into a separate method and then use completely separate loops for both forward iteration and reverse iteration (using reverse iterators).

Share this post


Link to post
Share on other sites
Zipster et al,

Reimplemented. Is this better:

auto DrawAtlas = [&] (AtlasBufferPair begin)
{
const Atlas * tempAtlas = begin.first;
VIAggregator tempVIAggregator = begin.second;
std::vector<SimpleVertex> tempVertexAggregator = tempVIAggregator.first;
std::vector<uint32_t> tempIndexAggregator = tempVIAggregator.second;

// Only update VRAM buffers if they have changed
if(mVertexBufferChanged)
{
ID3D11Buffer * vertexBuffer = 0;
CreateVertexBuffer(&vertexBuffer, tempVertexAggregator);
unsigned int stride = sizeof(SimpleVertex);
unsigned int offset = 0;
mContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
ReleaseCOM(vertexBuffer);
// Index buffer only changed if vertex buffer changed
if(mIndexBufferChanged)
{
ID3D11Buffer * indexBuffer = 0;
CreateIndexBuffer(&indexBuffer, tempIndexAggregator);
mContext->IASetIndexBuffer(indexBuffer, DXGI_FORMAT_R32_UINT, 0);
ReleaseCOM(indexBuffer);
}

}

// Only reset texture if it has changed.
if(tempAtlas != mLastAtlasUsed)
{
mContext->PSSetShaderResources( 0, 1, (ID3D11ShaderResourceView*const*) &(mAtlasBuffers.begin()->first->GetTexture()) );
mLastAtlasUsed = tempAtlas;
}
// One draw call per atlas
mContext->DrawIndexed( tempIndexAggregator.size(), 0, 0 );
};
if(mFromTop)
{
auto begin = mAtlasBuffers.rbegin();
auto end = mAtlasBuffers.rend();
for(auto begin = mAtlasBuffers.rbegin(); begin != mAtlasBuffers.rend(); ++begin)
{
DrawAtlas(*begin);
}
mFromTop = false;
}
else
{
for(auto begin = mAtlasBuffers.begin();begin != mAtlasBuffers.end(); ++begin)
{
DrawAtlas(*begin);
}
mFromTop = true;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!