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/>.
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
++) {
33 dst_line
+= bp
->pitch
;
36 src_line
+= bp
->sprite_width
* ScaleByZoom(1, zoom
);
38 for (int x
= 0; x
< bp
->width
; x
++) {
41 /* In case the m-channel is zero, do not remap this pixel in any way */
43 if (src
->a
!= 0) *dst
= ComposeColourRGBA(src
->r
, src
->g
, src
->b
, src
->a
, *dst
);
45 if (bp
->remap
[src
->m
] != 0) *dst
= ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp
->remap
[src
->m
]), src
->v
), src
->a
, *dst
);
52 uint8_t g
= MakeDark(src
->r
, src
->g
, src
->b
);
53 *dst
= ComposeColourRGBA(g
, g
, g
, src
->a
, *dst
);
56 if (bp
->remap
[src
->m
] != 0) *dst
= ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp
->remap
[src
->m
]), src
->v
), src
->a
, *dst
);
62 *dst
= Colour(0, 0, 0);
67 /* Make the current colour a bit more black, so it looks like this image is transparent */
69 *dst
= MakeTransparent(*dst
, 192);
73 case BM_TRANSPARENT_REMAP
:
74 /* Apply custom transparency remap. */
76 *dst
= this->LookupColourInPalette(bp
->remap
[GetNearestColourIndex(*dst
)]);
81 if (src
->a
!= 0) *dst
= ComposeColourRGBA(src
->r
, src
->g
, src
->b
, src
->a
, *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
) {
96 for (int i
= 0; i
!= width
; i
++) {
97 *udst
= MakeTransparent(*udst
, 154);
100 udst
= udst
- width
+ _screen
.pitch
;
104 if (pal
== PALETTE_NEWSPAPER
) {
106 for (int i
= 0; i
!= width
; i
++) {
107 *udst
= MakeGrey(*udst
);
110 udst
= udst
- width
+ _screen
.pitch
;
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
++) {
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
;
147 /* Pre-convert the mapping channel to a RGB value */
148 Colour colour
= this->AdjustBrightness(this->LookupColourInPalette(src
->m
), dst
[i
].v
);