32bpp_simple.cpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_simple.cpp 22397 2011-05-01 10:15:33Z rubidium $ */
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 SpriteLoader::CommonPixel *src, *src_line;
00024   uint32 *dst, *dst_line;
00025 
00026   /* Find where to start reading in the source sprite */
00027   src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
00028   dst_line = (uint32 *)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->LookupColourInPalette(bp->remap[src->m]), src->a, *dst);
00045           }
00046           break;
00047 
00048         case BM_TRANSPARENT:
00049           /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
00050            *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
00051            *  we produce a result the newgrf maker didn't expect ;) */
00052 
00053           /* Make the current colour a bit more black, so it looks like this image is transparent */
00054           if (src->a != 0) *dst = MakeTransparent(*dst, 192);
00055           break;
00056 
00057         default:
00058           if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
00059           break;
00060       }
00061       dst++;
00062       src += ScaleByZoom(1, zoom);
00063     }
00064   }
00065 }
00066 
00067 void Blitter_32bppSimple::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
00068 {
00069   uint32 *udst = (uint32 *)dst;
00070 
00071   if (pal == PALETTE_TO_TRANSPARENT) {
00072     do {
00073       for (int i = 0; i != width; i++) {
00074         *udst = MakeTransparent(*udst, 154);
00075         udst++;
00076       }
00077       udst = udst - width + _screen.pitch;
00078     } while (--height);
00079     return;
00080   }
00081   if (pal == PALETTE_NEWSPAPER) {
00082     do {
00083       for (int i = 0; i != width; i++) {
00084         *udst = MakeGrey(*udst);
00085         udst++;
00086       }
00087       udst = udst - width + _screen.pitch;
00088     } while (--height);
00089     return;
00090   }
00091 
00092   DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
00093 }
00094 
00095 Sprite *Blitter_32bppSimple::Encode(SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
00096 {
00097   Sprite *dest_sprite;
00098   SpriteLoader::CommonPixel *dst;
00099   dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
00100 
00101   dest_sprite->height = sprite->height;
00102   dest_sprite->width  = sprite->width;
00103   dest_sprite->x_offs = sprite->x_offs;
00104   dest_sprite->y_offs = sprite->y_offs;
00105 
00106   dst = (SpriteLoader::CommonPixel *)dest_sprite->data;
00107 
00108   memcpy(dst, sprite->data, sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
00109   for (int i = 0; i < sprite->height * sprite->width; i++) {
00110     if (dst[i].m != 0) {
00111       /* Pre-convert the mapping channel to a RGB value */
00112       uint colour = this->LookupColourInPalette(dst[i].m);
00113       dst[i].r = GB(colour, 16, 8);
00114       dst[i].g = GB(colour, 8,  8);
00115       dst[i].b = GB(colour, 0,  8);
00116     }
00117   }
00118 
00119   return dest_sprite;
00120 }