1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-22 03:42:41 +01:00

Codechange: Use span instead of raw pointer for animated cursors. (#14575)

This allows the terminator entry to be removed.
This commit is contained in:
Peter Nelson
2025-09-02 21:48:24 +01:00
committed by GitHub
parent 7d4282b341
commit 736998b595
4 changed files with 33 additions and 52 deletions

View File

@@ -1659,15 +1659,15 @@ static void SetCursorSprite(CursorID cursor, PaletteID pal)
static void SwitchAnimatedCursor()
{
const AnimCursor *cur = _cursor.animate_cur;
if (cur == nullptr || cur->sprite == AnimCursor::LAST) cur = _cursor.animate_list;
if (_cursor.animate_cur == std::end(_cursor.animate_list)) {
_cursor.animate_cur = std::begin(_cursor.animate_list);
}
assert(!_cursor.sprites.empty());
SetCursorSprite(cur->sprite, _cursor.sprites[0].image.pal);
SetCursorSprite(_cursor.animate_cur->sprite, _cursor.sprites[0].image.pal);
_cursor.animate_timeout = cur->display_time;
_cursor.animate_cur = cur + 1;
_cursor.animate_timeout = _cursor.animate_cur->display_time;
++_cursor.animate_cur;
}
void CursorTick()
@@ -1710,11 +1710,11 @@ void SetMouseCursor(CursorID sprite, PaletteID pal)
* @param table Array of animation states.
* @see SetMouseCursor
*/
void SetAnimatedMouseCursor(const AnimCursor *table)
void SetAnimatedMouseCursor(std::span<const AnimCursor> table)
{
assert(!_cursor.sprites.empty());
_cursor.animate_list = table;
_cursor.animate_cur = nullptr;
_cursor.animate_cur = std::end(table);
_cursor.sprites[0].image.pal = PAL_NONE;
SwitchAnimatedCursor();
}

View File

@@ -169,7 +169,7 @@ void DrawOverlappedWindowForAll(int left, int top, int right, int bottom);
void SetMouseCursorBusy(bool busy);
void SetMouseCursor(CursorID cursor, PaletteID pal);
void SetAnimatedMouseCursor(const AnimCursor *table);
void SetAnimatedMouseCursor(std::span<const AnimCursor> table);
void CursorTick();
void UpdateCursorSize();
bool ChangeResInGame(int w, int h);

View File

@@ -108,8 +108,7 @@ enum WindowKeyCodes : uint16_t {
/** A single sprite of a list of animated cursors */
struct AnimCursor {
static const CursorID LAST = std::numeric_limits<CursorID>::max();
CursorID sprite; ///< Must be set to LAST_ANIM when it is the last sprite of the loop
CursorID sprite; ///< Must be set to LAST_ANIM when it is the last sprite of the loop
uint8_t display_time; ///< Amount of ticks this sprite will be shown
};
@@ -139,8 +138,8 @@ struct CursorVars {
Point draw_pos, draw_size; ///< position and size bounding-box for drawing
const AnimCursor *animate_list; ///< in case of animated cursor, list of frames
const AnimCursor *animate_cur; ///< in case of animated cursor, current frame
std::span<const AnimCursor> animate_list{}; ///< in case of animated cursor, list of frames
std::span<const AnimCursor>::iterator animate_cur = std::end(animate_list); ///< in case of animated cursor, current frame
uint animate_timeout; ///< in case of animated cursor, number of ticks to show the current cursor
bool visible; ///< cursor is visible

View File

@@ -14,67 +14,49 @@
* is to be displayed.
*/
/**
* Creates two array entries that define one
* status of the cursor.
* @param Sprite The Sprite to be displayed
* @param display_time The Number of ticks to display the sprite
*/
#define ANIM_CURSOR_LINE(Sprite, display_time) { Sprite, display_time },
/**
* This indicates the termination of the cursor list
*/
#define ANIM_CURSOR_END() ANIM_CURSOR_LINE(AnimCursor::LAST, 0)
/**
* Animated cursor elements for demolition
*/
static const AnimCursor _demolish_animcursor[] = {
ANIM_CURSOR_LINE(SPR_CURSOR_DEMOLISH_FIRST, 8)
ANIM_CURSOR_LINE(SPR_CURSOR_DEMOLISH_1, 8)
ANIM_CURSOR_LINE(SPR_CURSOR_DEMOLISH_2, 8)
ANIM_CURSOR_LINE(SPR_CURSOR_DEMOLISH_LAST, 8)
ANIM_CURSOR_END()
static constexpr AnimCursor _demolish_animcursor[] = {
{SPR_CURSOR_DEMOLISH_FIRST, 8},
{SPR_CURSOR_DEMOLISH_1, 8},
{SPR_CURSOR_DEMOLISH_2, 8},
{SPR_CURSOR_DEMOLISH_LAST, 8},
};
/**
* Animated cursor elements for lower land
*/
static const AnimCursor _lower_land_animcursor[] = {
ANIM_CURSOR_LINE(SPR_CURSOR_LOWERLAND_FIRST, 10)
ANIM_CURSOR_LINE(SPR_CURSOR_LOWERLAND_1, 10)
ANIM_CURSOR_LINE(SPR_CURSOR_LOWERLAND_LAST, 29)
ANIM_CURSOR_END()
static constexpr AnimCursor _lower_land_animcursor[] = {
{SPR_CURSOR_LOWERLAND_FIRST, 10},
{SPR_CURSOR_LOWERLAND_1, 10},
{SPR_CURSOR_LOWERLAND_LAST, 29},
};
/**
* Animated cursor elements for raise land
*/
static const AnimCursor _raise_land_animcursor[] = {
ANIM_CURSOR_LINE(SPR_CURSOR_RAISELAND_FIRST, 10)
ANIM_CURSOR_LINE(SPR_CURSOR_RAISELAND_1, 10)
ANIM_CURSOR_LINE(SPR_CURSOR_RAISELAND_LAST, 29)
ANIM_CURSOR_END()
static constexpr AnimCursor _raise_land_animcursor[] = {
{SPR_CURSOR_RAISELAND_FIRST, 10},
{SPR_CURSOR_RAISELAND_1, 10},
{SPR_CURSOR_RAISELAND_LAST, 29},
};
/**
* Animated cursor elements for the goto icon
*/
static const AnimCursor _order_goto_animcursor[] = {
ANIM_CURSOR_LINE(SPR_CURSOR_PICKSTATION_FIRST, 10)
ANIM_CURSOR_LINE(SPR_CURSOR_PICKSTATION_1, 10)
ANIM_CURSOR_LINE(SPR_CURSOR_PICKSTATION_LAST, 29)
ANIM_CURSOR_END()
static constexpr AnimCursor _order_goto_animcursor[] = {
{SPR_CURSOR_PICKSTATION_FIRST, 10},
{SPR_CURSOR_PICKSTATION_1, 10},
{SPR_CURSOR_PICKSTATION_LAST, 29},
};
/**
* Animated cursor elements for the build signal icon
*/
static const AnimCursor _build_signals_animcursor[] = {
ANIM_CURSOR_LINE(SPR_CURSOR_BUILDSIGNALS_FIRST, 20)
ANIM_CURSOR_LINE(SPR_CURSOR_BUILDSIGNALS_LAST, 20)
ANIM_CURSOR_END()
static constexpr AnimCursor _build_signals_animcursor[] = {
{SPR_CURSOR_BUILDSIGNALS_FIRST, 20},
{SPR_CURSOR_BUILDSIGNALS_LAST, 20},
};
/**
@@ -82,7 +64,7 @@ static const AnimCursor _build_signals_animcursor[] = {
* definitions we have above. This is the only thing that is
* accessed directly from other files
*/
static const AnimCursor * const _animcursors[] = {
static constexpr std::span<const AnimCursor> _animcursors[] = {
_demolish_animcursor,
_lower_land_animcursor,
_raise_land_animcursor,