32bpp_base.hpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_base.hpp 24610 2012-10-17 20:21:43Z frosch $ */
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 Colour LookupColourInPalette(uint index)
00040   {
00041     return _cur_palette.palette[index];
00042   }
00043 
00047   static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
00048   {
00049     uint cr = current.r;
00050     uint cg = current.g;
00051     uint cb = current.b;
00052 
00053     /* The 256 is wrong, it should be 255, but 256 is much faster... */
00054     return Colour(
00055               ((int)(r - cr) * a) / 256 + cr,
00056               ((int)(g - cg) * a) / 256 + cg,
00057               ((int)(b - cb) * a) / 256 + cb);
00058   }
00059 
00064   static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
00065   {
00066     if (a == 0) return current;
00067     if (a >= 255) return Colour(r, g, b);
00068 
00069     return ComposeColourRGBANoCheck(r, g, b, a, current);
00070   }
00071 
00075   static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
00076   {
00077     uint r  = colour.r;
00078     uint g  = colour.g;
00079     uint b  = colour.b;
00080 
00081     return ComposeColourRGBANoCheck(r, g, b, a, current);
00082   }
00083 
00088   static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
00089   {
00090     if (a == 0) return current;
00091     if (a >= 255) {
00092       colour.a = 255;
00093       return colour;
00094     }
00095 
00096     return ComposeColourPANoCheck(colour, a, current);
00097   }
00098 
00106   static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
00107   {
00108     uint r = colour.r;
00109     uint g = colour.g;
00110     uint b = colour.b;
00111 
00112     return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
00113   }
00114 
00120   static inline Colour MakeGrey(Colour colour)
00121   {
00122     uint r = colour.r;
00123     uint g = colour.g;
00124     uint b = colour.b;
00125 
00126     /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
00127      *  divide by it to normalize the value to a byte again. See heightmap.cpp for
00128      *  information about the formula. */
00129     uint grey = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
00130 
00131     return Colour(grey, grey, grey);
00132   }
00133 
00134   static const int DEFAULT_BRIGHTNESS = 128;
00135 
00136   static inline Colour AdjustBrightness(Colour colour, uint8 brightness)
00137   {
00138     /* Shortcut for normal brightness */
00139     if (brightness == DEFAULT_BRIGHTNESS) return colour;
00140 
00141     uint16 ob = 0;
00142     uint16 r = colour.r * brightness / DEFAULT_BRIGHTNESS;
00143     uint16 g = colour.g * brightness / DEFAULT_BRIGHTNESS;
00144     uint16 b = colour.b * brightness / DEFAULT_BRIGHTNESS;
00145 
00146     /* Sum overbright */
00147     if (r > 255) ob += r - 255;
00148     if (g > 255) ob += g - 255;
00149     if (b > 255) ob += b - 255;
00150 
00151     if (ob == 0) return Colour(r, g, b, colour.a);
00152 
00153     /* Reduce overbright strength */
00154     ob /= 2;
00155     return Colour(
00156                          r >= 255 ? 255 : min(r + ob * (255 - r) / 256, 255),
00157                          g >= 255 ? 255 : min(g + ob * (255 - g) / 256, 255),
00158                          b >= 255 ? 255 : min(b + ob * (255 - b) / 256, 255),
00159                          colour.a);
00160   }
00161 };
00162 
00163 #endif /* BLITTER_32BPP_BASE_HPP */