From 2cb389946da14f400bfd3f809704357999c31dc3 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 21 Mar 2025 19:55:32 +0000 Subject: [PATCH] Codechange: Move GrfProcessingState to separate file. Allows reuse by other files. --- src/newgrf.cpp | 116 +------------------------------ src/newgrf/CMakeLists.txt | 1 + src/newgrf/newgrf_internal.h | 130 +++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 114 deletions(-) create mode 100644 src/newgrf/newgrf_internal.h diff --git a/src/newgrf.cpp b/src/newgrf.cpp index b5ed37dc67..4274167dfe 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -55,6 +55,7 @@ #include "road.h" #include "newgrf_roadstop.h" #include "newgrf/newgrf_bytereader.h" +#include "newgrf/newgrf_internal.h" #include "table/strings.h" #include "table/build_industry.h" @@ -87,120 +88,7 @@ static uint32_t _ttdpatch_flags[8]; /** Indicates which are the newgrf features currently loaded ingame */ GRFLoadedFeatures _loaded_newgrf_features; -static const uint MAX_SPRITEGROUP = UINT8_MAX; ///< Maximum GRF-local ID for a spritegroup. - -/** Temporary data during loading of GRFs */ -struct GrfProcessingState { -private: - /** Definition of a single Action1 spriteset */ - struct SpriteSet { - SpriteID sprite; ///< SpriteID of the first sprite of the set. - uint num_sprites; ///< Number of sprites in the set. - }; - - /** Currently referenceable spritesets */ - std::map spritesets[GSF_END]; - -public: - /* Global state */ - GrfLoadingStage stage; ///< Current loading stage - SpriteID spriteid; ///< First available SpriteID for loading realsprites. - - /* Local state in the file */ - SpriteFile *file; ///< File of currently processed GRF file. - GRFFile *grffile; ///< Currently processed GRF file. - GRFConfig *grfconfig; ///< Config of the currently processed GRF file. - uint32_t nfo_line; ///< Currently processed pseudo sprite number in the GRF. - - /* Kind of return values when processing certain actions */ - int skip_sprites; ///< Number of pseudo sprites to skip before processing the next one. (-1 to skip to end of file) - - /* Currently referenceable spritegroups */ - std::array spritegroups{}; - - /** Clear temporary data before processing the next file in the current loading stage */ - void ClearDataForNextFile() - { - this->nfo_line = 0; - this->skip_sprites = 0; - - for (uint i = 0; i < GSF_END; i++) { - this->spritesets[i].clear(); - } - - this->spritegroups = {}; - } - - /** - * Records new spritesets. - * @param feature GrfSpecFeature the set is defined for. - * @param first_sprite SpriteID of the first sprite in the set. - * @param first_set First spriteset to define. - * @param numsets Number of sets to define. - * @param numents Number of sprites per set to define. - */ - void AddSpriteSets(uint8_t feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents) - { - assert(feature < GSF_END); - for (uint i = 0; i < numsets; i++) { - SpriteSet &set = this->spritesets[feature][first_set + i]; - set.sprite = first_sprite + i * numents; - set.num_sprites = numents; - } - } - - /** - * Check whether there are any valid spritesets for a feature. - * @param feature GrfSpecFeature to check. - * @return true if there are any valid sets. - * @note Spritesets with zero sprites are valid to allow callback-failures. - */ - bool HasValidSpriteSets(uint8_t feature) const - { - assert(feature < GSF_END); - return !this->spritesets[feature].empty(); - } - - /** - * Check whether a specific set is defined. - * @param feature GrfSpecFeature to check. - * @param set Set to check. - * @return true if the set is valid. - * @note Spritesets with zero sprites are valid to allow callback-failures. - */ - bool IsValidSpriteSet(uint8_t feature, uint set) const - { - assert(feature < GSF_END); - return this->spritesets[feature].find(set) != this->spritesets[feature].end(); - } - - /** - * Returns the first sprite of a spriteset. - * @param feature GrfSpecFeature to query. - * @param set Set to query. - * @return First sprite of the set. - */ - SpriteID GetSprite(uint8_t feature, uint set) const - { - assert(IsValidSpriteSet(feature, set)); - return this->spritesets[feature].find(set)->second.sprite; - } - - /** - * Returns the number of sprites in a spriteset - * @param feature GrfSpecFeature to query. - * @param set Set to query. - * @return Number of sprites in the set. - */ - uint GetNumEnts(uint8_t feature, uint set) const - { - assert(IsValidSpriteSet(feature, set)); - return this->spritesets[feature].find(set)->second.num_sprites; - } -}; - -static GrfProcessingState _cur; - +GrfProcessingState _cur; /** * Helper to check whether an image index is valid for a particular NewGRF vehicle. diff --git a/src/newgrf/CMakeLists.txt b/src/newgrf/CMakeLists.txt index 42314e960a..b6f8d9129c 100644 --- a/src/newgrf/CMakeLists.txt +++ b/src/newgrf/CMakeLists.txt @@ -1,4 +1,5 @@ add_files( newgrf_bytereader.cpp newgrf_bytereader.h + newgrf_internal.h ) diff --git a/src/newgrf/newgrf_internal.h b/src/newgrf/newgrf_internal.h new file mode 100644 index 0000000000..35d07a9d25 --- /dev/null +++ b/src/newgrf/newgrf_internal.h @@ -0,0 +1,130 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file newgrf_internal.h NewGRF internal processing state. */ + +#ifndef NEWGRF_INTERNAL_H +#define NEWGRF_INTERNAL_H + +#include "../newgrf.h" +#include "../spriteloader/sprite_file_type.hpp" + +static constexpr uint MAX_SPRITEGROUP = UINT8_MAX; ///< Maximum GRF-local ID for a spritegroup. + +/** Temporary data during loading of GRFs */ +struct GrfProcessingState { +private: + /** Definition of a single Action1 spriteset */ + struct SpriteSet { + SpriteID sprite; ///< SpriteID of the first sprite of the set. + uint num_sprites; ///< Number of sprites in the set. + }; + + /** Currently referenceable spritesets */ + std::map spritesets[GSF_END]; + +public: + /* Global state */ + GrfLoadingStage stage; ///< Current loading stage + SpriteID spriteid; ///< First available SpriteID for loading realsprites. + + /* Local state in the file */ + SpriteFile *file; ///< File of currently processed GRF file. + GRFFile *grffile; ///< Currently processed GRF file. + GRFConfig *grfconfig; ///< Config of the currently processed GRF file. + uint32_t nfo_line; ///< Currently processed pseudo sprite number in the GRF. + + /* Kind of return values when processing certain actions */ + int skip_sprites; ///< Number of pseudo sprites to skip before processing the next one. (-1 to skip to end of file) + + /* Currently referenceable spritegroups */ + std::array spritegroups{}; + + /** Clear temporary data before processing the next file in the current loading stage */ + void ClearDataForNextFile() + { + this->nfo_line = 0; + this->skip_sprites = 0; + + for (uint i = 0; i < GSF_END; i++) { + this->spritesets[i].clear(); + } + + this->spritegroups = {}; + } + + /** + * Records new spritesets. + * @param feature GrfSpecFeature the set is defined for. + * @param first_sprite SpriteID of the first sprite in the set. + * @param first_set First spriteset to define. + * @param numsets Number of sets to define. + * @param numents Number of sprites per set to define. + */ + void AddSpriteSets(uint8_t feature, SpriteID first_sprite, uint first_set, uint numsets, uint numents) + { + assert(feature < GSF_END); + for (uint i = 0; i < numsets; i++) { + SpriteSet &set = this->spritesets[feature][first_set + i]; + set.sprite = first_sprite + i * numents; + set.num_sprites = numents; + } + } + + /** + * Check whether there are any valid spritesets for a feature. + * @param feature GrfSpecFeature to check. + * @return true if there are any valid sets. + * @note Spritesets with zero sprites are valid to allow callback-failures. + */ + bool HasValidSpriteSets(uint8_t feature) const + { + assert(feature < GSF_END); + return !this->spritesets[feature].empty(); + } + + /** + * Check whether a specific set is defined. + * @param feature GrfSpecFeature to check. + * @param set Set to check. + * @return true if the set is valid. + * @note Spritesets with zero sprites are valid to allow callback-failures. + */ + bool IsValidSpriteSet(uint8_t feature, uint set) const + { + assert(feature < GSF_END); + return this->spritesets[feature].find(set) != this->spritesets[feature].end(); + } + + /** + * Returns the first sprite of a spriteset. + * @param feature GrfSpecFeature to query. + * @param set Set to query. + * @return First sprite of the set. + */ + SpriteID GetSprite(uint8_t feature, uint set) const + { + assert(IsValidSpriteSet(feature, set)); + return this->spritesets[feature].find(set)->second.sprite; + } + + /** + * Returns the number of sprites in a spriteset + * @param feature GrfSpecFeature to query. + * @param set Set to query. + * @return Number of sprites in the set. + */ + uint GetNumEnts(uint8_t feature, uint set) const + { + assert(IsValidSpriteSet(feature, set)); + return this->spritesets[feature].find(set)->second.num_sprites; + } +}; + +extern GrfProcessingState _cur; + +#endif /* NEWGRF_INTERNAL_H */