32bpp_base.hpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_base.hpp 23670 2011-12-24 23:33:45Z peter1138 $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef BLITTER_32BPP_BASE_HPP
00013 #define BLITTER_32BPP_BASE_HPP
00014 
00015 #include "base.hpp"
00016 #include "../core/bitmath_func.hpp"
00017 #include "../core/math_func.hpp"
00018 #include "../gfx_func.h"
00019 
00021 class Blitter_32bppBase : public Blitter {
00022 public:
00023   /* virtual */ uint8 GetScreenDepth() { return 32; }
00024   /* virtual */ void *MoveTo(void *video, int x, int y);
00025   /* virtual */ void SetPixel(void *video, int x, int y, uint8 colour);
00026   /* virtual */ void DrawRect(void *video, int width, int height, uint8 colour);
00027   /* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height);
00028   /* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height);
00029   /* virtual */ void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
00030   /* virtual */ void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y);
00031   /* virtual */ int BufferSize(int width, int height);
00032   /* virtual */ void PaletteAnimate(const Palette &palette);
00033   /* virtual */ Blitter::PaletteAnimation UsePaletteAnimation();
00034   /* virtual */ int GetBytesPerPixel() { return 4; }
00035 
00039   static inline uint32 ComposeColour(uint a, uint r, uint g, uint b)
00040   {
00041     return (((a) << 24) & 0xFF000000) | (((r) << 16) & 0x00FF0000) | (((g) << 8) & 0x0000FF00) | ((b) & 0x000000FF);
00042   }
00043 
00047   static inline uint32 LookupColourInPalette(uint index)
00048   {
00049     return _cur_palette.palette[index].data;
00050   }
00051 
00055   static inline uint32 ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, uint32 current)
00056   {
00057     uint cr = GB(current, 16, 8);
00058     uint cg = GB(current, 8,  8);
00059     uint cb = GB(current, 0,  8);
00060 
00061     /* The 256 is wrong, it should be 255, but 256 is much faster... */
00062     return ComposeColour(0xFF,
00063               ((int)(r - cr) * a) / 256 + cr,
00064               ((int)(g - cg) * a) / 256 + cg,
00065               ((int)(b - cb) * a) / 256 + cb);
00066   }
00067 
00072   static inline uint32 ComposeColourRGBA(uint r, uint g, uint b, uint a, uint32 current)
00073   {
00074     if (a == 0) return current;
00075     if (a >= 255) return ComposeColour(0xFF, r, g, b);
00076 
00077     return ComposeColourRGBANoCheck(r, g, b, a, current);
00078   }
00079 
00083   static inline uint32 ComposeColourPANoCheck(uint32 colour, uint a, uint32 current)
00084   {
00085     uint r  = GB(colour,  16, 8);
00086     uint g  = GB(colour,  8,  8);
00087     uint b  = GB(colour,  0,  8);
00088 
00089     return ComposeColourRGBANoCheck(r, g, b, a, current);
00090   }
00091 
00096   static inline uint32 ComposeColourPA(uint32 colour, uint a, uint32 current)
00097   {
00098     if (a == 0) return current;
00099     if (a >= 255) return (colour | 0xFF000000);
00100 
00101     return ComposeColourPANoCheck(colour, a, current);
00102   }
00103 
00111   static inline uint32 MakeTransparent(uint32 colour, uint nom, uint denom = 256)
00112   {
00113     uint r = GB(colour, 16, 8);
00114     uint g = GB(colour, 8,  8);
00115     uint b = GB(colour, 0,  8);
00116 
00117     return ComposeColour(0xFF, r * nom / denom, g * nom / denom, b * nom / denom);
00118   }
00119 
00125   static inline uint32 MakeGrey(uint32 colour)
00126   {
00127     uint r = GB(colour, 16, 8);
00128     uint g = GB(colour, 8,  8);
00129     uint b = GB(colour, 0,  8);
00130 
00131     /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
00132      *  divide by it to normalize the value to a byte again. See heightmap.cpp for
00133      *  information about the formula. */
00134     colour = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
00135 
00136     return ComposeColour(0xFF, colour, colour, colour);
00137   }
00138 
00139   static const int DEFAULT_BRIGHTNESS = 64;
00140 
00141   static inline uint32 AdjustBrightness(uint32 colour, uint8 brightness)
00142   {
00143     /* Shortcut for normal brightness */
00144     if (brightness == DEFAULT_BRIGHTNESS) return colour;
00145 
00146     uint16 ob = 0;
00147     uint16 r = GB(colour, 16, 8) * brightness / DEFAULT_BRIGHTNESS;
00148     uint16 g = GB(colour, 8,  8) * brightness / DEFAULT_BRIGHTNESS;
00149     uint16 b = GB(colour, 0,  8) * brightness / DEFAULT_BRIGHTNESS;
00150 
00151     /* Sum overbright */
00152     if (r > 255) ob += r - 255;
00153     if (g > 255) ob += g - 255;
00154     if (b > 255) ob += b - 255;
00155 
00156     if (ob == 0) return ComposeColour(GB(colour, 24, 8), r, g, b);
00157 
00158     /* Reduce overbright strength */
00159     ob /= 2;
00160     return ComposeColour(GB(colour, 24, 8),
00161                          r >= 255 ? 255 : min(r + ob * (255 - r) / 256, 255),
00162                          g >= 255 ? 255 : min(g + ob * (255 - g) / 256, 255),
00163                          b >= 255 ? 255 : min(b + ob * (255 - b) / 256, 255));
00164   }
00165 };
00166 
00167 #endif /* BLITTER_32BPP_BASE_HPP */