32bpp_base.hpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "../gfx_func.h"
00018 
00020 class Blitter_32bppBase : public Blitter {
00021 public:
00022   /* virtual */ uint8 GetScreenDepth() { return 32; }
00023   /* virtual */ void *MoveTo(const void *video, int x, int y);
00024   /* virtual */ void SetPixel(void *video, int x, int y, uint8 colour);
00025   /* virtual */ void DrawRect(void *video, int width, int height, uint8 colour);
00026   /* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height);
00027   /* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height);
00028   /* virtual */ void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
00029   /* virtual */ void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y);
00030   /* virtual */ int BufferSize(int width, int height);
00031   /* virtual */ void PaletteAnimate(uint start, uint count);
00032   /* virtual */ Blitter::PaletteAnimation UsePaletteAnimation();
00033   /* virtual */ int GetBytesPerPixel() { return 4; }
00034 
00038   static inline uint32 ComposeColour(uint a, uint r, uint g, uint b)
00039   {
00040     return (((a) << 24) & 0xFF000000) | (((r) << 16) & 0x00FF0000) | (((g) << 8) & 0x0000FF00) | ((b) & 0x000000FF);
00041   }
00042 
00046   static inline uint32 LookupColourInPalette(uint index)
00047   {
00048     return _cur_palette[index].data;
00049   }
00050 
00054   static inline uint32 ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, uint32 current)
00055   {
00056     uint cr = GB(current, 16, 8);
00057     uint cg = GB(current, 8,  8);
00058     uint cb = GB(current, 0,  8);
00059 
00060     /* The 256 is wrong, it should be 255, but 256 is much faster... */
00061     return ComposeColour(0xFF,
00062               ((int)(r - cr) * a) / 256 + cr,
00063               ((int)(g - cg) * a) / 256 + cg,
00064               ((int)(b - cb) * a) / 256 + cb);
00065   }
00066 
00071   static inline uint32 ComposeColourRGBA(uint r, uint g, uint b, uint a, uint32 current)
00072   {
00073     if (a == 0) return current;
00074     if (a >= 255) return ComposeColour(0xFF, r, g, b);
00075 
00076     return ComposeColourRGBANoCheck(r, g, b, a, current);
00077   }
00078 
00082   static inline uint32 ComposeColourPANoCheck(uint32 colour, uint a, uint32 current)
00083   {
00084     uint r  = GB(colour,  16, 8);
00085     uint g  = GB(colour,  8,  8);
00086     uint b  = GB(colour,  0,  8);
00087 
00088     return ComposeColourRGBANoCheck(r, g, b, a, current);
00089   }
00090 
00095   static inline uint32 ComposeColourPA(uint32 colour, uint a, uint32 current)
00096   {
00097     if (a == 0) return current;
00098     if (a >= 255) return (colour | 0xFF000000);
00099 
00100     return ComposeColourPANoCheck(colour, a, current);
00101   }
00102 
00110   static inline uint32 MakeTransparent(uint32 colour, uint nom, uint denom = 256)
00111   {
00112     uint r = GB(colour, 16, 8);
00113     uint g = GB(colour, 8,  8);
00114     uint b = GB(colour, 0,  8);
00115 
00116     return ComposeColour(0xFF, r * nom / denom, g * nom / denom, b * nom / denom);
00117   }
00118 
00124   static inline uint32 MakeGrey(uint32 colour)
00125   {
00126     uint r = GB(colour, 16, 8);
00127     uint g = GB(colour, 8,  8);
00128     uint b = GB(colour, 0,  8);
00129 
00130     /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
00131      *  divide by it to normalize the value to a byte again. See heightmap.cpp for
00132      *  information about the formula. */
00133     colour = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
00134 
00135     return ComposeColour(0xFF, colour, colour, colour);
00136   }
00137 };
00138 
00139 #endif /* BLITTER_32BPP_BASE_HPP */

Generated on Fri May 27 04:19:39 2011 for OpenTTD by  doxygen 1.6.1