OpenTTD Source 20250522-master-g467f832c2f
8bpp_optimized.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"
11#include "../zoom_func.h"
12#include "../settings_type.h"
13#include "../core/math_func.hpp"
14#include "8bpp_optimized.hpp"
15
16#include "../safeguards.h"
17
20
22{
23 /* Find the offset of this zoom-level */
24 const SpriteData *sprite_src = (const SpriteData *)bp->sprite;
25 uint offset = sprite_src->offset[zoom];
26
27 /* Find where to start reading in the source sprite */
28 const uint8_t *src = sprite_src->data + offset;
29 uint8_t *dst_line = (uint8_t *)bp->dst + bp->top * bp->pitch + bp->left;
30
31 /* Skip over the top lines in the source image */
32 for (int y = 0; y < bp->skip_top; y++) {
33 for (;;) {
34 uint trans = *src++;
35 uint pixels = *src++;
36 if (trans == 0 && pixels == 0) break;
37 src += pixels;
38 }
39 }
40
41 const uint8_t *src_next = src;
42
43 for (int y = 0; y < bp->height; y++) {
44 uint8_t *dst = dst_line;
45 dst_line += bp->pitch;
46
47 uint skip_left = bp->skip_left;
48 int width = bp->width;
49
50 for (;;) {
51 src = src_next;
52 uint trans = *src++;
53 uint pixels = *src++;
54 src_next = src + pixels;
55 if (trans == 0 && pixels == 0) break;
56 if (width <= 0) continue;
57
58 if (skip_left != 0) {
59 if (skip_left < trans) {
60 trans -= skip_left;
61 skip_left = 0;
62 } else {
63 skip_left -= trans;
64 trans = 0;
65 }
66 if (skip_left < pixels) {
67 src += skip_left;
68 pixels -= skip_left;
69 skip_left = 0;
70 } else {
71 src += pixels;
72 skip_left -= pixels;
73 pixels = 0;
74 }
75 }
76 if (skip_left != 0) continue;
77
78 /* Skip transparent pixels */
79 dst += trans;
80 width -= trans;
81 if (width <= 0 || pixels == 0) continue;
82 pixels = std::min<uint>(pixels, width);
83 width -= pixels;
84
85 switch (mode) {
88 const uint8_t *remap = bp->remap;
89 do {
90 uint m = remap[*src];
91 if (m != 0) *dst = m;
92 dst++; src++;
93 } while (--pixels != 0);
94 break;
95 }
96
98 std::fill_n(dst, pixels, 0);
99 dst += pixels;
100 break;
101
104 const uint8_t *remap = bp->remap;
105 src += pixels;
106 do {
107 *dst = remap[*dst];
108 dst++;
109 } while (--pixels != 0);
110 break;
111 }
112
113 default:
114 std::copy_n(src, pixels, dst);
115 dst += pixels; src += pixels;
116 break;
117 }
118 }
119 }
120}
121
123{
124 /* Make memory for all zoom-levels */
125 uint memory = sizeof(SpriteData);
126
127 ZoomLevel zoom_min;
128 ZoomLevel zoom_max;
129
130 if (sprite_type == SpriteType::Font) {
131 zoom_min = ZoomLevel::Min;
132 zoom_max = ZoomLevel::Min;
133 } else {
134 zoom_min = _settings_client.gui.zoom_min;
135 zoom_max = _settings_client.gui.zoom_max;
136 if (zoom_max == zoom_min) zoom_max = ZoomLevel::Max;
137 }
138
139 for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
140 memory += sprite[i].width * sprite[i].height;
141 }
142
143 /* We have no idea how much memory we really need, so just guess something */
144 memory *= 5;
145
146 /* Don't allocate memory each time, but just keep some
147 * memory around as this function is called quite often
148 * and the memory usage is quite low. */
149 static ReusableBuffer<uint8_t> temp_buffer;
150 SpriteData *temp_dst = reinterpret_cast<SpriteData *>(temp_buffer.ZeroAllocate(memory));
151 uint8_t *dst = temp_dst->data;
152
153 /* Make the sprites per zoom-level */
154 for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
155 const SpriteLoader::Sprite &src_orig = sprite[i];
156 /* Store the index table */
157 uint offset = dst - temp_dst->data;
158 temp_dst->offset[i] = offset;
159
160 /* cache values, because compiler can't cache it */
161 int scaled_height = src_orig.height;
162 int scaled_width = src_orig.width;
163
164 for (int y = 0; y < scaled_height; y++) {
165 uint trans = 0;
166 uint pixels = 0;
167 uint last_colour = 0;
168 uint8_t *count_dst = nullptr;
169
170 /* Store the scaled image */
171 const SpriteLoader::CommonPixel *src = &src_orig.data[y * src_orig.width];
172
173 for (int x = 0; x < scaled_width; x++) {
174 uint colour = src++->m;
175
176 if (last_colour == 0 || colour == 0 || pixels == 255) {
177 if (count_dst != nullptr) {
178 /* Write how many non-transparent bytes we get */
179 *count_dst = pixels;
180 pixels = 0;
181 count_dst = nullptr;
182 }
183 /* As long as we find transparency bytes, keep counting */
184 if (colour == 0 && trans != 255) {
185 last_colour = 0;
186 trans++;
187 continue;
188 }
189 /* No longer transparency, so write the amount of transparent bytes */
190 *dst = trans;
191 dst++;
192 trans = 0;
193 /* Reserve a byte for the pixel counter */
194 count_dst = dst;
195 dst++;
196 }
197 last_colour = colour;
198 if (colour == 0) {
199 trans++;
200 } else {
201 pixels++;
202 *dst = colour;
203 dst++;
204 }
205 }
206
207 if (count_dst != nullptr) *count_dst = pixels;
208
209 /* Write line-ending */
210 *dst = 0; dst++;
211 *dst = 0; dst++;
212 }
213 }
214
215 uint size = dst - (uint8_t *)temp_dst;
216
217 /* Safety check, to make sure we guessed the size correctly */
218 assert(size < memory);
219
220 /* Allocate the exact amount of memory we need */
221 Sprite *dest_sprite = allocator.Allocate<Sprite>(sizeof(*dest_sprite) + size);
222
223 const auto &root_sprite = sprite.Root();
224 dest_sprite->height = root_sprite.height;
225 dest_sprite->width = root_sprite.width;
226 dest_sprite->x_offs = root_sprite.x_offs;
227 dest_sprite->y_offs = root_sprite.y_offs;
228 std::copy_n(reinterpret_cast<std::byte *>(temp_dst), size, dest_sprite->data);
229
230 return dest_sprite;
231}
static FBlitter_8bppOptimized iFBlitter_8bppOptimized
Instantiation of the 8bpp optimised blitter factory.
An optimized 8 bpp blitter.
BlitterMode
The modes of blitting we can do.
Definition base.hpp:17
@ Transparent
Perform transparency darkening remapping.
@ CrashRemap
Perform a crash remapping.
@ BlackRemap
Perform remapping to a completely blackened sprite.
@ TransparentRemap
Perform transparency colour remapping.
@ ColourRemap
Perform a colour remapping.
Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override
Draw an image to the screen, given an amount of params defined above.
Factory for the 8bpp blitter optimised for speed.
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
T * ZeroAllocate(size_t count)
Get buffer of at least count times T of default initialised elements.
Interface for something that can allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
Map zoom level to data.
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:352
@ Font
A sprite used for fonts.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:59
Parameters related to blitting.
Definition base.hpp:32
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition base.hpp:37
void * dst
Destination buffer.
Definition base.hpp:45
int left
The left offset in the 'dst' in pixels to start drawing.
Definition base.hpp:42
int pitch
The pitch of the destination buffer.
Definition base.hpp:46
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition base.hpp:36
int height
The height in pixels that needs to be drawn to dst.
Definition base.hpp:39
const uint8_t * remap
XXX – Temporary storage for remap array.
Definition base.hpp:34
int width
The width in pixels that needs to be drawn to dst.
Definition base.hpp:38
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition base.hpp:33
int top
The top offset in the 'dst' in pixels to start drawing.
Definition base.hpp:43
Data stored about a (single) sprite.
SpriteCollMap< uint32_t > offset
Offsets (from .data) to streams for different zoom levels.
uint8_t data[]
Data, all zoomlevels.
GUISettings gui
settings related to the GUI
ZoomLevel zoom_min
minimum zoom out level
ZoomLevel zoom_max
maximum zoom out level
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.
uint16_t width
Width of the sprite.
SpriteLoader::CommonPixel * data
The sprite itself.
uint16_t height
Height of the sprite.
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
std::byte 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
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ Max
Maximum zoom level.
@ Min
Minimum zoom level.