OpenTTD: src/blitter/32bpp_simple.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

32bpp_simple.cpp
Go to the documentation of this file.
1 /* $Id: 32bpp_simple.cpp 26969 2014-10-06 18:45:51Z rubidium $ */
2
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 */
9
12 #include "../stdafx.h"
13 #include "../zoom_func.h"
14 #include "32bpp_simple.hpp"
15
16 #include "../table/sprites.h"
17
18 #include "../safeguards.h"
19
21 static FBlitter_32bppSimple iFBlitter_32bppSimple;
22
23 void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
24 {
25 const Blitter_32bppSimple::Pixel *src, *src_line;
26 Colour *dst, *dst_line;
27
28 /* Find where to start reading in the source sprite */
29 src_line = (const Blitter_32bppSimple::Pixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
30 dst_line = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
31
32 for (int y = 0; y < bp->height; y++) {
33 dst = dst_line;
34 dst_line += bp->pitch;
35
36 src = src_line;
37 src_line += bp->sprite_width * ScaleByZoom(1, zoom);
38
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(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
47 }
48 break;
49
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);
55 }
56 } else {
57 if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
58 }
59 break;
60
61 case BM_BLACK_REMAP:
62 if (src->a != 0) {
63 *dst = Colour(0, 0, 0);
64 }
65 break;
66
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 ;) */
71
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;
75
76 default:
77 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
78 break;
79 }
80 dst++;
81 src += ScaleByZoom(1, zoom);
82 }
83 }
84 }
85
86 void Blitter_32bppSimple::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
87 {
88 Colour *udst = (Colour *)dst;
89
90 if (pal == PALETTE_TO_TRANSPARENT) {
91 do {
92 for (int i = 0; i != width; i++) {
93 *udst = MakeTransparent(*udst, 154);
94 udst++;
95 }
96 udst = udst - width + _screen.pitch;
97 } while (--height);
98 return;
99 }
100 if (pal == PALETTE_NEWSPAPER) {
101 do {
102 for (int i = 0; i != width; i++) {
103 *udst = MakeGrey(*udst);
104 udst++;
105 }
106 udst = udst - width + _screen.pitch;
107 } while (--height);
108 return;
109 }
110
111 DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
112 }
113
114 Sprite *Blitter_32bppSimple::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
115 {
116 Blitter_32bppSimple::Pixel *dst;
117 Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + (size_t)sprite->height * (size_t)sprite->width * sizeof(*dst));
118
119 dest_sprite->height = sprite->height;
120 dest_sprite->width = sprite->width;
121 dest_sprite->x_offs = sprite->x_offs;
122 dest_sprite->y_offs = sprite->y_offs;
123
124 dst = (Blitter_32bppSimple::Pixel *)dest_sprite->data;
125 SpriteLoader::CommonPixel *src = (SpriteLoader::CommonPixel *)sprite->data;
126
127 for (int i = 0; i < sprite->height * sprite->width; i++) {
128 if (src->m == 0) {
129 dst[i].r = src->r;
130 dst[i].g = src->g;
131 dst[i].b = src->b;
132 dst[i].a = src->a;
133 dst[i].m = 0;
134 dst[i].v = 0;
135 } else {
136 /* Get brightest value */
137 uint8 rgb_max = max(src->r, max(src->g, src->b));
138
139 /* Black pixel (8bpp or old 32bpp image), so use default value */
140 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
141 dst[i].v = rgb_max;
142
143 /* Pre-convert the mapping channel to a RGB value */
144 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), dst[i].v);
145 dst[i].r = colour.r;
146 dst[i].g = colour.g;
147 dst[i].b = colour.b;
148 dst[i].a = src->a;
149 dst[i].m = src->m;
150 }
151 src++;
152 }
153
154 return dest_sprite;
155 }