OpenTTD
32bpp_base.hpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #ifndef BLITTER_32BPP_BASE_HPP
13 #define BLITTER_32BPP_BASE_HPP
14 
15 #include "base.hpp"
16 #include "../core/bitmath_func.hpp"
17 #include "../core/math_func.hpp"
18 #include "../gfx_func.h"
19 
21 class Blitter_32bppBase : public Blitter {
22 public:
23  /* virtual */ uint8 GetScreenDepth() { return 32; }
24  /* virtual */ void *MoveTo(void *video, int x, int y);
25  /* virtual */ void SetPixel(void *video, int x, int y, uint8 colour);
26  /* virtual */ void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash);
27  /* virtual */ void DrawRect(void *video, int width, int height, uint8 colour);
28  /* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height);
29  /* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height);
30  /* virtual */ void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
31  /* virtual */ void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y);
32  /* virtual */ int BufferSize(int width, int height);
33  /* virtual */ void PaletteAnimate(const Palette &palette);
35  /* virtual */ int GetBytesPerPixel() { return 4; }
36 
40  static inline Colour LookupColourInPalette(uint index)
41  {
42  return _cur_palette.palette[index];
43  }
44 
48  static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
49  {
50  uint cr = current.r;
51  uint cg = current.g;
52  uint cb = current.b;
53 
54  /* The 256 is wrong, it should be 255, but 256 is much faster... */
55  return Colour(
56  ((int)(r - cr) * a) / 256 + cr,
57  ((int)(g - cg) * a) / 256 + cg,
58  ((int)(b - cb) * a) / 256 + cb);
59  }
60 
65  static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
66  {
67  if (a == 0) return current;
68  if (a >= 255) return Colour(r, g, b);
69 
70  return ComposeColourRGBANoCheck(r, g, b, a, current);
71  }
72 
76  static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
77  {
78  uint r = colour.r;
79  uint g = colour.g;
80  uint b = colour.b;
81 
82  return ComposeColourRGBANoCheck(r, g, b, a, current);
83  }
84 
89  static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
90  {
91  if (a == 0) return current;
92  if (a >= 255) {
93  colour.a = 255;
94  return colour;
95  }
96 
97  return ComposeColourPANoCheck(colour, a, current);
98  }
99 
107  static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
108  {
109  uint r = colour.r;
110  uint g = colour.g;
111  uint b = colour.b;
112 
113  return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
114  }
115 
123  static inline uint8 MakeDark(uint8 r, uint8 g, uint8 b)
124  {
125  /* Magic-numbers are ~66% of those used in MakeGrey() */
126  return ((r * 13063) + (g * 25647) + (b * 4981)) / 65536;
127  }
128 
134  static inline Colour MakeGrey(Colour colour)
135  {
136  uint r = colour.r;
137  uint g = colour.g;
138  uint b = colour.b;
139 
140  /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
141  * divide by it to normalize the value to a byte again. See heightmap.cpp for
142  * information about the formula. */
143  uint grey = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
144 
145  return Colour(grey, grey, grey);
146  }
147 
148  static const int DEFAULT_BRIGHTNESS = 128;
149 
150  static Colour ReallyAdjustBrightness(Colour colour, uint8 brightness);
151 
152  static inline Colour AdjustBrightness(Colour colour, uint8 brightness)
153  {
154  /* Shortcut for normal brightness */
155  if (brightness == DEFAULT_BRIGHTNESS) return colour;
156 
157  return ReallyAdjustBrightness(colour, brightness);
158  }
159 };
160 
161 #endif /* BLITTER_32BPP_BASE_HPP */
Blitter::PaletteAnimation UsePaletteAnimation()
Check if the blitter uses palette animation at all.
Definition: 32bpp_base.cpp:185
void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
Copy from the screen to a buffer in a palette format for 8bpp and RGBA format for 32bpp...
Definition: 32bpp_base.cpp:74
Information about the currently used palette.
Definition: gfx_type.h:309
void * MoveTo(void *video, int x, int y)
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
Definition: 32bpp_base.cpp:18
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:310
uint8 a
colour channels in LE order
Definition: gfx_type.h:170
int BufferSize(int width, int height)
Calculate how much memory there is needed for an image of this size in the video-buffer.
Definition: 32bpp_base.cpp:145
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition: 32bpp_base.hpp:40
How all blitters should look like.
Definition: base.hpp:30
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:76
void DrawRect(void *video, int width, int height, uint8 colour)
Make a single horizontal line in a single colour on the video-buffer.
Definition: 32bpp_base.cpp:36
void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
Scroll the videobuffer some &#39;x&#39; and &#39;y&#39; value.
Definition: 32bpp_base.cpp:86
void SetPixel(void *video, int x, int y, uint8 colour)
Draw a pixel with a given colour on the video-buffer.
Definition: 32bpp_base.cpp:23
void CopyFromBuffer(void *video, const void *src, int width, int height)
Copy from a buffer to the screen.
Definition: 32bpp_base.cpp:50
Base for all blitters.
void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash)
Draw a line with a given colour.
Definition: 32bpp_base.cpp:28
Palette _cur_palette
Current palette.
Definition: gfx.cpp:49
int GetBytesPerPixel()
Get how many bytes are needed to store a pixel.
Definition: 32bpp_base.hpp:35
static Colour MakeGrey(Colour colour)
Make a colour grey - based.
Definition: 32bpp_base.hpp:134
void CopyToBuffer(const void *video, void *dst, int width, int height)
Copy from the screen to a buffer.
Definition: 32bpp_base.cpp:62
static uint8 MakeDark(uint8 r, uint8 g, uint8 b)
Make a colour dark grey, for specialized 32bpp remapping.
Definition: 32bpp_base.hpp:123
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:89
void PaletteAnimate(const Palette &palette)
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
Definition: 32bpp_base.cpp:150
Base for all 32bpp blitters.
Definition: 32bpp_base.hpp:21
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:164
PaletteAnimation
Types of palette animation.
Definition: base.hpp:51
uint8 GetScreenDepth()
Get the screen depth this blitter works for.
Definition: 32bpp_base.hpp:23
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:65
static Colour MakeTransparent(Colour colour, uint nom, uint denom=256)
Make a pixel looks like it is transparent.
Definition: 32bpp_base.hpp:107
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:48