OpenTTD Source: src/blitter/32bpp_base.hpp Source File

OpenTTD Source 15.3

32bpp_base.hpp
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef BLITTER_32BPP_BASE_HPP
11#define BLITTER_32BPP_BASE_HPP
12
13#include "base.hpp"
14#include "../gfx_func.h"
15#include "../palette_func.h"
16
18 class Blitter_32bppBase : public Blitter {
19public:
20 uint8_t GetScreenDepth() override { return 32; }
21 void *MoveTo(void *video, int x, int y) override;
22 void SetPixel(void *video, int x, int y, PixelColour colour) override;
23 void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, PixelColour colour, int width, int dash) override;
24 void DrawRect(void *video, int width, int height, PixelColour colour) override;
25 void CopyFromBuffer(void *video, const void *src, int width, int height) override;
26 void CopyToBuffer(const void *video, void *dst, int width, int height) override;
27 void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch) override;
28 void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override;
29 size_t BufferSize(uint width, uint height) override;
30 void PaletteAnimate(const Palette &palette) override;
31 Blitter::PaletteAnimation UsePaletteAnimation() override;
32
36 static inline Colour LookupColourInPalette(uint index)
37 {
38 return _cur_palette.palette[index];
39 }
40
44 static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
45 {
46 uint cr = current.r;
47 uint cg = current.g;
48 uint cb = current.b;
49
50 /* The 256 is wrong, it should be 255, but 256 is much faster... */
51 return Colour(
52 ((int)(r - cr) * a) / 256 + cr,
53 ((int)(g - cg) * a) / 256 + cg,
54 ((int)(b - cb) * a) / 256 + cb);
55 }
56
61 static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
62 {
63 if (a == 0) return current;
64 if (a >= 255) return Colour(r, g, b);
65
66 return ComposeColourRGBANoCheck(r, g, b, a, current);
67 }
68
72 static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
73 {
74 uint r = colour.r;
75 uint g = colour.g;
76 uint b = colour.b;
77
78 return ComposeColourRGBANoCheck(r, g, b, a, current);
79 }
80
85 static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
86 {
87 if (a == 0) return current;
88 if (a >= 255) {
89 colour.a = 255;
90 return colour;
91 }
92
93 return ComposeColourPANoCheck(colour, a, current);
94 }
95
103 static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
104 {
105 uint r = colour.r;
106 uint g = colour.g;
107 uint b = colour.b;
108
109 return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
110 }
111
119 static inline uint8_t MakeDark(uint8_t r, uint8_t g, uint8_t b)
120 {
121 /* Magic-numbers are ~66% of those used in MakeGrey() */
122 return ((r * 13063) + (g * 25647) + (b * 4981)) / 65536;
123 }
124
130 static inline Colour MakeDark(Colour colour)
131 {
132 uint8_t d = MakeDark(colour.r, colour.g, colour.b);
133
134 return Colour(d, d, d);
135 }
136
142 static inline Colour MakeGrey(Colour colour)
143 {
144 uint r = colour.r;
145 uint g = colour.g;
146 uint b = colour.b;
147
148 /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
149 * divide by it to normalize the value to a byte again. See heightmap.cpp for
150 * information about the formula. */
151 uint grey = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
152
153 return Colour(grey, grey, grey);
154 }
155};
156
157#endif /* BLITTER_32BPP_BASE_HPP */
base.hpp
Base for all blitters.
Blitter_32bppBase
Base for all 32bpp blitters.
Definition 32bpp_base.hpp:18
Blitter_32bppBase::BufferSize
size_t BufferSize(uint width, uint height) override
Calculate how much memory there is needed for an image of this size in the video-buffer.
Definition 32bpp_base.cpp:133
Blitter_32bppBase::ComposeColourRGBANoCheck
static Colour ComposeColourRGBANoCheck(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:44
Blitter_32bppBase::ComposeColourPANoCheck
static Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
Compose a colour based on Pixel value, alpha value, and the current pixel value.
Definition 32bpp_base.hpp:72
Blitter_32bppBase::CopyToBuffer
void CopyToBuffer(const void *video, void *dst, int width, int height) override
Copy from the screen to a buffer.
Definition 32bpp_base.cpp:60
Blitter_32bppBase::LookupColourInPalette
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition 32bpp_base.hpp:36
Blitter_32bppBase::DrawRect
void DrawRect(void *video, int width, int height, PixelColour colour) override
Make a single horizontal line in a single colour on the video-buffer.
Definition 32bpp_base.cpp:34
Blitter_32bppBase::SetPixel
void SetPixel(void *video, int x, int y, PixelColour colour) override
Draw a pixel with a given colour on the video-buffer.
Definition 32bpp_base.cpp:21
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:103
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:85
Blitter_32bppBase::UsePaletteAnimation
Blitter::PaletteAnimation UsePaletteAnimation() override
Check if the blitter uses palette animation at all.
Definition 32bpp_base.cpp:143
Blitter_32bppBase::MoveTo
void * MoveTo(void *video, int x, int y) override
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
Definition 32bpp_base.cpp:16
Blitter_32bppBase::CopyImageToBuffer
void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch) override
Copy from the screen to a buffer in a palette format for 8bpp and RGBA format for 32bpp.
Definition 32bpp_base.cpp:72
Blitter_32bppBase::PaletteAnimate
void PaletteAnimate(const Palette &palette) override
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
Definition 32bpp_base.cpp:138
Blitter_32bppBase::CopyFromBuffer
void CopyFromBuffer(void *video, const void *src, int width, int height) override
Copy from a buffer to the screen.
Definition 32bpp_base.cpp:48
Blitter_32bppBase::MakeGrey
static Colour MakeGrey(Colour colour)
Make a colour grey - based.
Definition 32bpp_base.hpp:142
Blitter_32bppBase::MakeDark
static uint8_t MakeDark(uint8_t r, uint8_t g, uint8_t b)
Make a colour dark grey, for specialized 32bpp remapping.
Definition 32bpp_base.hpp:119
Blitter_32bppBase::ScrollBuffer
void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) override
Scroll the videobuffer some 'x' and 'y' value.
Definition 32bpp_base.cpp:84
Blitter_32bppBase::MakeDark
static Colour MakeDark(Colour colour)
Make a colour dark grey, for specialized 32bpp remapping.
Definition 32bpp_base.hpp:130
Blitter_32bppBase::GetScreenDepth
uint8_t GetScreenDepth() override
Get the screen depth this blitter works for.
Definition 32bpp_base.hpp:20
Blitter_32bppBase::DrawLine
void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, PixelColour colour, int width, int dash) override
Draw a line with a given colour.
Definition 32bpp_base.cpp:26
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:61
Blitter
How all blitters should look like.
Definition base.hpp:29
Blitter::PaletteAnimation
PaletteAnimation
Types of palette animation.
Definition base.hpp:50
_cur_palette
Palette _cur_palette
Current palette.
Definition palette.cpp:24
Palette
Information about the currently used palette.
Definition gfx_type.h:373
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:374
PixelColour
Colour for pixel/line drawing.
Definition gfx_type.h:405