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/>.
10 /** @file 32bpp_base.cpp Implementation of base for 32 bpp blitters. */
12 #include "../stdafx.h"
13 #include "32bpp_base.hpp"
15 #include "../safeguards.h"
17 void *Blitter_32bppBase::MoveTo(void *video
, int x
, int y
)
19 return (uint32
*)video
+ x
+ y
* _screen
.pitch
;
22 void Blitter_32bppBase::SetPixel(void *video
, int x
, int y
, uint8 colour
)
24 *((Colour
*)video
+ x
+ y
* _screen
.pitch
) = LookupColourInPalette(colour
);
27 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 const Colour c
= LookupColourInPalette(colour
);
30 this->DrawLineGeneric(x
, y
, x2
, y2
, screen_width
, screen_height
, width
, dash
, [=](int x
, int y
) {
31 *((Colour
*)video
+ x
+ y
* _screen
.pitch
) = c
;
35 void Blitter_32bppBase::SetLine(void *video
, int x
, int y
, uint8
*colours
, uint width
)
37 Colour
*dst
= (Colour
*)video
+ x
+ y
* _screen
.pitch
;
39 *dst
= LookupColourInPalette(*colours
);
45 void Blitter_32bppBase::SetLine32(void *video
, int x
, int y
, uint32
*colours
, uint width
)
47 Colour
*dst
= (Colour
*)video
+ x
+ y
* _screen
.pitch
;
55 void Blitter_32bppBase::DrawRect(void *video
, int width
, int height
, uint8 colour
)
57 Colour colour32
= LookupColourInPalette(colour
);
60 Colour
*dst
= (Colour
*)video
;
61 for (int i
= width
; i
> 0; i
--) {
65 video
= (uint32
*)video
+ _screen
.pitch
;
69 void Blitter_32bppBase::CopyFromBuffer(void *video
, const void *src
, int width
, int height
)
71 uint32
*dst
= (uint32
*)video
;
72 const uint32
*usrc
= (const uint32
*)src
;
74 for (; height
> 0; height
--) {
75 memcpy(dst
, usrc
, width
* sizeof(uint32
));
81 void Blitter_32bppBase::CopyToBuffer(const void *video
, void *dst
, int width
, int height
)
83 uint32
*udst
= (uint32
*)dst
;
84 const uint32
*src
= (const uint32
*)video
;
86 for (; height
> 0; height
--) {
87 memcpy(udst
, src
, width
* sizeof(uint32
));
93 void Blitter_32bppBase::CopyImageToBuffer(const void *video
, void *dst
, int width
, int height
, int dst_pitch
)
95 uint32
*udst
= (uint32
*)dst
;
96 const uint32
*src
= (const uint32
*)video
;
98 for (; height
> 0; height
--) {
99 memcpy(udst
, src
, width
* sizeof(uint32
));
100 src
+= _screen
.pitch
;
105 void Blitter_32bppBase::ScrollBuffer(void *video
, int &left
, int &top
, int &width
, int &height
, int scroll_x
, int scroll_y
)
111 /* Calculate pointers */
112 dst
= (uint32
*)video
+ left
+ (top
+ height
- 1) * _screen
.pitch
;
113 src
= dst
- scroll_y
* _screen
.pitch
;
115 /* Decrease height and increase top */
120 /* Adjust left & width */
130 for (int h
= height
; h
> 0; h
--) {
131 memcpy(dst
, src
, width
* sizeof(uint32
));
132 src
-= _screen
.pitch
;
133 dst
-= _screen
.pitch
;
136 /* Calculate pointers */
137 dst
= (uint32
*)video
+ left
+ top
* _screen
.pitch
;
138 src
= dst
- scroll_y
* _screen
.pitch
;
140 /* Decrease height. (scroll_y is <=0). */
144 /* Adjust left & width */
154 /* the y-displacement may be 0 therefore we have to use memmove,
155 * because source and destination may overlap */
156 for (int h
= height
; h
> 0; h
--) {
157 memmove(dst
, src
, width
* sizeof(uint32
));
158 src
+= _screen
.pitch
;
159 dst
+= _screen
.pitch
;
164 int Blitter_32bppBase::BufferSize(int width
, int height
)
166 return width
* height
* sizeof(uint32
);
169 void Blitter_32bppBase::PaletteAnimate(const Palette
&palette
)
171 /* By default, 32bpp doesn't have palette animation */
174 Colour
Blitter_32bppBase::ReallyAdjustBrightness(Colour colour
, uint8 brightness
)
176 assert(DEFAULT_BRIGHTNESS
== 1 << 7);
178 uint64 combined
= (((uint64
)colour
.r
) << 32) | (((uint64
)colour
.g
) << 16) | ((uint64
)colour
.b
);
179 combined
*= brightness
;
181 uint16 r
= GB(combined
, 39, 9);
182 uint16 g
= GB(combined
, 23, 9);
183 uint16 b
= GB(combined
, 7, 9);
185 if ((combined
& 0x800080008000L
) == 0L) {
186 return Colour(r
, g
, b
, colour
.a
);
191 if (r
> 255) ob
+= r
- 255;
192 if (g
> 255) ob
+= g
- 255;
193 if (b
> 255) ob
+= b
- 255;
195 /* Reduce overbright strength */
198 r
>= 255 ? 255 : min(r
+ ob
* (255 - r
) / 256, 255),
199 g
>= 255 ? 255 : min(g
+ ob
* (255 - g
) / 256, 255),
200 b
>= 255 ? 255 : min(b
+ ob
* (255 - b
) / 256, 255),
204 Blitter::PaletteAnimation
Blitter_32bppBase::UsePaletteAnimation()
206 return Blitter::PALETTE_ANIMATION_NONE
;