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

OpenTTD Source 15.0-RC2

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
18 static FBlitter_8bppOptimized iFBlitter_8bppOptimized;
19
20 void Blitter_8bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
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) {
85 case BlitterMode::ColourRemap:
86 case BlitterMode::CrashRemap: {
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
96 case BlitterMode::BlackRemap:
97 std::fill_n(dst, pixels, 0);
98 dst += pixels;
99 break;
100
101 case BlitterMode::Transparent:
102 case BlitterMode::TransparentRemap: {
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
121 Sprite *Blitter_8bppOptimized::Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)
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}
iFBlitter_8bppOptimized
static FBlitter_8bppOptimized iFBlitter_8bppOptimized
Instantiation of the 8bpp optimised blitter factory.
Definition 8bpp_optimized.cpp:18
8bpp_optimized.hpp
An optimized 8 bpp blitter.
BlitterMode
BlitterMode
The modes of blitting we can do.
Definition base.hpp:17
BlitterMode::Transparent
@ Transparent
Perform transparency darkening remapping.
BlitterMode::CrashRemap
@ CrashRemap
Perform a crash remapping.
BlitterMode::BlackRemap
@ BlackRemap
Perform remapping to a completely blackened sprite.
BlitterMode::TransparentRemap
@ TransparentRemap
Perform transparency colour remapping.
BlitterMode::ColourRemap
@ ColourRemap
Perform a colour remapping.
Blitter_8bppOptimized::Encode
Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
Definition 8bpp_optimized.cpp:121
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:20
FBlitter_8bppOptimized
Factory for the 8bpp blitter optimised for speed.
Definition 8bpp_optimized.hpp:32
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:22
ReusableBuffer::ZeroAllocate
T * ZeroAllocate(size_t count)
Get buffer of at least count times T of default initialised elements.
Definition alloc_type.hpp:47
SpriteAllocator
Interface for something that can allocate memory for a sprite.
Definition spriteloader.hpp:104
SpriteAllocator::Allocate
T * Allocate(size_t size)
Allocate memory for a sprite.
Definition spriteloader.hpp:115
SpriteCollMap
Map zoom level to data.
Definition spriteloader.hpp:34
SpriteType
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:357
SpriteType::Font
@ Font
A sprite used for fonts.
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Blitter::BlitterParams
Parameters related to blitting.
Definition base.hpp:32
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:37
Blitter::BlitterParams::dst
void * dst
Destination buffer.
Definition base.hpp:45
Blitter::BlitterParams::left
int left
The left offset in the 'dst' in pixels to start drawing.
Definition base.hpp:42
Blitter::BlitterParams::pitch
int pitch
The pitch of the destination buffer.
Definition base.hpp:46
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:36
Blitter::BlitterParams::height
int height
The height in pixels that needs to be drawn to dst.
Definition base.hpp:39
Blitter::BlitterParams::remap
const uint8_t * remap
XXX – Temporary storage for remap array.
Definition base.hpp:34
Blitter::BlitterParams::width
int width
The width in pixels that needs to be drawn to dst.
Definition base.hpp:38
Blitter::BlitterParams::sprite
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition base.hpp:33
Blitter::BlitterParams::top
int top
The top offset in the 'dst' in pixels to start drawing.
Definition base.hpp:43
Blitter_8bppOptimized::SpriteData
Data stored about a (single) sprite.
Definition 8bpp_optimized.hpp:20
Blitter_8bppOptimized::SpriteData::offset
SpriteCollMap< uint32_t > offset
Offsets (from .data) to streams for different zoom levels.
Definition 8bpp_optimized.hpp:21
Blitter_8bppOptimized::SpriteData::data
uint8_t data[]
Data, all zoomlevels.
Definition 8bpp_optimized.hpp:22
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition settings_type.h:650
GUISettings::zoom_min
ZoomLevel zoom_min
minimum zoom out level
Definition settings_type.h:191
GUISettings::zoom_max
ZoomLevel zoom_max
maximum zoom out level
Definition settings_type.h:192
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition spriteloader.hpp:48
SpriteLoader::CommonPixel::m
uint8_t m
Remap-channel.
Definition spriteloader.hpp:53
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition spriteloader.hpp:62
SpriteLoader::Sprite::width
uint16_t width
Width of the sprite.
Definition spriteloader.hpp:64
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition spriteloader.hpp:68
SpriteLoader::Sprite::height
uint16_t height
Height of the sprite.
Definition spriteloader.hpp:63
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16
Sprite::width
uint16_t width
Width of the sprite.
Definition spritecache_type.h:18
Sprite::height
uint16_t height
Height of the sprite.
Definition spritecache_type.h:17
Sprite::y_offs
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition spritecache_type.h:20
Sprite::data
std::byte data[]
Sprite data.
Definition spritecache_type.h:21
Sprite::x_offs
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition spritecache_type.h:19
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20
ZoomLevel::Max
@ Max
Maximum zoom level.
ZoomLevel::Min
@ Min
Minimum zoom level.