OpenTTD Source 20250524-master-gc366e6a48e
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) {
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(AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
46 }
47 break;
48
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(AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
57 }
58 break;
59
61 if (src->a != 0) {
62 *dst = Colour(0, 0, 0);
63 }
64 break;
65
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{
120 const auto &root_sprite = sprite.Root();
122 Sprite *dest_sprite = allocator.Allocate<Sprite>(sizeof(*dest_sprite) + static_cast<size_t>(root_sprite.height) * static_cast<size_t>(root_sprite.width) * sizeof(*dst));
123
124 dest_sprite->height = root_sprite.height;
125 dest_sprite->width = root_sprite.width;
126 dest_sprite->x_offs = root_sprite.x_offs;
127 dest_sprite->y_offs = root_sprite.y_offs;
128
129 dst = reinterpret_cast<Blitter_32bppSimple::Pixel *>(dest_sprite->data);
130 SpriteLoader::CommonPixel *src = reinterpret_cast<SpriteLoader::CommonPixel *>(root_sprite.data);
131
132 for (int i = 0; i < root_sprite.height * root_sprite.width; i++) {
133 if (src->m == 0) {
134 dst[i].r = src->r;
135 dst[i].g = src->g;
136 dst[i].b = src->b;
137 dst[i].a = src->a;
138 dst[i].m = 0;
139 dst[i].v = 0;
140 } else {
141 /* Get brightest value */
142 uint8_t rgb_max = std::max({src->r, src->g, src->b});
143
144 /* Black pixel (8bpp or old 32bpp image), so use default value */
145 if (rgb_max == 0) rgb_max = DEFAULT_BRIGHTNESS;
146 dst[i].v = rgb_max;
147
148 /* Pre-convert the mapping channel to a RGB value */
149 Colour colour = AdjustBrightness(this->LookupColourInPalette(src->m), dst[i].v);
150 dst[i].r = colour.r;
151 dst[i].g = colour.g;
152 dst[i].b = colour.b;
153 dst[i].a = src->a;
154 dst[i].m = src->m;
155 }
156 src++;
157 }
158
159 return dest_sprite;
160}
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
@ Transparent
Perform transparency darkening remapping.
@ CrashRemap
Perform a crash remapping.
@ BlackRemap
Perform remapping to a completely blackened sprite.
@ TransparentRemap
Perform transparency colour remapping.
@ ColourRemap
Perform a colour remapping.
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.
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.
Sprite * Encode(SpriteType sprite_type, const SpriteLoader::SpriteCollection &sprite, SpriteAllocator &allocator) override
Convert a sprite from the loader to our own format.
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.
Map zoom level to data.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
SpriteType
Types of sprites that might be loaded.
Definition gfx_type.h:352
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
uint8_t GetNearestColourIndex(uint8_t r, uint8_t g, uint8_t b)
Get nearest colour palette index from an RGB colour.
Definition palette.cpp:151
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition sprites.h:1606
static const PaletteID PALETTE_NEWSPAPER
Recolour sprite for newspaper-greying.
Definition sprites.h:1608
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
std::byte 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
int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZoomLevel::Min) When shifting right,...
Definition zoom_func.h:22
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:16