OpenTTD Source 20241224-master-gf74b0cf984
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 "../core/mem_func.hpp"
15#include "8bpp_optimized.hpp"
16
17#include "../safeguards.h"
18
21
23{
24 /* Find the offset of this zoom-level */
25 const SpriteData *sprite_src = (const SpriteData *)bp->sprite;
26 uint offset = sprite_src->offset[zoom];
27
28 /* Find where to start reading in the source sprite */
29 const uint8_t *src = sprite_src->data + offset;
30 uint8_t *dst_line = (uint8_t *)bp->dst + bp->top * bp->pitch + bp->left;
31
32 /* Skip over the top lines in the source image */
33 for (int y = 0; y < bp->skip_top; y++) {
34 for (;;) {
35 uint trans = *src++;
36 uint pixels = *src++;
37 if (trans == 0 && pixels == 0) break;
38 src += pixels;
39 }
40 }
41
42 const uint8_t *src_next = src;
43
44 for (int y = 0; y < bp->height; y++) {
45 uint8_t *dst = dst_line;
46 dst_line += bp->pitch;
47
48 uint skip_left = bp->skip_left;
49 int width = bp->width;
50
51 for (;;) {
52 src = src_next;
53 uint trans = *src++;
54 uint pixels = *src++;
55 src_next = src + pixels;
56 if (trans == 0 && pixels == 0) break;
57 if (width <= 0) continue;
58
59 if (skip_left != 0) {
60 if (skip_left < trans) {
61 trans -= skip_left;
62 skip_left = 0;
63 } else {
64 skip_left -= trans;
65 trans = 0;
66 }
67 if (skip_left < pixels) {
68 src += skip_left;
69 pixels -= skip_left;
70 skip_left = 0;
71 } else {
72 src += pixels;
73 skip_left -= pixels;
74 pixels = 0;
75 }
76 }
77 if (skip_left != 0) continue;
78
79 /* Skip transparent pixels */
80 dst += trans;
81 width -= trans;
82 if (width <= 0 || pixels == 0) continue;
83 pixels = std::min<uint>(pixels, width);
84 width -= pixels;
85
86 switch (mode) {
87 case BM_COLOUR_REMAP:
88 case BM_CRASH_REMAP: {
89 const uint8_t *remap = bp->remap;
90 do {
91 uint m = remap[*src];
92 if (m != 0) *dst = m;
93 dst++; src++;
94 } while (--pixels != 0);
95 break;
96 }
97
98 case BM_BLACK_REMAP:
99 MemSetT(dst, 0, pixels);
100 dst += pixels;
101 break;
102
103 case BM_TRANSPARENT:
105 const uint8_t *remap = bp->remap;
106 src += pixels;
107 do {
108 *dst = remap[*dst];
109 dst++;
110 } while (--pixels != 0);
111 break;
112 }
113
114 default:
115 MemCpyT(dst, src, pixels);
116 dst += pixels; src += pixels;
117 break;
118 }
119 }
120 }
121}
122
124{
125 /* Make memory for all zoom-levels */
126 uint memory = sizeof(SpriteData);
127
128 ZoomLevel zoom_min;
129 ZoomLevel zoom_max;
130
131 if (sprite[ZOOM_LVL_MIN].type == SpriteType::Font) {
132 zoom_min = ZOOM_LVL_MIN;
133 zoom_max = ZOOM_LVL_MIN;
134 } else {
135 zoom_min = _settings_client.gui.zoom_min;
136 zoom_max = _settings_client.gui.zoom_max;
137 if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
138 }
139
140 for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
141 memory += sprite[i].width * sprite[i].height;
142 }
143
144 /* We have no idea how much memory we really need, so just guess something */
145 memory *= 5;
146
147 /* Don't allocate memory each time, but just keep some
148 * memory around as this function is called quite often
149 * and the memory usage is quite low. */
150 static ReusableBuffer<uint8_t> temp_buffer;
151 SpriteData *temp_dst = (SpriteData *)temp_buffer.Allocate(memory);
152 memset(temp_dst, 0, sizeof(*temp_dst));
153 uint8_t *dst = temp_dst->data;
154
155 /* Make the sprites per zoom-level */
156 for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
157 /* Store the index table */
158 uint offset = dst - temp_dst->data;
159 temp_dst->offset[i] = offset;
160
161 /* cache values, because compiler can't cache it */
162 int scaled_height = sprite[i].height;
163 int scaled_width = sprite[i].width;
164
165 for (int y = 0; y < scaled_height; y++) {
166 uint trans = 0;
167 uint pixels = 0;
168 uint last_colour = 0;
169 uint8_t *count_dst = nullptr;
170
171 /* Store the scaled image */
172 const SpriteLoader::CommonPixel *src = &sprite[i].data[y * sprite[i].width];
173
174 for (int x = 0; x < scaled_width; x++) {
175 uint colour = src++->m;
176
177 if (last_colour == 0 || colour == 0 || pixels == 255) {
178 if (count_dst != nullptr) {
179 /* Write how many non-transparent bytes we get */
180 *count_dst = pixels;
181 pixels = 0;
182 count_dst = nullptr;
183 }
184 /* As long as we find transparency bytes, keep counting */
185 if (colour == 0 && trans != 255) {
186 last_colour = 0;
187 trans++;
188 continue;
189 }
190 /* No longer transparency, so write the amount of transparent bytes */
191 *dst = trans;
192 dst++;
193 trans = 0;
194 /* Reserve a byte for the pixel counter */
195 count_dst = dst;
196 dst++;
197 }
198 last_colour = colour;
199 if (colour == 0) {
200 trans++;
201 } else {
202 pixels++;
203 *dst = colour;
204 dst++;
205 }
206 }
207
208 if (count_dst != nullptr) *count_dst = pixels;
209
210 /* Write line-ending */
211 *dst = 0; dst++;
212 *dst = 0; dst++;
213 }
214 }
215
216 uint size = dst - (uint8_t *)temp_dst;
217
218 /* Safety check, to make sure we guessed the size correctly */
219 assert(size < memory);
220
221 /* Allocate the exact amount of memory we need */
222 Sprite *dest_sprite = allocator.Allocate<Sprite>(sizeof(*dest_sprite) + size);
223
224 dest_sprite->height = sprite[ZOOM_LVL_MIN].height;
225 dest_sprite->width = sprite[ZOOM_LVL_MIN].width;
226 dest_sprite->x_offs = sprite[ZOOM_LVL_MIN].x_offs;
227 dest_sprite->y_offs = sprite[ZOOM_LVL_MIN].y_offs;
228 memcpy(dest_sprite->data, temp_dst, size);
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
@ BM_BLACK_REMAP
Perform remapping to a completely blackened sprite.
Definition base.hpp:23
@ BM_COLOUR_REMAP
Perform a colour remapping.
Definition base.hpp:19
@ BM_TRANSPARENT_REMAP
Perform transparency colour remapping.
Definition base.hpp:21
@ BM_TRANSPARENT
Perform transparency darkening remapping.
Definition base.hpp:20
@ BM_CRASH_REMAP
Perform a crash remapping.
Definition base.hpp:22
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override
Draw an image to the screen, given an amount of params defined above.
Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
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 * Allocate(size_t count)
Get buffer of at least count times T.
Interface for something that can allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
@ Font
A sprite used for fonts.
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
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:56
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.
uint32_t offset[ZOOM_LVL_END]
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.
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
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition zoom_type.h:42
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition zoom_type.h:41