32bpp_simple.cpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_simple.cpp 26541 2014-04-29 18:18:52Z 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 #include "../stdafx.h"
00013 #include "../zoom_func.h"
00014 #include "32bpp_simple.hpp"
00015 
00016 #include "../table/sprites.h"
00017 
00019 static FBlitter_32bppSimple iFBlitter_32bppSimple;
00020 
00021 void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
00022 {
00023   const Blitter_32bppSimple::Pixel *src, *src_line;
00024   Colour *dst, *dst_line;
00025 
00026   /* Find where to start reading in the source sprite */
00027   src_line = (const Blitter_32bppSimple::Pixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
00028   dst_line = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
00029 
00030   for (int y = 0; y < bp->height; y++) {
00031     dst = dst_line;
00032     dst_line += bp->pitch;
00033 
00034     src = src_line;
00035     src_line += bp->sprite_width * ScaleByZoom(1, zoom);
00036 
00037     for (int x = 0; x < bp->width; x++) {
00038       switch (mode) {
00039         case BM_COLOUR_REMAP:
00040           /* In case the m-channel is zero, do not remap this pixel in any way */
00041           if (src->m == 0) {
00042             if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
00043           } else {
00044             if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
00045           }
00046           break;
00047 
00048         case BM_CRASH_REMAP:
00049           if (src->m == 0) {
00050             if (src->a != 0) {
00051               uint8 g = MakeDark(src->r, src->g, src->b);
00052               *dst = ComposeColourRGBA(g, g, g, src->a, *dst);
00053             }
00054           } else {
00055             if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
00056           }
00057           break;
00058 
00059         case BM_TRANSPARENT:
00060           /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
00061            *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
00062            *  we produce a result the newgrf maker didn't expect ;) */
00063 
00064           /* Make the current colour a bit more black, so it looks like this image is transparent */
00065           if (src->a != 0) *dst = MakeTransparent(*dst, 192);
00066           break;
00067 
00068         default:
00069           if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
00070           break;
00071       }
00072       dst++;
00073       src += ScaleByZoom(1, zoom);
00074     }
00075   }
00076 }
00077 
00078 void Blitter_32bppSimple::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
00079 {
00080   Colour *udst = (Colour *)dst;
00081 
00082   if (pal == PALETTE_TO_TRANSPARENT) {
00083     do {
00084       for (int i = 0; i != width; i++) {
00085         *udst = MakeTransparent(*udst, 154);
00086         udst++;
00087       }
00088       udst = udst - width + _screen.pitch;
00089     } while (--height);
00090     return;
00091   }
00092   if (pal == PALETTE_NEWSPAPER) {
00093     do {
00094       for (int i = 0; i != width; i++) {
00095         *udst = MakeGrey(*udst);
00096         udst++;
00097       }
00098       udst = udst - width + _screen.pitch;
00099     } while (--height);
00100     return;
00101   }
00102 
00103   DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
00104 }
00105 
00106 Sprite *Blitter_32bppSimple::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
00107 {
00108   Blitter_32bppSimple::Pixel *dst;
00109   Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + (size_t)sprite->height * (size_t)sprite->width * sizeof(*dst));
00110 
00111   dest_sprite->height = sprite->height;
00112   dest_sprite->width  = sprite->width;
00113   dest_sprite->x_offs = sprite->x_offs;
00114   dest_sprite->y_offs = sprite->y_offs;
00115 
00116   dst = (Blitter_32bppSimple::Pixel *)dest_sprite->data;
00117   SpriteLoader::CommonPixel *src = (SpriteLoader::CommonPixel *)sprite->data;
00118 
00119   for (int i = 0; i < sprite->height * sprite->width; i++) {
00120     if (src->m == 0) {
00121       dst[i].r = src->r;
00122       dst[i].g = src->g;
00123       dst[i].b = src->b;
00124       dst[i].a = src->a;
00125       dst[i].m = 0;
00126       dst[i].v = 0;
00127     } else {
00128       /* Get brightest value */
00129       uint8 rgb_max = max(src->r, max(src->g, src->b));
00130 
00131       /* Black pixel (8bpp or old 32bpp image), so use default value */
00132       if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
00133       dst[i].v = rgb_max;
00134 
00135       /* Pre-convert the mapping channel to a RGB value */
00136       Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), dst[i].v);
00137       dst[i].r = colour.r;
00138       dst[i].g = colour.g;
00139       dst[i].b = colour.b;
00140       dst[i].a = src->a;
00141       dst[i].m = src->m;
00142     }
00143     src++;
00144   }
00145 
00146   return dest_sprite;
00147 }