13#include "3rdparty/md5/md5.h"
33 _landscape_spriteindexes_arctic,
34 _landscape_spriteindexes_tropic,
35 _landscape_spriteindexes_toyland,
47 SpriteID load_index_org = load_index;
52 Debug(sprite, 2,
"Reading grf-file '{}'", filename);
55 if (container_ver == 0) UserError(
"Base grf '{}' is corrupt", filename);
57 if (container_ver >= 2) {
59 uint8_t compression = file.
ReadByte();
60 if (compression != 0) UserError(
"Unsupported compression format");
67 UserError(
"Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
70 Debug(sprite, 2,
"Currently {} sprites are loaded", load_index);
72 return load_index - load_index_org;
82static void LoadGrfFileIndexed(
const std::string &filename, std::span<
const std::pair<SpriteID, SpriteID>> index_tbl,
bool needs_palette_remap)
88 Debug(sprite, 2,
"Reading indexed grf-file '{}'", filename);
91 if (container_ver == 0) UserError(
"Base grf '{}' is corrupt", filename);
93 if (container_ver >= 2) {
95 uint8_t compression = file.
ReadByte();
96 if (compression != 0) UserError(
"Unsupported compression format");
99 for (
const auto &pair : index_tbl) {
100 for (
SpriteID load_index = pair.first; load_index <= pair.second; ++load_index) {
101 [[maybe_unused]]
bool b =
LoadNextSprite(load_index, file, sprite_id);
82static void LoadGrfFileIndexed(
const std::string &filename, std::span<
const std::pair<SpriteID, SpriteID>> index_tbl,
bool needs_palette_remap) {
…}
119 Debug(grf, 1,
"Using the {} base graphics set", used_set->
name);
121 std::string error_msg;
122 auto output_iterator = std::back_inserter(error_msg);
125 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 1.4 of README.md.\n\nThe following files are corrupted or missing:\n", used_set->
name);
126 for (
const auto &file : used_set->
files) {
130 fmt::format_to(output_iterator,
"\n");
135 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 1.4 of README.md.\n\nThe following files are corrupted or missing:\n", sounds_set->
name);
143 if (!error_msg.empty()) ShowInfoI(error_msg);
152 auto gc = std::make_unique<GRFConfig>(
"OPENTTD.GRF");
166 if (gc->param.empty()) gc->SetParameterDefaults();
210 std::string default_filename = default_extra->filename;
218 Debug(sprite, 4,
"Checking sprites from fallback grf");
231static void RealChangeBlitter(std::string_view repl_blitter)
234 if (cur_blitter == repl_blitter)
return;
236 Debug(driver, 1,
"Switching blitter from '{}' to '{}'... ", cur_blitter, repl_blitter);
238 if (new_blitter ==
nullptr) NOT_REACHED();
239 Debug(driver, 1,
"Successfully switched to {}.", repl_blitter);
272 uint depth_wanted_by_grf = _support8bpp !=
S8BPP_NONE ? 8 : 32;
278 if (GetFontAAState()) depth_wanted_by_grf = 32;
281 static const struct {
282 const std::string_view name;
284 uint min_base_depth, max_base_depth, min_grf_depth, max_grf_depth;
285 } replacement_blitters[] = {
286 {
"8bpp-optimized", 2, 8, 8, 8, 8 },
287 {
"40bpp-anim", 2, 8, 32, 8, 32 },
289 {
"32bpp-sse4", 0, 32, 32, 8, 32 },
290 {
"32bpp-ssse3", 0, 32, 32, 8, 32 },
291 {
"32bpp-sse2", 0, 32, 32, 8, 32 },
292 {
"32bpp-sse4-anim", 1, 32, 32, 8, 32 },
294 {
"32bpp-optimized", 0, 8, 32, 8, 32 },
296 {
"32bpp-sse2-anim", 1, 8, 32, 8, 32 },
298 {
"32bpp-anim", 1, 8, 32, 8, 32 },
304 for (
const auto &replacement_blitter : replacement_blitters) {
305 if (animation_wanted && (replacement_blitter.animation == 0))
continue;
306 if (!animation_wanted && (replacement_blitter.animation == 1))
continue;
308 if (!
IsInsideMM(depth_wanted_by_base, replacement_blitter.min_base_depth, replacement_blitter.max_base_depth + 1))
continue;
309 if (!
IsInsideMM(depth_wanted_by_grf, replacement_blitter.min_grf_depth, replacement_blitter.max_grf_depth + 1))
continue;
311 if (replacement_blitter.name == cur_blitter) {
350GraphicsSet::GraphicsSet() =
default;
353GraphicsSet::~GraphicsSet() =
default;
355bool GraphicsSet::FillSetDetails(
const IniFile &ini,
const std::string &path,
const std::string &full_filename)
360 assert(metadata !=
nullptr);
364 if (item ==
nullptr)
return false;
368 item = metadata->
GetItem(
"blitter");
397bool GraphicsSet::IsConfigurable()
const
405void GraphicsSet::CopyCompatibleConfig(
const GraphicsSet &src)
407 const GRFConfig *src_cfg = src.GetExtraConfig();
408 if (src_cfg ==
nullptr || src_cfg->
param.empty())
return;
450 size = std::min(size, max_size);
453 uint8_t buffer[1024];
457 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, *f)) != 0 && size != 0) {
459 checksum.Append(buffer, len);
462 checksum.Finish(digest);
467static const std::string_view
_graphics_file_names[] = {
"base",
"logos",
"arctic",
"tropical",
"toyland",
"extra" };
483 auto IsBetter = [&best] (
const auto *current) {
485 if (best ==
nullptr)
return true;
487 if (best->
fallback && !current->fallback)
return true;
489 if (best->
valid_files < current->valid_files)
return true;
491 if (best->
valid_files != current->valid_files)
return false;
493 if (best->
shortname == current->shortname && best->
version < current->version)
return true;
500 if (c->GetNumMissing() != 0)
continue;
502 if (IsBetter(c)) best = c;
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
static BlitterFactory * GetBlitterFactory(std::string_view name)
Get the blitter factory with the given name.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
static Blitter * SelectBlitter(std::string_view name)
Find the requested blitter and return its class.
How all blitters should look like.
virtual std::string_view GetName()=0
Get the name of the blitter, the same as the Factory-instance returns.
uint8_t ReadByte()
Read a byte from the file.
RandomAccessFile with some extra information specific for sprite files.
uint8_t GetContainerVersion() const
Get the version number of container type used by the file.
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.
virtual void ClearSystemSprites()
Clear all cached sprites.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Factory to 'query' all available blitters.
bool _blitter_autodetected
Was the blitter autodetected or specified by the user?
std::optional< FileHandle > FioFOpenFile(std::string_view filename, std::string_view mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Declarations for savegames operations.
Functions to read fonts from files and cache them.
void InitializeUnicodeGlyphMap()
Initialize the glyph map.
void UpdateCursorSize()
Update cursor dimension.
Functions related to the gfx engine.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
@ S8BPP_NONE
No support for 8bpp by OS or hardware, force 32bpp blitters.
@ PAL_DOS
Use the DOS palette.
@ PAL_WINDOWS
Use the Windows palette.
static std::unique_ptr< GRFConfig > GetDefaultExtraGRFConfig()
Get GRFConfig for the default extra graphics.
static std::unique_ptr< GRFConfig > GetBasesetExtraGRFConfig()
Get GRFConfig for the baseset extra graphics.
static void LoadSpriteTables()
Actually load the sprite tables.
static bool SwitchNewGRFBlitter()
Check blitter needed by NewGRF config and switch if needed.
void CheckBlitter()
Check whether we still use the right blitter, or use another (better) one.
static const std::string_view _graphics_file_names[]
Names corresponding to the GraphicsFileType.
static constexpr std::span< const std::pair< SpriteID, SpriteID > > _landscape_spriteindexes[]
Offsets for loading the different "replacement" sprites in the files.
static void LoadGrfFileIndexed(const std::string &filename, std::span< const std::pair< SpriteID, SpriteID > > index_tbl, bool needs_palette_remap)
Load an old fashioned GRF file to replace already loaded sprites.
static uint LoadGrfFile(const std::string &filename, SpriteID load_index, bool needs_palette_remap)
Load an old fashioned GRF file.
void CheckExternalFiles()
Checks whether the MD5 checksums of the files are correct.
void GfxLoadSprites()
Initialise and load all the sprites.
Offsets of sprites to replace for non-temperate landscapes.
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
void LoadNewGRF(SpriteID load_index, uint num_baseset)
Load all the NewGRFs.
Base for the NewGRF implementation.
GRFConfigList _grfconfig
First item in list of current GRF set up.
uint _missing_extra_graphics
Number of sprites provided by the fallback extra GRF, i.e. missing in the baseset.
bool FillGRFDetails(GRFConfig &config, bool is_static, Subdirectory subdir)
Find the GRFID of a given grf, and calculate its md5sum.
size_t GRFGetSizeOfDataSection(FileHandle &f)
Get the data section size of a GRF.
@ GCS_DISABLED
GRF file is disabled.
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
@ InitOnly
GRF file is processed up to GLS_INIT.
@ GRFP_GRF_WINDOWS
The NewGRF says the Windows palette can be used.
@ GRFP_GRF_DOS
The NewGRF says the DOS palette can be used.
@ GRFP_BLT_32BPP
The NewGRF prefers a 32 bpp blitter.
@ DO_FULL_ANIMATION
Perform palette animation.
Functions related to palettes.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
Count the sprites which originate from a specific file in a range of SpriteIDs.
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 LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
void ReadGRFSpriteOffsets(SpriteFile &file)
Parse the sprite section of GRFs.
This file contains all sprite-related enums and defines.
static constexpr uint32_t MAX_SPRITES
Masks needed for sprite operations.
static const SpriteID SPR_OPENTTD_BASE
Extra graphic spritenumbers.
Definition of base types and functions in a cross-platform compatible way.
Information about a single base set.
std::array< MD5File, BaseSet< T >::NUM_FILES > files
All files part of this set.
uint valid_files
Number of the files that could be found and are valid.
const IniItem * GetMandatoryItem(std::string_view full_filename, const IniGroup &group, std::string_view name) const
Try to read a single piece of metadata and return nullptr if it doesn't exist.
static constexpr size_t NUM_FILES
Number of files in this set.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied file.
static std::span< const std::string_view > GetFilenames()
Get the internal names of the files in this set.
std::string name
The name of the base set.
bool fallback
This set is a fallback set, i.e. it should be used only as last resort.
std::vector< uint32_t > version
The version of this base set.
uint32_t shortname
Four letter short variant of the name.
int GetNumInvalid() const
Get the number of invalid files.
Information about GRF, used in the game and (part of it) in savegames.
std::vector< std::optional< GRFParameterInfo > > param_info
NOSAVE: extra information about the parameters.
uint32_t version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
std::vector< uint32_t > param
GRF parameters.
bool IsCompatible(uint32_t old_version) const
Return whether this NewGRF can replace an older version of the same NewGRF.
void CopyParams(const GRFConfig &src)
Copy the parameter information from the src config.
LandscapeType landscape
the landscape we're currently in
GameCreationSettings game_creation
settings used during the creation of a game (map)
All data of a graphics set.
std::unique_ptr< GRFConfig > extra_cfg
Parameters for extra GRF.
PaletteType palette
Palette of this graphics set.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied GRF.
BlitterType blitter
Blitter of this graphics set.
GRFConfig & GetOrCreateExtraConfig() const
Return configuration for the extra GRF, or lazily create it.
Ini file that supports both loading and saving.
A group within an ini file.
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
A single "line" in an ini file.
std::optional< std::string > value
The value of this item.
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Structure holding filename and MD5 information about a single file.
ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const
Calculate and check the MD5 hash of the supplied filename.
ChecksumResult
The result of a checksum check.
@ CR_MATCH
The file did exist and the md5 checksum did match.
@ CR_MISMATCH
The file did exist, just the md5 checksum did not match.
@ CR_NO_FILE
The file did not exist.
MD5Hash hash
md5 sum of the file
std::string filename
filename
All data of a sounds set.
Functions related to transparency.
uint8_t _display_opt
What do we want to draw/do?
Base of all video drivers.
void ReInitAllWindows(bool zoom_changed)
Re-initialize all windows.
Window functions not directly related to making/drawing windows.