Fix: Data races on cursor state in OpenGL backends
[openttd-github.git] / src / blitter / 32bpp_simple.cpp
bloba8096d801db0da3bd9596e1fe979398b0114a3de
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file 32bpp_simple.cpp Implementation of the simple 32 bpp blitter. */
10 #include "../stdafx.h"
11 #include "../zoom_func.h"
12 #include "32bpp_simple.hpp"
14 #include "../table/sprites.h"
16 #include "../safeguards.h"
18 /** Instantiation of the simple 32bpp blitter factory. */
19 static FBlitter_32bppSimple iFBlitter_32bppSimple;
21 void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
23 const Blitter_32bppSimple::Pixel *src, *src_line;
24 Colour *dst, *dst_line;
26 /* Find where to start reading in the source sprite */
27 src_line = (const Blitter_32bppSimple::Pixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
28 dst_line = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
30 for (int y = 0; y < bp->height; y++) {
31 dst = dst_line;
32 dst_line += bp->pitch;
34 src = src_line;
35 src_line += bp->sprite_width * ScaleByZoom(1, zoom);
37 for (int x = 0; x < bp->width; x++) {
38 switch (mode) {
39 case BM_COLOUR_REMAP:
40 /* In case the m-channel is zero, do not remap this pixel in any way */
41 if (src->m == 0) {
42 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
43 } else {
44 if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
46 break;
48 case BM_CRASH_REMAP:
49 if (src->m == 0) {
50 if (src->a != 0) {
51 uint8 g = MakeDark(src->r, src->g, src->b);
52 *dst = ComposeColourRGBA(g, g, g, src->a, *dst);
54 } else {
55 if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
57 break;
59 case BM_BLACK_REMAP:
60 if (src->a != 0) {
61 *dst = Colour(0, 0, 0);
63 break;
65 case BM_TRANSPARENT:
66 /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
67 * This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
68 * we produce a result the newgrf maker didn't expect ;) */
70 /* Make the current colour a bit more black, so it looks like this image is transparent */
71 if (src->a != 0) *dst = MakeTransparent(*dst, 192);
72 break;
74 default:
75 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
76 break;
78 dst++;
79 src += ScaleByZoom(1, zoom);
84 void Blitter_32bppSimple::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
86 Colour *udst = (Colour *)dst;
88 if (pal == PALETTE_TO_TRANSPARENT) {
89 do {
90 for (int i = 0; i != width; i++) {
91 *udst = MakeTransparent(*udst, 154);
92 udst++;
94 udst = udst - width + _screen.pitch;
95 } while (--height);
96 return;
98 if (pal == PALETTE_NEWSPAPER) {
99 do {
100 for (int i = 0; i != width; i++) {
101 *udst = MakeGrey(*udst);
102 udst++;
104 udst = udst - width + _screen.pitch;
105 } while (--height);
106 return;
109 DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
112 Sprite *Blitter_32bppSimple::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
114 Blitter_32bppSimple::Pixel *dst;
115 Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + (size_t)sprite->height * (size_t)sprite->width * sizeof(*dst));
117 dest_sprite->height = sprite->height;
118 dest_sprite->width = sprite->width;
119 dest_sprite->x_offs = sprite->x_offs;
120 dest_sprite->y_offs = sprite->y_offs;
122 dst = (Blitter_32bppSimple::Pixel *)dest_sprite->data;
123 SpriteLoader::CommonPixel *src = (SpriteLoader::CommonPixel *)sprite->data;
125 for (int i = 0; i < sprite->height * sprite->width; i++) {
126 if (src->m == 0) {
127 dst[i].r = src->r;
128 dst[i].g = src->g;
129 dst[i].b = src->b;
130 dst[i].a = src->a;
131 dst[i].m = 0;
132 dst[i].v = 0;
133 } else {
134 /* Get brightest value */
135 uint8 rgb_max = std::max({src->r, src->g, src->b});
137 /* Black pixel (8bpp or old 32bpp image), so use default value */
138 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
139 dst[i].v = rgb_max;
141 /* Pre-convert the mapping channel to a RGB value */
142 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), dst[i].v);
143 dst[i].r = colour.r;
144 dst[i].g = colour.g;
145 dst[i].b = colour.b;
146 dst[i].a = src->a;
147 dst[i].m = src->m;
149 src++;
152 return dest_sprite;