OpenTTD Source  20241120-master-g6d3adc6169
spritecache.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
12 #include "spriteloader/grf.hpp"
13 #include "gfx_func.h"
14 #include "error.h"
15 #include "error_func.h"
16 #include "zoom_func.h"
17 #include "settings_type.h"
18 #include "blitter/factory.hpp"
19 #include "core/math_func.hpp"
20 #include "core/mem_func.hpp"
21 #include "video/video_driver.hpp"
22 #include "spritecache.h"
23 #include "spritecache_internal.h"
24 
25 #include "table/sprites.h"
26 #include "table/strings.h"
27 #include "table/palette_convert.h"
28 
29 #include "safeguards.h"
30 
31 /* Default of 4MB spritecache */
32 uint _sprite_cache_size = 4;
33 
34 
35 static uint _spritecache_items = 0;
36 static SpriteCache *_spritecache = nullptr;
37 static std::vector<std::unique_ptr<SpriteFile>> _sprite_files;
38 
39 static inline SpriteCache *GetSpriteCache(uint index)
40 {
41  return &_spritecache[index];
42 }
43 
44 SpriteCache *AllocateSpriteCache(uint index)
45 {
46  if (index >= _spritecache_items) {
47  /* Add another 1024 items to the 'pool' */
48  uint items = Align(index + 1, 1024);
49 
50  Debug(sprite, 4, "Increasing sprite cache to {} items ({} bytes)", items, items * sizeof(*_spritecache));
51 
52  _spritecache = ReallocT(_spritecache, items);
53 
54  /* Reset the new items and update the count */
55  memset(_spritecache + _spritecache_items, 0, (items - _spritecache_items) * sizeof(*_spritecache));
56  _spritecache_items = items;
57  }
58 
59  return GetSpriteCache(index);
60 }
61 
67 static SpriteFile *GetCachedSpriteFileByName(const std::string &filename)
68 {
69  for (auto &f : _sprite_files) {
70  if (f->GetFilename() == filename) {
71  return f.get();
72  }
73  }
74  return nullptr;
75 }
76 
81 std::span<const std::unique_ptr<SpriteFile>> GetCachedSpriteFiles()
82 {
83  return _sprite_files;
84 }
85 
93 SpriteFile &OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
94 {
95  SpriteFile *file = GetCachedSpriteFileByName(filename);
96  if (file == nullptr) {
97  file = _sprite_files.insert(std::end(_sprite_files), std::make_unique<SpriteFile>(filename, subdir, palette_remap))->get();
98  } else {
99  file->SeekToBegin();
100  }
101  return *file;
102 }
103 
104 struct MemBlock {
105  size_t size;
106  uint8_t data[];
107 };
108 
109 static uint _sprite_lru_counter;
110 static MemBlock *_spritecache_ptr;
111 static uint _allocated_sprite_cache_size = 0;
112 static int _compact_cache_counter;
113 
114 static void CompactSpriteCache();
115 
122 bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
123 {
124  if (type & 2) {
125  file.SkipBytes(num);
126  } else {
127  while (num > 0) {
128  int8_t i = file.ReadByte();
129  if (i >= 0) {
130  int size = (i == 0) ? 0x80 : i;
131  if (size > num) return false;
132  num -= size;
133  file.SkipBytes(size);
134  } else {
135  i = -(i >> 3);
136  num -= i;
137  file.ReadByte();
138  }
139  }
140  }
141  return true;
142 }
143 
144 /* Check if the given Sprite ID exists */
145 bool SpriteExists(SpriteID id)
146 {
147  if (id >= _spritecache_items) return false;
148 
149  /* Special case for Sprite ID zero -- its position is also 0... */
150  if (id == 0) return true;
151  return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file == nullptr);
152 }
153 
160 {
161  if (!SpriteExists(sprite)) return SpriteType::Invalid;
162  return GetSpriteCache(sprite)->type;
163 }
164 
171 {
172  if (!SpriteExists(sprite)) return nullptr;
173  return GetSpriteCache(sprite)->file;
174 }
175 
181 uint32_t GetSpriteLocalID(SpriteID sprite)
182 {
183  if (!SpriteExists(sprite)) return 0;
184  return GetSpriteCache(sprite)->id;
185 }
186 
194 uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
195 {
196  SpriteFile *file = GetCachedSpriteFileByName(filename);
197  if (file == nullptr) return 0;
198 
199  uint count = 0;
200  for (SpriteID i = begin; i != end; i++) {
201  if (SpriteExists(i)) {
202  SpriteCache *sc = GetSpriteCache(i);
203  if (sc->file == file) {
204  count++;
205  Debug(sprite, 4, "Sprite: {}", i);
206  }
207  }
208  }
209  return count;
210 }
211 
221 {
222  return _spritecache_items;
223 }
224 
225 static bool ResizeSpriteIn(SpriteLoader::SpriteCollection &sprite, ZoomLevel src, ZoomLevel tgt)
226 {
227  uint8_t scaled_1 = ScaleByZoom(1, (ZoomLevel)(src - tgt));
228 
229  /* Check for possible memory overflow. */
230  if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX) return false;
231 
232  sprite[tgt].width = sprite[src].width * scaled_1;
233  sprite[tgt].height = sprite[src].height * scaled_1;
234  sprite[tgt].x_offs = sprite[src].x_offs * scaled_1;
235  sprite[tgt].y_offs = sprite[src].y_offs * scaled_1;
236  sprite[tgt].colours = sprite[src].colours;
237 
238  sprite[tgt].AllocateData(tgt, static_cast<size_t>(sprite[tgt].width) * sprite[tgt].height);
239 
240  SpriteLoader::CommonPixel *dst = sprite[tgt].data;
241  for (int y = 0; y < sprite[tgt].height; y++) {
242  const SpriteLoader::CommonPixel *src_ln = &sprite[src].data[y / scaled_1 * sprite[src].width];
243  for (int x = 0; x < sprite[tgt].width; x++) {
244  *dst = src_ln[x / scaled_1];
245  dst++;
246  }
247  }
248 
249  return true;
250 }
251 
252 static void ResizeSpriteOut(SpriteLoader::SpriteCollection &sprite, ZoomLevel zoom)
253 {
254  /* Algorithm based on 32bpp_Optimized::ResizeSprite() */
255  sprite[zoom].width = UnScaleByZoom(sprite[ZOOM_LVL_MIN].width, zoom);
256  sprite[zoom].height = UnScaleByZoom(sprite[ZOOM_LVL_MIN].height, zoom);
257  sprite[zoom].x_offs = UnScaleByZoom(sprite[ZOOM_LVL_MIN].x_offs, zoom);
258  sprite[zoom].y_offs = UnScaleByZoom(sprite[ZOOM_LVL_MIN].y_offs, zoom);
259  sprite[zoom].colours = sprite[ZOOM_LVL_MIN].colours;
260 
261  sprite[zoom].AllocateData(zoom, static_cast<size_t>(sprite[zoom].height) * sprite[zoom].width);
262 
263  SpriteLoader::CommonPixel *dst = sprite[zoom].data;
264  const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data;
265  [[maybe_unused]] const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width;
266 
267  for (uint y = 0; y < sprite[zoom].height; y++) {
268  const SpriteLoader::CommonPixel *src_ln = src + sprite[zoom - 1].width;
269  assert(src_ln <= src_end);
270  for (uint x = 0; x < sprite[zoom].width; x++) {
271  assert(src < src_ln);
272  if (src + 1 != src_ln && (src + 1)->a != 0) {
273  *dst = *(src + 1);
274  } else {
275  *dst = *src;
276  }
277  dst++;
278  src += 2;
279  }
280  src = src_ln + sprite[zoom - 1].width;
281  }
282 }
283 
284 static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
285 {
286  uint width = sprite->width + pad_left + pad_right;
287  uint height = sprite->height + pad_top + pad_bottom;
288 
289  if (width > UINT16_MAX || height > UINT16_MAX) return false;
290 
291  /* Copy source data and reallocate sprite memory. */
292  size_t sprite_size = static_cast<size_t>(sprite->width) * sprite->height;
293  std::vector<SpriteLoader::CommonPixel> src_data(sprite->data, sprite->data + sprite_size);
294  sprite->AllocateData(zoom, static_cast<size_t>(width) * height);
295 
296  /* Copy with padding to destination. */
297  SpriteLoader::CommonPixel *src = src_data.data();
298  SpriteLoader::CommonPixel *data = sprite->data;
299  for (uint y = 0; y < height; y++) {
300  if (y < pad_top || pad_bottom + y >= height) {
301  /* Top/bottom padding. */
302  MemSetT(data, 0, width);
303  data += width;
304  } else {
305  if (pad_left > 0) {
306  /* Pad left. */
307  MemSetT(data, 0, pad_left);
308  data += pad_left;
309  }
310 
311  /* Copy pixels. */
312  MemCpyT(data, src, sprite->width);
313  src += sprite->width;
314  data += sprite->width;
315 
316  if (pad_right > 0) {
317  /* Pad right. */
318  MemSetT(data, 0, pad_right);
319  data += pad_right;
320  }
321  }
322  }
323 
324  /* Update sprite size. */
325  sprite->width = width;
326  sprite->height = height;
327  sprite->x_offs -= pad_left;
328  sprite->y_offs -= pad_top;
329 
330  return true;
331 }
332 
333 static bool PadSprites(SpriteLoader::SpriteCollection &sprite, uint8_t sprite_avail, SpriteEncoder *encoder)
334 {
335  /* Get minimum top left corner coordinates. */
336  int min_xoffs = INT32_MAX;
337  int min_yoffs = INT32_MAX;
338  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
339  if (HasBit(sprite_avail, zoom)) {
340  min_xoffs = std::min(min_xoffs, ScaleByZoom(sprite[zoom].x_offs, zoom));
341  min_yoffs = std::min(min_yoffs, ScaleByZoom(sprite[zoom].y_offs, zoom));
342  }
343  }
344 
345  /* Get maximum dimensions taking necessary padding at the top left into account. */
346  int max_width = INT32_MIN;
347  int max_height = INT32_MIN;
348  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
349  if (HasBit(sprite_avail, zoom)) {
350  max_width = std::max(max_width, ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom), zoom));
351  max_height = std::max(max_height, ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom), zoom));
352  }
353  }
354 
355  /* Align height and width if required to match the needs of the sprite encoder. */
356  uint align = encoder->GetSpriteAlignment();
357  if (align != 0) {
358  max_width = Align(max_width, align);
359  max_height = Align(max_height, align);
360  }
361 
362  /* Pad sprites where needed. */
363  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
364  if (HasBit(sprite_avail, zoom)) {
365  /* Scaling the sprite dimensions in the blitter is done with rounding up,
366  * so a negative padding here is not an error. */
367  int pad_left = std::max(0, sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom));
368  int pad_top = std::max(0, sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom));
369  int pad_right = std::max(0, UnScaleByZoom(max_width, zoom) - sprite[zoom].width - pad_left);
370  int pad_bottom = std::max(0, UnScaleByZoom(max_height, zoom) - sprite[zoom].height - pad_top);
371 
372  if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
373  if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom)) return false;
374  }
375  }
376  }
377 
378  return true;
379 }
380 
381 static bool ResizeSprites(SpriteLoader::SpriteCollection &sprite, uint8_t sprite_avail, SpriteEncoder *encoder)
382 {
383  /* Create a fully zoomed image if it does not exist */
384  ZoomLevel first_avail = static_cast<ZoomLevel>(FindFirstBit(sprite_avail));
385  if (first_avail != ZOOM_LVL_MIN) {
386  if (!ResizeSpriteIn(sprite, first_avail, ZOOM_LVL_MIN)) return false;
387  SetBit(sprite_avail, ZOOM_LVL_MIN);
388  }
389 
390  /* Pad sprites to make sizes match. */
391  if (!PadSprites(sprite, sprite_avail, encoder)) return false;
392 
393  /* Create other missing zoom levels */
394  for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
395  if (zoom == ZOOM_LVL_MIN) continue;
396 
397  if (HasBit(sprite_avail, zoom)) {
398  /* Check that size and offsets match the fully zoomed image. */
399  assert(sprite[zoom].width == UnScaleByZoom(sprite[ZOOM_LVL_MIN].width, zoom));
400  assert(sprite[zoom].height == UnScaleByZoom(sprite[ZOOM_LVL_MIN].height, zoom));
401  assert(sprite[zoom].x_offs == UnScaleByZoom(sprite[ZOOM_LVL_MIN].x_offs, zoom));
402  assert(sprite[zoom].y_offs == UnScaleByZoom(sprite[ZOOM_LVL_MIN].y_offs, zoom));
403  }
404 
405  /* Zoom level is not available, or unusable, so create it */
406  if (!HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
407  }
408 
409  /* Upscale to desired sprite_min_zoom if provided sprite only had zoomed in versions. */
410  if (first_avail < _settings_client.gui.sprite_zoom_min) {
413  }
414 
415  return true;
416 }
417 
424 static void *ReadRecolourSprite(SpriteFile &file, uint num, SpriteAllocator &allocator)
425 {
426  /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
427  * number of recolour sprites that are 17 bytes that only exist in DOS
428  * GRFs which are the same as 257 byte recolour sprites, but with the last
429  * 240 bytes zeroed. */
430  static const uint RECOLOUR_SPRITE_SIZE = 257;
431  uint8_t *dest = allocator.Allocate<uint8_t>(std::max(RECOLOUR_SPRITE_SIZE, num));
432 
433  if (file.NeedsPaletteRemap()) {
434  uint8_t *dest_tmp = new uint8_t[std::max(RECOLOUR_SPRITE_SIZE, num)];
435 
436  /* Only a few recolour sprites are less than 257 bytes */
437  if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
438  file.ReadBlock(dest_tmp, num);
439 
440  /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
441  for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
442  dest[i] = _palmap_w2d[dest_tmp[_palmap_d2w[i - 1] + 1]];
443  }
444  delete[] dest_tmp;
445  } else {
446  file.ReadBlock(dest, num);
447  }
448 
449  return dest;
450 }
451 
461 static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, SpriteAllocator &allocator, SpriteEncoder *encoder)
462 {
463  /* Use current blitter if no other sprite encoder is given. */
464  if (encoder == nullptr) encoder = BlitterFactory::GetCurrentBlitter();
465 
466  SpriteFile &file = *sc->file;
467  size_t file_pos = sc->file_pos;
468 
469  assert(sprite_type != SpriteType::Recolour);
470  assert(IsMapgenSpriteID(id) == (sprite_type == SpriteType::MapGen));
471  assert(sc->type == sprite_type);
472 
473  Debug(sprite, 9, "Load sprite {}", id);
474 
476  uint8_t sprite_avail = 0;
477  sprite[ZOOM_LVL_MIN].type = sprite_type;
478 
479  SpriteLoaderGrf sprite_loader(file.GetContainerVersion());
480  if (sprite_type != SpriteType::MapGen && encoder->Is32BppSupported()) {
481  /* Try for 32bpp sprites first. */
482  sprite_avail = sprite_loader.LoadSprite(sprite, file, file_pos, sprite_type, true, sc->control_flags);
483  }
484  if (sprite_avail == 0) {
485  sprite_avail = sprite_loader.LoadSprite(sprite, file, file_pos, sprite_type, false, sc->control_flags);
486  }
487 
488  if (sprite_avail == 0) {
489  if (sprite_type == SpriteType::MapGen) return nullptr;
490  if (id == SPR_IMG_QUERY) UserError("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
491  return (void*)GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, &allocator, encoder);
492  }
493 
494  if (sprite_type == SpriteType::MapGen) {
495  /* Ugly hack to work around the problem that the old landscape
496  * generator assumes that those sprites are stored uncompressed in
497  * the memory, and they are only read directly by the code, never
498  * send to the blitter. So do not send it to the blitter (which will
499  * result in a data array in the format the blitter likes most), but
500  * extract the data directly and store that as sprite.
501  * Ugly: yes. Other solution: no. Blame the original author or
502  * something ;) The image should really have been a data-stream
503  * (so type = 0xFF basically). */
504  uint num = sprite[ZOOM_LVL_MIN].width * sprite[ZOOM_LVL_MIN].height;
505 
506  Sprite *s = allocator.Allocate<Sprite>(sizeof(*s) + num);
507  s->width = sprite[ZOOM_LVL_MIN].width;
508  s->height = sprite[ZOOM_LVL_MIN].height;
509  s->x_offs = sprite[ZOOM_LVL_MIN].x_offs;
510  s->y_offs = sprite[ZOOM_LVL_MIN].y_offs;
511 
512  SpriteLoader::CommonPixel *src = sprite[ZOOM_LVL_MIN].data;
513  uint8_t *dest = s->data;
514  while (num-- > 0) {
515  *dest++ = src->m;
516  src++;
517  }
518 
519  return s;
520  }
521 
522  if (!ResizeSprites(sprite, sprite_avail, encoder)) {
523  if (id == SPR_IMG_QUERY) UserError("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
524  return (void*)GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, &allocator, encoder);
525  }
526 
527  if (sprite[ZOOM_LVL_MIN].type == SpriteType::Font && _font_zoom != ZOOM_LVL_MIN) {
528  /* Make ZOOM_LVL_MIN be ZOOM_LVL_GUI */
529  sprite[ZOOM_LVL_MIN].width = sprite[_font_zoom].width;
530  sprite[ZOOM_LVL_MIN].height = sprite[_font_zoom].height;
531  sprite[ZOOM_LVL_MIN].x_offs = sprite[_font_zoom].x_offs;
532  sprite[ZOOM_LVL_MIN].y_offs = sprite[_font_zoom].y_offs;
533  sprite[ZOOM_LVL_MIN].data = sprite[_font_zoom].data;
534  sprite[ZOOM_LVL_MIN].colours = sprite[_font_zoom].colours;
535  }
536 
537  return encoder->Encode(sprite, allocator);
538 }
539 
541  size_t file_pos;
542  uint8_t control_flags;
543 };
544 
546 static std::map<uint32_t, GrfSpriteOffset> _grf_sprite_offsets;
547 
553 size_t GetGRFSpriteOffset(uint32_t id)
554 {
555  return _grf_sprite_offsets.find(id) != _grf_sprite_offsets.end() ? _grf_sprite_offsets[id].file_pos : SIZE_MAX;
556 }
557 
563 {
564  _grf_sprite_offsets.clear();
565 
566  if (file.GetContainerVersion() >= 2) {
567  /* Seek to sprite section of the GRF. */
568  size_t data_offset = file.ReadDword();
569  size_t old_pos = file.GetPos();
570  file.SeekTo(data_offset, SEEK_CUR);
571 
572  GrfSpriteOffset offset = { 0, 0 };
573 
574  /* Loop over all sprite section entries and store the file
575  * offset for each newly encountered ID. */
576  SpriteID id, prev_id = 0;
577  while ((id = file.ReadDword()) != 0) {
578  if (id != prev_id) {
579  _grf_sprite_offsets[prev_id] = offset;
580  offset.file_pos = file.GetPos() - 4;
581  offset.control_flags = 0;
582  }
583  prev_id = id;
584  uint length = file.ReadDword();
585  if (length > 0) {
586  uint8_t colour = file.ReadByte() & SCC_MASK;
587  length--;
588  if (length > 0) {
589  uint8_t zoom = file.ReadByte();
590  length--;
591  if (colour != 0 && zoom == 0) { // ZOOM_LVL_NORMAL (normal zoom)
592  SetBit(offset.control_flags, (colour != SCC_PAL) ? SCCF_ALLOW_ZOOM_MIN_1X_32BPP : SCCF_ALLOW_ZOOM_MIN_1X_PAL);
593  SetBit(offset.control_flags, (colour != SCC_PAL) ? SCCF_ALLOW_ZOOM_MIN_2X_32BPP : SCCF_ALLOW_ZOOM_MIN_2X_PAL);
594  }
595  if (colour != 0 && zoom == 2) { // ZOOM_LVL_IN_2X (2x zoomed in)
596  SetBit(offset.control_flags, (colour != SCC_PAL) ? SCCF_ALLOW_ZOOM_MIN_2X_32BPP : SCCF_ALLOW_ZOOM_MIN_2X_PAL);
597  }
598  }
599  }
600  file.SkipBytes(length);
601  }
602  if (prev_id != 0) _grf_sprite_offsets[prev_id] = offset;
603 
604  /* Continue processing the data section. */
605  file.SeekTo(old_pos, SEEK_SET);
606  }
607 }
608 
609 
618 bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
619 {
620  size_t file_pos = file.GetPos();
621 
622  /* Read sprite header. */
623  uint32_t num = file.GetContainerVersion() >= 2 ? file.ReadDword() : file.ReadWord();
624  if (num == 0) return false;
625  uint8_t grf_type = file.ReadByte();
626 
627  SpriteType type;
628  void *data = nullptr;
629  uint8_t control_flags = 0;
630  if (grf_type == 0xFF) {
631  /* Some NewGRF files have "empty" pseudo-sprites which are 1
632  * byte long. Catch these so the sprites won't be displayed. */
633  if (num == 1) {
634  file.ReadByte();
635  return false;
636  }
637  type = SpriteType::Recolour;
638  CacheSpriteAllocator allocator;
639  data = ReadRecolourSprite(file, num, allocator);
640  } else if (file.GetContainerVersion() >= 2 && grf_type == 0xFD) {
641  if (num != 4) {
642  /* Invalid sprite section include, ignore. */
643  file.SkipBytes(num);
644  return false;
645  }
646  /* It is not an error if no sprite with the provided ID is found in the sprite section. */
647  auto iter = _grf_sprite_offsets.find(file.ReadDword());
648  if (iter != _grf_sprite_offsets.end()) {
649  file_pos = iter->second.file_pos;
650  control_flags = iter->second.control_flags;
651  } else {
652  file_pos = SIZE_MAX;
653  }
654  type = SpriteType::Normal;
655  } else {
656  file.SkipBytes(7);
657  type = SkipSpriteData(file, grf_type, num - 8) ? SpriteType::Normal : SpriteType::Invalid;
658  /* Inline sprites are not supported for container version >= 2. */
659  if (file.GetContainerVersion() >= 2) return false;
660  }
661 
662  if (type == SpriteType::Invalid) return false;
663 
664  if (load_index >= MAX_SPRITES) {
665  UserError("Tried to load too many sprites (#{}; max {})", load_index, MAX_SPRITES);
666  }
667 
668  bool is_mapgen = IsMapgenSpriteID(load_index);
669 
670  if (is_mapgen) {
671  if (type != SpriteType::Normal) UserError("Uhm, would you be so kind not to load a NewGRF that changes the type of the map generator sprites?");
672  type = SpriteType::MapGen;
673  }
674 
675  SpriteCache *sc = AllocateSpriteCache(load_index);
676  sc->file = &file;
677  sc->file_pos = file_pos;
678  sc->ptr = data;
679  sc->lru = 0;
680  sc->id = file_sprite_id;
681  sc->type = type;
682  sc->warned = false;
683  sc->control_flags = control_flags;
684 
685  return true;
686 }
687 
688 
689 void DupSprite(SpriteID old_spr, SpriteID new_spr)
690 {
691  SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
692  SpriteCache *scold = GetSpriteCache(old_spr);
693 
694  scnew->file = scold->file;
695  scnew->file_pos = scold->file_pos;
696  scnew->ptr = nullptr;
697  scnew->id = scold->id;
698  scnew->type = scold->type;
699  scnew->warned = false;
700  scnew->control_flags = scold->control_flags;
701 }
702 
709 static const size_t S_FREE_MASK = sizeof(size_t) - 1;
710 
711 /* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
712 static_assert(sizeof(MemBlock) == sizeof(size_t));
713 /* make sure it's a power of two */
714 static_assert((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
715 
716 static inline MemBlock *NextBlock(MemBlock *block)
717 {
718  return (MemBlock*)((uint8_t*)block + (block->size & ~S_FREE_MASK));
719 }
720 
721 static size_t GetSpriteCacheUsage()
722 {
723  size_t tot_size = 0;
724  MemBlock *s;
725 
726  for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
727  if (!(s->size & S_FREE_MASK)) tot_size += s->size;
728  }
729 
730  return tot_size;
731 }
732 
733 
734 void IncreaseSpriteLRU()
735 {
736  /* Increase all LRU values */
737  if (_sprite_lru_counter > 16384) {
738  SpriteID i;
739 
740  Debug(sprite, 5, "Fixing lru {}, inuse={}", _sprite_lru_counter, GetSpriteCacheUsage());
741 
742  for (i = 0; i != _spritecache_items; i++) {
743  SpriteCache *sc = GetSpriteCache(i);
744  if (sc->ptr != nullptr) {
745  if (sc->lru >= 0) {
746  sc->lru = -1;
747  } else if (sc->lru != -32768) {
748  sc->lru--;
749  }
750  }
751  }
752  _sprite_lru_counter = 0;
753  }
754 
755  /* Compact sprite cache every now and then. */
756  if (++_compact_cache_counter >= 740) {
758  _compact_cache_counter = 0;
759  }
760 }
761 
766 static void CompactSpriteCache()
767 {
768  MemBlock *s;
769 
770  Debug(sprite, 3, "Compacting sprite cache, inuse={}", GetSpriteCacheUsage());
771 
772  for (s = _spritecache_ptr; s->size != 0;) {
773  if (s->size & S_FREE_MASK) {
774  MemBlock *next = NextBlock(s);
775  MemBlock temp;
776  SpriteID i;
777 
778  /* Since free blocks are automatically coalesced, this should hold true. */
779  assert(!(next->size & S_FREE_MASK));
780 
781  /* If the next block is the sentinel block, we can safely return */
782  if (next->size == 0) break;
783 
784  /* Locate the sprite belonging to the next pointer. */
785  for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
786  assert(i != _spritecache_items);
787  }
788 
789  GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
790  /* Swap this and the next block */
791  temp = *s;
792  memmove(s, next, next->size);
793  s = NextBlock(s);
794  *s = temp;
795 
796  /* Coalesce free blocks */
797  while (NextBlock(s)->size & S_FREE_MASK) {
798  s->size += NextBlock(s)->size & ~S_FREE_MASK;
799  }
800  } else {
801  s = NextBlock(s);
802  }
803  }
804 }
805 
810 static void DeleteEntryFromSpriteCache(uint item)
811 {
812  /* Mark the block as free (the block must be in use) */
813  MemBlock *s = (MemBlock*)GetSpriteCache(item)->ptr - 1;
814  assert(!(s->size & S_FREE_MASK));
815  s->size |= S_FREE_MASK;
816  GetSpriteCache(item)->ptr = nullptr;
817 
818  /* And coalesce adjacent free blocks */
819  for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
820  if (s->size & S_FREE_MASK) {
821  while (NextBlock(s)->size & S_FREE_MASK) {
822  s->size += NextBlock(s)->size & ~S_FREE_MASK;
823  }
824  }
825  }
826 }
827 
828 static void DeleteEntryFromSpriteCache()
829 {
830  uint best = UINT_MAX;
831  int cur_lru;
832 
833  Debug(sprite, 3, "DeleteEntryFromSpriteCache, inuse={}", GetSpriteCacheUsage());
834 
835  cur_lru = 0xffff;
836  for (SpriteID i = 0; i != _spritecache_items; i++) {
837  SpriteCache *sc = GetSpriteCache(i);
838  if (sc->type != SpriteType::Recolour && sc->ptr != nullptr && sc->lru < cur_lru) {
839  cur_lru = sc->lru;
840  best = i;
841  }
842  }
843 
844  /* Display an error message and die, in case we found no sprite at all.
845  * This shouldn't really happen, unless all sprites are locked. */
846  if (best == UINT_MAX) FatalError("Out of sprite memory");
847 
849 }
850 
852 {
853  mem_req += sizeof(MemBlock);
854 
855  /* Align this to correct boundary. This also makes sure at least one
856  * bit is not used, so we can use it for other things. */
857  mem_req = Align(mem_req, S_FREE_MASK + 1);
858 
859  for (;;) {
860  MemBlock *s;
861 
862  for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
863  if (s->size & S_FREE_MASK) {
864  size_t cur_size = s->size & ~S_FREE_MASK;
865 
866  /* Is the block exactly the size we need or
867  * big enough for an additional free block? */
868  if (cur_size == mem_req ||
869  cur_size >= mem_req + sizeof(MemBlock)) {
870  /* Set size and in use */
871  s->size = mem_req;
872 
873  /* Do we need to inject a free block too? */
874  if (cur_size != mem_req) {
875  NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
876  }
877 
878  return s->data;
879  }
880  }
881  }
882 
883  /* Reached sentinel, but no block found yet. Delete some old entry. */
885  }
886 }
887 
892 {
893  return MallocT<uint8_t>(size);
894 }
895 
897 {
898  this->data = std::make_unique<uint8_t[]>(size);
899  return this->data.get();
900 }
901 
911 static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, SpriteAllocator *allocator)
912 {
913  static const char * const sprite_types[] = {
914  "normal", // SpriteType::Normal
915  "map generator", // SpriteType::MapGen
916  "character", // SpriteType::Font
917  "recolour", // SpriteType::Recolour
918  };
919 
920  SpriteType available = sc->type;
921  if (requested == SpriteType::Font && available == SpriteType::Normal) {
922  if (sc->ptr == nullptr) sc->type = SpriteType::Font;
923  return GetRawSprite(sprite, sc->type, allocator);
924  }
925 
926  uint8_t warning_level = sc->warned ? 6 : 0;
927  sc->warned = true;
928  Debug(sprite, warning_level, "Tried to load {} sprite #{} as a {} sprite. Probable cause: NewGRF interference", sprite_types[static_cast<uint8_t>(available)], sprite, sprite_types[static_cast<uint8_t>(requested)]);
929 
930  switch (requested) {
931  case SpriteType::Normal:
932  if (sprite == SPR_IMG_QUERY) UserError("Uhm, would you be so kind not to load a NewGRF that makes the 'query' sprite a non-normal sprite?");
933  [[fallthrough]];
934  case SpriteType::Font:
935  return GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, allocator);
937  if (sprite == PALETTE_TO_DARK_BLUE) UserError("Uhm, would you be so kind not to load a NewGRF that makes the 'PALETTE_TO_DARK_BLUE' sprite a non-remap sprite?");
938  return GetRawSprite(PALETTE_TO_DARK_BLUE, SpriteType::Recolour, allocator);
939  case SpriteType::MapGen:
940  /* this shouldn't happen, overriding of SpriteType::MapGen sprites is checked in LoadNextSprite()
941  * (the only case the check fails is when these sprites weren't even loaded...) */
942  default:
943  NOT_REACHED();
944  }
945 }
946 
956 void *GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
957 {
958  assert(type != SpriteType::MapGen || IsMapgenSpriteID(sprite));
959  assert(type < SpriteType::Invalid);
960 
961  if (!SpriteExists(sprite)) {
962  Debug(sprite, 1, "Tried to load non-existing sprite #{}. Probable cause: Wrong/missing NewGRFs", sprite);
963 
964  /* SPR_IMG_QUERY is a BIG FAT RED ? */
965  sprite = SPR_IMG_QUERY;
966  }
967 
968  SpriteCache *sc = GetSpriteCache(sprite);
969 
970  if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
971 
972  if (allocator == nullptr && encoder == nullptr) {
973  /* Load sprite into/from spritecache */
974  CacheSpriteAllocator cache_allocator;
975 
976  /* Update LRU */
977  sc->lru = ++_sprite_lru_counter;
978 
979  /* Load the sprite, if it is not loaded, yet */
980  if (sc->ptr == nullptr) sc->ptr = ReadSprite(sc, sprite, type, cache_allocator, nullptr);
981 
982  return sc->ptr;
983  } else {
984  /* Do not use the spritecache, but a different allocator. */
985  return ReadSprite(sc, sprite, type, *allocator, encoder);
986  }
987 }
988 
989 
990 static void GfxInitSpriteCache()
991 {
992  /* initialize sprite cache heap */
994  uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
995 
996  /* Remember 'target_size' from the previous allocation attempt, so we do not try to reach the target_size multiple times in case of failure. */
997  static uint last_alloc_attempt = 0;
998 
999  if (_spritecache_ptr == nullptr || (_allocated_sprite_cache_size != target_size && target_size != last_alloc_attempt)) {
1000  delete[] reinterpret_cast<uint8_t *>(_spritecache_ptr);
1001 
1002  last_alloc_attempt = target_size;
1003  _allocated_sprite_cache_size = target_size;
1004 
1005  do {
1006  /* Try to allocate 50% more to make sure we do not allocate almost all available. */
1007  _spritecache_ptr = reinterpret_cast<MemBlock *>(new(std::nothrow) uint8_t[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
1008 
1009  if (_spritecache_ptr != nullptr) {
1010  /* Allocation succeeded, but we wanted less. */
1011  delete[] reinterpret_cast<uint8_t *>(_spritecache_ptr);
1012  _spritecache_ptr = reinterpret_cast<MemBlock *>(new uint8_t[_allocated_sprite_cache_size]);
1013  } else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) {
1014  UserError("Cannot allocate spritecache");
1015  } else {
1016  /* Try again to allocate half. */
1017  _allocated_sprite_cache_size >>= 1;
1018  }
1019  } while (_spritecache_ptr == nullptr);
1020 
1021  if (_allocated_sprite_cache_size != target_size) {
1022  Debug(misc, 0, "Not enough memory to allocate {} MiB of spritecache. Spritecache was reduced to {} MiB.", target_size / 1024 / 1024, _allocated_sprite_cache_size / 1024 / 1024);
1023 
1024  ErrorMessageData msg(STR_CONFIG_ERROR_OUT_OF_MEMORY, STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG);
1025  msg.SetDParam(0, target_size);
1026  msg.SetDParam(1, _allocated_sprite_cache_size);
1027  ScheduleErrorMessage(msg);
1028  }
1029  }
1030 
1031  /* A big free block */
1032  _spritecache_ptr->size = (_allocated_sprite_cache_size - sizeof(MemBlock)) | S_FREE_MASK;
1033  /* Sentinel block (identified by size == 0) */
1034  NextBlock(_spritecache_ptr)->size = 0;
1035 }
1036 
1037 void GfxInitSpriteMem()
1038 {
1039  GfxInitSpriteCache();
1040 
1041  /* Reset the spritecache 'pool' */
1042  free(_spritecache);
1043  _spritecache_items = 0;
1044  _spritecache = nullptr;
1045 
1046  _compact_cache_counter = 0;
1047  _sprite_files.clear();
1048 }
1049 
1055 {
1056  /* Clear sprite ptr for all cached items */
1057  for (uint i = 0; i != _spritecache_items; i++) {
1058  SpriteCache *sc = GetSpriteCache(i);
1059  if (sc->type != SpriteType::Recolour && sc->ptr != nullptr) DeleteEntryFromSpriteCache(i);
1060  }
1061 
1063 }
1064 
1070 {
1071  /* Clear sprite ptr for all cached font items */
1072  for (uint i = 0; i != _spritecache_items; i++) {
1073  SpriteCache *sc = GetSpriteCache(i);
1074  if (sc->type == SpriteType::Font && sc->ptr != nullptr) DeleteEntryFromSpriteCache(i);
1075  }
1076 }
1077 
T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
Definition: alloc_func.hpp:111
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:138
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
SpriteAllocator that allocates memory from the sprite cache.
void * AllocatePtr(size_t size) override
Allocate memory for a sprite.
The data of the error message.
Definition: error.h:31
void ReadBlock(void *ptr, size_t size)
Read a block.
size_t GetPos() const
Get position in the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
uint8_t ReadByte()
Read a byte from the file.
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
void * AllocatePtr(size_t size) override
Sprite allocator simply using malloc.
Interface for something that can allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
Interface for something that can encode a sprite.
virtual bool Is32BppSupported()=0
Can the sprite encoder make use of RGBA sprites?
virtual uint GetSpriteAlignment()
Get the value which the height and width on a sprite have to be aligned by.
virtual Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
RandomAccessFile with some extra information specific for sprite files.
bool NeedsPaletteRemap() const
Whether a palette remap is needed when loading sprites from this file.
uint8_t GetContainerVersion() const
Get the version number of container type used by the file.
void SeekToBegin()
Seek to the begin of the content, i.e.
Sprite loader for graphics coming from a (New)GRF.
Definition: grf.hpp:16
uint8_t LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
Definition: grf.cpp:353
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
void * AllocatePtr(size_t size) override
Allocate memory for a sprite.
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.
Definition: debug.h:37
Functions related to errors.
void ScheduleErrorMessage(ErrorList &datas)
Schedule a list of errors.
Definition: error_gui.cpp:452
Error reporting related functions.
Factory to 'query' all available blitters.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
Definition: fileio_type.h:115
ZoomLevel _font_zoom
Sprite font Zoom level (not clamped)
Definition: gfx.cpp:62
Functions related to the gfx engine.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:18
SpriteType
Types of sprites that might be loaded.
Definition: gfx_type.h:312
@ Recolour
Recolour sprite.
@ Font
A sprite used for fonts.
@ MapGen
Special sprite for the map generator.
@ Invalid
Pseudosprite or other unusable sprite, used only internally.
@ Normal
The most basic (normal) sprite.
Base for reading sprites from (New)GRFs.
Integer math functions.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition: math_func.hpp:37
Functions related to memory operations.
void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:23
void MemSetT(T *ptr, uint8_t value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
Translation tables from one GRF to another GRF.
const uint8_t _palmap_w2d[]
Converting from the Windows palette to the DOS palette.
static const uint8_t _palmap_d2w[]
Converting from the DOS palette to the Windows palette.
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
Types related to global configuration settings.
static void DeleteEntryFromSpriteCache(uint item)
Delete a single entry from the sprite cache.
static std::map< uint32_t, GrfSpriteOffset > _grf_sprite_offsets
Map from sprite numbers to position in the GRF file.
SpriteType GetSpriteType(SpriteID sprite)
Get the sprite type of a given sprite.
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
static void * ReadRecolourSprite(SpriteFile &file, uint num, SpriteAllocator &allocator)
Load a recolour sprite into memory.
uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
Count the sprites which originate from a specific file in a range of SpriteIDs.
void GfxClearFontSpriteCache()
Remove all encoded font sprites from the sprite cache without discarding sprite location information.
SpriteFile & OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
Open/get the SpriteFile that is cached for use in the sprite cache.
Definition: spritecache.cpp:93
std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles()
Get the list of cached SpriteFiles.
Definition: spritecache.cpp:81
bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
static void * HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, SpriteAllocator *allocator)
Handles the case when a sprite of different type is requested than is present in the SpriteCache.
SpriteFile * GetOriginFile(SpriteID sprite)
Get the SpriteFile of a given sprite.
uint GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
void * GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
Reads a sprite (from disk or sprite cache).
static void * ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, SpriteAllocator &allocator, SpriteEncoder *encoder)
Read a sprite from disk.
size_t GetGRFSpriteOffset(uint32_t id)
Get the file offset for a specific sprite in the sprite section of a GRF.
bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
Skip the given amount of sprite graphics data.
static SpriteFile * GetCachedSpriteFileByName(const std::string &filename)
Get the cached SpriteFile given the name of the file.
Definition: spritecache.cpp:67
uint32_t GetSpriteLocalID(SpriteID sprite)
Get the GRF-local sprite id of a given sprite.
static void CompactSpriteCache()
Called when holes in the sprite cache should be removed.
static const size_t S_FREE_MASK
S_FREE_MASK is used to mask-out lower bits of MemBlock::size If they are non-zero,...
void ReadGRFSpriteOffsets(SpriteFile &file)
Parse the sprite section of GRFs.
Functions to cache sprites in memory.
@ SCCF_ALLOW_ZOOM_MIN_2X_32BPP
Allow use of sprite min zoom setting at 2x in 32bpp mode.
Definition: spritecache.h:29
@ SCCF_ALLOW_ZOOM_MIN_1X_32BPP
Allow use of sprite min zoom setting at 1x in 32bpp mode.
Definition: spritecache.h:27
@ SCCF_ALLOW_ZOOM_MIN_1X_PAL
Allow use of sprite min zoom setting at 1x in palette mode.
Definition: spritecache.h:26
@ SCCF_ALLOW_ZOOM_MIN_2X_PAL
Allow use of sprite min zoom setting at 2x in palette mode.
Definition: spritecache.h:28
Internal functions to cache sprites in memory.
@ SCC_MASK
Mask of valid colour bits.
@ SCC_PAL
Sprite has palette data.
This file contains all sprite-related enums and defines.
static constexpr uint32_t MAX_SPRITES
Masks needed for sprite operations.
Definition: sprites.h:1555
Definition of base types and functions in a cross-platform compatible way.
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:334
GUISettings gui
settings related to the GUI
ZoomLevel sprite_zoom_min
maximum zoom level at which higher-resolution alternative sprites will be used (if available) instead...
uint8_t control_flags
Control flags, see SpriteCacheCtrlFlags.
bool warned
True iff the user has been warned about incorrect use of this sprite.
SpriteType type
In some cases a single sprite is misused by two NewGRFs. Once as real sprite and once as recolour spr...
SpriteFile * file
The file the sprite in this entry can be found in.
Definition of a common pixel in OpenTTD's realm.
uint8_t m
Remap-channel.
Structure for passing information from the sprite loader to the blitter.
static ReusableBuffer< SpriteLoader::CommonPixel > buffer[ZOOM_LVL_END]
Allocated memory to pass sprite data around.
void AllocateData(ZoomLevel zoom, size_t size)
Allocate the sprite data of this sprite.
uint16_t width
Width of the sprite.
int16_t x_offs
The x-offset of where the sprite will be drawn.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
int16_t y_offs
The y-offset of where the sprite will be drawn.
Data structure describing a sprite.
Definition: spritecache.h:17
uint16_t width
Width of the sprite.
Definition: spritecache.h:19
uint16_t height
Height of the sprite.
Definition: spritecache.h:18
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:21
uint8_t data[]
Sprite data.
Definition: spritecache.h:22
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:20
Base of all video drivers.
Functions related to zooming.
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition: zoom_func.h:22
int UnScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition: zoom_func.h:34
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:16
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:21
@ ZOOM_LVL_BEGIN
Begin for iteration.
Definition: zoom_type.h:18
@ ZOOM_LVL_IN_2X
Zoomed 2 times in.
Definition: zoom_type.h:20
@ ZOOM_LVL_END
End for iteration.
Definition: zoom_type.h:25
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition: zoom_type.h:41
@ ZOOM_LVL_IN_4X
Zoomed 4 times in.
Definition: zoom_type.h:19