2009-08-21 20:21:05 +00:00
/*
* 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 < http : //www.gnu.org/licenses/>.
*/
2008-05-06 15:11:33 +00:00
/** @file gfxinit.cpp Initializing of the (GRF) graphics. */
2007-03-01 01:24:44 +00:00
2005-08-14 18:10:18 +00:00
# include "stdafx.h"
2007-10-29 23:02:31 +00:00
# include "fios.h"
2005-08-14 18:10:18 +00:00
# include "newgrf.h"
2009-08-21 20:15:17 +00:00
# include "3rdparty/md5/md5.h"
2006-11-24 20:47:29 +00:00
# include "fontcache.h"
2007-12-23 10:56:02 +00:00
# include "gfx_func.h"
2014-01-03 08:51:49 +00:00
# include "transparency.h"
2011-10-04 21:35:47 +00:00
# include "blitter/factory.hpp"
# include "video/video_driver.hpp"
2014-09-25 16:04:02 +00:00
# include "window_func.h"
2009-08-09 16:54:03 +00:00
/* The type of set we're replacing */
# define SET_TYPE "graphics"
# include "base_media_func.h"
2005-08-14 18:10:18 +00:00
2008-01-13 01:21:35 +00:00
# include "table/sprites.h"
2014-04-23 20:13:33 +00:00
# include "safeguards.h"
2005-08-14 18:10:18 +00:00
# include "table/landscape_sprite.h"
2011-08-24 14:31:18 +00:00
/** Offsets for loading the different "replacement" sprites in the files. */
2005-08-14 18:10:18 +00:00
static const SpriteID * const _landscape_spriteindexes [ ] = {
2011-08-24 14:31:18 +00:00
_landscape_spriteindexes_arctic ,
_landscape_spriteindexes_tropic ,
_landscape_spriteindexes_toyland ,
2005-08-14 18:10:18 +00:00
} ;
2011-08-24 14:31:18 +00:00
/**
* Load an old fashioned GRF file .
* @ param filename The name of the file to open .
* @ param load_index The offset of the first sprite .
2021-04-14 15:20:39 +00:00
* @ param needs_palette_remap Whether the colours in the GRF file need a palette remap .
2011-08-24 14:31:18 +00:00
* @ return The number of loaded sprites .
*/
2023-04-18 20:21:17 +00:00
static uint LoadGrfFile ( const std : : string & filename , uint load_index , bool needs_palette_remap )
2005-08-14 18:10:18 +00:00
{
2005-08-15 11:39:13 +00:00
uint load_index_org = load_index ;
2007-06-14 14:31:48 +00:00
uint sprite_id = 0 ;
2005-08-14 18:10:18 +00:00
2021-04-14 15:41:45 +00:00
SpriteFile & file = OpenCachedSpriteFile ( filename , BASESET_DIR , needs_palette_remap ) ;
2005-08-14 18:10:18 +00:00
2021-06-12 07:10:17 +00:00
Debug ( sprite , 2 , " Reading grf-file '{}' " , filename ) ;
2005-08-14 18:10:18 +00:00
2021-04-14 15:20:39 +00:00
byte container_ver = file . GetContainerVersion ( ) ;
2023-04-19 20:47:36 +00:00
if ( container_ver = = 0 ) UserError ( " Base grf '{}' is corrupt " , filename ) ;
2021-04-14 15:20:39 +00:00
ReadGRFSpriteOffsets ( file ) ;
2012-02-04 13:29:04 +00:00
if ( container_ver > = 2 ) {
/* Read compression. */
2021-04-14 15:20:39 +00:00
byte compression = file . ReadByte ( ) ;
2023-04-19 20:47:36 +00:00
if ( compression ! = 0 ) UserError ( " Unsupported compression format " ) ;
2012-02-04 13:29:04 +00:00
}
2021-04-14 15:20:39 +00:00
while ( LoadNextSprite ( load_index , file , sprite_id ) ) {
2005-08-14 18:10:18 +00:00
load_index + + ;
2007-06-14 14:31:48 +00:00
sprite_id + + ;
2005-08-14 18:10:18 +00:00
if ( load_index > = MAX_SPRITES ) {
2023-04-19 20:47:36 +00:00
UserError ( " Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files. " ) ;
2005-08-14 18:10:18 +00:00
}
}
2021-06-12 07:10:17 +00:00
Debug ( sprite , 2 , " Currently {} sprites are loaded " , load_index ) ;
2005-08-14 18:10:18 +00:00
return load_index - load_index_org ;
}
2011-08-24 14:31:18 +00:00
/**
* Load an old fashioned GRF file to replace already loaded sprites .
* @ param filename The name of the file to open .
2018-10-28 02:17:36 +00:00
* @ param index_tbl The offsets of each of the sprites .
2021-04-14 15:20:39 +00:00
* @ param needs_palette_remap Whether the colours in the GRF file need a palette remap .
2011-08-24 14:31:18 +00:00
* @ return The number of loaded sprites .
*/
2023-04-18 20:21:17 +00:00
static void LoadGrfFileIndexed ( const std : : string & filename , const SpriteID * index_tbl , bool needs_palette_remap )
2005-08-14 18:10:18 +00:00
{
2005-08-15 11:39:13 +00:00
uint start ;
2011-08-24 14:31:18 +00:00
uint sprite_id = 0 ;
2021-04-14 15:41:45 +00:00
SpriteFile & file = OpenCachedSpriteFile ( filename , BASESET_DIR , needs_palette_remap ) ;
2011-08-24 14:31:18 +00:00
2021-06-12 07:10:17 +00:00
Debug ( sprite , 2 , " Reading indexed grf-file '{}' " , filename ) ;
2011-08-24 14:31:18 +00:00
2021-04-14 15:20:39 +00:00
byte container_ver = file . GetContainerVersion ( ) ;
2023-04-19 20:47:36 +00:00
if ( container_ver = = 0 ) UserError ( " Base grf '{}' is corrupt " , filename ) ;
2021-04-14 15:20:39 +00:00
ReadGRFSpriteOffsets ( file ) ;
2012-02-04 13:29:04 +00:00
if ( container_ver > = 2 ) {
/* Read compression. */
2021-04-14 15:20:39 +00:00
byte compression = file . ReadByte ( ) ;
2023-04-19 20:47:36 +00:00
if ( compression ! = 0 ) UserError ( " Unsupported compression format " ) ;
2012-02-04 13:29:04 +00:00
}
2005-11-03 15:25:45 +00:00
while ( ( start = * index_tbl + + ) ! = END ) {
2005-08-15 11:39:13 +00:00
uint end = * index_tbl + + ;
2008-01-28 17:51:45 +00:00
do {
2021-06-03 14:55:08 +00:00
[[maybe_unused]] bool b = LoadNextSprite ( start , file , sprite_id ) ;
2008-01-28 17:51:45 +00:00
assert ( b ) ;
2011-08-24 14:31:18 +00:00
sprite_id + + ;
2008-01-28 17:51:45 +00:00
} while ( + + start < = end ) ;
2005-08-14 18:10:18 +00:00
}
}
2007-11-07 23:29:43 +00:00
/**
* Checks whether the MD5 checksums of the files are correct .
*
* @ note Also checks sample . cat and other required non - NewGRF GRFs for corruption .
*/
void CheckExternalFiles ( )
{
2019-04-10 21:07:06 +00:00
if ( BaseGraphics : : GetUsedSet ( ) = = nullptr | | BaseSounds : : GetUsedSet ( ) = = nullptr ) return ;
2009-08-09 19:50:44 +00:00
2009-08-09 16:54:03 +00:00
const GraphicsSet * used_set = BaseGraphics : : GetUsedSet ( ) ;
2007-11-07 23:29:43 +00:00
2021-06-12 07:10:17 +00:00
Debug ( grf , 1 , " Using the {} base graphics set " , used_set - > name ) ;
2008-09-02 18:45:15 +00:00
2023-05-24 20:40:12 +00:00
std : : string error_msg ;
auto output_iterator = std : : back_inserter ( error_msg ) ;
2009-08-20 17:02:44 +00:00
if ( used_set - > GetNumInvalid ( ) ! = 0 ) {
2010-07-31 21:02:56 +00:00
/* Not all files were loaded successfully, see which ones */
2023-05-24 20:40:12 +00:00
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 \n The following files are corrupted or missing: \n " , used_set - > name ) ;
2009-08-20 15:37:24 +00:00
for ( uint i = 0 ; i < GraphicsSet : : NUM_FILES ; i + + ) {
2012-02-04 13:29:00 +00:00
MD5File : : ChecksumResult res = GraphicsSet : : CheckMD5 ( & used_set - > files [ i ] , BASESET_DIR ) ;
2023-05-24 20:40:12 +00:00
if ( res ! = MD5File : : CR_MATCH ) fmt : : format_to ( output_iterator , " \t {} is {} ({}) \n " , used_set - > files [ i ] . filename , res = = MD5File : : CR_MISMATCH ? " corrupt " : " missing " , used_set - > files [ i ] . missing_warning ) ;
2007-11-07 23:29:43 +00:00
}
2023-05-24 20:40:12 +00:00
fmt : : format_to ( output_iterator , " \n " ) ;
2007-11-07 23:29:43 +00:00
}
2009-08-09 19:50:44 +00:00
const SoundsSet * sounds_set = BaseSounds : : GetUsedSet ( ) ;
2009-08-20 17:02:44 +00:00
if ( sounds_set - > GetNumInvalid ( ) ! = 0 ) {
2023-05-24 20:40:12 +00:00
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 \n The following files are corrupted or missing: \n " , sounds_set - > name ) ;
2009-08-20 15:37:24 +00:00
2020-12-27 10:44:22 +00:00
static_assert ( SoundsSet : : NUM_FILES = = 1 ) ;
2009-08-20 15:37:24 +00:00
/* No need to loop each file, as long as there is only a single
* sound file . */
2023-05-24 20:40:12 +00:00
fmt : : format_to ( output_iterator , " \t {} is {} ({}) \n " , sounds_set - > files - > filename , SoundsSet : : CheckMD5 ( sounds_set - > files , BASESET_DIR ) = = MD5File : : CR_MISMATCH ? " corrupt " : " missing " , sounds_set - > files - > missing_warning ) ;
2007-11-07 23:29:43 +00:00
}
2023-05-24 20:40:12 +00:00
if ( ! error_msg . empty ( ) ) ShowInfoI ( error_msg ) ;
2007-11-07 23:29:43 +00:00
}
2011-05-02 17:42:12 +00:00
/** Actually load the sprite tables. */
2007-03-07 11:47:46 +00:00
static void LoadSpriteTables ( )
2005-08-14 18:10:18 +00:00
{
2009-08-09 16:54:03 +00:00
const GraphicsSet * used_set = BaseGraphics : : GetUsedSet ( ) ;
2005-08-14 18:10:18 +00:00
2021-04-14 15:41:45 +00:00
LoadGrfFile ( used_set - > files [ GFT_BASE ] . filename , 0 , PAL_DOS ! = used_set - > palette ) ;
2005-08-20 18:14:32 +00:00
2007-11-07 23:29:43 +00:00
/*
* The second basic file always starts at the given location and does
* contain a different amount of sprites depending on the " type " ; DOS
* has a few sprites less . However , we do not care about those missing
* sprites as they are not shown anyway ( logos in intro game ) .
*/
2021-04-14 15:41:45 +00:00
LoadGrfFile ( used_set - > files [ GFT_LOGOS ] . filename , 4793 , PAL_DOS ! = used_set - > palette ) ;
2005-08-14 18:10:18 +00:00
2007-11-07 23:29:43 +00:00
/*
* Load additional sprites for climates other than temperate .
* This overwrites some of the temperate sprites , such as foundations
* and the ground sprites .
*/
2008-05-29 15:13:28 +00:00
if ( _settings_game . game_creation . landscape ! = LT_TEMPERATE ) {
2011-08-24 14:31:18 +00:00
LoadGrfFileIndexed (
2009-08-09 16:54:03 +00:00
used_set - > files [ GFT_ARCTIC + _settings_game . game_creation . landscape - 1 ] . filename ,
2008-05-29 15:13:28 +00:00
_landscape_spriteindexes [ _settings_game . game_creation . landscape - 1 ] ,
2021-04-14 15:41:45 +00:00
PAL_DOS ! = used_set - > palette
2005-08-14 18:10:18 +00:00
) ;
}
2006-11-24 20:47:29 +00:00
/* Initialize the unicode to sprite mapping table */
InitializeUnicodeGlyphMap ( ) ;
2007-11-15 07:42:25 +00:00
/*
2017-01-14 15:48:19 +00:00
* Load the base and extra NewGRF with OTTD required graphics as first NewGRF .
2007-11-15 07:42:25 +00:00
* However , we do not want it to show up in the list of used NewGRFs ,
* so we have to manually add it , and then remove it later .
*/
GRFConfig * top = _grfconfig ;
2017-01-14 15:48:19 +00:00
/* Default extra graphics */
2021-05-08 12:02:42 +00:00
static const char * master_filename = " OPENTTD.GRF " ;
GRFConfig * master = new GRFConfig ( master_filename ) ;
2017-01-14 15:48:19 +00:00
master - > palette | = GRFP_GRF_DOS ;
FillGRFDetails ( master , false , BASESET_DIR ) ;
ClrBit ( master - > flags , GCF_INIT_ONLY ) ;
/* Baseset extra graphics */
2023-04-18 20:21:17 +00:00
GRFConfig * extra = new GRFConfig ( used_set - > files [ GFT_EXTRA ] . filename . c_str ( ) ) ;
2010-08-02 15:29:31 +00:00
/* 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
2011-05-03 20:30:26 +00:00
* one which might be the wrong palette for this base NewGRF .
* The value set here might be overridden via action14 later . */
2011-05-03 20:48:53 +00:00
switch ( used_set - > palette ) {
2017-01-14 15:48:19 +00:00
case PAL_DOS : extra - > palette | = GRFP_GRF_DOS ; break ;
case PAL_WINDOWS : extra - > palette | = GRFP_GRF_WINDOWS ; break ;
2011-05-03 20:30:26 +00:00
default : break ;
}
2017-01-14 15:48:19 +00:00
FillGRFDetails ( extra , false , BASESET_DIR ) ;
ClrBit ( extra - > flags , GCF_INIT_ONLY ) ;
2010-08-02 15:29:31 +00:00
2017-01-14 15:48:19 +00:00
extra - > next = top ;
master - > next = extra ;
2007-11-15 07:42:25 +00:00
_grfconfig = master ;
2021-04-14 15:41:45 +00:00
LoadNewGRF ( SPR_NEWGRFS_BASE , 2 ) ;
2007-11-15 07:42:25 +00:00
2017-01-14 18:30:26 +00:00
uint total_extra_graphics = SPR_NEWGRFS_BASE - SPR_OPENTTD_BASE ;
2023-06-27 10:46:14 +00:00
Debug ( sprite , 4 , " Checking sprites from fallback grf " ) ;
2021-05-08 12:02:42 +00:00
_missing_extra_graphics = GetSpriteCountForFile ( master_filename , SPR_OPENTTD_BASE , SPR_NEWGRFS_BASE ) ;
2021-06-12 07:10:17 +00:00
Debug ( sprite , 1 , " {} extra sprites, {} from baseset, {} from fallback " , total_extra_graphics , total_extra_graphics - _missing_extra_graphics , _missing_extra_graphics ) ;
2017-01-14 18:30:26 +00:00
/* The original baseset extra graphics intentionally make use of the fallback graphics.
* Let ' s say everything which provides less than 500 sprites misses the rest intentionally . */
if ( 500 + _missing_extra_graphics > total_extra_graphics ) _missing_extra_graphics = 0 ;
2007-11-15 07:42:25 +00:00
/* Free and remove the top element. */
2017-01-14 15:48:19 +00:00
delete extra ;
2010-02-25 20:06:11 +00:00
delete master ;
2007-11-15 07:42:25 +00:00
_grfconfig = top ;
2005-08-14 18:10:18 +00:00
}
2021-05-01 17:39:03 +00:00
static void RealChangeBlitter ( const char * repl_blitter )
{
const char * cur_blitter = BlitterFactory : : GetCurrentBlitter ( ) - > GetName ( ) ;
if ( strcmp ( cur_blitter , repl_blitter ) = = 0 ) return ;
2021-06-12 07:10:17 +00:00
Debug ( driver , 1 , " Switching blitter from '{}' to '{}'... " , cur_blitter , repl_blitter ) ;
2021-05-01 17:39:03 +00:00
Blitter * new_blitter = BlitterFactory : : SelectBlitter ( repl_blitter ) ;
if ( new_blitter = = nullptr ) NOT_REACHED ( ) ;
2021-06-12 07:10:17 +00:00
Debug ( driver , 1 , " Successfully switched to {}. " , repl_blitter ) ;
2021-05-01 17:39:03 +00:00
if ( ! VideoDriver : : GetInstance ( ) - > AfterBlitterChange ( ) ) {
/* Failed to switch blitter, let's hope we can return to the old one. */
2023-04-19 20:47:36 +00:00
if ( BlitterFactory : : SelectBlitter ( cur_blitter ) = = nullptr | | ! VideoDriver : : GetInstance ( ) - > AfterBlitterChange ( ) ) UserError ( " Failed to reinitialize video driver. Specify a fixed blitter in the config " ) ;
2021-05-01 17:39:03 +00:00
}
/* Clear caches that might have sprites for another blitter. */
VideoDriver : : GetInstance ( ) - > ClearSystemSprites ( ) ;
ClearFontCache ( ) ;
GfxClearSpriteCache ( ) ;
ReInitAllWindows ( false ) ;
}
2011-10-04 21:35:47 +00:00
/**
* Check blitter needed by NewGRF config and switch if needed .
2014-01-03 08:51:49 +00:00
* @ return False when nothing changed , true otherwise .
2011-10-04 21:35:47 +00:00
*/
2014-01-03 08:51:49 +00:00
static bool SwitchNewGRFBlitter ( )
2011-10-04 21:35:47 +00:00
{
2014-01-03 08:31:32 +00:00
/* Never switch if the blitter was specified by the user. */
2014-01-03 08:51:49 +00:00
if ( ! _blitter_autodetected ) return false ;
2011-10-04 21:35:47 +00:00
2014-01-03 08:31:32 +00:00
/* Null driver => dedicated server => do nothing. */
2014-01-03 08:51:49 +00:00
if ( BlitterFactory : : GetCurrentBlitter ( ) - > GetScreenDepth ( ) = = 0 ) return false ;
2014-01-03 08:31:32 +00:00
2014-04-27 12:15:14 +00:00
/* Get preferred depth.
* - depth_wanted_by_base : Depth required by the baseset , i . e . the majority of the sprites .
* - depth_wanted_by_grf : Depth required by some NewGRF .
* Both can force using a 32 bpp blitter . depth_wanted_by_base is used to select
* between multiple 32 bpp blitters , which perform differently with 8 bpp sprites .
*/
2014-01-03 08:31:32 +00:00
uint depth_wanted_by_base = BaseGraphics : : GetUsedSet ( ) - > blitter = = BLT_32BPP ? 32 : 8 ;
2021-02-24 23:29:30 +00:00
uint depth_wanted_by_grf = _support8bpp ! = S8BPP_NONE ? 8 : 32 ;
2019-04-10 21:07:06 +00:00
for ( GRFConfig * c = _grfconfig ; c ! = nullptr ; c = c - > next ) {
2011-10-04 21:35:52 +00:00
if ( c - > status = = GCS_DISABLED | | c - > status = = GCS_NOT_FOUND | | HasBit ( c - > flags , GCF_INIT_ONLY ) ) continue ;
2014-01-03 08:31:32 +00:00
if ( c - > palette & GRFP_BLT_32BPP ) depth_wanted_by_grf = 32 ;
}
2021-02-23 19:54:50 +00:00
/* We need a 32bpp blitter for font anti-alias. */
if ( HasAntialiasedFonts ( ) ) depth_wanted_by_grf = 32 ;
2011-10-04 21:35:52 +00:00
2014-01-03 08:31:32 +00:00
/* Search the best blitter. */
2014-04-27 12:15:14 +00:00
static const struct {
2014-01-03 08:31:32 +00:00
const char * name ;
2014-01-03 08:51:49 +00:00
uint animation ; ///< 0: no support, 1: do support, 2: both
2014-01-03 08:31:32 +00:00
uint min_base_depth , max_base_depth , min_grf_depth , max_grf_depth ;
} replacement_blitters [ ] = {
2021-01-16 15:43:44 +00:00
{ " 8bpp-optimized " , 2 , 8 , 8 , 8 , 8 } ,
{ " 40bpp-anim " , 2 , 8 , 32 , 8 , 32 } ,
2014-01-03 08:31:32 +00:00
# ifdef WITH_SSE
2014-01-03 08:51:49 +00:00
{ " 32bpp-sse4 " , 0 , 32 , 32 , 8 , 32 } ,
{ " 32bpp-ssse3 " , 0 , 32 , 32 , 8 , 32 } ,
{ " 32bpp-sse2 " , 0 , 32 , 32 , 8 , 32 } ,
{ " 32bpp-sse4-anim " , 1 , 32 , 32 , 8 , 32 } ,
2014-01-03 08:31:32 +00:00
# endif
2014-01-03 08:51:49 +00:00
{ " 32bpp-optimized " , 0 , 8 , 32 , 8 , 32 } ,
2018-05-23 08:55:04 +00:00
# ifdef WITH_SSE
{ " 32bpp-sse2-anim " , 1 , 8 , 32 , 8 , 32 } ,
# endif
2014-01-03 08:51:49 +00:00
{ " 32bpp-anim " , 1 , 8 , 32 , 8 , 32 } ,
2014-01-03 08:31:32 +00:00
} ;
2014-01-03 08:51:49 +00:00
const bool animation_wanted = HasBit ( _display_opt , DO_FULL_ANIMATION ) ;
2014-01-03 08:31:32 +00:00
const char * cur_blitter = BlitterFactory : : GetCurrentBlitter ( ) - > GetName ( ) ;
for ( uint i = 0 ; i < lengthof ( replacement_blitters ) ; i + + ) {
2014-01-03 08:51:49 +00:00
if ( animation_wanted & & ( replacement_blitters [ i ] . animation = = 0 ) ) continue ;
if ( ! animation_wanted & & ( replacement_blitters [ i ] . animation = = 1 ) ) continue ;
2014-01-03 08:31:32 +00:00
if ( ! IsInsideMM ( depth_wanted_by_base , replacement_blitters [ i ] . min_base_depth , replacement_blitters [ i ] . max_base_depth + 1 ) ) continue ;
if ( ! IsInsideMM ( depth_wanted_by_grf , replacement_blitters [ i ] . min_grf_depth , replacement_blitters [ i ] . max_grf_depth + 1 ) ) continue ;
const char * repl_blitter = replacement_blitters [ i ] . name ;
2017-03-11 13:05:54 +00:00
if ( strcmp ( repl_blitter , cur_blitter ) = = 0 ) {
return false ;
}
2019-04-10 21:07:06 +00:00
if ( BlitterFactory : : GetBlitterFactory ( repl_blitter ) = = nullptr ) continue ;
2014-01-03 08:31:32 +00:00
2021-03-08 13:50:06 +00:00
/* Inform the video driver we want to switch blitter as soon as possible. */
2021-05-01 17:39:03 +00:00
VideoDriver : : GetInstance ( ) - > QueueOnMainThread ( std : : bind ( & RealChangeBlitter , repl_blitter ) ) ;
2014-01-03 08:31:32 +00:00
break ;
2011-10-04 21:35:52 +00:00
}
2014-01-03 08:51:49 +00:00
return true ;
}
/** Check whether we still use the right blitter, or use another (better) one. */
void CheckBlitter ( )
{
if ( ! SwitchNewGRFBlitter ( ) ) return ;
ClearFontCache ( ) ;
GfxClearSpriteCache ( ) ;
2021-04-05 17:43:12 +00:00
ReInitAllWindows ( false ) ;
2011-10-04 21:35:47 +00:00
}
2011-05-02 17:42:12 +00:00
/** Initialise and load all the sprites. */
2007-03-07 11:47:46 +00:00
void GfxLoadSprites ( )
2005-08-14 18:10:18 +00:00
{
2021-06-12 07:10:17 +00:00
Debug ( sprite , 2 , " Loading sprite set {} " , _settings_game . game_creation . landscape ) ;
2005-08-14 18:10:18 +00:00
2011-10-04 21:35:47 +00:00
SwitchNewGRFBlitter ( ) ;
2021-02-24 21:10:25 +00:00
VideoDriver : : GetInstance ( ) - > ClearSystemSprites ( ) ;
2012-02-25 17:20:02 +00:00
ClearFontCache ( ) ;
2006-11-24 12:39:13 +00:00
GfxInitSpriteMem ( ) ;
LoadSpriteTables ( ) ;
GfxInitPalettes ( ) ;
2010-08-05 19:23:19 +00:00
UpdateCursorSize ( ) ;
2005-08-14 18:10:18 +00:00
}
2008-08-24 08:41:38 +00:00
2023-04-18 20:21:17 +00:00
bool GraphicsSet : : FillSetDetails ( IniFile * ini , const std : : string & path , const std : : string & full_filename )
2008-08-31 08:46:43 +00:00
{
2011-11-14 19:24:22 +00:00
bool ret = this - > BaseSet < GraphicsSet , MAX_GFT , true > : : FillSetDetails ( ini , path , full_filename , false ) ;
2009-08-09 16:54:03 +00:00
if ( ret ) {
IniGroup * metadata = ini - > GetGroup ( " metadata " ) ;
IniItem * item ;
2008-08-31 08:46:43 +00:00
2009-08-09 16:54:03 +00:00
fetch_metadata ( " palette " ) ;
2020-12-14 21:25:07 +00:00
this - > palette = ( ( * item - > value ) [ 0 ] = = ' D ' | | ( * item - > value ) [ 0 ] = = ' d ' ) ? PAL_DOS : PAL_WINDOWS ;
2011-10-04 21:35:47 +00:00
/* Get optional blitter information. */
2023-06-05 18:29:52 +00:00
item = metadata - > GetItem ( " blitter " ) ;
2020-12-14 21:25:07 +00:00
this - > blitter = ( item ! = nullptr & & ( * item - > value ) [ 0 ] = = ' 3 ' ) ? BLT_32BPP : BLT_8BPP ;
2008-08-31 08:46:43 +00:00
}
2010-04-19 09:34:56 +00:00
return ret ;
2008-08-31 08:46:43 +00:00
}
2012-02-04 13:29:04 +00:00
/**
* Calculate and check the MD5 hash of the supplied GRF .
* @ param file The file get the hash of .
* @ param subdir The sub directory to get the files from .
* @ return
* - # CR_MATCH if the MD5 hash matches
* - # CR_MISMATCH if the MD5 does not match
* - # CR_NO_FILE if the file misses
*/
/* static */ MD5File : : ChecksumResult GraphicsSet : : CheckMD5 ( const MD5File * file , Subdirectory subdir )
{
size_t size = 0 ;
FILE * f = FioFOpenFile ( file - > filename , " rb " , subdir , & size ) ;
2019-04-10 21:07:06 +00:00
if ( f = = nullptr ) return MD5File : : CR_NO_FILE ;
2012-02-04 13:29:04 +00:00
size_t max = GRFGetSizeOfDataSection ( f ) ;
FioFCloseFile ( f ) ;
return file - > CheckMD5 ( subdir , max ) ;
}
2008-08-31 08:46:43 +00:00
/**
2009-08-09 16:54:03 +00:00
* Calculate and check the MD5 hash of the supplied filename .
2009-12-22 21:40:29 +00:00
* @ param subdir The sub directory to get the files from
2012-02-04 13:29:00 +00:00
* @ param max_size Only calculate the hash for this many bytes from the file start .
2009-08-20 17:02:44 +00:00
* @ return
2011-05-14 18:35:40 +00:00
* - # CR_MATCH if the MD5 hash matches
* - # CR_MISMATCH if the MD5 does not match
* - # CR_NO_FILE if the file misses
2008-08-31 08:46:43 +00:00
*/
2012-02-04 13:29:00 +00:00
MD5File : : ChecksumResult MD5File : : CheckMD5 ( Subdirectory subdir , size_t max_size ) const
2008-08-24 08:41:38 +00:00
{
2009-08-09 16:54:03 +00:00
size_t size ;
2009-12-22 21:40:29 +00:00
FILE * f = FioFOpenFile ( this - > filename , " rb " , subdir , & size ) ;
2008-08-31 08:46:43 +00:00
2019-04-10 21:07:06 +00:00
if ( f = = nullptr ) return CR_NO_FILE ;
2008-08-24 08:41:38 +00:00
2021-01-08 10:16:18 +00:00
size = std : : min ( size , max_size ) ;
2012-02-04 13:29:00 +00:00
2009-08-20 17:02:44 +00:00
Md5 checksum ;
2023-05-08 17:01:06 +00:00
uint8_t buffer [ 1024 ] ;
2023-05-18 20:38:56 +00:00
MD5Hash digest ;
2009-08-20 17:02:44 +00:00
size_t len ;
2008-08-24 08:41:38 +00:00
2009-08-20 17:02:44 +00:00
while ( ( len = fread ( buffer , 1 , ( size > sizeof ( buffer ) ) ? sizeof ( buffer ) : size , f ) ) ! = 0 & & size ! = 0 ) {
size - = len ;
checksum . Append ( buffer , len ) ;
2008-08-24 08:41:38 +00:00
}
2009-08-20 17:02:44 +00:00
FioFCloseFile ( f ) ;
checksum . Finish ( digest ) ;
2023-05-18 20:38:56 +00:00
return this - > hash = = digest ? CR_MATCH : CR_MISMATCH ;
2008-08-24 08:41:38 +00:00
}
2009-08-09 16:54:03 +00:00
/** Names corresponding to the GraphicsFileType */
2009-09-20 23:11:01 +00:00
static const char * const _graphics_file_names [ ] = { " base " , " logos " , " arctic " , " tropical " , " toyland " , " extra " } ;
2009-08-10 10:25:40 +00:00
/** Implementation */
2011-11-14 19:24:22 +00:00
template < class T , size_t Tnum_files , bool Tsearch_in_tars >
/* static */ const char * const * BaseSet < T , Tnum_files , Tsearch_in_tars > : : file_names = _graphics_file_names ;
2009-01-17 16:53:32 +00:00
2009-08-09 16:54:03 +00:00
template < class Tbase_set >
/* static */ bool BaseMedia < Tbase_set > : : DetermineBestSet ( )
2009-02-07 01:01:02 +00:00
{
2019-04-10 21:07:06 +00:00
if ( BaseMedia < Tbase_set > : : used_set ! = nullptr ) return true ;
2009-08-09 16:54:03 +00:00
2019-04-10 21:07:06 +00:00
const Tbase_set * best = nullptr ;
for ( const Tbase_set * c = BaseMedia < Tbase_set > : : available_sets ; c ! = nullptr ; c = c - > next ) {
2013-01-08 22:46:42 +00:00
/* Skip unusable sets */
2009-08-20 17:02:44 +00:00
if ( c - > GetNumMissing ( ) ! = 0 ) continue ;
2019-04-10 21:07:06 +00:00
if ( best = = nullptr | |
2010-02-22 16:24:23 +00:00
( best - > fallback & & ! c - > fallback ) | |
2009-08-20 17:02:44 +00:00
best - > valid_files < c - > valid_files | |
( best - > valid_files = = c - > valid_files & & (
2009-08-09 16:54:03 +00:00
( best - > shortname = = c - > shortname & & best - > version < c - > version ) | |
2011-05-04 17:12:37 +00:00
( best - > palette ! = PAL_DOS & & c - > palette = = PAL_DOS ) ) ) ) {
2009-08-09 16:54:03 +00:00
best = c ;
}
2009-02-07 01:01:02 +00:00
}
2009-05-17 18:21:21 +00:00
2009-08-09 16:54:03 +00:00
BaseMedia < Tbase_set > : : used_set = best ;
2019-04-10 21:07:06 +00:00
return BaseMedia < Tbase_set > : : used_set ! = nullptr ;
2009-05-17 18:21:21 +00:00
}
2009-08-09 16:54:03 +00:00
template < class Tbase_set >
/* static */ const char * BaseMedia < Tbase_set > : : GetExtension ( )
2009-05-17 18:21:21 +00:00
{
2009-08-09 16:54:03 +00:00
return " .obg " ; // OpenTTD Base Graphics
2009-05-17 18:21:21 +00:00
}
2009-08-09 16:54:03 +00:00
INSTANTIATE_BASE_MEDIA_METHODS ( BaseMedia < GraphicsSet > , GraphicsSet )