# gfxinit.cpp File Reference

Initializing of the (GRF) graphics. More...

`#include "stdafx.h"`
`#include "debug.h"`
`#include "spritecache.h"`
`#include "fileio_func.h"`
`#include "fios.h"`
`#include "newgrf.h"`
`#include "md5.h"`
`#include "fontcache.h"`
`#include "gfx_func.h"`
`#include "settings_type.h"`
`#include "string_func.h"`
`#include "ini_type.h"`
`#include "table/sprites.h"`
`#include "table/palette_convert.h"`
`#include "table/files.h"`
`#include "table/landscape_sprite.h"`

Go to the source code of this file.

## Data Structures
struct | MD5File
| Structure holding filename and MD5 information about a single file. More...
struct | GraphicsSet
| Information about a single graphics set. More...
class | OBGFileScanner
| Helper for scanning for files with GRF as extension. More...
## Defines
#define | fetch_metadata(name)
| Try to read a single piece of metadata and return false if it doesn't exist.
## Enumerations
enum | GraphicsFileType { GFT_BASE, GFT_LOGOS, GFT_ARCTIC, GFT_TROPICAL, GFT_TOYLAND, GFT_EXTRA, MAX_GFT }
| Types of graphics in the base graphics set. More...
## Functions
static uint | LoadGrfFile (const char *filename, uint load_index, int file_index)
void | LoadSpritesIndexed (int file_index, uint *sprite_id, const SpriteID *index_tbl)
static void | LoadGrfIndexed (const char *filename, const SpriteID *index_tbl, int file_index)
static bool | FileMD5 (const MD5File file)
| Calculate and check the MD5 hash of the supplied filename.
static bool | DetermineGraphicsPack ()
| Determine the graphics pack that has to be used.
void | UpdateNewGRFConfigPalette ()
| Update the palettes of the graphics from the config file.
static void | DeterminePalette ()
| Determine the palette that has to be used.
void | CheckExternalFiles ()
| Checks whether the MD5 checksums of the files are correct.
static void | LoadSpriteTables ()
void | GfxLoadSprites ()
static bool | FillGraphicsSetDetails (GraphicsSet *graphics, IniFile *ini, const char *path)
| Read the graphics set information from a loaded ini.
void | FindGraphicsSets ()
| Scan for all Grahpics sets.
bool | SetGraphicsSet (const char *name)
| Set the graphics set to be used.
char * | GetGraphicsSetsList (char *p, const char *last)
| Returns a list with the graphics sets.
int | GetNumGraphicsSets ()
| Count the number of available graphics sets.
int | GetIndexOfCurrentGraphicsSet ()
| Get the index of the currently active graphics set.
const char * | GetGraphicsSetName (int index)
| Get the name of the graphics set at the specified index.
## Variables
PaletteType | _use_palette = PAL_AUTODETECT
| The currently used palette.
bool | _palette_remap_grf [MAX_FILE_SLOTS]
| Whether the given NewGRFs must get a palette remap or not.
const byte * | _palette_remap = NULL
| Palette map to go from the !_use_palette to the _use_palette.
const byte * | _palette_reverse_remap = NULL
| Palette map to go from the _use_palette to the !_use_palette.
char * | _ini_graphics_set
static GraphicsSet * | _available_graphics_sets = NULL
| All graphics sets currently available.
static const GraphicsSet * | _used_graphics_set = NULL
| The one and only graphics set that is currently being used.
static const SpriteID *const | _landscape_spriteindexes []
static const char * | _gft_names [MAX_GFT] = { "base", "logos", "arctic", "tropical", "toyland", "extra" }
| Names corresponding to the GraphicsFileType.

## Detailed Description

Initializing of the (GRF) graphics.

Definition in file gfxinit.cpp.

## Define Documentation

#define fetch_metadata | ( | name |  | ) |

Value:

    item = metadata->GetItem(name, false); \
      if (item == NULL || strlen(item->value) == 0) { \
        DEBUG(grf, 0, "Base graphics set detail loading: %s field missing", name); \
        return false; \
      }

Try to read a single piece of metadata and return false if it doesn't exist.

Parameters:

  | name | the name of the item to fetch.

Definition at line 326 of file gfxinit.cpp.

Referenced by FillGraphicsSetDetails().

## Enumeration Type Documentation

enum GraphicsFileType

Types of graphics in the base graphics set.

Enumerator:

  GFT_BASE | Base sprites for all climates.
  GFT_LOGOS | Logos, landscape icons and original terrain generator sprites.
  GFT_ARCTIC | Landscape replacement sprites for arctic.
  GFT_TROPICAL | Landscape replacement sprites for tropical.
  GFT_TOYLAND | Landscape replacement sprites for toyland.
  GFT_EXTRA | Extra sprites that were not part of the original sprites.
  MAX_GFT | We are looking for this amount of GRFs.

Definition at line 40 of file gfxinit.cpp.

## Function Documentation

void CheckExternalFiles | ( |  | ) |

Checks whether the MD5 checksums of the files are correct.

Note:
  Also checks sample.cat and other required non-NewGRF GRFs for corruption.

Definition at line 226 of file gfxinit.cpp.

References _use_palette, DeterminePalette(), FileMD5(), MD5File::filename, GraphicsSet::files, lastof, lengthof, MAX_GFT, MD5File::missing_warning, GraphicsSet::name, PAL_DOS, seprintf(), and ShowInfoF().

Referenced by SetGraphicsSet().

static bool DetermineGraphicsPack | ( |  | ) | `[static]`

Determine the graphics pack that has to be used.

The one with the most correct files wins.

Definition at line 173 of file gfxinit.cpp.

References _use_palette, GraphicsSet::found_grfs, GraphicsSet::next, GraphicsSet::palette, GraphicsSet::shortname, and GraphicsSet::version.

Referenced by SetGraphicsSet().

static void DeterminePalette | ( |  | ) | `[static]`

Determine the palette that has to be used.

- forced palette via command line -> leave it that way
- otherwise -> palette based on the graphics pack

Definition at line 198 of file gfxinit.cpp.

References _palette_remap, _palette_reverse_remap, _palmap_d2w, _palmap_w2d, _use_palette, MAX_PAL, PAL_DOS, PAL_WINDOWS, GraphicsSet::palette, and UpdateNewGRFConfigPalette().

Referenced by CheckExternalFiles().

static bool FileMD5 | ( | const MD5File | file | ) | `[static]`

Calculate and check the MD5 hash of the supplied filename.

Parameters:

  | file | filename and expected MD5 hash for the given filename.

Returns:
  true if the checksum is correct.

Definition at line 144 of file gfxinit.cpp.

References DATA_DIR, MD5File::filename, FioFCloseFile(), FioFOpenFile(), and MD5File::hash.

Referenced by CheckExternalFiles(), and FillGraphicsSetDetails().

static bool FillGraphicsSetDetails | ( | GraphicsSet * | graphics,
|  | IniFile * | ini,
|  | const char * | path |
| ) |  |  | `[static]`

Read the graphics set information from a loaded ini.

Parameters:

  | graphics | the graphics set to write to
  | ini | the ini to read from
  | path | the path to this ini file (for filenames)

Returns:
  true if loading was successful.

Definition at line 343 of file gfxinit.cpp.

References GraphicsSet::description, fetch_metadata, FileMD5(), MD5File::filename, GraphicsSet::files, GraphicsSet::found_grfs, IniFile::GetGroup(), IniGroup::GetItem(), MD5File::hash, MAX_GFT, MD5File::missing_warning, GraphicsSet::name, PAL_DOS, PAL_WINDOWS, GraphicsSet::palette, GraphicsSet::shortname, IniItem::value, and GraphicsSet::version.

Referenced by OBGFileScanner::AddFile().

char* GetGraphicsSetsList | ( | char * | p,
|  | const char * | last |
| ) |  |  |

Returns a list with the graphics sets.

Parameters:

  | p | where to print to
  | last | the last character to print to

Returns:
  the last printed character

Definition at line 546 of file gfxinit.cpp.

References MAX_GFT, GraphicsSet::next, and seprintf().

Referenced by ShowHelp().

bool SetGraphicsSet | ( | const char * | name | ) |

Set the graphics set to be used.

Parameters:

  | name | of the graphics set to use

Returns:
  true if it could be loaded

Definition at line 522 of file gfxinit.cpp.

References CheckExternalFiles(), DetermineGraphicsPack(), GraphicsSet::next, and StrEmpty().

void UpdateNewGRFConfigPalette | ( |  | ) |

Update the palettes of the graphics from the config file.

This is needed because the config file gets read and parsed before the palette is chosen (one can configure the base graphics set governing the palette in the config after all). As a result of this we update the settings from the config once we have determined the palette.

Definition at line 32 of file newgrf_config.cpp.

References _use_palette, GRFConfig::next, and PAL_WINDOWS.

Referenced by DeterminePalette().

## Variable Documentation

const SpriteID* const _landscape_spriteindexes[]`[static]`

Initial value:

     {
      _landscape_spriteindexes_1,
      _landscape_spriteindexes_2,
      _landscape_spriteindexes_3,
    }

Definition at line 85 of file gfxinit.cpp.

bool _palette_remap_grf[MAX_FILE_SLOTS]

Whether the given NewGRFs must get a palette remap or not.

Definition at line 24 of file gfxinit.cpp.

const GraphicsSet* _used_graphics_set = NULL`[static]`

The one and only graphics set that is currently being used.

Definition at line 80 of file gfxinit.cpp.