OpenTTD
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;
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 MakeGrey(Colour colour)
133  {
134  uint r = colour.r;
135  uint g = colour.g;
136  uint b = colour.b;
137 
138  /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
139  * divide by it to normalize the value to a byte again. See heightmap.cpp for
140  * information about the formula. */
141  uint grey = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
142 
143  return Colour(grey, grey, grey);
144  }
145 
146  static const int DEFAULT_BRIGHTNESS = 128;
147 
148  static Colour ReallyAdjustBrightness(Colour colour, uint8 brightness);
149 
150  static inline Colour AdjustBrightness(Colour colour, uint8 brightness)
151  {
152  /* Shortcut for normal brightness */
153  if (brightness == DEFAULT_BRIGHTNESS) return colour;
154 
155  return ReallyAdjustBrightness(colour, brightness);
156  }
157 };
158 
159 #endif /* BLITTER_32BPP_BASE_HPP */
Information about the currently used palette.
Definition: gfx_type.h:308
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
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:309
uint8 a
colour channels in LE order
Definition: gfx_type.h:168
uint8 GetScreenDepth() override
Get the screen depth this blitter works for.
Definition: 32bpp_base.hpp:21
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition: 32bpp_base.hpp:38
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
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
How all blitters should look like.
Definition: base.hpp:28
void CopyToBuffer(const void *video, void *dst, int width, int height) override
Copy from the screen to a buffer.
Definition: 32bpp_base.cpp:60
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
void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override
Scroll the videobuffer some &#39;x&#39; and &#39;y&#39; value.
Definition: 32bpp_base.cpp:84
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
Base for all blitters.
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
Palette _cur_palette
Current palette.
Definition: gfx.cpp:48
static Colour MakeGrey(Colour colour)
Make a colour grey - based.
Definition: 32bpp_base.hpp:132
void CopyFromBuffer(void *video, const void *src, int width, int height) override
Copy from a buffer to the screen.
Definition: 32bpp_base.cpp:48
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
static uint8 MakeDark(uint8 r, uint8 g, uint8 b)
Make a colour dark grey, for specialized 32bpp remapping.
Definition: 32bpp_base.hpp:121
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
int GetBytesPerPixel() override
Get how many bytes are needed to store a pixel.
Definition: 32bpp_base.hpp:33
Base for all 32bpp blitters.
Definition: 32bpp_base.hpp:19
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:162
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
PaletteAnimation
Types of palette animation.
Definition: base.hpp:49
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
Blitter::PaletteAnimation UsePaletteAnimation() override
Check if the blitter uses palette animation at all.
Definition: 32bpp_base.cpp:183
static Colour MakeTransparent(Colour colour, uint nom, uint denom=256)
Make a pixel looks like it is transparent.
Definition: 32bpp_base.hpp:105
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