mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Move std::unique_ptr out of LRUCache implementation.
This is an implementation detail of how OpenGLSprites are stored.pull/10004/merge
parent
8bbfbd0347
commit
8c4f8af66e
|
@ -16,12 +16,12 @@
|
|||
/**
|
||||
* Size limited cache with a least recently used eviction strategy.
|
||||
* @tparam Tkey Type of the cache key.
|
||||
* @tparam Tdata Type of the cache item. The cache will store a pointer of this type.
|
||||
* @tparam Tdata Type of the cache item.
|
||||
*/
|
||||
template <class Tkey, class Tdata>
|
||||
class LRUCache {
|
||||
private:
|
||||
typedef std::pair<Tkey, std::unique_ptr<Tdata>> Tpair;
|
||||
typedef std::pair<Tkey, Tdata> Tpair;
|
||||
typedef typename std::list<Tpair>::iterator Titer;
|
||||
|
||||
std::list<Tpair> data; ///< Ordered list of all items.
|
||||
|
@ -51,7 +51,7 @@ public:
|
|||
* @param key Key under which the item should be stored.
|
||||
* @param item Item to insert.
|
||||
*/
|
||||
void Insert(const Tkey key, std::unique_ptr<Tdata> &&item)
|
||||
void Insert(const Tkey key, Tdata &&item)
|
||||
{
|
||||
if (this->Contains(key)) {
|
||||
/* Replace old value. */
|
||||
|
@ -85,13 +85,14 @@ public:
|
|||
* @return The item value.
|
||||
* @note Throws if item not found.
|
||||
*/
|
||||
inline Tdata *Get(const Tkey key)
|
||||
inline const Tdata &Get(const Tkey key)
|
||||
{
|
||||
if (this->lookup.find(key) == this->lookup.end()) throw std::out_of_range("item not found");
|
||||
auto it = this->lookup.find(key);
|
||||
if (it == this->lookup.end()) throw std::out_of_range("item not found");
|
||||
/* Move to front if needed. */
|
||||
this->data.splice(this->data.begin(), this->data, this->lookup[key]);
|
||||
this->data.splice(this->data.begin(), this->data, it->second);
|
||||
|
||||
return this->data.front().second.get();
|
||||
return this->data.front().second;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -1077,7 +1077,7 @@ void OpenGLBackend::DrawMouseCursor()
|
|||
for (const auto &cs : this->cursor_sprites) {
|
||||
/* Sprites are cached by PopulateCursorCache(). */
|
||||
if (this->cursor_cache.Contains(cs.image.sprite)) {
|
||||
OpenGLSprite *spr = this->cursor_cache.Get(cs.image.sprite);
|
||||
const OpenGLSprite *spr = this->cursor_cache.Get(cs.image.sprite).get();
|
||||
|
||||
this->RenderOglSprite(spr, cs.image.pal,
|
||||
this->cursor_pos.x + cs.pos.x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI),
|
||||
|
@ -1089,10 +1089,10 @@ void OpenGLBackend::DrawMouseCursor()
|
|||
|
||||
class OpenGLSpriteAllocator : public SpriteAllocator {
|
||||
public:
|
||||
LRUCache<SpriteID, OpenGLSprite> &lru;
|
||||
OpenGLSpriteLRUCache &lru;
|
||||
SpriteID sprite;
|
||||
|
||||
OpenGLSpriteAllocator(LRUCache<SpriteID, OpenGLSprite> &lru, SpriteID sprite) : lru(lru), sprite(sprite) {}
|
||||
OpenGLSpriteAllocator(OpenGLSpriteLRUCache &lru, SpriteID sprite) : lru(lru), sprite(sprite) {}
|
||||
protected:
|
||||
void *AllocatePtr(size_t) override { NOT_REACHED(); }
|
||||
};
|
||||
|
@ -1274,7 +1274,7 @@ void OpenGLBackend::ReleaseAnimBuffer(const Rect &update_rect)
|
|||
* @param y Y position of the sprite.
|
||||
* @param zoom Zoom level to use.
|
||||
*/
|
||||
void OpenGLBackend::RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
|
||||
void OpenGLBackend::RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom)
|
||||
{
|
||||
/* Set textures. */
|
||||
bool rgb = gl_sprite->BindTextures();
|
||||
|
@ -1518,7 +1518,7 @@ inline Dimension OpenGLSprite::GetSize(ZoomLevel level) const
|
|||
* Bind textures for rendering this sprite.
|
||||
* @return True if the sprite has RGBA data.
|
||||
*/
|
||||
bool OpenGLSprite::BindTextures()
|
||||
bool OpenGLSprite::BindTextures() const
|
||||
{
|
||||
_glActiveTexture(GL_TEXTURE0);
|
||||
_glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA] != 0 ? this->tex[TEX_RGBA] : OpenGLSprite::dummy_tex[TEX_RGBA]);
|
||||
|
|
|
@ -23,6 +23,8 @@ bool HasStringInExtensionList(std::string_view string, std::string_view substrin
|
|||
|
||||
class OpenGLSprite;
|
||||
|
||||
using OpenGLSpriteLRUCache = LRUCache<SpriteID, std::unique_ptr<OpenGLSprite>>;
|
||||
|
||||
/** Platform-independent back-end class for OpenGL video drivers. */
|
||||
class OpenGLBackend : public SpriteEncoder {
|
||||
private:
|
||||
|
@ -58,7 +60,7 @@ private:
|
|||
GLint sprite_rgb_loc = 0; ///< Uniform location for RGB mode flag.
|
||||
GLint sprite_crash_loc = 0; ///< Uniform location for crash remap mode flag.
|
||||
|
||||
LRUCache<SpriteID, OpenGLSprite> cursor_cache; ///< Cache of encoded cursor sprites.
|
||||
OpenGLSpriteLRUCache cursor_cache; ///< Cache of encoded cursor sprites.
|
||||
PaletteID last_sprite_pal = (PaletteID)-1; ///< Last uploaded remap palette.
|
||||
bool clear_cursor_cache = false; ///< A clear of the cursor cache is pending.
|
||||
|
||||
|
@ -74,7 +76,7 @@ private:
|
|||
|
||||
void InternalClearCursorCache();
|
||||
|
||||
void RenderOglSprite(OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom);
|
||||
void RenderOglSprite(const OpenGLSprite *gl_sprite, PaletteID pal, int x, int y, ZoomLevel zoom);
|
||||
|
||||
public:
|
||||
/** Get singleton instance of this class. */
|
||||
|
@ -134,7 +136,7 @@ private:
|
|||
static bool Create();
|
||||
static void Destroy();
|
||||
|
||||
bool BindTextures();
|
||||
bool BindTextures() const;
|
||||
|
||||
public:
|
||||
OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite);
|
||||
|
|
Loading…
Reference in New Issue