Skip to content

Commit

Permalink
To avoid vector end iterator dereference (this is undefined behaviour…
Browse files Browse the repository at this point in the history
…). And code simplification.
  • Loading branch information
intorr committed Dec 8, 2017
1 parent 34f1b6a commit 81c93ed
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions src/Layers/xrRender/WallmarksEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ CWallmarksEngine::~CWallmarksEngine()
void CWallmarksEngine::clear()
{
{
for (auto p_it = marks.begin(); p_it != marks.end(); p_it++)
for (auto &p_it : marks)
{
for (auto m_it = (*p_it)->static_items.begin(); m_it != (*p_it)->static_items.end(); m_it++)
static_wm_destroy(*m_it);
xr_delete(*p_it);
for (auto &m_it : p_it->static_items)
static_wm_destroy(m_it);
xr_delete(p_it);
}
marks.clear();
}
Expand Down Expand Up @@ -110,13 +110,13 @@ void CWallmarksEngine::static_wm_render(CWallmarksEngine::static_wallmark* W, FV
int aC = iFloor(a * 255.f);
clamp(aC, 0, 255);
u32 C = color_rgba(128, 128, 128, aC);
FVF::LIT* S = &*W->verts.begin();
FVF::LIT* E = &*W->verts.end();
for (; S != E; S++, V++)

for (auto &i : W->verts)
{
V->p.set(S->p);
V->p.set(i.p);
V->color = C;
V->t.set(S->t);
V->t.set(i.t);
V++;
}
}
//--------------------------------------------------------------------------------
Expand Down Expand Up @@ -255,10 +255,8 @@ void CWallmarksEngine::AddWallmark_internal(
Fbox bb;
bb.invalidate();

FVF::LIT* I = &*W->verts.begin();
FVF::LIT* E = &*W->verts.end();
for (; I != E; I++)
bb.modify(I->p);
for (auto &i : W->verts)
bb.modify(i.p);
bb.getsphere(W->bounds.P, W->bounds.R);
}

Expand All @@ -268,15 +266,13 @@ void CWallmarksEngine::AddWallmark_internal(
wm_slot* slot = FindSlot(hShader);
if (slot)
{
auto it = slot->static_items.begin();
auto end = slot->static_items.end();
for (; it != end; it++)
for (auto &it : slot->static_items)
{
static_wallmark* wm = *it;
static_wallmark* wm = it;
if (wm->bounds.P.similar(W->bounds.P, 0.02f))
{ // replace
static_wm_destroy(wm);
*it = W;
it = W;
return;
}
}
Expand Down Expand Up @@ -394,16 +390,17 @@ void CWallmarksEngine::Render()

lock.Enter(); // Physics may add wallmarks in parallel with rendering

for (auto slot_it = marks.begin(); slot_it != marks.end(); slot_it++)
for (auto &slot_it : marks)
{
u32 w_offset;
FVF::LIT *w_verts, *w_start;
BeginStream(hGeom, w_offset, w_verts, w_start);
wm_slot* slot = *slot_it;
wm_slot* slot = slot_it;

// static wallmarks
for (auto w_it = slot->static_items.begin(); w_it != slot->static_items.end();)
for (auto &w_it : slot->static_items)
{
static_wallmark* W = *w_it;
static_wallmark* W = w_it;
if (RImplementation.ViewBase.testSphere_dirty(W->bounds.P, W->bounds.R))
{
RImplementation.BasicStats.StaticWMCount++;
Expand All @@ -429,12 +426,9 @@ void CWallmarksEngine::Render()
if (W->ttl <= EPS)
{
static_wm_destroy(W);
*w_it = slot->static_items.back();
w_it = slot->static_items.back();
slot->static_items.pop_back();
}
else
{
w_it++;
break;
}
}
// Flush stream
Expand All @@ -443,10 +437,9 @@ void CWallmarksEngine::Render()
BeginStream(hGeom, w_offset, w_verts, w_start);

// dynamic wallmarks
for (xr_vector<intrusive_ptr<CSkeletonWallmark>>::iterator w_it = slot->skeleton_items.begin();
w_it != slot->skeleton_items.end(); w_it++)
for (auto &w_it : slot->skeleton_items)
{
intrusive_ptr<CSkeletonWallmark> W = *w_it;
intrusive_ptr<CSkeletonWallmark> W = w_it;
if (!W)
{
continue;
Expand Down

0 comments on commit 81c93ed

Please sign in to comment.