OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
13 #include "3rdparty/md5/md5.h"
23 #define SET_TYPE "graphics"
34 _landscape_spriteindexes_arctic,
35 _landscape_spriteindexes_tropic,
36 _landscape_spriteindexes_toyland,
46 static uint
LoadGrfFile(
const std::string &filename, uint load_index,
bool needs_palette_remap)
48 uint load_index_org = load_index;
53 Debug(sprite, 2,
"Reading grf-file '{}'", filename);
56 if (container_ver == 0) UserError(
"Base grf '{}' is corrupt", filename);
58 if (container_ver >= 2) {
60 uint8_t compression = file.
ReadByte();
61 if (compression != 0) UserError(
"Unsupported compression format");
68 UserError(
"Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
71 Debug(sprite, 2,
"Currently {} sprites are loaded", load_index);
73 return load_index - load_index_org;
90 Debug(sprite, 2,
"Reading indexed grf-file '{}'", filename);
93 if (container_ver == 0) UserError(
"Base grf '{}' is corrupt", filename);
95 if (container_ver >= 2) {
97 uint8_t compression = file.
ReadByte();
98 if (compression != 0) UserError(
"Unsupported compression format");
101 while ((start = *index_tbl++) != END) {
102 uint end = *index_tbl++;
108 }
while (++start <= end);
123 Debug(grf, 1,
"Using the {} base graphics set", used_set->
name);
125 std::string error_msg;
126 auto output_iterator = std::back_inserter(error_msg);
129 fmt::format_to(output_iterator,
"Trying to load graphics set '{}', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of README.md.\n\nThe following files are corrupted or missing:\n", used_set->
name);
130 for (
const auto &file : used_set->
files) {
134 fmt::format_to(output_iterator,
"\n");
139 fmt::format_to(output_iterator,
"Trying to load sound set '{}', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of README.md.\n\nThe following files are corrupted or missing:\n", sounds_set->
name);
147 if (!error_msg.empty()) ShowInfoI(error_msg);
189 static const char *master_filename =
"OPENTTD.GRF";
201 master->
next = extra;
207 Debug(sprite, 4,
"Checking sprites from fallback grf");
222 static void RealChangeBlitter(
const std::string_view repl_blitter)
225 if (cur_blitter == repl_blitter)
return;
227 Debug(driver, 1,
"Switching blitter from '{}' to '{}'... ", cur_blitter, repl_blitter);
229 if (new_blitter ==
nullptr) NOT_REACHED();
230 Debug(driver, 1,
"Successfully switched to {}.", repl_blitter);
263 uint depth_wanted_by_grf = _support8bpp !=
S8BPP_NONE ? 8 : 32;
269 if (GetFontAAState()) depth_wanted_by_grf = 32;
272 static const struct {
273 const std::string_view name;
275 uint min_base_depth, max_base_depth, min_grf_depth, max_grf_depth;
276 } replacement_blitters[] = {
277 {
"8bpp-optimized", 2, 8, 8, 8, 8 },
278 {
"40bpp-anim", 2, 8, 32, 8, 32 },
280 {
"32bpp-sse4", 0, 32, 32, 8, 32 },
281 {
"32bpp-ssse3", 0, 32, 32, 8, 32 },
282 {
"32bpp-sse2", 0, 32, 32, 8, 32 },
283 {
"32bpp-sse4-anim", 1, 32, 32, 8, 32 },
285 {
"32bpp-optimized", 0, 8, 32, 8, 32 },
287 {
"32bpp-sse2-anim", 1, 8, 32, 8, 32 },
289 {
"32bpp-anim", 1, 8, 32, 8, 32 },
295 for (
const auto &replacement_blitter : replacement_blitters) {
296 if (animation_wanted && (replacement_blitter.animation == 0))
continue;
297 if (!animation_wanted && (replacement_blitter.animation == 1))
continue;
299 if (!
IsInsideMM(depth_wanted_by_base, replacement_blitter.min_base_depth, replacement_blitter.max_base_depth + 1))
continue;
300 if (!
IsInsideMM(depth_wanted_by_grf, replacement_blitter.min_grf_depth, replacement_blitter.max_grf_depth + 1))
continue;
302 if (replacement_blitter.name == cur_blitter) {
340 GraphicsSet::GraphicsSet()
346 GraphicsSet::~GraphicsSet()
351 bool GraphicsSet::FillSetDetails(
const IniFile &ini,
const std::string &path,
const std::string &full_filename)
356 assert(metadata !=
nullptr);
363 item = metadata->
GetItem(
"blitter");
392 bool GraphicsSet::IsConfigurable()
const
400 void GraphicsSet::CopyCompatibleConfig(
const GraphicsSet &src)
402 const GRFConfig *src_cfg = src.GetExtraConfig();
403 if (src_cfg ==
nullptr || src_cfg->
num_params == 0)
return;
445 size = std::min(size, max_size);
448 uint8_t buffer[1024];
452 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, *f)) != 0 && size != 0) {
454 checksum.Append(buffer, len);
457 checksum.Finish(digest);
462 static const char *
const _graphics_file_names[] = {
"base",
"logos",
"arctic",
"tropical",
"toyland",
"extra" };
465 template <
class T,
size_t Tnum_files,
bool Tsearch_in_tars>
468 template <
class Tbase_set>
473 const Tbase_set *best =
nullptr;
476 if (c->GetNumMissing() != 0)
continue;
478 if (best ==
nullptr ||
479 (best->fallback && !c->fallback) ||
480 best->valid_files < c->valid_files ||
481 (best->valid_files == c->valid_files && (
482 (best->shortname == c->shortname && best->version < c->version) ||
492 template <
class Tbase_set>
uint8_t ReadByte()
Read a byte from the file.
uint8_t _display_opt
What do we want to draw/do?
@ PAL_DOS
Use the DOS palette.
All data of a sounds set.
static void LoadSpriteTables()
Actually load the sprite tables.
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
How all blitters should look like.
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename=true)
Read the set information from a loaded ini.
ChecksumResult
The result of a checksum check.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
std::string name
The name of the base set.
A single "line" in an ini file.
static const size_t NUM_FILES
Number of files in this set.
A group within an ini file.
size_t GRFGetSizeOfDataSection(FileHandle &f)
Get the data section size of a GRF.
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
virtual void ClearSystemSprites()
Clear all cached sprites.
uint8_t landscape
the landscape we're currently in
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
GameCreationSettings game_creation
settings used during the creation of a game (map)
@ GCF_INIT_ONLY
GRF file is processed up to GLS_INIT.
void UpdateCursorSize()
Update cursor dimension.
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
@ CR_MATCH
The file did exist and the md5 checksum did match.
std::string missing_warning
warning when this file is missing
@ DO_FULL_ANIMATION
Perform palette animation.
@ GRFP_GRF_DOS
The NewGRF says the DOS palette can be used.
static const SpriteID *const _landscape_spriteindexes[]
Offsets for loading the different "replacement" sprites in the files.
void LoadNewGRF(uint load_index, uint num_baseset)
Load all the NewGRFs.
uint32_t version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
bool LoadNextSprite(int load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
Information about GRF, used in the game and (part of it) in savegames.
@ MAX_SPRITES
Maximum number of sprites that can be loaded at a given time.
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
@ S8BPP_NONE
No support for 8bpp by OS or hardware, force 32bpp blitters.
void SetParameterDefaults()
Set the default value for all parameters as specified by action14.
static bool SwitchNewGRFBlitter()
Check blitter needed by NewGRF config and switch if needed.
std::optional< std::string > value
The value of this item.
uint _missing_extra_graphics
Number of sprites provided by the fallback extra GRF, i.e. missing in the baseset.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
std::string filename
filename
@ GRFP_BLT_32BPP
The NewGRF prefers a 32 bpp blitter.
ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const
Calculate and check the MD5 hash of the supplied filename.
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
BlitterType blitter
Blitter of this graphics set.
bool IsCompatible(uint32_t old_version) const
Return whether this NewGRF can replace an older version of the same NewGRF.
void QueueOnMainThread(std::function< void()> &&func)
Queue a function to be called on the main thread with game state lock held and video buffer locked.
void CopyParams(const GRFConfig &src)
Copy the parameter information from the src config.
uint8_t GetContainerVersion() const
Get the version number of container type used by the file.
virtual std::string_view GetName()=0
Get the name of the blitter, the same as the Factory-instance returns.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
uint8_t palette
GRFPalette, bitset.
static const char *const _graphics_file_names[]
Names corresponding to the GraphicsFileType.
std::vector< std::optional< GRFParameterInfo > > param_info
NOSAVE: extra information about the parameters.
GRFConfig & GetOrCreateExtraConfig() const
Return configuration for the extra GRF, or lazily create it.
static Blitter * SelectBlitter(const std::string_view name)
Find the requested blitter and return its class.
void CheckBlitter()
Check whether we still use the right blitter, or use another (better) one.
RandomAccessFile with some extra information specific for sprite files.
bool _blitter_autodetected
Was the blitter autodetected or specified by the user?
@ GCS_DISABLED
GRF file is disabled.
uint8_t flags
NOSAVE: GCF_Flags, bitset.
@ GRFP_GRF_WINDOWS
The NewGRF says the Windows palette can be used.
Ini file that supports both loading and saving.
Structure holding filename and MD5 information about a single file.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied GRF.
static void LoadGrfFileIndexed(const std::string &filename, const SpriteID *index_tbl, bool needs_palette_remap)
Load an old fashioned GRF file to replace already loaded sprites.
struct GRFConfig * next
NOSAVE: Next item in the linked list.
static const SpriteID SPR_OPENTTD_BASE
Extra graphic spritenumbers.
PaletteType palette
Palette of this graphics set.
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Information about a single base set.
@ CR_NO_FILE
The file did not exist.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
void GfxLoadSprites()
Initialise and load all the sprites.
std::optional< FileHandle > FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
GRFConfig * _grfconfig
First item in list of current GRF set up.
uint8_t num_params
Number of used parameters.
static uint LoadGrfFile(const std::string &filename, uint load_index, bool needs_palette_remap)
Load an old fashioned GRF file.
void ReadGRFSpriteOffsets(SpriteFile &file)
Parse the sprite section of GRFs.
std::unique_ptr< GRFConfig > extra_cfg
Parameters for extra GRF.
@ PAL_WINDOWS
Use the Windows palette.
int GetNumInvalid() const
Get the number of invalid files.
MD5Hash hash
md5 sum of the file
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied file.
SpriteFile & OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
Open/get the SpriteFile that is cached for use in the sprite cache.
bool FillGRFDetails(GRFConfig *config, bool is_static, Subdirectory subdir)
Find the GRFID of a given grf, and calculate its md5sum.
All data of a graphics set.
uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
Count the sprites which originate from a specific file in a range of SpriteIDs.
MD5File files[NUM_FILES]
All files part of this set.
static BlitterFactory * GetBlitterFactory(const std::string_view name)
Get the blitter factory with the given name.
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.