OpenTTD Source 20241224-master-gf74b0cf984
32bpp_simple.cpp
Go to the documentation of this file.
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 */
7
10#include "../stdafx.h"
11#include "../zoom_func.h"
12#include "../palette_func.h"
13#include "32bpp_simple.hpp"
14
15#include "../table/sprites.h"
16
17#include "../safeguards.h"
18
21
23{
24 const Blitter_32bppSimple::Pixel *src, *src_line;
25 Colour *dst, *dst_line;
26
27 /* Find where to start reading in the source sprite */
28 src_line = (const Blitter_32bppSimple::Pixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
29 dst_line = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
30
31 for (int y = 0; y < bp->height; y++) {
32 dst = dst_line;
33 dst_line += bp->pitch;
34
35 src = src_line;
36 src_line += bp->sprite_width * ScaleByZoom(1, zoom);
37
38 for (int x = 0; x < bp->width; x++) {
39 switch (mode) {
40 case BM_COLOUR_REMAP:
41 /* In case the m-channel is zero, do not remap this pixel in any way */
42 if (src->m == 0) {
43 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
44 } else {
45 if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
46 }
47 break;
48
49 case BM_CRASH_REMAP:
50 if (src->m == 0) {
51 if (src->a != 0) {
52 uint8_t g = MakeDark(src->r, src->g, src->b);
53 *dst = ComposeColourRGBA(g, g, g, src->a, *dst);
54 }
55 } else {
56 if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
57 }
58 break;
59
60 case BM_BLACK_REMAP:
61 if (src->a != 0) {
62 *dst = Colour(0, 0, 0);
63 }
64 break;
65
66 case BM_TRANSPARENT:
67 /* Make the current colour a bit more black, so it looks like this image is transparent */
68 if (src->a != 0) {
69 *dst = MakeTransparent(*dst, 192);
70 }
71 break;
72
74 /* Apply custom transparency remap. */
75 if (src->a != 0) {
76 *dst = this->LookupColourInPalette(bp->remap[GetNearestColourIndex(*dst)]);
77 }
78 break;
79
80 default:
81 if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
82 break;
83 }
84 dst++;
85 src += ScaleByZoom(1, zoom);
86 }
87 }
88}
89
90void Blitter_32bppSimple::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
91{
92 Colour *udst = (Colour *)dst;
93
94 if (pal == PALETTE_TO_TRANSPARENT) {
95 do {
96 for (int i = 0; i != width; i++) {
97 *udst = MakeTransparent(*udst, 154);
98 udst++;
99 }
100 udst = udst - width + _screen.pitch;
101 } while (--height);
102 return;
103 }
104 if (pal == PALETTE_NEWSPAPER) {
105 do {
106 for (int i = 0; i != width; i++) {
107 *udst = MakeGrey(*udst);
108 udst++;
109 }
110 udst = udst - width + _screen.pitch;
111 } while (--height);
112 return;
113 }
114
115 Debug(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('{}')", pal);
116}
117
119{
121 Sprite *dest_sprite = allocator.Allocate<Sprite>(sizeof(*dest_sprite) + static_cast<size_t>(sprite[ZOOM_LVL_MIN].height) * static_cast<size_t>(sprite[ZOOM_LVL_MIN].width) * sizeof(*dst));
122
123 dest_sprite->height = sprite[ZOOM_LVL_MIN].height;
124 dest_sprite->width = sprite[ZOOM_LVL_MIN].width;
125 dest_sprite->x_offs = sprite[ZOOM_LVL_MIN].x_offs;
126 dest_sprite->y_offs = sprite[ZOOM_LVL_MIN].y_offs;
127
128 dst = (Blitter_32bppSimple::Pixel *)dest_sprite->data;
130
131 for (int i = 0; i < sprite[ZOOM_LVL_MIN].height * sprite[ZOOM_LVL_MIN].width; i++) {
132 if (src->m == 0) {
133 dst[i].r = src->r;
134 dst[i].g = src->g;
135 dst[i].b = src->b;
136 dst[i].a = src->a;
137 dst[i].m = 0;
138 dst[i].v = 0;
139 } else {
140 /* Get brightest value */
141 uint8_t rgb_max = std::max({src->r, src->g, src->b});
142
143 /* Black pixel (8bpp or old 32bpp image), so use default value */
144 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
145 dst[i].v = rgb_max;
146
147 /* Pre-convert the mapping channel to a RGB value */
148 Colour colour = this->AdjustBrightness(this->LookupColourInPalette(src->m), dst[i].v);
149 dst[i].r = colour.r;
150 dst[i].g = colour.g;
151 dst[i].b = colour.b;
152 dst[i].a = src->a;
153 dst[i].m = src->m;
154 }
155 src++;
156 }
157
158 return dest_sprite;
159}
static FBlitter_32bppSimple iFBlitter_32bppSimple
Instantiation of the simple 32bpp blitter factory.
Simple 32 bpp blitter.
BlitterMode
The modes of blitting we can do.
Definition base.hpp:17
@ BM_BLACK_REMAP
Perform remapping to a completely blackened sprite.
Definition base.hpp:23
@ BM_COLOUR_REMAP
Perform a colour remapping.
Definition base.hpp:19
@ BM_TRANSPARENT_REMAP
Perform transparency colour remapping.
Definition base.hpp:21
@ BM_TRANSPARENT
Perform transparency darkening remapping.
Definition base.hpp:20
@ BM_CRASH_REMAP
Perform a crash remapping.
Definition base.hpp:22
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
static Colour MakeTransparent(Colour colour, uint nom, uint denom=256)
Make a pixel looks like it is transparent.
static Colour ComposeColourPA(Colour colour, uint a, Colour current)
Compose a colour based on Pixel value, alpha value, and the current pixel value.
static Colour MakeGrey(Colour colour)
Make a colour grey - based.
static uint8_t MakeDark(uint8_t r, uint8_t g, uint8_t b)
Make a colour dark grey, for specialized 32bpp remapping.
static Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
Compose a colour based on RGBA values and the current pixel value.
Sprite * Encode(const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal) override
Draw a colourtable to the screen.
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override
Draw an image to the screen, given an amount of params defined above.
Factory for the simple 32 bpp blitter.
Interface for something that can allocate memory for a sprite.
T * Allocate(size_t size)
Allocate memory for a sprite.
std::array< Sprite, ZOOM_LVL_END > SpriteCollection
Type defining a collection of sprites, one for each zoom level.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:19
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition palette.cpp:127
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition sprites.h:1602
static const PaletteID PALETTE_NEWSPAPER
Recolour sprite for newspaper-greying.
Definition sprites.h:1604
Parameters related to blitting.
Definition base.hpp:32
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition base.hpp:37
void * dst
Destination buffer.
Definition base.hpp:45
int left
The left offset in the 'dst' in pixels to start drawing.
Definition base.hpp:42
int pitch
The pitch of the destination buffer.
Definition base.hpp:46
int sprite_width
Real width of the sprite.
Definition base.hpp:40
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition base.hpp:36
int height
The height in pixels that needs to be drawn to dst.
Definition base.hpp:39
const uint8_t * remap
XXX – Temporary storage for remap array.
Definition base.hpp:34
int width
The width in pixels that needs to be drawn to dst.
Definition base.hpp:38
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition base.hpp:33
int top
The top offset in the 'dst' in pixels to start drawing.
Definition base.hpp:43
uint8_t m
Remap-channel.
uint8_t g
Green-channel.
uint8_t b
Blue-channel.
uint8_t r
Red-channel.
uint8_t a
Alpha-channel.
uint8_t v
Brightness-channel.
Definition of a common pixel in OpenTTD's realm.
uint8_t m
Remap-channel.
uint8_t b
Blue-channel.
uint8_t r
Red-channel.
uint8_t g
Green-channel.
uint8_t a
Alpha-channel.
Data structure describing a sprite.
Definition spritecache.h:17
uint16_t width
Width of the sprite.
Definition spritecache.h:19
uint16_t height
Height of the sprite.
Definition spritecache.h:18
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition spritecache.h:21
uint8_t data[]
Sprite data.
Definition spritecache.h:22
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition spritecache.h:20
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition gfx_type.h:165
uint8_t b
colour channels in BE order
Definition gfx_type.h:171
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_MIN) When shifting right,...
Definition zoom_func.h:22
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16
@ ZOOM_LVL_MIN
Minimum zoom level.
Definition zoom_type.h:41