OpenTTD Source 20251213-master-g1091fa6071
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../zoom_func.h"
12#include "../settings_type.h"
13#include "8bpp_optimized.hpp"
14
15#include "../safeguards.h"
16
19
21{
22 /* Find the offset of this zoom-level */
23 const SpriteData *sprite_src = (const SpriteData *)bp->sprite;
24 uint offset = sprite_src->offset[zoom];
25
26 /* Find where to start reading in the source sprite */
27 const uint8_t *src = sprite_src->data + offset;
28 uint8_t *dst_line = (uint8_t *)bp->dst + bp->top * bp->pitch + bp->left;
29
30 /* Skip over the top lines in the source image */
31 for (int y = 0; y < bp->skip_top; y++) {
32 for (;;) {
33 uint trans = *src++;
34 uint pixels = *src++;
35 if (trans == 0 && pixels == 0) break;
36 src += pixels;
37 }
38 }
39
40 const uint8_t *src_next = src;
41
42 for (int y = 0; y < bp->height; y++) {
43 uint8_t *dst = dst_line;
44 dst_line += bp->pitch;
45
46 uint skip_left = bp->skip_left;
47 int width = bp->width;
48
49 for (;;) {
50 src = src_next;
51 uint trans = *src++;
52 uint pixels = *src++;
53 src_next = src + pixels;
54 if (trans == 0 && pixels == 0) break;
55 if (width <= 0) continue;
56
57 if (skip_left != 0) {
58 if (skip_left < trans) {
59 trans -= skip_left;
60 skip_left = 0;
61 } else {
62 skip_left -= trans;
63 trans = 0;
64 }
65 if (skip_left < pixels) {
66 src += skip_left;
67 pixels -= skip_left;
68 skip_left = 0;
69 } else {
70 src += pixels;
71 skip_left -= pixels;
72 pixels = 0;
73 }
74 }
75 if (skip_left != 0) continue;
76
77 /* Skip transparent pixels */
78 dst += trans;
79 width -= trans;
80 if (width <= 0 || pixels == 0) continue;
81 pixels = std::min<uint>(pixels, width);
82 width -= pixels;
83
84 switch (mode) {
87 const uint8_t *remap = bp->remap;
88 do {
89 uint m = remap[*src];
90 if (m != 0) *dst = m;
91 dst++; src++;
92 } while (--pixels != 0);
93 break;
94 }
95
97 std::fill_n(dst, pixels, 0);
98 dst += pixels;
99 break;
100
103 const uint8_t *remap = bp->remap;
104 src += pixels;
105 do {
106 *dst = remap[*dst];
107 dst++;
108 } while (--pixels != 0);
109 break;
110 }
111
112 default:
113 std::copy_n(src, pixels, dst);
114 dst += pixels; src += pixels;
115 break;
116 }
117 }
118 }
119}
120
122{
123 /* Make memory for all zoom-levels */
124 uint memory = sizeof(SpriteData);
125
126 ZoomLevel zoom_min;
127 ZoomLevel zoom_max;
128
129 if (sprite_type == SpriteType::Font) {
130 zoom_min = ZoomLevel::Min;
131 zoom_max = ZoomLevel::Min;
132 } else {
133 zoom_min = _settings_client.gui.zoom_min;
134 zoom_max = _settings_client.gui.zoom_max;
135 if (zoom_max == zoom_min) zoom_max = ZoomLevel::Max;
136 }
137
138 for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
139 memory += sprite[i].width * sprite[i].height;
140 }
141
142 /* We have no idea how much memory we really need, so just guess something */
143 memory *= 5;
144
145 /* Don't allocate memory each time, but just keep some
146 * memory around as this function is called quite often
147 * and the memory usage is quite low. */
148 static ReusableBuffer<uint8_t> temp_buffer;
149 SpriteData *temp_dst = reinterpret_cast<SpriteData *>(temp_buffer.ZeroAllocate(memory));
150 uint8_t *dst = temp_dst->data;
151
152 /* Make the sprites per zoom-level */
153 for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
154 const SpriteLoader::Sprite &src_orig = sprite[i];
155 /* Store the index table */
156 uint offset = dst - temp_dst->data;
157 temp_dst->offset[i] = offset;
158
159 /* cache values, because compiler can't cache it */
160 int scaled_height = src_orig.height;
161 int scaled_width = src_orig.width;
162
163 for (int y = 0; y < scaled_height; y++) {
164 uint trans = 0;
165 uint pixels = 0;
166 uint last_colour = 0;
167 uint8_t *count_dst = nullptr;
168
169 /* Store the scaled image */
170 const SpriteLoader::CommonPixel *src = &src_orig.data[y * src_orig.width];
171
172 for (int x = 0; x < scaled_width; x++) {
173 uint colour = src++->m;
174
175 if (last_colour == 0 || colour == 0 || pixels == 255) {
176 if (count_dst != nullptr) {
177 /* Write how many non-transparent bytes we get */
178 *count_dst = pixels;
179 pixels = 0;
180 count_dst = nullptr;
181 }
182 /* As long as we find transparency bytes, keep counting */
183 if (colour == 0 && trans != 255) {
184 last_colour = 0;
185 trans++;
186 continue;
187 }
188 /* No longer transparency, so write the amount of transparent bytes */
189 *dst = trans;
190 dst++;
191 trans = 0;
192 /* Reserve a byte for the pixel counter */
193 count_dst = dst;
194 dst++;
195 }
196 last_colour = colour;
197 if (colour == 0) {
198 trans++;
199 } else {
200 pixels++;
201 *dst = colour;
202 dst++;
203 }
204 }
205
206 if (count_dst != nullptr) *count_dst = pixels;
207
208 /* Write line-ending */
209 *dst = 0; dst++;
210 *dst = 0; dst++;
211 }
212 }
213
214 uint size = dst - (uint8_t *)temp_dst;
215
216 /* Safety check, to make sure we guessed the size correctly */
217 assert(size < memory);
218
219 /* Allocate the exact amount of memory we need */
220 Sprite *dest_sprite = allocator.Allocate<Sprite>(sizeof(*dest_sprite) + size);
221
222 const auto &root_sprite = sprite.Root();
223 dest_sprite->height = root_sprite.height;
224 dest_sprite->width = root_sprite.width;
225 dest_sprite->x_offs = root_sprite.x_offs;
226 dest_sprite->y_offs = root_sprite.y_offs;
227 std::copy_n(reinterpret_cast<std::byte *>(temp_dst), size, dest_sprite->data);
228
229 return dest_sprite;
230}
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:357
@ Font
A sprite used for fonts.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
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.
uint16_t width
Width of the sprite.
uint16_t height
Height of the sprite.
int16_t y_offs
Number of pixels to shift the sprite downwards.
std::byte data[]
Sprite data.
int16_t x_offs
Number of pixels to shift the sprite to the right.
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
@ Max
Maximum zoom level.
@ Min
Minimum zoom level.