Rework ChoosePylonPosition to simplify the loop
[openttd/fttd.git] / src / blitter / 32bpp_simple.cpp
blob3cfb4e564c544b1db4eb00f36aa8cdce00180122
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file 32bpp_simple.cpp Implementation of the simple 32 bpp blitter. */
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "../zoom_func.h"
15 #include "32bpp_simple.hpp"
17 #include "../table/sprites.h"
19 const char Blitter_32bppSimple::name[] = "32bpp-simple";
20 const char Blitter_32bppSimple::desc[] = "32bpp Simple Blitter (no palette animation)";
22 void Blitter_32bppSimple::Surface::draw (const BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
24 const Blitter_32bppSimple::Pixel *src, *src_line;
25 Colour *dst, *dst_line;
27 /* Find where to start reading in the source sprite */
28 const Sprite *sprite = static_cast<const Sprite*> (bp->sprite);
29 src_line = sprite->data + (bp->skip_top * sprite->width + bp->skip_left) * ScaleByZoom(1, zoom);
30 dst_line = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
32 for (int y = 0; y < bp->height; y++) {
33 dst = dst_line;
34 dst_line += bp->pitch;
36 src = src_line;
37 src_line += sprite->width * ScaleByZoom(1, zoom);
39 for (int x = 0; x < bp->width; x++) {
40 switch (mode) {
41 case BM_COLOUR_REMAP:
42 /* In case the m-channel is zero, do not remap this pixel in any way */
43 if (src->m == 0) {
44 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
45 } else {
46 if (bp->remap[src->m] != 0) *dst = ComposeColourPA (AdjustBrightness (LookupColourInPalette (bp->remap[src->m]), src->v), src->a, *dst);
48 break;
50 case BM_CRASH_REMAP:
51 if (src->m == 0) {
52 if (src->a != 0) {
53 uint8 g = MakeDark(src->r, src->g, src->b);
54 *dst = ComposeColourRGBA(g, g, g, src->a, *dst);
56 } else {
57 if (bp->remap[src->m] != 0) *dst = ComposeColourPA (AdjustBrightness (LookupColourInPalette (bp->remap[src->m]), src->v), src->a, *dst);
59 break;
61 case BM_BLACK_REMAP:
62 if (src->a != 0) {
63 *dst = Colour(0, 0, 0);
65 break;
67 case BM_TRANSPARENT:
68 /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
69 * This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
70 * we produce a result the newgrf maker didn't expect ;) */
72 /* Make the current colour a bit more black, so it looks like this image is transparent */
73 if (src->a != 0) *dst = MakeTransparent(*dst, 192);
74 break;
76 default:
77 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
78 break;
80 dst++;
81 src += ScaleByZoom(1, zoom);
86 ::Sprite *Blitter_32bppSimple::Encode (const RawSprite *sprite, bool is_font, AllocatorProc *allocator)
88 Blitter_32bppSimple::Pixel *dst;
89 Sprite *dest_sprite = AllocateSprite<Sprite> (sprite, allocator, (size_t)sprite->height * (size_t)sprite->width * sizeof(*dst));
91 dst = dest_sprite->data;
92 RawSprite::Pixel *src = (RawSprite::Pixel *)sprite->data;
94 for (int i = 0; i < sprite->height * sprite->width; i++) {
95 if (src->m == 0) {
96 dst[i].r = src->r;
97 dst[i].g = src->g;
98 dst[i].b = src->b;
99 dst[i].a = src->a;
100 dst[i].m = 0;
101 dst[i].v = 0;
102 } else {
103 /* Get brightest value */
104 uint8 rgb_max = max(src->r, max(src->g, src->b));
106 /* Black pixel (8bpp or old 32bpp image), so use default value */
107 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
108 dst[i].v = rgb_max;
110 /* Pre-convert the mapping channel to a RGB value */
111 Colour colour = AdjustBrightness (LookupColourInPalette(src->m), dst[i].v);
112 dst[i].r = colour.r;
113 dst[i].g = colour.g;
114 dst[i].b = colour.b;
115 dst[i].a = src->a;
116 dst[i].m = src->m;
118 src++;
121 return dest_sprite;