Codechange: Store cursor sprites in vector. (#12564)

Combine two separate fixed length arrays to allow simpler iteration.

No need to check that arrays are all the same length.
No need to separately store the number of sprites to draw.
Removes the upper limit of the number of sprites that can be drawn.

Removes lengthof and array indices.
This commit is contained in:
Peter Nelson
2024-04-24 21:26:07 +01:00
committed by GitHub
parent 9121770582
commit 5e689ce25e
5 changed files with 45 additions and 53 deletions

View File

@@ -1074,16 +1074,14 @@ void OpenGLBackend::DrawMouseCursor()
/* Draw cursor on screen */
_cur_dpi = &_screen;
for (uint i = 0; i < this->cursor_sprite_count; ++i) {
SpriteID sprite = this->cursor_sprite_seq[i].sprite;
for (const auto &cs : this->cursor_sprites) {
/* Sprites are cached by PopulateCursorCache(). */
if (this->cursor_cache.Contains(sprite)) {
Sprite *spr = this->cursor_cache.Get(sprite);
if (this->cursor_cache.Contains(cs.image.sprite)) {
Sprite *spr = this->cursor_cache.Get(cs.image.sprite);
this->RenderOglSprite((OpenGLSprite *)spr->data, this->cursor_sprite_seq[i].pal,
this->cursor_pos.x + this->cursor_sprite_pos[i].x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI),
this->cursor_pos.y + this->cursor_sprite_pos[i].y + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI),
this->RenderOglSprite((OpenGLSprite *)spr->data, cs.image.pal,
this->cursor_pos.x + cs.pos.x + UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI),
this->cursor_pos.y + cs.pos.y + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI),
ZOOM_LVL_GUI);
}
}
@@ -1091,9 +1089,6 @@ void OpenGLBackend::DrawMouseCursor()
void OpenGLBackend::PopulateCursorCache()
{
static_assert(lengthof(_cursor.sprite_seq) == lengthof(this->cursor_sprite_seq));
static_assert(lengthof(_cursor.sprite_pos) == lengthof(this->cursor_sprite_pos));
if (this->clear_cursor_cache) {
/* We have a pending cursor cache clear to do first. */
this->clear_cursor_cache = false;
@@ -1103,16 +1098,14 @@ void OpenGLBackend::PopulateCursorCache()
}
this->cursor_pos = _cursor.pos;
this->cursor_sprite_count = _cursor.sprite_count;
this->cursor_in_window = _cursor.in_window;
for (uint i = 0; i < _cursor.sprite_count; ++i) {
this->cursor_sprite_seq[i] = _cursor.sprite_seq[i];
this->cursor_sprite_pos[i] = _cursor.sprite_pos[i];
SpriteID sprite = _cursor.sprite_seq[i].sprite;
this->cursor_sprites.clear();
for (const auto &sc : _cursor.sprites) {
this->cursor_sprites.emplace_back(sc);
if (!this->cursor_cache.Contains(sprite)) {
Sprite *old = this->cursor_cache.Insert(sprite, (Sprite *)GetRawSprite(sprite, SpriteType::Normal, &SimpleSpriteAlloc, this));
if (!this->cursor_cache.Contains(sc.image.sprite)) {
Sprite *old = this->cursor_cache.Insert(sc.image.sprite, (Sprite *)GetRawSprite(sc.image.sprite, SpriteType::Normal, &SimpleSpriteAlloc, this));
if (old != nullptr) {
OpenGLSprite *gl_sprite = (OpenGLSprite *)old->data;
gl_sprite->~OpenGLSprite();