# 32bpp_simple.cpp

Go to the documentation of this file.

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