Index: source/graphics/TextureManager.cpp =================================================================== --- source/graphics/TextureManager.cpp +++ source/graphics/TextureManager.cpp @@ -28,6 +28,7 @@ #include "lib/timer.h" #include "maths/MathUtil.h" #include "maths/MD5.h" +#include "ps/Algorithm.h" #include "ps/CacheLoader.h" #include "ps/CLogger.h" #include "ps/ConfigDB.h" @@ -710,22 +711,24 @@ // (Iterating over all textures isn't optimally efficient, but it // doesn't seem to be a problem yet and it's simpler than maintaining // multiple queues.) - for (TextureCache::iterator it = m_TextureCache.begin(); it != m_TextureCache.end(); ++it) + const TextureCache::const_iterator it = PS::Find(m_TextureCache, + CTexture::HIGH_NEEDS_CONVERTING, &CTexture::m_State); + + if (it != m_TextureCache.end()) { - if ((*it)->m_State == CTexture::HIGH_NEEDS_CONVERTING) - { - // Start converting this texture - (*it)->m_State = CTexture::HIGH_IS_CONVERTING; - ConvertTexture(*it); - return true; - } + // Start converting this texture + (*it)->m_State = CTexture::HIGH_IS_CONVERTING; + ConvertTexture(*it); + return true; } } - // Try loading prefetched textures from their cache - for (TextureCache::iterator it = m_TextureCache.begin(); it != m_TextureCache.end(); ++it) { - if ((*it)->m_State == CTexture::PREFETCH_NEEDS_LOADING) + // Try loading prefetched textures from their cache + const TextureCache::const_iterator it = PS::Find(m_TextureCache, + CTexture::PREFETCH_NEEDS_LOADING, &CTexture::m_State); + + if (it != m_TextureCache.end()) { if (TryLoadingCached(*it)) { @@ -742,14 +745,14 @@ // If we've got nothing better to do, then start converting prefetched textures. if (!converterBusy) { - for (TextureCache::iterator it = m_TextureCache.begin(); it != m_TextureCache.end(); ++it) + const TextureCache::const_iterator it = PS::Find(m_TextureCache, + CTexture::PREFETCH_NEEDS_CONVERTING, &CTexture::m_State); + + if (it != m_TextureCache.end()) { - if ((*it)->m_State == CTexture::PREFETCH_NEEDS_CONVERTING) - { - (*it)->m_State = CTexture::PREFETCH_IS_CONVERTING; - ConvertTexture(*it); - return true; - } + (*it)->m_State = CTexture::PREFETCH_IS_CONVERTING; + ConvertTexture(*it); + return true; } } Index: source/ps/Algorithm.h =================================================================== --- /dev/null +++ source/ps/Algorithm.h @@ -0,0 +1,53 @@ +/* Copyright (C) 2022 Wildfire Games. + * This file is part of 0 A.D. + * + * 0 A.D. is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#ifndef INCLUDED_ALGORITHM +#define INCLUDED_ALGORITHM + +#include "precompiled.h" + +#include +#include +#include +#include + +// Implement algorithms which wrap/extend the standard library. + +namespace PS +{ + +struct Identity +{ + template + constexpr T&& operator() (T&& v) + { + return std::forward(v); + } +}; + +template +constexpr auto Find(R&& r, const T& value, Proj proj = {}) +{ + return std::find_if(std::begin(r), std::end(r), [&](auto&& elem) + { + return std::invoke(proj, std::forward(elem)) == value; + }); +} + +} + +#endif // INCLUDED_ALGORITHM