# 32bpp_base.hpp

Go to the documentation of this file.

    00001 /* $Id: 32bpp_base.hpp 21782 2011-01-14 15:34:33Z 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 "../gfx_func.h"
    00018
    00019 class Blitter_32bppBase : public Blitter {
    00020 public:
    00021   /* virtual */ uint8 GetScreenDepth() { return 32; }
    00022 //  /* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
    00023 //  /* virtual */ void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal);
    00024 //  /* virtual */ Sprite *Encode(SpriteLoader::Sprite *sprite, AllocatorProc *allocator);
    00025   /* virtual */ void *MoveTo(const void *video, int x, int y);
    00026   /* virtual */ void SetPixel(void *video, int x, int y, uint8 colour);
    00027   /* virtual */ void DrawRect(void *video, int width, int height, uint8 colour);
    00028   /* virtual */ void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour);
    00029   /* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height);
    00030   /* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height);
    00031   /* virtual */ void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
    00032   /* virtual */ void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y);
    00033   /* virtual */ int BufferSize(int width, int height);
    00034   /* virtual */ void PaletteAnimate(uint start, uint count);
    00035   /* virtual */ Blitter::PaletteAnimation UsePaletteAnimation();
    00036   /* virtual */ int GetBytesPerPixel() { return 4; }
    00037
    00041   static inline uint32 ComposeColour(uint a, uint r, uint g, uint b)
    00042   {
    00043     return (((a) << 24) & 0xFF000000) | (((r) << 16) & 0x00FF0000) | (((g) << 8) & 0x0000FF00) | ((b) & 0x000000FF);
    00044   }
    00045
    00049   static inline uint32 LookupColourInPalette(uint index)
    00050   {
    00051     return _cur_palette[index].data;
    00052   }
    00053
    00057   static inline uint32 ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, uint32 current)
    00058   {
    00059     uint cr = GB(current, 16, 8);
    00060     uint cg = GB(current, 8,  8);
    00061     uint cb = GB(current, 0,  8);
    00062
    00063     /* The 256 is wrong, it should be 255, but 256 is much faster... */
    00064     return ComposeColour(0xFF,
    00065               ((int)(r - cr) * a) / 256 + cr,
    00066               ((int)(g - cg) * a) / 256 + cg,
    00067               ((int)(b - cb) * a) / 256 + cb);
    00068   }
    00069
    00074   static inline uint32 ComposeColourRGBA(uint r, uint g, uint b, uint a, uint32 current)
    00075   {
    00076     if (a == 0) return current;
    00077     if (a >= 255) return ComposeColour(0xFF, r, g, b);
    00078
    00079     return ComposeColourRGBANoCheck(r, g, b, a, current);
    00080   }
    00081
    00085   static inline uint32 ComposeColourPANoCheck(uint32 colour, uint a, uint32 current)
    00086   {
    00087     uint r  = GB(colour,  16, 8);
    00088     uint g  = GB(colour,  8,  8);
    00089     uint b  = GB(colour,  0,  8);
    00090
    00091     return ComposeColourRGBANoCheck(r, g, b, a, current);
    00092   }
    00093
    00098   static inline uint32 ComposeColourPA(uint32 colour, uint a, uint32 current)
    00099   {
    00100     if (a == 0) return current;
    00101     if (a >= 255) return (colour | 0xFF000000);
    00102
    00103     return ComposeColourPANoCheck(colour, a, current);
    00104   }
    00105
    00113   static inline uint32 MakeTransparent(uint32 colour, uint nom, uint denom = 256)
    00114   {
    00115     uint r = GB(colour, 16, 8);
    00116     uint g = GB(colour, 8,  8);
    00117     uint b = GB(colour, 0,  8);
    00118
    00119     return ComposeColour(0xFF, r * nom / denom, g * nom / denom, b * nom / denom);
    00120   }
    00121
    00127   static inline uint32 MakeGrey(uint32 colour)
    00128   {
    00129     uint r = GB(colour, 16, 8);
    00130     uint g = GB(colour, 8,  8);
    00131     uint b = GB(colour, 0,  8);
    00132
    00133     /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
    00134      *  divide by it to normalize the value to a byte again. See heightmap.cpp for
    00135      *  information about the formula. */
    00136     colour = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
    00137
    00138     return ComposeColour(0xFF, colour, colour, colour);
    00139   }
    00140 };
    00141
    00142 #endif /* BLITTER_32BPP_BASE_HPP */