From f62f72818721649da3c7325efbda7485d7182f6b Mon Sep 17 00:00:00 2001 From: frosch Date: Sat, 26 Apr 2025 20:53:50 +0200 Subject: [PATCH] Codechange: Move usage of ResolverObjects to newgrf_xxx.cpp (#14116) --- src/newgrf_house.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/newgrf_house.h | 1 + src/newgrf_roadstop.cpp | 9 +++++++++ src/newgrf_roadstop.h | 3 +++ src/station_cmd.cpp | 7 +++---- src/town_gui.cpp | 39 --------------------------------------- 6 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/newgrf_house.cpp b/src/newgrf_house.cpp index 52cb437590..bcaf36395d 100644 --- a/src/newgrf_house.cpp +++ b/src/newgrf_house.cpp @@ -514,6 +514,45 @@ void DrawNewHouseTile(TileInfo *ti, HouseID house_id) } } +/** + * Draw representation of a house tile for GUI purposes. + * @param x Position x of image. + * @param y Position y of image. + * @param spec House spec to draw. + * @param house_id House ID to draw. + * @param view The house's 'view'. + */ +void DrawNewHouseTileInGUI(int x, int y, const HouseSpec *spec, HouseID house_id, int view) +{ + HouseResolverObject object(house_id, INVALID_TILE, nullptr, CBID_NO_CALLBACK, 0, 0, true, view); + const SpriteGroup *group = object.Resolve(); + if (group == nullptr || group->type != SGT_TILELAYOUT) return; + + uint8_t stage = TOWN_HOUSE_COMPLETED; + const DrawTileSprites *dts = reinterpret_cast(group)->ProcessRegisters(&stage); + + PaletteID palette = GetColourPalette(spec->random_colour[0]); + if (spec->callback_mask.Test(HouseCallbackMask::Colour)) { + uint16_t callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, nullptr, INVALID_TILE, true, view); + if (callback != CALLBACK_FAILED) { + /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */ + palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback; + } + } + + SpriteID image = dts->ground.sprite; + PaletteID pal = dts->ground.pal; + + if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage; + if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage; + + if (GB(image, 0, SPRITE_WIDTH) != 0) { + DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y); + } + + DrawNewGRFTileSeqInGUI(x, y, dts, stage, palette); +} + /* Simple wrapper for GetHouseCallback to keep the animation unified. */ uint16_t GetSimpleHouseCallback(CallbackID callback, uint32_t param1, uint32_t param2, const HouseSpec *spec, Town *town, TileIndex tile, CargoTypes extra_data) { diff --git a/src/newgrf_house.h b/src/newgrf_house.h index 1201ff9054..bfdea4ddfb 100644 --- a/src/newgrf_house.h +++ b/src/newgrf_house.h @@ -98,6 +98,7 @@ void DecreaseBuildingCount(Town *t, HouseID house_id); std::span GetBuildingHouseIDCounts(); void DrawNewHouseTile(TileInfo *ti, HouseID house_id); +void DrawNewHouseTileInGUI(int x, int y, const HouseSpec *spec, HouseID house_id, int view); void AnimateNewHouseTile(TileIndex tile); /* see also: void TriggerHouseAnimation_TileLoop(TileIndex tile, uint16_t random_bits) */ void TriggerHouseAnimation_ConstructionStageChanged(TileIndex tile, bool first_call); diff --git a/src/newgrf_roadstop.cpp b/src/newgrf_roadstop.cpp index 1d0dc82641..f0700b1ebb 100644 --- a/src/newgrf_roadstop.cpp +++ b/src/newgrf_roadstop.cpp @@ -21,6 +21,7 @@ #include "window_type.h" #include "timer/timer_game_calendar.h" #include "town.h" +#include "tile_cmd.h" #include "viewport_func.h" #include "newgrf_animation_base.h" #include "newgrf_sound.h" @@ -349,6 +350,14 @@ 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) +{ + RoadStopResolverObject object(spec, st, ti->tile, INVALID_ROADTYPE, type, view); + const SpriteGroup *group = object.Resolve(); + if (group == nullptr || group->type != SGT_TILELAYOUT) return nullptr; + return static_cast(group); +} + /** Wrapper for animation control, see GetRoadStopCallback. */ uint16_t GetAnimRoadStopCallback(CallbackID callback, uint32_t param1, uint32_t param2, const RoadStopSpec *roadstopspec, BaseStation *st, TileIndex tile, int) { diff --git a/src/newgrf_roadstop.h b/src/newgrf_roadstop.h index 13ac38fed1..f7b61e7e1b 100644 --- a/src/newgrf_roadstop.h +++ b/src/newgrf_roadstop.h @@ -21,6 +21,8 @@ #include "newgrf_town.h" #include "road.h" +struct TileInfo; + /** The maximum amount of roadstops a single GRF is allowed to add */ static const int NUM_ROADSTOPS_PER_GRF = UINT16_MAX - 1; @@ -169,6 +171,7 @@ struct RoadStopSpec : NewGRFSpecBase { using RoadStopClass = NewGRFClass; +const TileLayoutSpriteGroup *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/station_cmd.cpp b/src/station_cmd.cpp index 2ffc87d2fa..a2d32442e6 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3328,16 +3328,15 @@ draw_default_foundation: if (stopspec != nullptr) { stop_draw_mode = stopspec->draw_mode; st = BaseStation::GetByTile(ti->tile); - RoadStopResolverObject object(stopspec, st, ti->tile, INVALID_ROADTYPE, type, view); - const SpriteGroup *group = object.Resolve(); - if (group != nullptr && group->type == SGT_TILELAYOUT) { + const TileLayoutSpriteGroup *group = GetRoadStopLayout(ti, stopspec, st, type, view); + if (group != nullptr) { 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; } - t = ((const TileLayoutSpriteGroup *)group)->ProcessRegisters(nullptr); + t = group->ProcessRegisters(nullptr); } } diff --git a/src/town_gui.cpp b/src/town_gui.cpp index f075577823..f3f6f3ed3b 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -1334,45 +1334,6 @@ void InitializeTownGui() _town_local_authority_kdtree.Clear(); } -/** - * Draw representation of a house tile for GUI purposes. - * @param x Position x of image. - * @param y Position y of image. - * @param spec House spec to draw. - * @param house_id House ID to draw. - * @param view The house's 'view'. - */ -void DrawNewHouseTileInGUI(int x, int y, const HouseSpec *spec, HouseID house_id, int view) -{ - HouseResolverObject object(house_id, INVALID_TILE, nullptr, CBID_NO_CALLBACK, 0, 0, true, view); - const SpriteGroup *group = object.Resolve(); - if (group == nullptr || group->type != SGT_TILELAYOUT) return; - - uint8_t stage = TOWN_HOUSE_COMPLETED; - const DrawTileSprites *dts = reinterpret_cast(group)->ProcessRegisters(&stage); - - PaletteID palette = GetColourPalette(spec->random_colour[0]); - if (spec->callback_mask.Test(HouseCallbackMask::Colour)) { - uint16_t callback = GetHouseCallback(CBID_HOUSE_COLOUR, 0, 0, house_id, nullptr, INVALID_TILE, true, view); - if (callback != CALLBACK_FAILED) { - /* If bit 14 is set, we should use a 2cc colour map, else use the callback value. */ - palette = HasBit(callback, 14) ? GB(callback, 0, 8) + SPR_2CCMAP_BASE : callback; - } - } - - SpriteID image = dts->ground.sprite; - PaletteID pal = dts->ground.pal; - - if (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) image += stage; - if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += stage; - - if (GB(image, 0, SPRITE_WIDTH) != 0) { - DrawSprite(image, GroundSpritePaletteTransform(image, pal, palette), x, y); - } - - DrawNewGRFTileSeqInGUI(x, y, dts, stage, palette); -} - /** * Draw a house that does not exist. * @param x Position x of image.