From 74030a63e6af495b9125d4f94c97e03ecadbf7df Mon Sep 17 00:00:00 2001 From: frosch Date: Mon, 5 May 2025 21:11:44 +0200 Subject: [PATCH] Codechange: Move SpriteLayoutProcessor's operations closer to the ResolverObject. --- src/newgrf_airporttiles.cpp | 9 ++++----- src/newgrf_house.cpp | 9 ++++----- src/newgrf_industrytiles.cpp | 9 ++++----- src/newgrf_object.cpp | 10 +++++----- src/newgrf_roadstop.cpp | 6 ++++-- src/newgrf_roadstop.h | 2 +- src/newgrf_station.cpp | 17 +++++++++++++---- src/newgrf_station.h | 1 + src/station_cmd.cpp | 11 ++++------- 9 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/newgrf_airporttiles.cpp b/src/newgrf_airporttiles.cpp index f5b37a1706..ed6e2d7203 100644 --- a/src/newgrf_airporttiles.cpp +++ b/src/newgrf_airporttiles.cpp @@ -241,11 +241,8 @@ uint16_t GetAirportTileCallback(CallbackID callback, uint32_t param1, uint32_t p return object.ResolveCallback(); } -static void AirportDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, Colours colour) +static void AirportDrawTileLayout(const TileInfo *ti, const DrawTileSpriteSpan &dts, Colours colour) { - auto processor = group->ProcessRegisters(nullptr); - auto dts = processor.GetLayout(); - SpriteID image = dts.ground.sprite; SpriteID pal = dts.ground.pal; @@ -279,7 +276,9 @@ bool DrawNewAirportTile(TileInfo *ti, Station *st, const AirportTileSpec *airts) return false; } - AirportDrawTileLayout(ti, group, Company::Get(st->owner)->colour); + auto processor = group->ProcessRegisters(nullptr); + auto dts = processor.GetLayout(); + AirportDrawTileLayout(ti, dts, Company::Get(st->owner)->colour); return true; } diff --git a/src/newgrf_house.cpp b/src/newgrf_house.cpp index d5feda3f7d..8376ca6a6e 100644 --- a/src/newgrf_house.cpp +++ b/src/newgrf_house.cpp @@ -461,11 +461,8 @@ uint16_t GetHouseCallback(CallbackID callback, uint32_t param1, uint32_t param2, return object.ResolveCallback(); } -static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, uint8_t stage, HouseID house_id) +static void DrawTileLayout(const TileInfo *ti, const DrawTileSpriteSpan &dts, uint8_t stage, HouseID house_id) { - auto processor = group->ProcessRegisters(&stage); - auto dts = processor.GetLayout(); - const HouseSpec *hs = HouseSpec::Get(house_id); PaletteID palette = GetColourPalette(hs->random_colour[TileHash2Bit(ti->x, ti->y)]); if (hs->callback_mask.Test(HouseCallbackMask::Colour)) { @@ -510,7 +507,9 @@ void DrawNewHouseTile(TileInfo *ti, HouseID house_id) if (group != nullptr) { /* Limit the building stage to the number of stages supplied. */ uint8_t stage = GetHouseBuildingStage(ti->tile); - DrawTileLayout(ti, group, stage, house_id); + auto processor = group->ProcessRegisters(&stage); + auto dts = processor.GetLayout(); + DrawTileLayout(ti, dts, stage, house_id); } } diff --git a/src/newgrf_industrytiles.cpp b/src/newgrf_industrytiles.cpp index 0976178fdd..d5db7331fd 100644 --- a/src/newgrf_industrytiles.cpp +++ b/src/newgrf_industrytiles.cpp @@ -158,11 +158,8 @@ uint32_t IndustryTileResolverObject::GetDebugID() const return GetIndustryTileSpec(gfx)->grf_prop.local_id; } -static void IndustryDrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, Colours rnd_colour, uint8_t stage) +static void IndustryDrawTileLayout(const TileInfo *ti, const DrawTileSpriteSpan &dts, Colours rnd_colour, uint8_t stage) { - auto processor = group->ProcessRegisters(&stage); - auto dts = processor.GetLayout(); - SpriteID image = dts.ground.sprite; PaletteID pal = dts.ground.pal; @@ -211,7 +208,9 @@ bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const Indus /* Limit the building stage to the number of stages supplied. */ uint8_t stage = GetIndustryConstructionStage(ti->tile); - IndustryDrawTileLayout(ti, group, i->random_colour, stage); + auto processor = group->ProcessRegisters(&stage); + auto dts = processor.GetLayout(); + IndustryDrawTileLayout(ti, dts, i->random_colour, stage); return true; } diff --git a/src/newgrf_object.cpp b/src/newgrf_object.cpp index 36d46b7a64..a1ccfbbcf6 100644 --- a/src/newgrf_object.cpp +++ b/src/newgrf_object.cpp @@ -439,13 +439,11 @@ uint16_t GetObjectCallback(CallbackID callback, uint32_t param1, uint32_t param2 /** * Draw an group of sprites on the map. * @param ti Information about the tile to draw on. - * @param group The group of sprites to draw. + * @param dts The sprite layout to draw. * @param spec Object spec to draw. */ -static void DrawTileLayout(const TileInfo *ti, const TileLayoutSpriteGroup *group, const ObjectSpec *spec) +static void DrawTileLayout(const TileInfo *ti, const DrawTileSpriteSpan &dts, const ObjectSpec *spec) { - auto processor = group->ProcessRegisters(nullptr); - auto dts = processor.GetLayout(); PaletteID palette = (spec->flags.Test(ObjectFlag::Uses2CC) ? SPR_2CCMAP_BASE : PALETTE_RECOLOUR_START) + Object::GetByTile(ti->tile)->colour; SpriteID image = dts.ground.sprite; @@ -477,7 +475,9 @@ void DrawNewObjectTile(TileInfo *ti, const ObjectSpec *spec) const auto *group = object.Resolve(); if (group == nullptr) return; - DrawTileLayout(ti, group, spec); + auto processor = group->ProcessRegisters(nullptr); + auto dts = processor.GetLayout(); + DrawTileLayout(ti, dts, spec); } /** diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index 82c3d30c6a..057c43ae3e 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -344,10 +344,12 @@ void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, DrawCommonTileSeqInGUI(x, y, &dts, 0, 0, palette, true); } -const TileLayoutSpriteGroup *GetRoadStopLayout(TileInfo *ti, const RoadStopSpec *spec, BaseStation *st, StationType type, int view) +std::optional GetRoadStopLayout(TileInfo *ti, const RoadStopSpec *spec, BaseStation *st, StationType type, int view) { RoadStopResolverObject object(spec, st, ti->tile, INVALID_ROADTYPE, type, view); - return object.Resolve(); + auto group = object.Resolve(); + if (group == nullptr) return std::nullopt; + return group->ProcessRegisters(nullptr); } /** Wrapper for animation control, see GetRoadStopCallback. */ diff --git a/src/newgrf_roadstop.h b/src/newgrf_roadstop.h index df6845cf46..2c6275cc8e 100644 --- a/src/newgrf_roadstop.h +++ b/src/newgrf_roadstop.h @@ -163,7 +163,7 @@ struct RoadStopSpec : NewGRFSpecBase { using RoadStopClass = NewGRFClass; -const TileLayoutSpriteGroup *GetRoadStopLayout(TileInfo *ti, const RoadStopSpec *spec, BaseStation *st, StationType type, int view); +std::optional GetRoadStopLayout(TileInfo *ti, const RoadStopSpec *spec, BaseStation *st, StationType type, int view); void DrawRoadStopTile(int x, int y, RoadType roadtype, const RoadStopSpec *spec, StationType type, int view); uint16_t GetRoadStopCallback(CallbackID callback, uint32_t param1, uint32_t param2, const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, RoadType roadtype, StationType type, uint8_t view); diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index 16dd6ee8a4..11b424cf46 100644 --- a/src/newgrf_station.cpp +++ b/src/newgrf_station.cpp @@ -627,6 +627,18 @@ SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st return group->sprite - SPR_RAIL_PLATFORM_Y_FRONT; } +void GetCustomStationRelocation(SpriteLayoutProcessor &processor, const StationSpec *statspec, BaseStation *st, TileIndex tile) +{ + StationResolverObject object(statspec, st, tile, CBID_NO_CALLBACK); + for (uint8_t var10 : processor.Var10Values()) { + object.ResetState(); + object.callback_param1 = var10; + const auto *group = object.Resolve(); + if (group == nullptr || group->num_sprites == 0) continue; + processor.ProcessRegisters(var10, group->sprite - SPR_RAIL_PLATFORM_Y_FRONT); + } +} + /** * Resolve the sprites for custom station foundations. * @param statspec Station spec @@ -820,10 +832,7 @@ bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID /* Sprite layout which needs preprocessing */ bool separate_ground = statspec->flags.Test(StationSpecFlag::SeparateGround); processor = SpriteLayoutProcessor(*layout, total_offset, rti->fallback_railtype, 0, 0, separate_ground); - for (uint8_t var10 : processor.Var10Values()) { - uint32_t var10_relocation = GetCustomStationRelocation(statspec, nullptr, INVALID_TILE, var10); - processor.ProcessRegisters(var10, var10_relocation); - } + GetCustomStationRelocation(processor, statspec, nullptr, INVALID_TILE); tmp_rail_layout = processor.GetLayout(); sprites = &tmp_rail_layout; total_offset = 0; diff --git a/src/newgrf_station.h b/src/newgrf_station.h index ff16084364..323cb14e92 100644 --- a/src/newgrf_station.h +++ b/src/newgrf_station.h @@ -208,6 +208,7 @@ inline bool IsWaypointClass(const StationClass &cls) uint32_t GetPlatformInfo(Axis axis, uint8_t tile, int platforms, int length, int x, int y, bool centred); SpriteID GetCustomStationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint32_t var10 = 0); +void GetCustomStationRelocation(SpriteLayoutProcessor &processor, const StationSpec *statspec, BaseStation *st, TileIndex tile); SpriteID GetCustomStationFoundationRelocation(const StationSpec *statspec, BaseStation *st, TileIndex tile, uint layout, uint edge_info); uint16_t GetStationCallback(CallbackID callback, uint32_t param1, uint32_t param2, const StationSpec *statspec, BaseStation *st, TileIndex tile); CommandCost PerformStationTileSlopeCheck(TileIndex north_tile, TileIndex cur_tile, const StationSpec *statspec, Axis axis, uint8_t plat_len, uint8_t numtracks); diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index fc4b24d227..ebce57f1aa 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3270,10 +3270,7 @@ draw_default_foundation: /* Sprite layout which needs preprocessing */ bool separate_ground = statspec->flags.Test(StationSpecFlag::SeparateGround); processor = SpriteLayoutProcessor(*layout, total_offset, rti->fallback_railtype, 0, 0, separate_ground); - for (uint8_t var10 : processor.Var10Values()) { - uint32_t var10_relocation = GetCustomStationRelocation(statspec, st, ti->tile, var10); - processor.ProcessRegisters(var10, var10_relocation); - } + GetCustomStationRelocation(processor, statspec, st, ti->tile); tmp_layout = processor.GetLayout(); t = &tmp_layout; total_offset = 0; @@ -3330,15 +3327,15 @@ draw_default_foundation: if (stopspec != nullptr) { stop_draw_mode = stopspec->draw_mode; st = BaseStation::GetByTile(ti->tile); - const TileLayoutSpriteGroup *group = GetRoadStopLayout(ti, stopspec, st, type, view); - if (group != nullptr) { + auto result = GetRoadStopLayout(ti, stopspec, st, type, view); + if (result.has_value()) { if (stopspec->flags.Test(RoadStopSpecFlag::DrawModeRegister)) { stop_draw_mode = static_cast(GetRegister(0x100)); } if (type == StationType::RoadWaypoint && stop_draw_mode.Test(RoadStopDrawMode::WaypGround)) { draw_ground = true; } - processor = group->ProcessRegisters(nullptr); + processor = std::move(*result); tmp_layout = processor.GetLayout(); t = &tmp_layout; }