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_base.hpp Base for all 32 bits blitters. */
10 #ifndef BLITTER_32BPP_BASE_HPP
11 #define BLITTER_32BPP_BASE_HPP
14 #include "../core/bitmath_func.hpp"
15 #include "../core/math_func.hpp"
16 #include "../gfx_func.h"
18 /** Base for all 32bpp blitters. */
19 class Blitter_32bppBase
: public Blitter
{
21 uint8_t GetScreenDepth() override
{ return 32; }
22 void *MoveTo(void *video
, int x
, int y
) override
;
23 void SetPixel(void *video
, int x
, int y
, uint8_t colour
) override
;
24 void DrawLine(void *video
, int x
, int y
, int x2
, int y2
, int screen_width
, int screen_height
, uint8_t colour
, int width
, int dash
) override
;
25 void DrawRect(void *video
, int width
, int height
, uint8_t colour
) override
;
26 void CopyFromBuffer(void *video
, const void *src
, int width
, int height
) override
;
27 void CopyToBuffer(const void *video
, void *dst
, int width
, int height
) override
;
28 void CopyImageToBuffer(const void *video
, void *dst
, int width
, int height
, int dst_pitch
) override
;
29 void ScrollBuffer(void *video
, int &left
, int &top
, int &width
, int &height
, int scroll_x
, int scroll_y
) override
;
30 size_t BufferSize(uint width
, uint height
) override
;
31 void PaletteAnimate(const Palette
&palette
) override
;
32 Blitter::PaletteAnimation
UsePaletteAnimation() override
;
35 * Look up the colour in the current palette.
37 static inline Colour
LookupColourInPalette(uint index
)
39 return _cur_palette
.palette
[index
];
43 * Compose a colour based on RGBA values and the current pixel value.
45 static inline Colour
ComposeColourRGBANoCheck(uint r
, uint g
, uint b
, uint a
, Colour current
)
51 /* The 256 is wrong, it should be 255, but 256 is much faster... */
53 ((int)(r
- cr
) * a
) / 256 + cr
,
54 ((int)(g
- cg
) * a
) / 256 + cg
,
55 ((int)(b
- cb
) * a
) / 256 + cb
);
59 * Compose a colour based on RGBA values and the current pixel value.
60 * Handles fully transparent and solid pixels in a special (faster) way.
62 static inline Colour
ComposeColourRGBA(uint r
, uint g
, uint b
, uint a
, Colour current
)
64 if (a
== 0) return current
;
65 if (a
>= 255) return Colour(r
, g
, b
);
67 return ComposeColourRGBANoCheck(r
, g
, b
, a
, current
);
71 * Compose a colour based on Pixel value, alpha value, and the current pixel value.
73 static inline Colour
ComposeColourPANoCheck(Colour colour
, uint a
, Colour current
)
79 return ComposeColourRGBANoCheck(r
, g
, b
, a
, current
);
83 * Compose a colour based on Pixel value, alpha value, and the current pixel value.
84 * Handles fully transparent and solid pixels in a special (faster) way.
86 static inline Colour
ComposeColourPA(Colour colour
, uint a
, Colour current
)
88 if (a
== 0) return current
;
94 return ComposeColourPANoCheck(colour
, a
, current
);
98 * Make a pixel looks like it is transparent.
99 * @param colour the colour already on the screen.
100 * @param nom the amount of transparency, nominator, makes colour lighter.
101 * @param denom denominator, makes colour darker.
102 * @return the new colour for the screen.
104 static inline Colour
MakeTransparent(Colour colour
, uint nom
, uint denom
= 256)
110 return Colour(r
* nom
/ denom
, g
* nom
/ denom
, b
* nom
/ denom
);
114 * Make a colour dark grey, for specialized 32bpp remapping.
115 * @param r red component
116 * @param g green component
117 * @param b blue component
118 * @return the brightness value of the new colour, now dark grey.
120 static inline uint8_t MakeDark(uint8_t r
, uint8_t g
, uint8_t b
)
122 /* Magic-numbers are ~66% of those used in MakeGrey() */
123 return ((r
* 13063) + (g
* 25647) + (b
* 4981)) / 65536;
127 * Make a colour dark grey, for specialized 32bpp remapping.
128 * @param colour the colour to make dark.
129 * @return the new colour, now darker.
131 static inline Colour
MakeDark(Colour colour
)
133 uint8_t d
= MakeDark(colour
.r
, colour
.g
, colour
.b
);
135 return Colour(d
, d
, d
);
139 * Make a colour grey - based.
140 * @param colour the colour to make grey.
141 * @return the new colour, now grey.
143 static inline Colour
MakeGrey(Colour colour
)
149 /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
150 * divide by it to normalize the value to a byte again. See heightmap.cpp for
151 * information about the formula. */
152 uint grey
= ((r
* 19595) + (g
* 38470) + (b
* 7471)) / 65536;
154 return Colour(grey
, grey
, grey
);
157 static const int DEFAULT_BRIGHTNESS
= 128;
159 static Colour
ReallyAdjustBrightness(Colour colour
, uint8_t brightness
);
161 static inline Colour
AdjustBrightness(Colour colour
, uint8_t brightness
)
163 /* Shortcut for normal brightness */
164 if (brightness
== DEFAULT_BRIGHTNESS
) return colour
;
166 return ReallyAdjustBrightness(colour
, brightness
);
169 static inline uint8_t GetColourBrightness(Colour colour
)
171 uint8_t rgb_max
= std::max(colour
.r
, std::max(colour
.g
, colour
.b
));
173 /* Black pixel (8bpp or old 32bpp image), so use default value */
174 if (rgb_max
== 0) rgb_max
= DEFAULT_BRIGHTNESS
;
180 #endif /* BLITTER_32BPP_BASE_HPP */