Update readme.md
[openttd-joker.git] / src / blitter / 32bpp_base.cpp
bloba0dc6ff814045f6cdfec1030099537915f338663
1 /* $Id$ */
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 */
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;
32 });
35 void Blitter_32bppBase::SetLine(void *video, int x, int y, uint8 *colours, uint width)
37 Colour *dst = (Colour *)video + x + y * _screen.pitch;
38 do {
39 *dst = LookupColourInPalette(*colours);
40 dst++;
41 colours++;
42 } while (--width);
45 void Blitter_32bppBase::SetLine32(void *video, int x, int y, uint32 *colours, uint width)
47 Colour *dst = (Colour *)video + x + y * _screen.pitch;
48 do {
49 *dst = *colours;
50 dst++;
51 colours++;
52 } while (--width);
55 void Blitter_32bppBase::DrawRect(void *video, int width, int height, uint8 colour)
57 Colour colour32 = LookupColourInPalette(colour);
59 do {
60 Colour *dst = (Colour *)video;
61 for (int i = width; i > 0; i--) {
62 *dst = colour32;
63 dst++;
65 video = (uint32 *)video + _screen.pitch;
66 } while (--height);
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));
76 usrc += width;
77 dst += _screen.pitch;
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));
88 src += _screen.pitch;
89 udst += width;
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;
101 udst += dst_pitch;
105 void Blitter_32bppBase::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
107 const uint32 *src;
108 uint32 *dst;
110 if (scroll_y > 0) {
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 */
116 top += scroll_y;
117 height -= scroll_y;
118 assert(height > 0);
120 /* Adjust left & width */
121 if (scroll_x >= 0) {
122 dst += scroll_x;
123 left += scroll_x;
124 width -= scroll_x;
125 } else {
126 src -= scroll_x;
127 width += scroll_x;
130 for (int h = height; h > 0; h--) {
131 memcpy(dst, src, width * sizeof(uint32));
132 src -= _screen.pitch;
133 dst -= _screen.pitch;
135 } else {
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). */
141 height += scroll_y;
142 assert(height > 0);
144 /* Adjust left & width */
145 if (scroll_x >= 0) {
146 dst += scroll_x;
147 left += scroll_x;
148 width -= scroll_x;
149 } else {
150 src -= scroll_x;
151 width += scroll_x;
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);
189 uint16 ob = 0;
190 /* Sum overbright */
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 */
196 ob /= 2;
197 return Colour(
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),
201 colour.a);
204 Blitter::PaletteAnimation Blitter_32bppBase::UsePaletteAnimation()
206 return Blitter::PALETTE_ANIMATION_NONE;