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.
|
* Size limited cache with a least recently used eviction strategy.
|
||||||
* @tparam Tkey Type of the cache key.
|
* @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>
|
template <class Tkey, class Tdata>
|
||||||
class LRUCache {
|
class LRUCache {
|
||||||
private:
|
private:
|
||||||
typedef std::pair<Tkey, std::unique_ptr<Tdata>> Tpair;
|
typedef std::pair<Tkey, Tdata> Tpair;
|
||||||
typedef typename std::list<Tpair>::iterator Titer;
|
typedef typename std::list<Tpair>::iterator Titer;
|
||||||
|
|
||||||
std::list<Tpair> data; ///< Ordered list of all items.
|
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 key Key under which the item should be stored.
|
||||||
* @param item Item to insert.
|
* @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)) {
|
if (this->Contains(key)) {
|
||||||
/* Replace old value. */
|
/* Replace old value. */
|
||||||
|
@ -85,13 +85,14 @@ public:
|
||||||
* @return The item value.
|
* @return The item value.
|
||||||
* @note Throws if item not found.
|
* @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. */
|
/* 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) {
|
for (const auto &cs : this->cursor_sprites) {
|
||||||
/* Sprites are cached by PopulateCursorCache(). */
|
/* Sprites are cached by PopulateCursorCache(). */
|
||||||
if (this->cursor_cache.Contains(cs.image.sprite)) {
|
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->RenderOglSprite(spr, cs.image.pal,
|
||||||
this->cursor_pos.x + cs.pos.x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI),
|
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 {
|
class OpenGLSpriteAllocator : public SpriteAllocator {
|
||||||
public:
|
public:
|
||||||
LRUCache<SpriteID, OpenGLSprite> &lru;
|
OpenGLSpriteLRUCache &lru;
|
||||||
SpriteID sprite;
|
SpriteID sprite;
|
||||||
|
|
||||||
OpenGLSpriteAllocator(LRUCache<SpriteID, OpenGLSprite> &lru, SpriteID sprite) : lru(lru), sprite(sprite) {}
|
OpenGLSpriteAllocator(OpenGLSpriteLRUCache &lru, SpriteID sprite) : lru(lru), sprite(sprite) {}
|
||||||
protected:
|
protected:
|
||||||
void *AllocatePtr(size_t) override { NOT_REACHED(); }
|
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 y Y position of the sprite.
|
||||||
* @param zoom Zoom level to use.
|
* @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. */
|
/* Set textures. */
|
||||||
bool rgb = gl_sprite->BindTextures();
|
bool rgb = gl_sprite->BindTextures();
|
||||||
|
@ -1518,7 +1518,7 @@ inline Dimension OpenGLSprite::GetSize(ZoomLevel level) const
|
||||||
* Bind textures for rendering this sprite.
|
* Bind textures for rendering this sprite.
|
||||||
* @return True if the sprite has RGBA data.
|
* @return True if the sprite has RGBA data.
|
||||||
*/
|
*/
|
||||||
bool OpenGLSprite::BindTextures()
|
bool OpenGLSprite::BindTextures() const
|
||||||
{
|
{
|
||||||
_glActiveTexture(GL_TEXTURE0);
|
_glActiveTexture(GL_TEXTURE0);
|
||||||
_glBindTexture(GL_TEXTURE_2D, this->tex[TEX_RGBA] != 0 ? this->tex[TEX_RGBA] : OpenGLSprite::dummy_tex[TEX_RGBA]);
|
_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;
|
class OpenGLSprite;
|
||||||
|
|
||||||
|
using OpenGLSpriteLRUCache = LRUCache<SpriteID, std::unique_ptr<OpenGLSprite>>;
|
||||||
|
|
||||||
/** Platform-independent back-end class for OpenGL video drivers. */
|
/** Platform-independent back-end class for OpenGL video drivers. */
|
||||||
class OpenGLBackend : public SpriteEncoder {
|
class OpenGLBackend : public SpriteEncoder {
|
||||||
private:
|
private:
|
||||||
|
@ -58,7 +60,7 @@ private:
|
||||||
GLint sprite_rgb_loc = 0; ///< Uniform location for RGB mode flag.
|
GLint sprite_rgb_loc = 0; ///< Uniform location for RGB mode flag.
|
||||||
GLint sprite_crash_loc = 0; ///< Uniform location for crash remap 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.
|
PaletteID last_sprite_pal = (PaletteID)-1; ///< Last uploaded remap palette.
|
||||||
bool clear_cursor_cache = false; ///< A clear of the cursor cache is pending.
|
bool clear_cursor_cache = false; ///< A clear of the cursor cache is pending.
|
||||||
|
|
||||||
|
@ -74,7 +76,7 @@ private:
|
||||||
|
|
||||||
void InternalClearCursorCache();
|
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:
|
public:
|
||||||
/** Get singleton instance of this class. */
|
/** Get singleton instance of this class. */
|
||||||
|
@ -134,7 +136,7 @@ private:
|
||||||
static bool Create();
|
static bool Create();
|
||||||
static void Destroy();
|
static void Destroy();
|
||||||
|
|
||||||
bool BindTextures();
|
bool BindTextures() const;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite);
|
OpenGLSprite(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite);
|
||||||
|
|
Loading…
Reference in New Issue