OpenTTD
32bpp_base.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../stdafx.h"
13 #include "32bpp_base.hpp"
14 #include "common.hpp"
15 
16 #include "../safeguards.h"
17 
18 void *Blitter_32bppBase::MoveTo(void *video, int x, int y)
19 {
20  return (uint32 *)video + x + y * _screen.pitch;
21 }
22 
23 void Blitter_32bppBase::SetPixel(void *video, int x, int y, uint8 colour)
24 {
25  *((Colour *)video + x + y * _screen.pitch) = LookupColourInPalette(colour);
26 }
27 
28 void Blitter_32bppBase::DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash)
29 {
30  const Colour c = LookupColourInPalette(colour);
31  this->DrawLineGeneric(x, y, x2, y2, screen_width, screen_height, width, dash, [=](int x, int y) {
32  *((Colour *)video + x + y * _screen.pitch) = c;
33  });
34 }
35 
36 void Blitter_32bppBase::DrawRect(void *video, int width, int height, uint8 colour)
37 {
38  Colour colour32 = LookupColourInPalette(colour);
39 
40  do {
41  Colour *dst = (Colour *)video;
42  for (int i = width; i > 0; i--) {
43  *dst = colour32;
44  dst++;
45  }
46  video = (uint32 *)video + _screen.pitch;
47  } while (--height);
48 }
49 
50 void Blitter_32bppBase::CopyFromBuffer(void *video, const void *src, int width, int height)
51 {
52  uint32 *dst = (uint32 *)video;
53  const uint32 *usrc = (const uint32 *)src;
54 
55  for (; height > 0; height--) {
56  memcpy(dst, usrc, width * sizeof(uint32));
57  usrc += width;
58  dst += _screen.pitch;
59  }
60 }
61 
62 void Blitter_32bppBase::CopyToBuffer(const void *video, void *dst, int width, int height)
63 {
64  uint32 *udst = (uint32 *)dst;
65  const uint32 *src = (const uint32 *)video;
66 
67  for (; height > 0; height--) {
68  memcpy(udst, src, width * sizeof(uint32));
69  src += _screen.pitch;
70  udst += width;
71  }
72 }
73 
74 void Blitter_32bppBase::CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
75 {
76  uint32 *udst = (uint32 *)dst;
77  const uint32 *src = (const uint32 *)video;
78 
79  for (; height > 0; height--) {
80  memcpy(udst, src, width * sizeof(uint32));
81  src += _screen.pitch;
82  udst += dst_pitch;
83  }
84 }
85 
86 void Blitter_32bppBase::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
87 {
88  const uint32 *src;
89  uint32 *dst;
90 
91  if (scroll_y > 0) {
92  /* Calculate pointers */
93  dst = (uint32 *)video + left + (top + height - 1) * _screen.pitch;
94  src = dst - scroll_y * _screen.pitch;
95 
96  /* Decrease height and increase top */
97  top += scroll_y;
98  height -= scroll_y;
99  assert(height > 0);
100 
101  /* Adjust left & width */
102  if (scroll_x >= 0) {
103  dst += scroll_x;
104  left += scroll_x;
105  width -= scroll_x;
106  } else {
107  src -= scroll_x;
108  width += scroll_x;
109  }
110 
111  for (int h = height; h > 0; h--) {
112  memcpy(dst, src, width * sizeof(uint32));
113  src -= _screen.pitch;
114  dst -= _screen.pitch;
115  }
116  } else {
117  /* Calculate pointers */
118  dst = (uint32 *)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  /* the y-displacement may be 0 therefore we have to use memmove,
136  * because source and destination may overlap */
137  for (int h = height; h > 0; h--) {
138  memmove(dst, src, width * sizeof(uint32));
139  src += _screen.pitch;
140  dst += _screen.pitch;
141  }
142  }
143 }
144 
145 int Blitter_32bppBase::BufferSize(int width, int height)
146 {
147  return width * height * sizeof(uint32);
148 }
149 
151 {
152  /* By default, 32bpp doesn't have palette animation */
153 }
154 
155 Colour Blitter_32bppBase::ReallyAdjustBrightness(Colour colour, uint8 brightness)
156 {
157  assert(DEFAULT_BRIGHTNESS == 1 << 7);
158 
159  uint64 combined = (((uint64) colour.r) << 32) | (((uint64) colour.g) << 16) | ((uint64) colour.b);
160  combined *= brightness;
161 
162  uint16 r = GB(combined, 39, 9);
163  uint16 g = GB(combined, 23, 9);
164  uint16 b = GB(combined, 7, 9);
165 
166  if ((combined & 0x800080008000L) == 0L) {
167  return Colour(r, g, b, colour.a);
168  }
169 
170  uint16 ob = 0;
171  /* Sum overbright */
172  if (r > 255) ob += r - 255;
173  if (g > 255) ob += g - 255;
174  if (b > 255) ob += b - 255;
175 
176  /* Reduce overbright strength */
177  ob /= 2;
178  return Colour(
179  r >= 255 ? 255 : min(r + ob * (255 - r) / 256, 255),
180  g >= 255 ? 255 : min(g + ob * (255 - g) / 256, 255),
181  b >= 255 ? 255 : min(b + ob * (255 - b) / 256, 255),
182  colour.a);
183 }
184 
186 {
188 }
Blitter::PaletteAnimation UsePaletteAnimation()
Check if the blitter uses palette animation at all.
Definition: 32bpp_base.cpp:185
void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
Copy from the screen to a buffer in a palette format for 8bpp and RGBA format for 32bpp...
Definition: 32bpp_base.cpp:74
Information about the currently used palette.
Definition: gfx_type.h:309
void * MoveTo(void *video, int x, int y)
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
Definition: 32bpp_base.cpp:18
uint8 a
colour channels in LE order
Definition: gfx_type.h:170
int BufferSize(int width, int height)
Calculate how much memory there is needed for an image of this size in the video-buffer.
Definition: 32bpp_base.cpp:145
static Colour LookupColourInPalette(uint index)
Look up the colour in the current palette.
Definition: 32bpp_base.hpp:40
No palette animation.
Definition: base.hpp:52
void DrawRect(void *video, int width, int height, uint8 colour)
Make a single horizontal line in a single colour on the video-buffer.
Definition: 32bpp_base.cpp:36
void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
Scroll the videobuffer some &#39;x&#39; and &#39;y&#39; value.
Definition: 32bpp_base.cpp:86
Base for all 32 bits blitters.
Common functionality for all blitter implementations.
void SetPixel(void *video, int x, int y, uint8 colour)
Draw a pixel with a given colour on the video-buffer.
Definition: 32bpp_base.cpp:23
void CopyFromBuffer(void *video, const void *src, int width, int height)
Copy from a buffer to the screen.
Definition: 32bpp_base.cpp:50
void DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 colour, int width, int dash)
Draw a line with a given colour.
Definition: 32bpp_base.cpp:28
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
void CopyToBuffer(const void *video, void *dst, int width, int height)
Copy from the screen to a buffer.
Definition: 32bpp_base.cpp:62
void PaletteAnimate(const Palette &palette)
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
Definition: 32bpp_base.cpp:150
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:164
PaletteAnimation
Types of palette animation.
Definition: base.hpp:51