Codechange: use EnumIndexArray for ZoomLevel-indexed arrays

This commit is contained in:
Peter Nelson
2026-05-20 21:38:26 +01:00
committed by Peter Nelson
parent fa1a5b998e
commit 6c39def287
3 changed files with 16 additions and 15 deletions
+5 -4
View File
@@ -120,9 +120,10 @@ static void StartSound(SoundID sound_id, float pan, uint volume)
MxActivateChannel(mc);
}
static const uint8_t _vol_factor_by_zoom[] = {255, 255, 255, 190, 134, 87};
static_assert(lengthof(_vol_factor_by_zoom) == to_underlying(ZoomLevel::End));
/** Volume scaling for each zoom level. */
static constexpr EnumIndexArray<uint8_t, ZoomLevel, ZoomLevel::End> _vol_factor_by_zoom{
255, 255, 255, 190, 134, 8
};
static const uint8_t _sound_base_vol[] = {
128, 90, 128, 128, 128, 128, 128, 128,
@@ -215,7 +216,7 @@ static void SndPlayScreenCoordFx(SoundID sound, int left, int right, int top, in
StartSound(
sound,
panning,
_vol_factor_by_zoom[to_underlying(vp.zoom)]
_vol_factor_by_zoom[vp.zoom]
);
return;
}
+5 -5
View File
@@ -32,13 +32,13 @@ using SpriteComponents = EnumBitSet<SpriteComponent, uint8_t, SpriteComponent::E
*/
template <class T>
class SpriteCollMap {
std::array<T, to_underlying(ZoomLevel::End)> data{};
EnumIndexArray<T, ZoomLevel, ZoomLevel::End> data{};
public:
inline constexpr T &operator[](const ZoomLevel &zoom) { return this->data[to_underlying(zoom)]; }
inline constexpr const T &operator[](const ZoomLevel &zoom) const { return this->data[to_underlying(zoom)]; }
inline constexpr T &operator[](const ZoomLevel &zoom) { return this->data[zoom]; }
inline constexpr const T &operator[](const ZoomLevel &zoom) const { return this->data[zoom]; }
T &Root() { return this->data[to_underlying(ZoomLevel::Min)]; }
const T &Root() const { return this->data[to_underlying(ZoomLevel::Min)]; }
T &Root() { return this->data[ZoomLevel::Min]; }
const T &Root() const { return this->data[ZoomLevel::Min]; }
};
/** Interface for the loader of our sprites. */
+6 -6
View File
@@ -1556,17 +1556,17 @@ void ViewportSign::UpdatePosition(int center, int top, std::string_view str, std
*/
void ViewportSign::MarkDirty(ZoomLevel maxzoom) const
{
Rect zoomlevels[to_underlying(ZoomLevel::End)];
EnumIndexArray<Rect, ZoomLevel, ZoomLevel::End> zoomlevels;
/* We don't know which size will be drawn, so mark the largest area dirty. */
const uint half_width = std::max(this->width_normal, this->width_small) / 2 + 1;
const uint height = WidgetDimensions::scaled.fullbevel.top + std::max(GetCharacterHeight(FontSize::Normal), GetCharacterHeight(FontSize::Small)) + WidgetDimensions::scaled.fullbevel.bottom + 1;
for (ZoomLevel zoom = ZoomLevel::Begin; zoom != ZoomLevel::End; zoom++) {
zoomlevels[to_underlying(zoom)].left = this->center - ScaleByZoom(half_width, zoom);
zoomlevels[to_underlying(zoom)].top = this->top - ScaleByZoom(1, zoom);
zoomlevels[to_underlying(zoom)].right = this->center + ScaleByZoom(half_width, zoom);
zoomlevels[to_underlying(zoom)].bottom = this->top + ScaleByZoom(height, zoom);
zoomlevels[zoom].left = this->center - ScaleByZoom(half_width, zoom);
zoomlevels[zoom].top = this->top - ScaleByZoom(1, zoom);
zoomlevels[zoom].right = this->center + ScaleByZoom(half_width, zoom);
zoomlevels[zoom].bottom = this->top + ScaleByZoom(height, zoom);
}
for (const Window *w : Window::Iterate()) {
@@ -1575,7 +1575,7 @@ void ViewportSign::MarkDirty(ZoomLevel maxzoom) const
Viewport &vp = *w->viewport;
if (vp.zoom <= maxzoom) {
assert(vp.width != 0);
Rect &zl = zoomlevels[to_underlying(vp.zoom)];
Rect &zl = zoomlevels[vp.zoom];
MarkViewportDirty(vp, zl.left, zl.top, zl.right, zl.bottom);
}
}