Add: Overlay cargo icon in vehicle/depot list when holding shift+ctrl. (#12938)
[openttd-github.git] / src / blitter / 32bpp_simple.cpp
blobae660e372fb543823a32782e34b1e5795319e3ff
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 */
8 /** @file 32bpp_simple.cpp Implementation of the simple 32 bpp blitter. */
10 #include "../stdafx.h"
11 #include "../zoom_func.h"
12 #include "../palette_func.h"
13 #include "32bpp_simple.hpp"
15 #include "../table/sprites.h"
17 #include "../safeguards.h"
19 /** Instantiation of the simple 32bpp blitter factory. */
20 static FBlitter_32bppSimple iFBlitter_32bppSimple;
22 void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
24 const Blitter_32bppSimple::Pixel *src, *src_line;
25 Colour *dst, *dst_line;
27 /* Find where to start reading in the source sprite */
28 src_line = (const Blitter_32bppSimple::Pixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
29 dst_line = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
31 for (int y = 0; y < bp->height; y++) {
32 dst = dst_line;
33 dst_line += bp->pitch;
35 src = src_line;
36 src_line += bp->sprite_width * ScaleByZoom(1, zoom);
38 for (int x = 0; x < bp->width; x++) {
39 switch (mode) {
40 case BM_COLOUR_REMAP:
41 /* In case the m-channel is zero, do not remap this pixel in any way */
42 if (src->m == 0) {
43 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
44 } else {
45 if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
47 break;
49 case BM_CRASH_REMAP:
50 if (src->m == 0) {
51 if (src->a != 0) {
52 uint8_t g = MakeDark(src->r, src->g, src->b);
53 *dst = ComposeColourRGBA(g, g, g, src->a, *dst);
55 } else {
56 if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
58 break;
60 case BM_BLACK_REMAP:
61 if (src->a != 0) {
62 *dst = Colour(0, 0, 0);
64 break;
66 case BM_TRANSPARENT:
67 /* Make the current colour a bit more black, so it looks like this image is transparent */
68 if (src->a != 0) {
69 *dst = MakeTransparent(*dst, 192);
71 break;
73 case BM_TRANSPARENT_REMAP:
74 /* Apply custom transparency remap. */
75 if (src->a != 0) {
76 *dst = this->LookupColourInPalette(bp->remap[GetNearestColourIndex(*dst)]);
78 break;
80 default:
81 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
82 break;
84 dst++;
85 src += ScaleByZoom(1, zoom);
90 void Blitter_32bppSimple::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
92 Colour *udst = (Colour *)dst;
94 if (pal == PALETTE_TO_TRANSPARENT) {
95 do {
96 for (int i = 0; i != width; i++) {
97 *udst = MakeTransparent(*udst, 154);
98 udst++;
100 udst = udst - width + _screen.pitch;
101 } while (--height);
102 return;
104 if (pal == PALETTE_NEWSPAPER) {
105 do {
106 for (int i = 0; i != width; i++) {
107 *udst = MakeGrey(*udst);
108 udst++;
110 udst = udst - width + _screen.pitch;
111 } while (--height);
112 return;
115 Debug(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('{}')", pal);
118 Sprite *Blitter_32bppSimple::Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator)
120 Blitter_32bppSimple::Pixel *dst;
121 Sprite *dest_sprite = allocator.Allocate<Sprite>(sizeof(*dest_sprite) + static_cast<size_t>(sprite[ZOOM_LVL_MIN].height) * static_cast<size_t>(sprite[ZOOM_LVL_MIN].width) * sizeof(*dst));
123 dest_sprite->height = sprite[ZOOM_LVL_MIN].height;
124 dest_sprite->width = sprite[ZOOM_LVL_MIN].width;
125 dest_sprite->x_offs = sprite[ZOOM_LVL_MIN].x_offs;
126 dest_sprite->y_offs = sprite[ZOOM_LVL_MIN].y_offs;
128 dst = (Blitter_32bppSimple::Pixel *)dest_sprite->data;
129 SpriteLoader::CommonPixel *src = (SpriteLoader::CommonPixel *)sprite[ZOOM_LVL_MIN].data;
131 for (int i = 0; i < sprite[ZOOM_LVL_MIN].height * sprite[ZOOM_LVL_MIN].width; i++) {
132 if (src->m == 0) {
133 dst[i].r = src->r;
134 dst[i].g = src->g;
135 dst[i].b = src->b;
136 dst[i].a = src->a;
137 dst[i].m = 0;
138 dst[i].v = 0;
139 } else {
140 /* Get brightest value */
141 uint8_t rgb_max = std::max({src->r, src->g, src->b});
143 /* Black pixel (8bpp or old 32bpp image), so use default value */
144 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
145 dst[i].v = rgb_max;
147 /* Pre-convert the mapping channel to a RGB value */
148 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), dst[i].v);
149 dst[i].r = colour.r;
150 dst[i].g = colour.g;
151 dst[i].b = colour.b;
152 dst[i].a = src->a;
153 dst[i].m = src->m;
155 src++;
158 return dest_sprite;