OpenTTD Source  1.11.0-beta2
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 "32bpp_simple.hpp"
13 
14 #include "../table/sprites.h"
15 
16 #include "../safeguards.h"
17 
20 
22 {
23  const Blitter_32bppSimple::Pixel *src, *src_line;
24  Colour *dst, *dst_line;
25 
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;
29 
30  for (int y = 0; y < bp->height; y++) {
31  dst = dst_line;
32  dst_line += bp->pitch;
33 
34  src = src_line;
35  src_line += bp->sprite_width * ScaleByZoom(1, zoom);
36 
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);
45  }
46  break;
47 
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);
53  }
54  } else {
55  if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->AdjustBrightness(this->LookupColourInPalette(bp->remap[src->m]), src->v), src->a, *dst);
56  }
57  break;
58 
59  case BM_BLACK_REMAP:
60  if (src->a != 0) {
61  *dst = Colour(0, 0, 0);
62  }
63  break;
64 
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 ;) */
69 
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;
73 
74  default:
75  if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
76  break;
77  }
78  dst++;
79  src += ScaleByZoom(1, zoom);
80  }
81  }
82 }
83 
84 void Blitter_32bppSimple::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
85 {
86  Colour *udst = (Colour *)dst;
87 
88  if (pal == PALETTE_TO_TRANSPARENT) {
89  do {
90  for (int i = 0; i != width; i++) {
91  *udst = MakeTransparent(*udst, 154);
92  udst++;
93  }
94  udst = udst - width + _screen.pitch;
95  } while (--height);
96  return;
97  }
98  if (pal == PALETTE_NEWSPAPER) {
99  do {
100  for (int i = 0; i != width; i++) {
101  *udst = MakeGrey(*udst);
102  udst++;
103  }
104  udst = udst - width + _screen.pitch;
105  } while (--height);
106  return;
107  }
108 
109  DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
110 }
111 
112 Sprite *Blitter_32bppSimple::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
113 {
115  Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + (size_t)sprite->height * (size_t)sprite->width * sizeof(*dst));
116 
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;
121 
122  dst = (Blitter_32bppSimple::Pixel *)dest_sprite->data;
124 
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});
136 
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;
140 
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;
148  }
149  src++;
150  }
151 
152  return dest_sprite;
153 }
Blitter::BlitterParams::top
int top
The top offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:42
BM_TRANSPARENT
@ BM_TRANSPARENT
Perform transparency colour remapping.
Definition: base.hpp:20
Blitter::BlitterParams::skip_left
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition: base.hpp:35
BlitterMode
BlitterMode
The modes of blitting we can do.
Definition: base.hpp:17
Blitter::BlitterParams::width
int width
The width in pixels that needs to be drawn to dst.
Definition: base.hpp:37
Sprite::data
byte data[]
Sprite data.
Definition: spritecache.h:22
Blitter_32bppSimple::Pixel
Definition: 32bpp_simple.hpp:18
Blitter::BlitterParams::dst
void * dst
Destination buffer.
Definition: base.hpp:44
PALETTE_TO_TRANSPARENT
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition: sprites.h:1591
Sprite::height
uint16 height
Height of the sprite.
Definition: spritecache.h:18
Sprite::x_offs
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:20
Blitter::BlitterParams::sprite_width
int sprite_width
Real width of the sprite.
Definition: base.hpp:39
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
Blitter::BlitterParams::pitch
int pitch
The pitch of the destination buffer.
Definition: base.hpp:45
Blitter_32bppSimple::Encode
Sprite * Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator) override
Convert a sprite from the loader to our own format.
Definition: 32bpp_simple.cpp:112
Blitter_32bppSimple::Pixel::b
uint8 b
Blue-channel.
Definition: 32bpp_simple.hpp:21
SpriteLoader::Sprite::data
SpriteLoader::CommonPixel * data
The sprite itself.
Definition: spriteloader.hpp:54
Blitter::BlitterParams::sprite
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition: base.hpp:32
Blitter_32bppSimple::Pixel::m
uint8 m
Remap-channel.
Definition: 32bpp_simple.hpp:23
Blitter_32bppBase::MakeGrey
static Colour MakeGrey(Colour colour)
Make a colour grey - based.
Definition: 32bpp_base.hpp:144
32bpp_simple.hpp
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
BM_COLOUR_REMAP
@ BM_COLOUR_REMAP
Perform a colour remapping.
Definition: base.hpp:19
SpriteLoader::CommonPixel
Definition of a common pixel in OpenTTD's realm.
Definition: spriteloader.hpp:33
Blitter_32bppSimple::Pixel::g
uint8 g
Green-channel.
Definition: 32bpp_simple.hpp:20
PALETTE_NEWSPAPER
static const PaletteID PALETTE_NEWSPAPER
Recolour sprite for newspaper-greying.
Definition: sprites.h:1593
BM_CRASH_REMAP
@ BM_CRASH_REMAP
Perform a crash remapping.
Definition: base.hpp:21
Blitter_32bppBase::ComposeColourPA
static Colour ComposeColourPA(Colour colour, uint a, Colour current)
Compose a colour based on Pixel value, alpha value, and the current pixel value.
Definition: 32bpp_base.hpp:87
Sprite::width
uint16 width
Width of the sprite.
Definition: spritecache.h:19
Blitter_32bppBase::LookupColourInPalette
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition: 32bpp_base.hpp:38
Blitter_32bppSimple::DrawColourMappingRect
void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal) override
Draw a colourtable to the screen.
Definition: 32bpp_simple.cpp:84
SpriteLoader::Sprite::x_offs
int16 x_offs
The x-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:50
BM_BLACK_REMAP
@ BM_BLACK_REMAP
Perform remapping to a completely blackened sprite.
Definition: base.hpp:22
Blitter_32bppBase::MakeDark
static uint8 MakeDark(uint8 r, uint8 g, uint8 b)
Make a colour dark grey, for specialized 32bpp remapping.
Definition: 32bpp_base.hpp:121
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:163
Blitter_32bppBase::ComposeColourRGBA
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.
Definition: 32bpp_base.hpp:63
Blitter_32bppSimple::Pixel::a
uint8 a
Alpha-channel.
Definition: 32bpp_simple.hpp:22
SpriteLoader::Sprite::width
uint16 width
Width of the sprite.
Definition: spriteloader.hpp:49
ScaleByZoom
static int ScaleByZoom(int value, ZoomLevel zoom)
Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_NORMAL) When shifting right,...
Definition: zoom_func.h:22
Blitter_32bppSimple::Pixel::r
uint8 r
Red-channel.
Definition: 32bpp_simple.hpp:19
iFBlitter_32bppSimple
static FBlitter_32bppSimple iFBlitter_32bppSimple
Instantiation of the simple 32bpp blitter factory.
Definition: 32bpp_simple.cpp:19
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
Blitter::BlitterParams::left
int left
The left offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:41
Blitter_32bppSimple::Pixel::v
uint8 v
Brightness-channel.
Definition: 32bpp_simple.hpp:24
Sprite::y_offs
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:21
SpriteLoader::Sprite
Structure for passing information from the sprite loader to the blitter.
Definition: spriteloader.hpp:47
Blitter_32bppBase::MakeTransparent
static Colour MakeTransparent(Colour colour, uint nom, uint denom=256)
Make a pixel looks like it is transparent.
Definition: 32bpp_base.hpp:105
Blitter::BlitterParams
Parameters related to blitting.
Definition: base.hpp:31
Blitter_32bppSimple::Draw
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override
Draw an image to the screen, given an amount of params defined above.
Definition: 32bpp_simple.cpp:21
Blitter::BlitterParams::height
int height
The height in pixels that needs to be drawn to dst.
Definition: base.hpp:38
SpriteLoader::Sprite::y_offs
int16 y_offs
The y-offset of where the sprite will be drawn.
Definition: spriteloader.hpp:51
SpriteLoader::Sprite::height
uint16 height
Height of the sprite.
Definition: spriteloader.hpp:48
Blitter::BlitterParams::skip_top
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition: base.hpp:36
Sprite
Data structure describing a sprite.
Definition: spritecache.h:17
Blitter::BlitterParams::remap
const byte * remap
XXX – Temporary storage for remap array.
Definition: base.hpp:33
FBlitter_32bppSimple
Factory for the simple 32 bpp blitter.
Definition: 32bpp_simple.hpp:35