OpenTTD Source: src/blitter/8bpp_optimized.cpp Source File

OpenTTD Source 12.2

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
20 static FBlitter_8bppOptimized iFBlitter_8bppOptimized;
21
22 void Blitter_8bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
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 *src = sprite_src->data + offset;
30 uint8 *dst_line = (uint8 *)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 *src_next = src;
43
44 for (int y = 0; y < bp->height; y++) {
45 uint8 *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 *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: {
104 const uint8 *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 MemCpyT(dst, src, pixels);
115 dst += pixels; src += pixels;
116 break;
117 }
118 }
119 }
120 }
121
122 Sprite *Blitter_8bppOptimized::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
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 == ST_FONT) {
131 zoom_min = ZOOM_LVL_NORMAL;
132 zoom_max = ZOOM_LVL_NORMAL;
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 = ZOOM_LVL_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<byte> temp_buffer;
150 SpriteData *temp_dst = (SpriteData *)temp_buffer.Allocate(memory);
151 memset(temp_dst, 0, sizeof(*temp_dst));
152 byte *dst = temp_dst->data;
153
154 /* Make the sprites per zoom-level */
155 for (ZoomLevel i = zoom_min; i <= zoom_max; 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 = sprite[i].height;
162 int scaled_width = sprite[i].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 byte *count_dst = nullptr;
169
170 /* Store the scaled image */
171 const SpriteLoader::CommonPixel *src = &sprite[i].data[y * sprite[i].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 - (byte *)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 = (Sprite *)allocator(sizeof(*dest_sprite) + size);
222
223 dest_sprite->height = sprite->height;
224 dest_sprite->width = sprite->width;
225 dest_sprite->x_offs = sprite->x_offs;
226 dest_sprite->y_offs = sprite->y_offs;
227 memcpy(dest_sprite->data, temp_dst, size);
228
229 return dest_sprite;
230 }
SpriteLoader::CommonPixel::m
uint8 m
Remap-channel.
Definition: spriteloader.hpp:39
Blitter_8bppOptimized::Encode
Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override
Convert a sprite from the loader to our own format.
Definition: 8bpp_optimized.cpp:122
8bpp_optimized.hpp
Blitter_8bppOptimized::SpriteData
Data stored about a (single) sprite.
Definition: 8bpp_optimized.hpp:20
ST_FONT
@ ST_FONT
A sprite used for fonts.
Definition: gfx_type.h:304
ReusableBuffer
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
Definition: alloc_type.hpp:24
Blitter_8bppOptimized::SpriteData::offset
uint32 offset[ZOOM_LVL_COUNT]
Offsets (from .data) to streams for different zoom levels.
Definition: 8bpp_optimized.hpp:21
Blitter::BlitterParams::top
int top
The top offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:42
BM_TRANSPARENT
@ BM_TRANSPARENT
Perform transparency colour remapping.
Definition: base.hpp:20
Blitter::BlitterParams::skip_left
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition: base.hpp:35
BlitterMode
BlitterMode
The modes of blitting we can do.
Definition: base.hpp:17
Blitter::BlitterParams::width
int width
The width in pixels that needs to be drawn to dst.
Definition: base.hpp:37
Sprite::data
byte data[]
Sprite data.
Definition: spritecache.h:22
Blitter::BlitterParams::dst
void * dst
Destination buffer.
Definition: base.hpp:44
Sprite::height
uint16 height
Height of the sprite.
Definition: spritecache.h:18
Sprite::x_offs
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:20
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
Blitter_8bppOptimized::Draw
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override
Draw an image to the screen, given an amount of params defined above.
Definition: 8bpp_optimized.cpp:22
Blitter::BlitterParams::pitch
int pitch
The pitch of the destination buffer.
Definition: base.hpp:45
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
ZOOM_LVL_MAX
@ ZOOM_LVL_MAX
Maximum zoom level.
Definition: zoom_type.h:48
ReusableBuffer::Allocate
T * Allocate(size_t count)
Get buffer of at least count times T.
Definition: alloc_type.hpp:42
MemCpyT
static void MemCpyT(T *destination, const T *source, size_t num=1)
Type-safe version of memcpy().
Definition: mem_func.hpp:23
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition: settings_type.h:132
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition: spriteloader.hpp:55
Blitter::BlitterParams::sprite
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition: base.hpp:32
iFBlitter_8bppOptimized
static FBlitter_8bppOptimized iFBlitter_8bppOptimized
Instantiation of the 8bpp optimised blitter factory.
Definition: 8bpp_optimized.cpp:20
SpriteLoader::Sprite::type
SpriteType type
The sprite type.
Definition: spriteloader.hpp:53
BM_COLOUR_REMAP
@ BM_COLOUR_REMAP
Perform a colour remapping.
Definition: base.hpp:19
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:34
FBlitter_8bppOptimized
Factory for the 8bpp blitter optimised for speed.
Definition: 8bpp_optimized.hpp:32
BM_CRASH_REMAP
@ BM_CRASH_REMAP
Perform a crash remapping.
Definition: base.hpp:21
Sprite::width
uint16 width
Width of the sprite.
Definition: spritecache.h:19
SpriteLoader::Sprite::x_offs
int16 x_offs
The x-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:51
BM_BLACK_REMAP
@ BM_BLACK_REMAP
Perform remapping to a completely blackened sprite.
Definition: base.hpp:22
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition: settings_type.h:131
SpriteLoader::Sprite::width
uint16 width
Width of the sprite.
Definition: spriteloader.hpp:50
Blitter::BlitterParams::left
int left
The left offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:41
Blitter_8bppOptimized::SpriteData::data
byte data[]
Data, all zoomlevels.
Definition: 8bpp_optimized.hpp:22
Sprite::y_offs
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:21
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:48
Blitter::BlitterParams
Parameters related to blitting.
Definition: base.hpp:31
MemSetT
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
ZOOM_LVL_NORMAL
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:24
Blitter::BlitterParams::height
int height
The height in pixels that needs to be drawn to dst.
Definition: base.hpp:38
SpriteLoader::Sprite::y_offs
int16 y_offs
The y-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:52
SpriteLoader::Sprite::height
uint16 height
Height of the sprite.
Definition: spriteloader.hpp:49
Blitter::BlitterParams::skip_top
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition: base.hpp:36
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
Blitter::BlitterParams::remap
const byte * remap
XXX – Temporary storage for remap array.
Definition: base.hpp:33
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:594