OpenTTD Source: src/blitter/32bpp_base.hpp Source File

OpenTTD Source 12.0-beta1

32bpp_base.hpp
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 #ifndef BLITTER_32BPP_BASE_HPP
11 #define BLITTER_32BPP_BASE_HPP
12
13 #include "base.hpp"
14 #include "../core/bitmath_func.hpp"
15 #include "../core/math_func.hpp"
16 #include "../gfx_func.h"
17
19 class Blitter_32bppBase : public Blitter {
20 public:
21 uint8 GetScreenDepth() override { return 32; }
22 void *MoveTo(void *video, int x, int y) override;
23 void SetPixel(void *video, int x, int y, uint8 colour) override;
24 void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash) override;
25 void DrawRect(void *video, int width, int height, uint8 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 int BufferSize(int width, int height) override;
31 void PaletteAnimate(const Palette &palette) override;
32 Blitter::PaletteAnimation UsePaletteAnimation() override;
33 int GetBytesPerPixel() override { return 4; }
34
38 static inline Colour LookupColourInPalette(uint index)
39 {
40 return _cur_palette.palette[index];
41 }
42
46 static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
47 {
48 uint cr = current.r;
49 uint cg = current.g;
50 uint cb = current.b;
51
52 /* The 256 is wrong, it should be 255, but 256 is much faster... */
53 return Colour(
54 ((int)(r - cr) * a) / 256 + cr,
55 ((int)(g - cg) * a) / 256 + cg,
56 ((int)(b - cb) * a) / 256 + cb);
57 }
58
63 static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
64 {
65 if (a == 0) return current;
66 if (a >= 255) return Colour(r, g, b);
67
68 return ComposeColourRGBANoCheck(r, g, b, a, current);
69 }
70
74 static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
75 {
76 uint r = colour.r;
77 uint g = colour.g;
78 uint b = colour.b;
79
80 return ComposeColourRGBANoCheck(r, g, b, a, current);
81 }
82
87 static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
88 {
89 if (a == 0) return current;
90 if (a >= 255) {
91 colour.a = 255;
92 return colour;
93 }
94
95 return ComposeColourPANoCheck(colour, a, current);
96 }
97
105 static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
106 {
107 uint r = colour.r;
108 uint g = colour.g;
109 uint b = colour.b;
110
111 return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
112 }
113
121 static inline uint8 MakeDark(uint8 r, uint8 g, uint8 b)
122 {
123 /* Magic-numbers are ~66% of those used in MakeGrey() */
124 return ((r * 13063) + (g * 25647) + (b * 4981)) / 65536;
125 }
126
132 static inline Colour MakeDark(Colour colour)
133 {
134 uint8 d = MakeDark(colour.r, colour.g, colour.b);
135
136 return Colour(d, d, d);
137 }
138
144 static inline Colour MakeGrey(Colour colour)
145 {
146 uint r = colour.r;
147 uint g = colour.g;
148 uint b = colour.b;
149
150 /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
151 * divide by it to normalize the value to a byte again. See heightmap.cpp for
152 * information about the formula. */
153 uint grey = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
154
155 return Colour(grey, grey, grey);
156 }
157
158 static const int DEFAULT_BRIGHTNESS = 128;
159
160 static Colour ReallyAdjustBrightness(Colour colour, uint8 brightness);
161
162 static inline Colour AdjustBrightness(Colour colour, uint8 brightness)
163 {
164 /* Shortcut for normal brightness */
165 if (brightness == DEFAULT_BRIGHTNESS) return colour;
166
167 return ReallyAdjustBrightness(colour, brightness);
168 }
169
170 static inline uint8 GetColourBrightness(Colour colour)
171 {
172 uint8 rgb_max = std::max(colour.r, std::max(colour.g, colour.b));
173
174 /* Black pixel (8bpp or old 32bpp image), so use default value */
175 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
176
177 return rgb_max;
178 }
179 };
180
181 #endif /* BLITTER_32BPP_BASE_HPP */
Blitter_32bppBase::MoveTo
void * MoveTo(void *video, int x, int y) override
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
Definition: 32bpp_base.cpp:16
Blitter
How all blitters should look like.
Definition: base.hpp:28
Blitter_32bppBase::PaletteAnimate
void PaletteAnimate(const Palette &palette) override
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
Definition: 32bpp_base.cpp:148
Blitter_32bppBase
Base for all 32bpp blitters.
Definition: 32bpp_base.hpp:19
Blitter_32bppBase::ComposeColourPANoCheck
static Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
Compose a colour based on Pixel value, alpha value, and the current pixel value.
Definition: 32bpp_base.hpp:74
Blitter_32bppBase::SetPixel
void SetPixel(void *video, int x, int y, uint8 colour) override
Draw a pixel with a given colour on the video-buffer.
Definition: 32bpp_base.cpp:21
Blitter_32bppBase::MakeDark
static Colour MakeDark(Colour colour)
Make a colour dark grey, for specialized 32bpp remapping.
Definition: 32bpp_base.hpp:132
Blitter_32bppBase::CopyToBuffer
void CopyToBuffer(const void *video, void *dst, int width, int height) override
Copy from the screen to a buffer.
Definition: 32bpp_base.cpp:60
Blitter_32bppBase::MakeGrey
static Colour MakeGrey(Colour colour)
Make a colour grey - based.
Definition: 32bpp_base.hpp:144
Blitter_32bppBase::ScrollBuffer
void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override
Scroll the videobuffer some 'x' and 'y' value.
Definition: 32bpp_base.cpp:84
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:314
Blitter_32bppBase::CopyFromBuffer
void CopyFromBuffer(void *video, const void *src, int width, int height) override
Copy from a buffer to the screen.
Definition: 32bpp_base.cpp:48
Blitter_32bppBase::GetScreenDepth
uint8 GetScreenDepth() override
Get the screen depth this blitter works for.
Definition: 32bpp_base.hpp:21
Blitter_32bppBase::ComposeColourPA
static Colour ComposeColourPA(Colour colour, uint a, Colour current)
Compose a colour based on Pixel value, alpha value, and the current pixel value.
Definition: 32bpp_base.hpp:87
Blitter_32bppBase::LookupColourInPalette
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition: 32bpp_base.hpp:38
Blitter_32bppBase::MakeDark
static uint8 MakeDark(uint8 r, uint8 g, uint8 b)
Make a colour dark grey, for specialized 32bpp remapping.
Definition: 32bpp_base.hpp:121
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:163
Blitter::PaletteAnimation
PaletteAnimation
Types of palette animation.
Definition: base.hpp:49
Blitter_32bppBase::ComposeColourRGBA
static Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
Compose a colour based on RGBA values and the current pixel value.
Definition: 32bpp_base.hpp:63
Colour::a
uint8 a
colour channels in LE order
Definition: gfx_type.h:171
Blitter_32bppBase::GetBytesPerPixel
int GetBytesPerPixel() override
Get how many bytes are needed to store a pixel.
Definition: 32bpp_base.hpp:33
base.hpp
Blitter_32bppBase::DrawRect
void DrawRect(void *video, int width, int height, uint8 colour) override
Make a single horizontal line in a single colour on the video-buffer.
Definition: 32bpp_base.cpp:34
Blitter_32bppBase::ComposeColourRGBANoCheck
static Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
Compose a colour based on RGBA values and the current pixel value.
Definition: 32bpp_base.hpp:46
Blitter_32bppBase::MakeTransparent
static Colour MakeTransparent(Colour colour, uint nom, uint denom=256)
Make a pixel looks like it is transparent.
Definition: 32bpp_base.hpp:105
_cur_palette
Palette _cur_palette
Current palette.
Definition: gfx.cpp:48
Blitter_32bppBase::UsePaletteAnimation
Blitter::PaletteAnimation UsePaletteAnimation() override
Check if the blitter uses palette animation at all.
Definition: 32bpp_base.cpp:183
Blitter_32bppBase::DrawLine
void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash) override
Draw a line with a given colour.
Definition: 32bpp_base.cpp:26
Blitter_32bppBase::BufferSize
int BufferSize(int width, int height) override
Calculate how much memory there is needed for an image of this size in the video-buffer.
Definition: 32bpp_base.cpp:143
Blitter_32bppBase::CopyImageToBuffer
void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch) override
Copy from the screen to a buffer in a palette format for 8bpp and RGBA format for 32bpp.
Definition: 32bpp_base.cpp:72
Palette
Information about the currently used palette.
Definition: gfx_type.h:313