OpenTTD Source 20250205-master-gfd85ab1e2c
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"
14#include "gfx_func.h"
15#include "error.h"
16#include "error_func.h"
17#include "zoom_func.h"
18#include "settings_type.h"
19#include "blitter/factory.hpp"
20#include "core/math_func.hpp"
21#include "core/mem_func.hpp"
23#include "spritecache.h"
25
26#include "table/sprites.h"
27#include "table/strings.h"
29
30#include "safeguards.h"
31
32/* Default of 4MB spritecache */
33uint _sprite_cache_size = 4;
34
35
36static std::vector<SpriteCache> _spritecache;
37static std::vector<std::unique_ptr<SpriteFile>> _sprite_files;
38
39static inline SpriteCache *GetSpriteCache(uint index)
40{
41 return &_spritecache[index];
42}
43
44SpriteCache *AllocateSpriteCache(uint index)
45{
46 if (index >= _spritecache.size()) {
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.resize(items);
53 }
54
55 return GetSpriteCache(index);
56}
57
63static SpriteFile *GetCachedSpriteFileByName(const std::string &filename)
64{
65 for (auto &f : _sprite_files) {
66 if (f->GetFilename() == filename) {
67 return f.get();
68 }
69 }
70 return nullptr;
71}
72
77std::span<const std::unique_ptr<SpriteFile>> GetCachedSpriteFiles()
78{
79 return _sprite_files;
80}
81
89SpriteFile &OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
90{
91 SpriteFile *file = GetCachedSpriteFileByName(filename);
92 if (file == nullptr) {
93 file = _sprite_files.insert(std::end(_sprite_files), std::make_unique<SpriteFile>(filename, subdir, palette_remap))->get();
94 } else {
95 file->SeekToBegin();
96 }
97 return *file;
98}
99
100struct MemBlock {
101 size_t size;
102 uint8_t data[];
103};
104
105static uint _sprite_lru_counter;
106static MemBlock *_spritecache_ptr;
107static uint _allocated_sprite_cache_size = 0;
108static int _compact_cache_counter;
109
110static void CompactSpriteCache();
111
118bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
119{
120 if (type & 2) {
121 file.SkipBytes(num);
122 } else {
123 while (num > 0) {
124 int8_t i = file.ReadByte();
125 if (i >= 0) {
126 int size = (i == 0) ? 0x80 : i;
127 if (size > num) return false;
128 num -= size;
129 file.SkipBytes(size);
130 } else {
131 i = -(i >> 3);
132 num -= i;
133 file.ReadByte();
134 }
135 }
136 }
137 return true;
138}
139
140/* Check if the given Sprite ID exists */
141bool SpriteExists(SpriteID id)
142{
143 if (id >= _spritecache.size()) return false;
144
145 /* Special case for Sprite ID zero -- its position is also 0... */
146 if (id == 0) return true;
147 return !(GetSpriteCache(id)->file_pos == 0 && GetSpriteCache(id)->file == nullptr);
148}
149
156{
157 if (!SpriteExists(sprite)) return SpriteType::Invalid;
158 return GetSpriteCache(sprite)->type;
159}
160
167{
168 if (!SpriteExists(sprite)) return nullptr;
169 return GetSpriteCache(sprite)->file;
170}
171
178{
179 if (!SpriteExists(sprite)) return 0;
180 return GetSpriteCache(sprite)->id;
181}
182
190uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
191{
192 SpriteFile *file = GetCachedSpriteFileByName(filename);
193 if (file == nullptr) return 0;
194
195 uint count = 0;
196 for (SpriteID i = begin; i != end; i++) {
197 if (SpriteExists(i)) {
198 SpriteCache *sc = GetSpriteCache(i);
199 if (sc->file == file) {
200 count++;
201 Debug(sprite, 4, "Sprite: {}", i);
202 }
203 }
204 }
205 return count;
206}
207
217{
218 return static_cast<SpriteID>(_spritecache.size());
219}
220
221static bool ResizeSpriteIn(SpriteLoader::SpriteCollection &sprite, ZoomLevel src, ZoomLevel tgt)
222{
223 uint8_t scaled_1 = ScaleByZoom(1, (ZoomLevel)(src - tgt));
224
225 /* Check for possible memory overflow. */
226 if (sprite[src].width * scaled_1 > UINT16_MAX || sprite[src].height * scaled_1 > UINT16_MAX) return false;
227
228 sprite[tgt].width = sprite[src].width * scaled_1;
229 sprite[tgt].height = sprite[src].height * scaled_1;
230 sprite[tgt].x_offs = sprite[src].x_offs * scaled_1;
231 sprite[tgt].y_offs = sprite[src].y_offs * scaled_1;
232 sprite[tgt].colours = sprite[src].colours;
233
234 sprite[tgt].AllocateData(tgt, static_cast<size_t>(sprite[tgt].width) * sprite[tgt].height);
235
236 SpriteLoader::CommonPixel *dst = sprite[tgt].data;
237 for (int y = 0; y < sprite[tgt].height; y++) {
238 const SpriteLoader::CommonPixel *src_ln = &sprite[src].data[y / scaled_1 * sprite[src].width];
239 for (int x = 0; x < sprite[tgt].width; x++) {
240 *dst = src_ln[x / scaled_1];
241 dst++;
242 }
243 }
244
245 return true;
246}
247
248static void ResizeSpriteOut(SpriteLoader::SpriteCollection &sprite, ZoomLevel zoom)
249{
250 /* Algorithm based on 32bpp_Optimized::ResizeSprite() */
251 sprite[zoom].width = UnScaleByZoom(sprite[ZOOM_LVL_MIN].width, zoom);
252 sprite[zoom].height = UnScaleByZoom(sprite[ZOOM_LVL_MIN].height, zoom);
253 sprite[zoom].x_offs = UnScaleByZoom(sprite[ZOOM_LVL_MIN].x_offs, zoom);
254 sprite[zoom].y_offs = UnScaleByZoom(sprite[ZOOM_LVL_MIN].y_offs, zoom);
255 sprite[zoom].colours = sprite[ZOOM_LVL_MIN].colours;
256
257 sprite[zoom].AllocateData(zoom, static_cast<size_t>(sprite[zoom].height) * sprite[zoom].width);
258
259 SpriteLoader::CommonPixel *dst = sprite[zoom].data;
260 const SpriteLoader::CommonPixel *src = sprite[zoom - 1].data;
261 [[maybe_unused]] const SpriteLoader::CommonPixel *src_end = src + sprite[zoom - 1].height * sprite[zoom - 1].width;
262
263 for (uint y = 0; y < sprite[zoom].height; y++) {
264 const SpriteLoader::CommonPixel *src_ln = src + sprite[zoom - 1].width;
265 assert(src_ln <= src_end);
266 for (uint x = 0; x < sprite[zoom].width; x++) {
267 assert(src < src_ln);
268 if (src + 1 != src_ln && (src + 1)->a != 0) {
269 *dst = *(src + 1);
270 } else {
271 *dst = *src;
272 }
273 dst++;
274 src += 2;
275 }
276 src = src_ln + sprite[zoom - 1].width;
277 }
278}
279
280static bool PadSingleSprite(SpriteLoader::Sprite *sprite, ZoomLevel zoom, uint pad_left, uint pad_top, uint pad_right, uint pad_bottom)
281{
282 uint width = sprite->width + pad_left + pad_right;
283 uint height = sprite->height + pad_top + pad_bottom;
284
285 if (width > UINT16_MAX || height > UINT16_MAX) return false;
286
287 /* Copy source data and reallocate sprite memory. */
288 size_t sprite_size = static_cast<size_t>(sprite->width) * sprite->height;
289 std::vector<SpriteLoader::CommonPixel> src_data(sprite->data, sprite->data + sprite_size);
290 sprite->AllocateData(zoom, static_cast<size_t>(width) * height);
291
292 /* Copy with padding to destination. */
293 SpriteLoader::CommonPixel *src = src_data.data();
294 SpriteLoader::CommonPixel *data = sprite->data;
295 for (uint y = 0; y < height; y++) {
296 if (y < pad_top || pad_bottom + y >= height) {
297 /* Top/bottom padding. */
298 MemSetT(data, 0, width);
299 data += width;
300 } else {
301 if (pad_left > 0) {
302 /* Pad left. */
303 MemSetT(data, 0, pad_left);
304 data += pad_left;
305 }
306
307 /* Copy pixels. */
308 MemCpyT(data, src, sprite->width);
309 src += sprite->width;
310 data += sprite->width;
311
312 if (pad_right > 0) {
313 /* Pad right. */
314 MemSetT(data, 0, pad_right);
315 data += pad_right;
316 }
317 }
318 }
319
320 /* Update sprite size. */
321 sprite->width = width;
322 sprite->height = height;
323 sprite->x_offs -= pad_left;
324 sprite->y_offs -= pad_top;
325
326 return true;
327}
328
329static bool PadSprites(SpriteLoader::SpriteCollection &sprite, uint8_t sprite_avail, SpriteEncoder *encoder)
330{
331 /* Get minimum top left corner coordinates. */
332 int min_xoffs = INT32_MAX;
333 int min_yoffs = INT32_MAX;
334 for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
335 if (HasBit(sprite_avail, zoom)) {
336 min_xoffs = std::min(min_xoffs, ScaleByZoom(sprite[zoom].x_offs, zoom));
337 min_yoffs = std::min(min_yoffs, ScaleByZoom(sprite[zoom].y_offs, zoom));
338 }
339 }
340
341 /* Get maximum dimensions taking necessary padding at the top left into account. */
342 int max_width = INT32_MIN;
343 int max_height = INT32_MIN;
344 for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
345 if (HasBit(sprite_avail, zoom)) {
346 max_width = std::max(max_width, ScaleByZoom(sprite[zoom].width + sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom), zoom));
347 max_height = std::max(max_height, ScaleByZoom(sprite[zoom].height + sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom), zoom));
348 }
349 }
350
351 /* Align height and width if required to match the needs of the sprite encoder. */
352 uint align = encoder->GetSpriteAlignment();
353 if (align != 0) {
354 max_width = Align(max_width, align);
355 max_height = Align(max_height, align);
356 }
357
358 /* Pad sprites where needed. */
359 for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
360 if (HasBit(sprite_avail, zoom)) {
361 /* Scaling the sprite dimensions in the blitter is done with rounding up,
362 * so a negative padding here is not an error. */
363 int pad_left = std::max(0, sprite[zoom].x_offs - UnScaleByZoom(min_xoffs, zoom));
364 int pad_top = std::max(0, sprite[zoom].y_offs - UnScaleByZoom(min_yoffs, zoom));
365 int pad_right = std::max(0, UnScaleByZoom(max_width, zoom) - sprite[zoom].width - pad_left);
366 int pad_bottom = std::max(0, UnScaleByZoom(max_height, zoom) - sprite[zoom].height - pad_top);
367
368 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0) {
369 if (!PadSingleSprite(&sprite[zoom], zoom, pad_left, pad_top, pad_right, pad_bottom)) return false;
370 }
371 }
372 }
373
374 return true;
375}
376
377static bool ResizeSprites(SpriteLoader::SpriteCollection &sprite, uint8_t sprite_avail, SpriteEncoder *encoder)
378{
379 /* Create a fully zoomed image if it does not exist */
380 ZoomLevel first_avail = static_cast<ZoomLevel>(FindFirstBit(sprite_avail));
381 if (first_avail != ZOOM_LVL_MIN) {
382 if (!ResizeSpriteIn(sprite, first_avail, ZOOM_LVL_MIN)) return false;
383 SetBit(sprite_avail, ZOOM_LVL_MIN);
384 }
385
386 /* Pad sprites to make sizes match. */
387 if (!PadSprites(sprite, sprite_avail, encoder)) return false;
388
389 /* Create other missing zoom levels */
390 for (ZoomLevel zoom = ZOOM_LVL_BEGIN; zoom != ZOOM_LVL_END; zoom++) {
391 if (zoom == ZOOM_LVL_MIN) continue;
392
393 if (HasBit(sprite_avail, zoom)) {
394 /* Check that size and offsets match the fully zoomed image. */
395 assert(sprite[zoom].width == UnScaleByZoom(sprite[ZOOM_LVL_MIN].width, zoom));
396 assert(sprite[zoom].height == UnScaleByZoom(sprite[ZOOM_LVL_MIN].height, zoom));
397 assert(sprite[zoom].x_offs == UnScaleByZoom(sprite[ZOOM_LVL_MIN].x_offs, zoom));
398 assert(sprite[zoom].y_offs == UnScaleByZoom(sprite[ZOOM_LVL_MIN].y_offs, zoom));
399 }
400
401 /* Zoom level is not available, or unusable, so create it */
402 if (!HasBit(sprite_avail, zoom)) ResizeSpriteOut(sprite, zoom);
403 }
404
405 /* Upscale to desired sprite_min_zoom if provided sprite only had zoomed in versions. */
406 if (first_avail < _settings_client.gui.sprite_zoom_min) {
409 }
410
411 return true;
412}
413
422static void *ReadRecolourSprite(SpriteFile &file, size_t file_pos, uint num, SpriteAllocator &allocator)
423{
424 /* "Normal" recolour sprites are ALWAYS 257 bytes. Then there is a small
425 * number of recolour sprites that are 17 bytes that only exist in DOS
426 * GRFs which are the same as 257 byte recolour sprites, but with the last
427 * 240 bytes zeroed. */
428 static const uint RECOLOUR_SPRITE_SIZE = 257;
429 uint8_t *dest = allocator.Allocate<uint8_t>(std::max(RECOLOUR_SPRITE_SIZE, num));
430
431 file.SeekTo(file_pos, SEEK_SET);
432 if (file.NeedsPaletteRemap()) {
433 uint8_t *dest_tmp = new uint8_t[std::max(RECOLOUR_SPRITE_SIZE, num)];
434
435 /* Only a few recolour sprites are less than 257 bytes */
436 if (num < RECOLOUR_SPRITE_SIZE) memset(dest_tmp, 0, RECOLOUR_SPRITE_SIZE);
437 file.ReadBlock(dest_tmp, num);
438
439 /* The data of index 0 is never used; "literal 00" according to the (New)GRF specs. */
440 for (uint i = 1; i < RECOLOUR_SPRITE_SIZE; i++) {
441 dest[i] = _palmap_w2d[dest_tmp[_palmap_d2w[i - 1] + 1]];
442 }
443 delete[] dest_tmp;
444 } else {
445 file.ReadBlock(dest, num);
446 }
447
448 return dest;
449}
450
460static void *ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, SpriteAllocator &allocator, SpriteEncoder *encoder)
461{
462 /* Use current blitter if no other sprite encoder is given. */
463 if (encoder == nullptr) encoder = BlitterFactory::GetCurrentBlitter();
464
465 SpriteFile &file = *sc->file;
466 size_t file_pos = sc->file_pos;
467
468 assert(sprite_type != SpriteType::Recolour);
469 assert(IsMapgenSpriteID(id) == (sprite_type == SpriteType::MapGen));
470 assert(sc->type == sprite_type);
471
472 Debug(sprite, 9, "Load sprite {}", id);
473
475 uint8_t sprite_avail = 0;
476 uint8_t avail_8bpp = 0;
477 uint8_t avail_32bpp = 0;
478 sprite[ZOOM_LVL_MIN].type = sprite_type;
479
480 SpriteLoaderGrf sprite_loader(file.GetContainerVersion());
481 if (sprite_type != SpriteType::MapGen && encoder->Is32BppSupported()) {
482 /* Try for 32bpp sprites first. */
483 sprite_avail = sprite_loader.LoadSprite(sprite, file, file_pos, sprite_type, true, sc->control_flags, avail_8bpp, avail_32bpp);
484 }
485 if (sprite_avail == 0) {
486 sprite_avail = sprite_loader.LoadSprite(sprite, file, file_pos, sprite_type, false, sc->control_flags, avail_8bpp, avail_32bpp);
487 if (sprite_type == SpriteType::Normal && avail_32bpp != 0 && !encoder->Is32BppSupported() && sprite_avail == 0) {
488 /* No 8bpp available, try converting from 32bpp. */
489 SpriteLoaderMakeIndexed make_indexed(sprite_loader);
490 sprite_avail = make_indexed.LoadSprite(sprite, file, file_pos, sprite_type, true, sc->control_flags, sprite_avail, avail_32bpp);
491 }
492 }
493
494 if (sprite_avail == 0) {
495 if (sprite_type == SpriteType::MapGen) return nullptr;
496 if (id == SPR_IMG_QUERY) UserError("Okay... something went horribly wrong. I couldn't load the fallback sprite. What should I do?");
497 return (void*)GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, &allocator, encoder);
498 }
499
500 if (sprite_type == SpriteType::MapGen) {
501 /* Ugly hack to work around the problem that the old landscape
502 * generator assumes that those sprites are stored uncompressed in
503 * the memory, and they are only read directly by the code, never
504 * send to the blitter. So do not send it to the blitter (which will
505 * result in a data array in the format the blitter likes most), but
506 * extract the data directly and store that as sprite.
507 * Ugly: yes. Other solution: no. Blame the original author or
508 * something ;) The image should really have been a data-stream
509 * (so type = 0xFF basically). */
510 uint num = sprite[ZOOM_LVL_MIN].width * sprite[ZOOM_LVL_MIN].height;
511
512 Sprite *s = allocator.Allocate<Sprite>(sizeof(*s) + num);
513 s->width = sprite[ZOOM_LVL_MIN].width;
514 s->height = sprite[ZOOM_LVL_MIN].height;
515 s->x_offs = sprite[ZOOM_LVL_MIN].x_offs;
516 s->y_offs = sprite[ZOOM_LVL_MIN].y_offs;
517
518 SpriteLoader::CommonPixel *src = sprite[ZOOM_LVL_MIN].data;
519 uint8_t *dest = s->data;
520 while (num-- > 0) {
521 *dest++ = src->m;
522 src++;
523 }
524
525 return s;
526 }
527
528 if (!ResizeSprites(sprite, sprite_avail, encoder)) {
529 if (id == SPR_IMG_QUERY) UserError("Okay... something went horribly wrong. I couldn't resize the fallback sprite. What should I do?");
530 return (void*)GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, &allocator, encoder);
531 }
532
533 if (sprite[ZOOM_LVL_MIN].type == SpriteType::Font && _font_zoom != ZOOM_LVL_MIN) {
534 /* Make ZOOM_LVL_MIN be ZOOM_LVL_GUI */
535 sprite[ZOOM_LVL_MIN].width = sprite[_font_zoom].width;
536 sprite[ZOOM_LVL_MIN].height = sprite[_font_zoom].height;
537 sprite[ZOOM_LVL_MIN].x_offs = sprite[_font_zoom].x_offs;
538 sprite[ZOOM_LVL_MIN].y_offs = sprite[_font_zoom].y_offs;
539 sprite[ZOOM_LVL_MIN].data = sprite[_font_zoom].data;
540 sprite[ZOOM_LVL_MIN].colours = sprite[_font_zoom].colours;
541 }
542
543 return encoder->Encode(sprite, allocator);
544}
545
547 size_t file_pos;
548 uint8_t control_flags;
549};
550
552static std::map<uint32_t, GrfSpriteOffset> _grf_sprite_offsets;
553
559size_t GetGRFSpriteOffset(uint32_t id)
560{
561 return _grf_sprite_offsets.find(id) != _grf_sprite_offsets.end() ? _grf_sprite_offsets[id].file_pos : SIZE_MAX;
562}
563
569{
570 _grf_sprite_offsets.clear();
571
572 if (file.GetContainerVersion() >= 2) {
573 /* Seek to sprite section of the GRF. */
574 size_t data_offset = file.ReadDword();
575 size_t old_pos = file.GetPos();
576 file.SeekTo(data_offset, SEEK_CUR);
577
578 GrfSpriteOffset offset = { 0, 0 };
579
580 /* Loop over all sprite section entries and store the file
581 * offset for each newly encountered ID. */
582 SpriteID id, prev_id = 0;
583 while ((id = file.ReadDword()) != 0) {
584 if (id != prev_id) {
585 _grf_sprite_offsets[prev_id] = offset;
586 offset.file_pos = file.GetPos() - 4;
587 offset.control_flags = 0;
588 }
589 prev_id = id;
590 uint length = file.ReadDword();
591 if (length > 0) {
592 uint8_t colour = file.ReadByte() & SCC_MASK;
593 length--;
594 if (length > 0) {
595 uint8_t zoom = file.ReadByte();
596 length--;
597 if (colour != 0 && zoom == 0) { // ZOOM_LVL_NORMAL (normal zoom)
598 SetBit(offset.control_flags, (colour != SCC_PAL) ? SCCF_ALLOW_ZOOM_MIN_1X_32BPP : SCCF_ALLOW_ZOOM_MIN_1X_PAL);
599 SetBit(offset.control_flags, (colour != SCC_PAL) ? SCCF_ALLOW_ZOOM_MIN_2X_32BPP : SCCF_ALLOW_ZOOM_MIN_2X_PAL);
600 }
601 if (colour != 0 && zoom == 2) { // ZOOM_LVL_IN_2X (2x zoomed in)
602 SetBit(offset.control_flags, (colour != SCC_PAL) ? SCCF_ALLOW_ZOOM_MIN_2X_32BPP : SCCF_ALLOW_ZOOM_MIN_2X_PAL);
603 }
604 }
605 }
606 file.SkipBytes(length);
607 }
608 if (prev_id != 0) _grf_sprite_offsets[prev_id] = offset;
609
610 /* Continue processing the data section. */
611 file.SeekTo(old_pos, SEEK_SET);
612 }
613}
614
615
624bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
625{
626 size_t file_pos = file.GetPos();
627
628 /* Read sprite header. */
629 uint32_t num = file.GetContainerVersion() >= 2 ? file.ReadDword() : file.ReadWord();
630 if (num == 0) return false;
631 uint8_t grf_type = file.ReadByte();
632
633 SpriteType type;
634 void *data = nullptr;
635 uint8_t control_flags = 0;
636 if (grf_type == 0xFF) {
637 /* Some NewGRF files have "empty" pseudo-sprites which are 1
638 * byte long. Catch these so the sprites won't be displayed. */
639 if (num == 1) {
640 file.ReadByte();
641 return false;
642 }
643 file_pos = file.GetPos();
645 file.SkipBytes(num);
646 } else if (file.GetContainerVersion() >= 2 && grf_type == 0xFD) {
647 if (num != 4) {
648 /* Invalid sprite section include, ignore. */
649 file.SkipBytes(num);
650 return false;
651 }
652 /* It is not an error if no sprite with the provided ID is found in the sprite section. */
653 auto iter = _grf_sprite_offsets.find(file.ReadDword());
654 if (iter != _grf_sprite_offsets.end()) {
655 file_pos = iter->second.file_pos;
656 control_flags = iter->second.control_flags;
657 } else {
658 file_pos = SIZE_MAX;
659 }
660 type = SpriteType::Normal;
661 } else {
662 file.SkipBytes(7);
663 type = SkipSpriteData(file, grf_type, num - 8) ? SpriteType::Normal : SpriteType::Invalid;
664 /* Inline sprites are not supported for container version >= 2. */
665 if (file.GetContainerVersion() >= 2) return false;
666 }
667
668 if (type == SpriteType::Invalid) return false;
669
670 if (load_index >= MAX_SPRITES) {
671 UserError("Tried to load too many sprites (#{}; max {})", load_index, MAX_SPRITES);
672 }
673
674 bool is_mapgen = IsMapgenSpriteID(load_index);
675
676 if (is_mapgen) {
677 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?");
678 type = SpriteType::MapGen;
679 }
680
681 SpriteCache *sc = AllocateSpriteCache(load_index);
682 sc->file = &file;
683 sc->file_pos = file_pos;
684 sc->length = num;
685 sc->ptr = data;
686 sc->lru = 0;
687 sc->id = file_sprite_id;
688 sc->type = type;
689 sc->warned = false;
690 sc->control_flags = control_flags;
691
692 return true;
693}
694
695
696void DupSprite(SpriteID old_spr, SpriteID new_spr)
697{
698 SpriteCache *scnew = AllocateSpriteCache(new_spr); // may reallocate: so put it first
699 SpriteCache *scold = GetSpriteCache(old_spr);
700
701 scnew->file = scold->file;
702 scnew->file_pos = scold->file_pos;
703 scnew->ptr = nullptr;
704 scnew->id = scold->id;
705 scnew->type = scold->type;
706 scnew->warned = false;
707 scnew->control_flags = scold->control_flags;
708}
709
716static const size_t S_FREE_MASK = sizeof(size_t) - 1;
717
718/* to make sure nobody adds things to MemBlock without checking S_FREE_MASK first */
719static_assert(sizeof(MemBlock) == sizeof(size_t));
720/* make sure it's a power of two */
721static_assert((sizeof(size_t) & (sizeof(size_t) - 1)) == 0);
722
723static inline MemBlock *NextBlock(MemBlock *block)
724{
725 return (MemBlock*)((uint8_t*)block + (block->size & ~S_FREE_MASK));
726}
727
728static size_t GetSpriteCacheUsage()
729{
730 size_t tot_size = 0;
731 MemBlock *s;
732
733 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
734 if (!(s->size & S_FREE_MASK)) tot_size += s->size;
735 }
736
737 return tot_size;
738}
739
740
741void IncreaseSpriteLRU()
742{
743 /* Increase all LRU values */
744 if (_sprite_lru_counter > 16384) {
745 Debug(sprite, 5, "Fixing lru {}, inuse={}", _sprite_lru_counter, GetSpriteCacheUsage());
746
747 for (SpriteCache &sc : _spritecache) {
748 if (sc.ptr != nullptr) {
749 if (sc.lru >= 0) {
750 sc.lru = -1;
751 } else if (sc.lru != -32768) {
752 sc.lru--;
753 }
754 }
755 }
756 _sprite_lru_counter = 0;
757 }
758
759 /* Compact sprite cache every now and then. */
760 if (++_compact_cache_counter >= 740) {
762 _compact_cache_counter = 0;
763 }
764}
765
771{
772 MemBlock *s;
773
774 Debug(sprite, 3, "Compacting sprite cache, inuse={}", GetSpriteCacheUsage());
775
776 for (s = _spritecache_ptr; s->size != 0;) {
777 if (s->size & S_FREE_MASK) {
778 MemBlock *next = NextBlock(s);
779 MemBlock temp;
780 SpriteID i;
781
782 /* Since free blocks are automatically coalesced, this should hold true. */
783 assert(!(next->size & S_FREE_MASK));
784
785 /* If the next block is the sentinel block, we can safely return */
786 if (next->size == 0) break;
787
788 /* Locate the sprite belonging to the next pointer. */
789 for (i = 0; GetSpriteCache(i)->ptr != next->data; i++) {
790 assert(i != _spritecache.size());
791 }
792
793 GetSpriteCache(i)->ptr = s->data; // Adjust sprite array entry
794 /* Swap this and the next block */
795 temp = *s;
796 memmove(s, next, next->size);
797 s = NextBlock(s);
798 *s = temp;
799
800 /* Coalesce free blocks */
801 while (NextBlock(s)->size & S_FREE_MASK) {
802 s->size += NextBlock(s)->size & ~S_FREE_MASK;
803 }
804 } else {
805 s = NextBlock(s);
806 }
807 }
808}
809
815{
816 /* Mark the block as free (the block must be in use) */
817 MemBlock *s = static_cast<MemBlock *>(item->ptr) - 1;
818 assert(!(s->size & S_FREE_MASK));
819 s->size |= S_FREE_MASK;
820 item->ptr = nullptr;
821
822 /* And coalesce adjacent free blocks */
823 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
824 if (s->size & S_FREE_MASK) {
825 while (NextBlock(s)->size & S_FREE_MASK) {
826 s->size += NextBlock(s)->size & ~S_FREE_MASK;
827 }
828 }
829 }
830}
831
832static void DeleteEntryFromSpriteCache()
833{
834 Debug(sprite, 3, "DeleteEntryFromSpriteCache, inuse={}", GetSpriteCacheUsage());
835
836 SpriteCache *best = nullptr;
837 int cur_lru = 0xffff;
838 for (SpriteCache &sc : _spritecache) {
839 if (sc.ptr != nullptr && sc.lru < cur_lru) {
840 cur_lru = sc.lru;
841 best = &sc;
842 }
843 }
844
845 /* Display an error message and die, in case we found no sprite at all.
846 * This shouldn't really happen, unless all sprites are locked. */
847 if (best == nullptr) FatalError("Out of sprite memory");
848
850}
851
853{
854 mem_req += sizeof(MemBlock);
855
856 /* Align this to correct boundary. This also makes sure at least one
857 * bit is not used, so we can use it for other things. */
858 mem_req = Align(mem_req, S_FREE_MASK + 1);
859
860 for (;;) {
861 MemBlock *s;
862
863 for (s = _spritecache_ptr; s->size != 0; s = NextBlock(s)) {
864 if (s->size & S_FREE_MASK) {
865 size_t cur_size = s->size & ~S_FREE_MASK;
866
867 /* Is the block exactly the size we need or
868 * big enough for an additional free block? */
869 if (cur_size == mem_req ||
870 cur_size >= mem_req + sizeof(MemBlock)) {
871 /* Set size and in use */
872 s->size = mem_req;
873
874 /* Do we need to inject a free block too? */
875 if (cur_size != mem_req) {
876 NextBlock(s)->size = (cur_size - mem_req) | S_FREE_MASK;
877 }
878
879 return s->data;
880 }
881 }
882 }
883
884 /* Reached sentinel, but no block found yet. Delete some old entry. */
886 }
887}
888
893{
894 return MallocT<uint8_t>(size);
895}
896
898{
899 this->data = std::make_unique<uint8_t[]>(size);
900 return this->data.get();
901}
902
912static void *HandleInvalidSpriteRequest(SpriteID sprite, SpriteType requested, SpriteCache *sc, SpriteAllocator *allocator)
913{
914 static const char * const sprite_types[] = {
915 "normal", // SpriteType::Normal
916 "map generator", // SpriteType::MapGen
917 "character", // SpriteType::Font
918 "recolour", // SpriteType::Recolour
919 };
920
921 SpriteType available = sc->type;
922 if (requested == SpriteType::Font && available == SpriteType::Normal) {
923 if (sc->ptr == nullptr) sc->type = SpriteType::Font;
924 return GetRawSprite(sprite, sc->type, allocator);
925 }
926
927 uint8_t warning_level = sc->warned ? 6 : 0;
928 sc->warned = true;
929 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)]);
930
931 switch (requested) {
933 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?");
934 [[fallthrough]];
935 case SpriteType::Font:
936 return GetRawSprite(SPR_IMG_QUERY, SpriteType::Normal, allocator);
938 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?");
939 return GetRawSprite(PALETTE_TO_DARK_BLUE, SpriteType::Recolour, allocator);
941 /* this shouldn't happen, overriding of SpriteType::MapGen sprites is checked in LoadNextSprite()
942 * (the only case the check fails is when these sprites weren't even loaded...) */
943 default:
944 NOT_REACHED();
945 }
946}
947
957void *GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
958{
959 assert(type != SpriteType::MapGen || IsMapgenSpriteID(sprite));
960 assert(type < SpriteType::Invalid);
961
962 if (!SpriteExists(sprite)) {
963 Debug(sprite, 1, "Tried to load non-existing sprite #{}. Probable cause: Wrong/missing NewGRFs", sprite);
964
965 /* SPR_IMG_QUERY is a BIG FAT RED ? */
966 sprite = SPR_IMG_QUERY;
967 }
968
969 SpriteCache *sc = GetSpriteCache(sprite);
970
971 if (sc->type != type) return HandleInvalidSpriteRequest(sprite, type, sc, allocator);
972
973 if (allocator == nullptr && encoder == nullptr) {
974 /* Load sprite into/from spritecache */
975 CacheSpriteAllocator cache_allocator;
976
977 /* Update LRU */
978 sc->lru = ++_sprite_lru_counter;
979
980 /* Load the sprite, if it is not loaded, yet */
981 if (sc->ptr == nullptr) {
982 if (sc->type == SpriteType::Recolour) {
983 sc->ptr = ReadRecolourSprite(*sc->file, sc->file_pos, sc->length, cache_allocator);
984 } else {
985 sc->ptr = ReadSprite(sc, sprite, type, cache_allocator, nullptr);
986 }
987 }
988
989 return sc->ptr;
990 } else {
991 /* Do not use the spritecache, but a different allocator. */
992 return ReadSprite(sc, sprite, type, *allocator, encoder);
993 }
994}
995
996
997static void GfxInitSpriteCache()
998{
999 /* initialize sprite cache heap */
1001 uint target_size = (bpp > 0 ? _sprite_cache_size * bpp / 8 : 1) * 1024 * 1024;
1002
1003 /* Remember 'target_size' from the previous allocation attempt, so we do not try to reach the target_size multiple times in case of failure. */
1004 static uint last_alloc_attempt = 0;
1005
1006 if (_spritecache_ptr == nullptr || (_allocated_sprite_cache_size != target_size && target_size != last_alloc_attempt)) {
1007 delete[] reinterpret_cast<uint8_t *>(_spritecache_ptr);
1008
1009 last_alloc_attempt = target_size;
1010 _allocated_sprite_cache_size = target_size;
1011
1012 do {
1013 /* Try to allocate 50% more to make sure we do not allocate almost all available. */
1014 _spritecache_ptr = reinterpret_cast<MemBlock *>(new(std::nothrow) uint8_t[_allocated_sprite_cache_size + _allocated_sprite_cache_size / 2]);
1015
1016 if (_spritecache_ptr != nullptr) {
1017 /* Allocation succeeded, but we wanted less. */
1018 delete[] reinterpret_cast<uint8_t *>(_spritecache_ptr);
1019 _spritecache_ptr = reinterpret_cast<MemBlock *>(new uint8_t[_allocated_sprite_cache_size]);
1020 } else if (_allocated_sprite_cache_size < 2 * 1024 * 1024) {
1021 UserError("Cannot allocate spritecache");
1022 } else {
1023 /* Try again to allocate half. */
1024 _allocated_sprite_cache_size >>= 1;
1025 }
1026 } while (_spritecache_ptr == nullptr);
1027
1028 if (_allocated_sprite_cache_size != target_size) {
1029 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);
1030
1031 ErrorMessageData msg(STR_CONFIG_ERROR_OUT_OF_MEMORY, STR_CONFIG_ERROR_SPRITECACHE_TOO_BIG);
1032 msg.SetDParam(0, target_size);
1033 msg.SetDParam(1, _allocated_sprite_cache_size);
1035 }
1036 }
1037
1038 /* A big free block */
1039 _spritecache_ptr->size = (_allocated_sprite_cache_size - sizeof(MemBlock)) | S_FREE_MASK;
1040 /* Sentinel block (identified by size == 0) */
1041 NextBlock(_spritecache_ptr)->size = 0;
1042}
1043
1044void GfxInitSpriteMem()
1045{
1046 GfxInitSpriteCache();
1047
1048 /* Reset the spritecache 'pool' */
1049 _spritecache.clear();
1050 _spritecache.shrink_to_fit();
1051
1052 _compact_cache_counter = 0;
1053 _sprite_files.clear();
1054}
1055
1061{
1062 /* Clear sprite ptr for all cached items */
1063 for (SpriteCache &sc : _spritecache) {
1064 if (sc.ptr != nullptr) DeleteEntryFromSpriteCache(&sc);
1065 }
1066
1068}
1069
1075{
1076 /* Clear sprite ptr for all cached font items */
1077 for (SpriteCache &sc : _spritecache) {
1078 if (sc.type == SpriteType::Font && sc.ptr != nullptr) DeleteEntryFromSpriteCache(&sc);
1079 }
1080}
1081
debug_inline constexpr 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).
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
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 Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)=0
Convert a sprite from the loader to our own format.
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.
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, uint8_t &avail_8bpp, uint8_t &avail_32bpp) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
Definition grf.cpp:359
Sprite loader for converting graphics coming from another source.
Definition makeindexed.h:16
uint8_t LoadSprite(SpriteLoader::SpriteCollection &sprite, SpriteFile &file, size_t file_pos, SpriteType sprite_type, bool load_32bpp, uint8_t control_flags, uint8_t &avail_8bpp, uint8_t &avail_32bpp) override
Load a sprite from the disk and return a sprite struct which is the same for all loaders.
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.
Error reporting related functions.
Factory to 'query' all available blitters.
Subdirectory
The different kinds of subdirectories OpenTTD uses.
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:17
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:344
@ 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.
Base for converting sprites from another source from 32bpp RGBA to indexed 8bpp.
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.
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 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 SpriteFile * GetCachedSpriteFileByName(const std::string &filename)
Get the cached SpriteFile given the name of the file.
uint GetSpriteCountForFile(const std::string &filename, SpriteID begin, SpriteID end)
Count the sprites which originate from a specific file in a range of SpriteIDs.
static void DeleteEntryFromSpriteCache(SpriteCache *item)
Delete a single entry from the sprite cache.
static void * ReadSprite(const SpriteCache *sc, SpriteID id, SpriteType sprite_type, SpriteAllocator &allocator, SpriteEncoder *encoder)
Read a sprite from disk.
void GfxClearFontSpriteCache()
Remove all encoded font sprites from the sprite cache without discarding sprite location information.
SpriteID GetMaxSpriteID()
Get a reasonable (upper bound) estimate of the maximum SpriteID used in OpenTTD; there will be no spr...
SpriteFile & OpenCachedSpriteFile(const std::string &filename, Subdirectory subdir, bool palette_remap)
Open/get the SpriteFile that is cached for use in the sprite cache.
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.
void * GetRawSprite(SpriteID sprite, SpriteType type, SpriteAllocator *allocator, SpriteEncoder *encoder)
Reads a sprite (from disk or sprite cache).
std::span< const std::unique_ptr< SpriteFile > > GetCachedSpriteFiles()
Get the list of cached SpriteFiles.
bool LoadNextSprite(SpriteID load_index, SpriteFile &file, uint file_sprite_id)
Load a real or recolour sprite.
SpriteFile * GetOriginFile(SpriteID sprite)
Get the SpriteFile of a given sprite.
size_t GetGRFSpriteOffset(uint32_t id)
Get the file offset for a specific sprite in the sprite section of a GRF.
static void * ReadRecolourSprite(SpriteFile &file, size_t file_pos, uint num, SpriteAllocator &allocator)
Load a recolour sprite into memory.
bool SkipSpriteData(SpriteFile &file, uint8_t type, uint16_t num)
Skip the given amount of sprite graphics data.
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.
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.
uint32_t length
Length of sprite data.
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