OpenTTD
32bpp_base.hpp
Go to the documentation of this file.
1 /* $Id: 32bpp_base.hpp 26463 2014-04-13 19:22:23Z peter1138 $ */
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 DrawRect(void *video, int width, int height, uint8 colour);
27  /* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height);
28  /* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height);
29  /* virtual */ void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
30  /* virtual */ void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y);
31  /* virtual */ int BufferSize(int width, int height);
32  /* virtual */ void PaletteAnimate(const Palette &palette);
34  /* virtual */ int GetBytesPerPixel() { return 4; }
35 
39  static inline Colour LookupColourInPalette(uint index)
40  {
41  return _cur_palette.palette[index];
42  }
43 
47  static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
48  {
49  uint cr = current.r;
50  uint cg = current.g;
51  uint cb = current.b;
52 
53  /* The 256 is wrong, it should be 255, but 256 is much faster... */
54  return Colour(
55  ((int)(r - cr) * a) / 256 + cr,
56  ((int)(g - cg) * a) / 256 + cg,
57  ((int)(b - cb) * a) / 256 + cb);
58  }
59 
64  static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
65  {
66  if (a == 0) return current;
67  if (a >= 255) return Colour(r, g, b);
68 
69  return ComposeColourRGBANoCheck(r, g, b, a, current);
70  }
71 
75  static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
76  {
77  uint r = colour.r;
78  uint g = colour.g;
79  uint b = colour.b;
80 
81  return ComposeColourRGBANoCheck(r, g, b, a, current);
82  }
83 
88  static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
89  {
90  if (a == 0) return current;
91  if (a >= 255) {
92  colour.a = 255;
93  return colour;
94  }
95 
96  return ComposeColourPANoCheck(colour, a, current);
97  }
98 
106  static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
107  {
108  uint r = colour.r;
109  uint g = colour.g;
110  uint b = colour.b;
111 
112  return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
113  }
114 
122  static inline uint8 MakeDark(uint8 r, uint8 g, uint8 b)
123  {
124  /* Magic-numbers are ~66% of those used in MakeGrey() */
125  return ((r * 13063) + (g * 25647) + (b * 4981)) / 65536;
126  }
127 
133  static inline Colour MakeGrey(Colour colour)
134  {
135  uint r = colour.r;
136  uint g = colour.g;
137  uint b = colour.b;
138 
139  /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
140  * divide by it to normalize the value to a byte again. See heightmap.cpp for
141  * information about the formula. */
142  uint grey = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
143 
144  return Colour(grey, grey, grey);
145  }
146 
147  static const int DEFAULT_BRIGHTNESS = 128;
148 
149  static inline Colour AdjustBrightness(Colour colour, uint8 brightness)
150  {
151  /* Shortcut for normal brightness */
152  if (brightness == DEFAULT_BRIGHTNESS) return colour;
153 
154  uint16 ob = 0;
155  uint16 r = colour.r * brightness / DEFAULT_BRIGHTNESS;
156  uint16 g = colour.g * brightness / DEFAULT_BRIGHTNESS;
157  uint16 b = colour.b * brightness / DEFAULT_BRIGHTNESS;
158 
159  /* Sum overbright */
160  if (r > 255) ob += r - 255;
161  if (g > 255) ob += g - 255;
162  if (b > 255) ob += b - 255;
163 
164  if (ob == 0) return Colour(r, g, b, colour.a);
165 
166  /* Reduce overbright strength */
167  ob /= 2;
168  return Colour(
169  r >= 255 ? 255 : min(r + ob * (255 - r) / 256, 255),
170  g >= 255 ? 255 : min(g + ob * (255 - g) / 256, 255),
171  b >= 255 ? 255 : min(b + ob * (255 - b) / 256, 255),
172  colour.a);
173  }
174 };
175 
176 #endif /* BLITTER_32BPP_BASE_HPP */