(svn r27756) -Codechange: Add StringTab enum
[openttd.git] / src / blitter / 32bpp_base.hpp
blob26c3dee3fd77457f36a233d34e6de3b9300750a9
1 /* $Id$ */
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 */
10 /** @file 32bpp_base.hpp Base for all 32 bits blitters. */
12 #ifndef BLITTER_32BPP_BASE_HPP
13 #define BLITTER_32BPP_BASE_HPP
15 #include "base.hpp"
16 #include "../core/bitmath_func.hpp"
17 #include "../core/math_func.hpp"
18 #include "../gfx_func.h"
20 /** Base for all 32bpp blitters. */
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);
33 /* virtual */ Blitter::PaletteAnimation UsePaletteAnimation();
34 /* virtual */ int GetBytesPerPixel() { return 4; }
36 /**
37 * Look up the colour in the current palette.
39 static inline Colour LookupColourInPalette(uint index)
41 return _cur_palette.palette[index];
44 /**
45 * Compose a colour based on RGBA values and the current pixel value.
47 static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
49 uint cr = current.r;
50 uint cg = current.g;
51 uint cb = current.b;
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);
60 /**
61 * Compose a colour based on RGBA values and the current pixel value.
62 * Handles fully transparent and solid pixels in a special (faster) way.
64 static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
66 if (a == 0) return current;
67 if (a >= 255) return Colour(r, g, b);
69 return ComposeColourRGBANoCheck(r, g, b, a, current);
72 /**
73 * Compose a colour based on Pixel value, alpha value, and the current pixel value.
75 static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
77 uint r = colour.r;
78 uint g = colour.g;
79 uint b = colour.b;
81 return ComposeColourRGBANoCheck(r, g, b, a, current);
84 /**
85 * Compose a colour based on Pixel value, alpha value, and the current pixel value.
86 * Handles fully transparent and solid pixels in a special (faster) way.
88 static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
90 if (a == 0) return current;
91 if (a >= 255) {
92 colour.a = 255;
93 return colour;
96 return ComposeColourPANoCheck(colour, a, current);
99 /**
100 * Make a pixel looks like it is transparent.
101 * @param colour the colour already on the screen.
102 * @param nom the amount of transparency, nominator, makes colour lighter.
103 * @param denom denominator, makes colour darker.
104 * @return the new colour for the screen.
106 static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
108 uint r = colour.r;
109 uint g = colour.g;
110 uint b = colour.b;
112 return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
116 * Make a colour dark grey, for specialized 32bpp remapping.
117 * @param r red component
118 * @param g green component
119 * @param b blue component
120 * @return the brightness value of the new colour, now dark grey.
122 static inline uint8 MakeDark(uint8 r, uint8 g, uint8 b)
124 /* Magic-numbers are ~66% of those used in MakeGrey() */
125 return ((r * 13063) + (g * 25647) + (b * 4981)) / 65536;
129 * Make a colour grey - based.
130 * @param colour the colour to make grey.
131 * @return the new colour, now grey.
133 static inline Colour MakeGrey(Colour colour)
135 uint r = colour.r;
136 uint g = colour.g;
137 uint b = colour.b;
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;
144 return Colour(grey, grey, grey);
147 static const int DEFAULT_BRIGHTNESS = 128;
149 static inline Colour AdjustBrightness(Colour colour, uint8 brightness)
151 /* Shortcut for normal brightness */
152 if (brightness == DEFAULT_BRIGHTNESS) return colour;
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;
159 /* Sum overbright */
160 if (r > 255) ob += r - 255;
161 if (g > 255) ob += g - 255;
162 if (b > 255) ob += b - 255;
164 if (ob == 0) return Colour(r, g, b, colour.a);
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);
176 #endif /* BLITTER_32BPP_BASE_HPP */