diff --git a/src/base_media_base.h b/src/base_media_base.h index 3ecc4125e1..9cd36ef336 100644 --- a/src/base_media_base.h +++ b/src/base_media_base.h @@ -237,12 +237,22 @@ enum BlitterType { BLT_32BPP, ///< Base set has both 8 bpp and 32 bpp sprites. }; +struct GRFConfig; + /** All data of a graphics set. */ struct GraphicsSet : BaseSet { +private: + mutable std::unique_ptr extra_cfg; ///< Parameters for extra GRF +public: PaletteType palette; ///< Palette of this graphics set BlitterType blitter; ///< Blitter of this graphics set + GraphicsSet(); + ~GraphicsSet(); + bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename); + GRFConfig *GetExtraConfig() const { return this->extra_cfg.get(); } + GRFConfig &GetOrCreateExtraConfig() const; static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir); }; diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index 2f8ac4702c..b2fb5fd70a 100644 --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -192,18 +192,7 @@ static void LoadSpriteTables() ClrBit(master->flags, GCF_INIT_ONLY); /* Baseset extra graphics */ - GRFConfig *extra = new GRFConfig(used_set->files[GFT_EXTRA].filename.c_str()); - - /* We know the palette of the base set, so if the base NewGRF is not - * setting one, use the palette of the base set and not the global - * one which might be the wrong palette for this base NewGRF. - * The value set here might be overridden via action14 later. */ - switch (used_set->palette) { - case PAL_DOS: extra->palette |= GRFP_GRF_DOS; break; - case PAL_WINDOWS: extra->palette |= GRFP_GRF_WINDOWS; break; - default: break; - } - FillGRFDetails(extra, false, BASESET_DIR); + GRFConfig *extra = new GRFConfig(used_set->GetOrCreateExtraConfig()); ClrBit(extra->flags, GCF_INIT_ONLY); extra->next = top; @@ -347,6 +336,17 @@ void GfxLoadSprites() UpdateCursorSize(); } +GraphicsSet::GraphicsSet() + : BaseSet{}, palette{}, blitter{} +{ + // instantiate here, because unique_ptr needs a complete type +} + +GraphicsSet::~GraphicsSet() +{ + // instantiate here, because unique_ptr needs a complete type +} + bool GraphicsSet::FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename) { bool ret = this->BaseSet::FillSetDetails(ini, path, full_filename, false); @@ -365,6 +365,29 @@ bool GraphicsSet::FillSetDetails(const IniFile &ini, const std::string &path, co return ret; } +/** + * Return configuration for the extra GRF, or lazily create it. + * @return NewGRF configuration + */ +GRFConfig &GraphicsSet::GetOrCreateExtraConfig() const +{ + if (!this->extra_cfg) { + this->extra_cfg.reset(new GRFConfig(this->files[GFT_EXTRA].filename)); + + /* We know the palette of the base set, so if the base NewGRF is not + * setting one, use the palette of the base set and not the global + * one which might be the wrong palette for this base NewGRF. + * The value set here might be overridden via action14 later. */ + switch (this->palette) { + case PAL_DOS: this->extra_cfg->palette |= GRFP_GRF_DOS; break; + case PAL_WINDOWS: this->extra_cfg->palette |= GRFP_GRF_WINDOWS; break; + default: break; + } + FillGRFDetails(this->extra_cfg.get(), false, BASESET_DIR); + } + return *this->extra_cfg; +} + /** * Calculate and check the MD5 hash of the supplied GRF. * @param file The file get the hash of.