OpenTTD Source: src/blitter/8bpp_base.cpp Source File

OpenTTD Source 15.0-RC2

8bpp_base.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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include "../gfx_func.h"
12#include "8bpp_base.hpp"
13#include "common.hpp"
14
15#include "../safeguards.h"
16
17 void Blitter_8bppBase::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
18{
19 const uint8_t *ctab = GetNonSprite(pal, SpriteType::Recolour) + 1;
20
21 do {
22 for (int i = 0; i != width; i++) *((uint8_t *)dst + i) = ctab[((uint8_t *)dst)[i]];
23 dst = (uint8_t *)dst + _screen.pitch;
24 } while (--height);
25}
26
27 void *Blitter_8bppBase::MoveTo(void *video, int x, int y)
28{
29 return (uint8_t *)video + x + y * _screen.pitch;
30}
31
32 void Blitter_8bppBase::SetPixel(void *video, int x, int y, PixelColour colour)
33{
34 *((uint8_t *)video + x + y * _screen.pitch) = colour.p;
35}
36
37 void Blitter_8bppBase::DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, PixelColour colour, int width, int dash)
38{
39 this->DrawLineGeneric(x, y, x2, y2, screen_width, screen_height, width, dash, [=](int x, int y) {
40 *((uint8_t *)video + x + y * _screen.pitch) = colour.p;
41 });
42}
43
44 void Blitter_8bppBase::DrawRect(void *video, int width, int height, PixelColour colour)
45{
46 std::byte *p = static_cast<std::byte *>(video);
47 do {
48 std::fill_n(p, width, static_cast<std::byte>(colour.p));
49 p += _screen.pitch;
50 } while (--height);
51}
52
53 void Blitter_8bppBase::CopyFromBuffer(void *video, const void *src, int width, int height)
54{
55 uint8_t *dst = (uint8_t *)video;
56 const uint8_t *usrc = (const uint8_t *)src;
57
58 for (; height > 0; height--) {
59 std::copy_n(usrc, width, dst);
60 usrc += width;
61 dst += _screen.pitch;
62 }
63}
64
65 void Blitter_8bppBase::CopyToBuffer(const void *video, void *dst, int width, int height)
66{
67 uint8_t *udst = (uint8_t *)dst;
68 const uint8_t *src = (const uint8_t *)video;
69
70 for (; height > 0; height--) {
71 std::copy_n(src, width, udst);
72 src += _screen.pitch;
73 udst += width;
74 }
75}
76
77 void Blitter_8bppBase::CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
78{
79 uint8_t *udst = (uint8_t *)dst;
80 const uint8_t *src = (const uint8_t *)video;
81
82 for (; height > 0; height--) {
83 std::copy_n(src, width, udst);
84 src += _screen.pitch;
85 udst += dst_pitch;
86 }
87}
88
89 void Blitter_8bppBase::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
90{
91 const uint8_t *src;
92 uint8_t *dst;
93
94 if (scroll_y > 0) {
95 /* Calculate pointers */
96 dst = (uint8_t *)video + left + (top + height - 1) * _screen.pitch;
97 src = dst - scroll_y * _screen.pitch;
98
99 /* Decrease height and increase top */
100 top += scroll_y;
101 height -= scroll_y;
102 assert(height > 0);
103
104 /* Adjust left & width */
105 if (scroll_x >= 0) {
106 dst += scroll_x;
107 left += scroll_x;
108 width -= scroll_x;
109 } else {
110 src -= scroll_x;
111 width += scroll_x;
112 }
113
114 Blitter::MovePixels(src, dst, width, height, -_screen.pitch);
115 } else {
116 /* Calculate pointers */
117 dst = (uint8_t *)video + left + top * _screen.pitch;
118 src = dst - scroll_y * _screen.pitch;
119
120 /* Decrease height. (scroll_y is <=0). */
121 height += scroll_y;
122 assert(height > 0);
123
124 /* Adjust left & width */
125 if (scroll_x >= 0) {
126 dst += scroll_x;
127 left += scroll_x;
128 width -= scroll_x;
129 } else {
130 src -= scroll_x;
131 width += scroll_x;
132 }
133
134 Blitter::MovePixels(src, dst, width, height, _screen.pitch);
135 }
136}
137
138 size_t Blitter_8bppBase::BufferSize(uint width, uint height)
139{
140 return static_cast<size_t>(width) * height;
141}
142
143 void Blitter_8bppBase::PaletteAnimate(const Palette &)
144{
145 /* Video backend takes care of the palette animation */
146}
147
148 Blitter::PaletteAnimation Blitter_8bppBase::UsePaletteAnimation()
149{
150 return Blitter::PaletteAnimation::VideoBackend;
151}
8bpp_base.hpp
Base for all 8 bpp blitters.
Blitter_8bppBase::SetPixel
void SetPixel(void *video, int x, int y, PixelColour colour) override
Draw a pixel with a given colour on the video-buffer.
Definition 8bpp_base.cpp:32
Blitter_8bppBase::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 8bpp_base.cpp:44
Blitter_8bppBase::CopyFromBuffer
void CopyFromBuffer(void *video, const void *src, int width, int height) override
Copy from a buffer to the screen.
Definition 8bpp_base.cpp:53
Blitter_8bppBase::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 8bpp_base.cpp:89
Blitter_8bppBase::DrawColourMappingRect
void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal) override
Draw a colourtable to the screen.
Definition 8bpp_base.cpp:17
Blitter_8bppBase::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 8bpp_base.cpp:143
Blitter_8bppBase::CopyToBuffer
void CopyToBuffer(const void *video, void *dst, int width, int height) override
Copy from the screen to a buffer.
Definition 8bpp_base.cpp:65
Blitter_8bppBase::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 8bpp_base.cpp:138
Blitter_8bppBase::UsePaletteAnimation
Blitter::PaletteAnimation UsePaletteAnimation() override
Check if the blitter uses palette animation at all.
Definition 8bpp_base.cpp:148
Blitter_8bppBase::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 8bpp_base.cpp:77
Blitter_8bppBase::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 8bpp_base.cpp:27
Blitter_8bppBase::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 8bpp_base.cpp:37
Blitter::PaletteAnimation
PaletteAnimation
Types of palette animation.
Definition base.hpp:50
Blitter::PaletteAnimation::VideoBackend
@ VideoBackend
Palette animation should be done by video backend (8bpp only!)
common.hpp
Common functionality for all blitter implementations.
SpriteType::Recolour
@ Recolour
Recolour sprite.
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
Palette
Information about the currently used palette.
Definition gfx_type.h:373
PixelColour
Colour for pixel/line drawing.
Definition gfx_type.h:405
PixelColour::p
uint8_t p
Palette index.
Definition gfx_type.h:406