OpenTTD Source  20240919-master-gdf0233f4c2
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_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;
33 
37  static inline Colour LookupColourInPalette(uint index)
38  {
39  return _cur_palette.palette[index];
40  }
41 
45  static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
46  {
47  uint cr = current.r;
48  uint cg = current.g;
49  uint cb = current.b;
50 
51  /* The 256 is wrong, it should be 255, but 256 is much faster... */
52  return Colour(
53  ((int)(r - cr) * a) / 256 + cr,
54  ((int)(g - cg) * a) / 256 + cg,
55  ((int)(b - cb) * a) / 256 + cb);
56  }
57 
62  static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
63  {
64  if (a == 0) return current;
65  if (a >= 255) return Colour(r, g, b);
66 
67  return ComposeColourRGBANoCheck(r, g, b, a, current);
68  }
69 
73  static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
74  {
75  uint r = colour.r;
76  uint g = colour.g;
77  uint b = colour.b;
78 
79  return ComposeColourRGBANoCheck(r, g, b, a, current);
80  }
81 
86  static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
87  {
88  if (a == 0) return current;
89  if (a >= 255) {
90  colour.a = 255;
91  return colour;
92  }
93 
94  return ComposeColourPANoCheck(colour, a, current);
95  }
96 
104  static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
105  {
106  uint r = colour.r;
107  uint g = colour.g;
108  uint b = colour.b;
109 
110  return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
111  }
112 
120  static inline uint8_t MakeDark(uint8_t r, uint8_t g, uint8_t b)
121  {
122  /* Magic-numbers are ~66% of those used in MakeGrey() */
123  return ((r * 13063) + (g * 25647) + (b * 4981)) / 65536;
124  }
125 
131  static inline Colour MakeDark(Colour colour)
132  {
133  uint8_t d = MakeDark(colour.r, colour.g, colour.b);
134 
135  return Colour(d, d, d);
136  }
137 
143  static inline Colour MakeGrey(Colour colour)
144  {
145  uint r = colour.r;
146  uint g = colour.g;
147  uint b = colour.b;
148 
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;
153 
154  return Colour(grey, grey, grey);
155  }
156 
157  static const int DEFAULT_BRIGHTNESS = 128;
158 
159  static Colour ReallyAdjustBrightness(Colour colour, uint8_t brightness);
160 
161  static inline Colour AdjustBrightness(Colour colour, uint8_t brightness)
162  {
163  /* Shortcut for normal brightness */
164  if (brightness == DEFAULT_BRIGHTNESS) return colour;
165 
166  return ReallyAdjustBrightness(colour, brightness);
167  }
168 
169  static inline uint8_t GetColourBrightness(Colour colour)
170  {
171  uint8_t rgb_max = std::max(colour.r, std::max(colour.g, colour.b));
172 
173  /* Black pixel (8bpp or old 32bpp image), so use default value */
174  if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
175 
176  return rgb_max;
177  }
178 };
179 
180 #endif /* BLITTER_32BPP_BASE_HPP */
_cur_palette
Palette _cur_palette
Current palette.
Definition: palette.cpp:24
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:29
Blitter_32bppBase::SetPixel
void SetPixel(void *video, int x, int y, uint8_t colour) override
Draw a pixel with a given colour on the video-buffer.
Definition: 32bpp_base.cpp:21
Blitter_32bppBase::BufferSize
size_t BufferSize(uint width, uint 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::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::DrawLine
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
Draw a line with a given colour.
Definition: 32bpp_base.cpp:26
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:73
Blitter_32bppBase::MakeDark
static Colour MakeDark(Colour colour)
Make a colour dark grey, for specialized 32bpp remapping.
Definition: 32bpp_base.hpp:131
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:143
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:331
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::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:86
Blitter_32bppBase::LookupColourInPalette
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition: 32bpp_base.hpp:37
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:165
Blitter::PaletteAnimation
PaletteAnimation
Types of palette animation.
Definition: base.hpp:50
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:62
Blitter_32bppBase::MakeDark
static uint8_t MakeDark(uint8_t r, uint8_t g, uint8_t b)
Make a colour dark grey, for specialized 32bpp remapping.
Definition: 32bpp_base.hpp:120
Blitter_32bppBase::DrawRect
void DrawRect(void *video, int width, int height, uint8_t colour) override
Make a single horizontal line in a single colour on the video-buffer.
Definition: 32bpp_base.cpp:34
base.hpp
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:45
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:104
Blitter_32bppBase::UsePaletteAnimation
Blitter::PaletteAnimation UsePaletteAnimation() override
Check if the blitter uses palette animation at all.
Definition: 32bpp_base.cpp:183
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
Blitter_32bppBase::GetScreenDepth
uint8_t GetScreenDepth() override
Get the screen depth this blitter works for.
Definition: 32bpp_base.hpp:21
Palette
Information about the currently used palette.
Definition: gfx_type.h:330
Colour::a
uint8_t a
colour channels in LE order
Definition: gfx_type.h:173