OpenTTD Source 20250205-master-gfd85ab1e2c
32bpp_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 <http://www.gnu.org/licenses/>.
6 */
7
10#include "../stdafx.h"
11#include "32bpp_base.hpp"
12#include "common.hpp"
13
14#include "../safeguards.h"
15
16void *Blitter_32bppBase::MoveTo(void *video, int x, int y)
17{
18 return (uint32_t *)video + x + y * _screen.pitch;
19}
20
21void Blitter_32bppBase::SetPixel(void *video, int x, int y, uint8_t colour)
22{
23 *((Colour *)video + x + y * _screen.pitch) = LookupColourInPalette(colour);
24}
25
26void Blitter_32bppBase::DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8_t colour, int width, int dash)
27{
28 const Colour c = LookupColourInPalette(colour);
29 this->DrawLineGeneric(x, y, x2, y2, screen_width, screen_height, width, dash, [=](int x, int y) {
30 *((Colour *)video + x + y * _screen.pitch) = c;
31 });
32}
33
34void Blitter_32bppBase::DrawRect(void *video, int width, int height, uint8_t colour)
35{
36 Colour colour32 = LookupColourInPalette(colour);
37
38 do {
39 Colour *dst = (Colour *)video;
40 for (int i = width; i > 0; i--) {
41 *dst = colour32;
42 dst++;
43 }
44 video = (uint32_t *)video + _screen.pitch;
45 } while (--height);
46}
47
48void Blitter_32bppBase::CopyFromBuffer(void *video, const void *src, int width, int height)
49{
50 uint32_t *dst = (uint32_t *)video;
51 const uint32_t *usrc = (const uint32_t *)src;
52
53 for (; height > 0; height--) {
54 memcpy(dst, usrc, width * sizeof(uint32_t));
55 usrc += width;
56 dst += _screen.pitch;
57 }
58}
59
60void Blitter_32bppBase::CopyToBuffer(const void *video, void *dst, int width, int height)
61{
62 uint32_t *udst = (uint32_t *)dst;
63 const uint32_t *src = (const uint32_t *)video;
64
65 for (; height > 0; height--) {
66 memcpy(udst, src, width * sizeof(uint32_t));
67 src += _screen.pitch;
68 udst += width;
69 }
70}
71
72void Blitter_32bppBase::CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
73{
74 uint32_t *udst = (uint32_t *)dst;
75 const uint32_t *src = (const uint32_t *)video;
76
77 for (; height > 0; height--) {
78 memcpy(udst, src, width * sizeof(uint32_t));
79 src += _screen.pitch;
80 udst += dst_pitch;
81 }
82}
83
84void Blitter_32bppBase::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
85{
86 const uint32_t *src;
87 uint32_t *dst;
88
89 if (scroll_y > 0) {
90 /* Calculate pointers */
91 dst = (uint32_t *)video + left + (top + height - 1) * _screen.pitch;
92 src = dst - scroll_y * _screen.pitch;
93
94 /* Decrease height and increase top */
95 top += scroll_y;
96 height -= scroll_y;
97 assert(height > 0);
98
99 /* Adjust left & width */
100 if (scroll_x >= 0) {
101 dst += scroll_x;
102 left += scroll_x;
103 width -= scroll_x;
104 } else {
105 src -= scroll_x;
106 width += scroll_x;
107 }
108
109 for (int h = height; h > 0; h--) {
110 memcpy(dst, src, width * sizeof(uint32_t));
111 src -= _screen.pitch;
112 dst -= _screen.pitch;
113 }
114 } else {
115 /* Calculate pointers */
116 dst = (uint32_t *)video + left + top * _screen.pitch;
117 src = dst - scroll_y * _screen.pitch;
118
119 /* Decrease height. (scroll_y is <=0). */
120 height += scroll_y;
121 assert(height > 0);
122
123 /* Adjust left & width */
124 if (scroll_x >= 0) {
125 dst += scroll_x;
126 left += scroll_x;
127 width -= scroll_x;
128 } else {
129 src -= scroll_x;
130 width += scroll_x;
131 }
132
133 /* the y-displacement may be 0 therefore we have to use memmove,
134 * because source and destination may overlap */
135 for (int h = height; h > 0; h--) {
136 memmove(dst, src, width * sizeof(uint32_t));
137 src += _screen.pitch;
138 dst += _screen.pitch;
139 }
140 }
141}
142
143size_t Blitter_32bppBase::BufferSize(uint width, uint height)
144{
145 return sizeof(uint32_t) * width * height;
146}
147
149{
150 /* By default, 32bpp doesn't have palette animation */
151}
152
Base for all 32 bits blitters.
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.
void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8_t colour, int width, int dash) override
Draw a line with a given colour.
void CopyToBuffer(const void *video, void *dst, int width, int height) override
Copy from the screen to a buffer.
void DrawRect(void *video, int width, int height, uint8_t colour) override
Make a single horizontal line in a single colour on the video-buffer.
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Blitter::PaletteAnimation UsePaletteAnimation() override
Check if the blitter uses palette animation at all.
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...
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.
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 CopyFromBuffer(void *video, const void *src, int width, int height) override
Copy from a buffer to the screen.
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.
void SetPixel(void *video, int x, int y, uint8_t colour) override
Draw a pixel with a given colour on the video-buffer.
PaletteAnimation
Types of palette animation.
Definition base.hpp:50
@ None
No palette animation.
Common functionality for all blitter implementations.
Information about the currently used palette.
Definition gfx_type.h:360