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