13 #include "3rdparty/md5/md5.h"
23 #define SET_TYPE "graphics"
34 _landscape_spriteindexes_arctic,
35 _landscape_spriteindexes_tropic,
36 _landscape_spriteindexes_toyland,
48 SpriteID 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;
83 static void LoadGrfFileIndexed(
const std::string &filename, std::span<
const std::pair<SpriteID, SpriteID>> index_tbl,
bool needs_palette_remap)
89 Debug(sprite, 2,
"Reading indexed grf-file '{}'", filename);
92 if (container_ver == 0) UserError(
"Base grf '{}' is corrupt", filename);
94 if (container_ver >= 2) {
96 uint8_t compression = file.
ReadByte();
97 if (compression != 0) UserError(
"Unsupported compression format");
100 for (
const auto &pair : index_tbl) {
101 for (
SpriteID load_index = pair.first; load_index <= pair.second; ++load_index) {
102 [[maybe_unused]]
bool b =
LoadNextSprite(load_index, file, sprite_id);
120 Debug(grf, 1,
"Using the {} base graphics set", used_set->
name);
122 std::string error_msg;
123 auto output_iterator = std::back_inserter(error_msg);
126 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);
127 for (
const auto &file : used_set->
files) {
131 fmt::format_to(output_iterator,
"\n");
136 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);
144 if (!error_msg.empty()) ShowInfoI(error_msg);
186 static const char *master_filename =
"OPENTTD.GRF";
198 master->
next = extra;
204 Debug(sprite, 4,
"Checking sprites from fallback grf");
219 static void RealChangeBlitter(
const std::string_view repl_blitter)
222 if (cur_blitter == repl_blitter)
return;
224 Debug(driver, 1,
"Switching blitter from '{}' to '{}'... ", cur_blitter, repl_blitter);
226 if (new_blitter ==
nullptr) NOT_REACHED();
227 Debug(driver, 1,
"Successfully switched to {}.", repl_blitter);
260 uint depth_wanted_by_grf = _support8bpp !=
S8BPP_NONE ? 8 : 32;
266 if (GetFontAAState()) depth_wanted_by_grf = 32;
269 static const struct {
270 const std::string_view name;
272 uint min_base_depth, max_base_depth, min_grf_depth, max_grf_depth;
273 } replacement_blitters[] = {
274 {
"8bpp-optimized", 2, 8, 8, 8, 8 },
275 {
"40bpp-anim", 2, 8, 32, 8, 32 },
277 {
"32bpp-sse4", 0, 32, 32, 8, 32 },
278 {
"32bpp-ssse3", 0, 32, 32, 8, 32 },
279 {
"32bpp-sse2", 0, 32, 32, 8, 32 },
280 {
"32bpp-sse4-anim", 1, 32, 32, 8, 32 },
282 {
"32bpp-optimized", 0, 8, 32, 8, 32 },
284 {
"32bpp-sse2-anim", 1, 8, 32, 8, 32 },
286 {
"32bpp-anim", 1, 8, 32, 8, 32 },
292 for (
const auto &replacement_blitter : replacement_blitters) {
293 if (animation_wanted && (replacement_blitter.animation == 0))
continue;
294 if (!animation_wanted && (replacement_blitter.animation == 1))
continue;
296 if (!
IsInsideMM(depth_wanted_by_base, replacement_blitter.min_base_depth, replacement_blitter.max_base_depth + 1))
continue;
297 if (!
IsInsideMM(depth_wanted_by_grf, replacement_blitter.min_grf_depth, replacement_blitter.max_grf_depth + 1))
continue;
299 if (replacement_blitter.name == cur_blitter) {
337 GraphicsSet::GraphicsSet()
343 GraphicsSet::~GraphicsSet()
348 bool GraphicsSet::FillSetDetails(
const IniFile &ini,
const std::string &path,
const std::string &full_filename)
353 assert(metadata !=
nullptr);
360 item = metadata->
GetItem(
"blitter");
389 bool GraphicsSet::IsConfigurable()
const
397 void GraphicsSet::CopyCompatibleConfig(
const GraphicsSet &src)
399 const GRFConfig *src_cfg = src.GetExtraConfig();
400 if (src_cfg ==
nullptr || src_cfg->
num_params == 0)
return;
442 size = std::min(size, max_size);
445 uint8_t buffer[1024];
449 while ((len = fread(buffer, 1, (size >
sizeof(buffer)) ?
sizeof(buffer) : size, *f)) != 0 && size != 0) {
451 checksum.Append(buffer, len);
454 checksum.Finish(digest);
459 static const char *
const _graphics_file_names[] = {
"base",
"logos",
"arctic",
"tropical",
"toyland",
"extra" };
462 template <
class T,
size_t Tnum_files,
bool Tsearch_in_tars>
465 template <
class Tbase_set>
470 const Tbase_set *best =
nullptr;
473 if (c->GetNumMissing() != 0)
continue;
475 if (best ==
nullptr ||
476 (best->fallback && !c->fallback) ||
477 best->valid_files < c->valid_files ||
478 (best->valid_files == c->valid_files && (
479 (best->shortname == c->shortname && best->version < c->version) ||
489 template <
class Tbase_set>
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
static Blitter * SelectBlitter(const std::string_view name)
Find the requested blitter and return its class.
static BlitterFactory * GetBlitterFactory(const 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).
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,...)
Ouptut a line of debugging information.
Factory to 'query' all available blitters.
bool _blitter_autodetected
Was the blitter autodetected or specified by the user?
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.
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.
@ PAL_DOS
Use the DOS palette.
@ PAL_WINDOWS
Use the Windows palette.
@ S8BPP_NONE
No support for 8bpp by OS or hardware, force 32bpp blitters.
static const char *const _graphics_file_names[]
Names corresponding to the GraphicsFileType.
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 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 constexpr std::span< const std::pair< SpriteID, SpriteID > > _landscape_spriteindexes[]
Offsets for loading the different "replacement" sprites in the files.
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 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.
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.
GRFConfig * _grfconfig
First item in list of current GRF set up.
size_t GRFGetSizeOfDataSection(FileHandle &f)
Get the data section size of a GRF.
@ 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.
@ GCF_INIT_ONLY
GRF file is processed up to GLS_INIT.
@ GCS_DISABLED
GRF file is disabled.
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
@ 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.
static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir)
Calculate and check the MD5 hash of the supplied file.
int GetNumInvalid() const
Get the number of invalid files.
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.
static const size_t NUM_FILES
Number of files in this set.
std::string name
The name of the base set.
MD5File files[NUM_FILES]
All files part of this set.
Information about GRF, used in the game and (part of it) in savegames.
void SetParameterDefaults()
Set the default value for all parameters as specified by action14.
uint8_t palette
GRFPalette, bitset.
uint8_t flags
NOSAVE: GCF_Flags, bitset.
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.
uint8_t num_params
Number of used parameters.
struct GRFConfig * next
NOSAVE: Next item in the linked list.
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.
uint8_t 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.
std::string missing_warning
warning when this file is missing
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.
ChecksumResult CheckMD5(Subdirectory subdir, size_t max_size) const
Calculate and check the MD5 hash of the supplied filename.
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.