OpenTTD Source  20240919-master-gdf0233f4c2
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 
16 void *Blitter_32bppBase::MoveTo(void *video, int x, int y)
17 {
18  return (uint32_t *)video + x + y * _screen.pitch;
19 }
20 
21 void Blitter_32bppBase::SetPixel(void *video, int x, int y, uint8_t colour)
22 {
23  *((Colour *)video + x + y * _screen.pitch) = LookupColourInPalette(colour);
24 }
25 
26 void 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 
34 void 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 
48 void 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 
60 void 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 
72 void 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 
84 void 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 
143 size_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 
153 Colour Blitter_32bppBase::ReallyAdjustBrightness(Colour colour, uint8_t brightness)
154 {
155  assert(DEFAULT_BRIGHTNESS == 1 << 7);
156 
157  uint64_t combined = (((uint64_t) colour.r) << 32) | (((uint64_t) colour.g) << 16) | ((uint64_t) colour.b);
158  combined *= brightness;
159 
160  uint16_t r = GB(combined, 39, 9);
161  uint16_t g = GB(combined, 23, 9);
162  uint16_t b = GB(combined, 7, 9);
163 
164  if ((combined & 0x800080008000L) == 0L) {
165  return Colour(r, g, b, colour.a);
166  }
167 
168  uint16_t ob = 0;
169  /* Sum overbright */
170  if (r > 255) ob += r - 255;
171  if (g > 255) ob += g - 255;
172  if (b > 255) ob += b - 255;
173 
174  /* Reduce overbright strength */
175  ob /= 2;
176  return Colour(
177  r >= 255 ? 255 : std::min(r + ob * (255 - r) / 256, 255),
178  g >= 255 ? 255 : std::min(g + ob * (255 - g) / 256, 255),
179  b >= 255 ? 255 : std::min(b + ob * (255 - b) / 256, 255),
180  colour.a);
181 }
182 
184 {
186 }
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::SetPixel
void SetPixel(void *video, int x, int y, uint8_t colour) override
Draw a pixel with a given colour on the video-buffer.
Definition: 32bpp_base.cpp:21
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:143
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:148
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
common.hpp
Blitter_32bppBase::DrawLine
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.
Definition: 32bpp_base.cpp:26
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
32bpp_base.hpp
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::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::LookupColourInPalette
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition: 32bpp_base.hpp:37
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:165
Blitter::PaletteAnimation
PaletteAnimation
Types of palette animation.
Definition: base.hpp:50
Blitter_32bppBase::DrawRect
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.
Definition: 32bpp_base.cpp:34
Blitter::PALETTE_ANIMATION_NONE
@ PALETTE_ANIMATION_NONE
No palette animation.
Definition: base.hpp:51
Blitter_32bppBase::UsePaletteAnimation
Blitter::PaletteAnimation UsePaletteAnimation() override
Check if the blitter uses palette animation at all.
Definition: 32bpp_base.cpp:183
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
Palette
Information about the currently used palette.
Definition: gfx_type.h:330
Colour::a
uint8_t a
colour channels in LE order
Definition: gfx_type.h:173