OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

32bpp_anim.cpp
Go to the documentation of this file.
1 /* $Id: 32bpp_anim.cpp 27796 2017-03-18 17:14:53Z frosch $ */
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 "../video/video_driver.hpp"
14 #include "32bpp_anim.hpp"
15
16 #include "../table/sprites.h"
17
18 #include "../safeguards.h"
19
21 static FBlitter_32bppAnim iFBlitter_32bppAnim;
22
23 Blitter_32bppAnim::~Blitter_32bppAnim()
24 {
25 free(this->anim_buf);
26 }
27
28 template <BlitterMode mode>
29 inline void Blitter_32bppAnim::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
30 {
31 const SpriteData *src = (const SpriteData *)bp->sprite;
32
33 const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]);
34 const uint16 *src_n = (const uint16 *)(src->data + src->offset[zoom][1]);
35
36 for (uint i = bp->skip_top; i != 0; i--) {
37 src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
38 src_n = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
39 }
40
41 Colour *dst = (Colour *)bp->dst + bp->top * bp->pitch + bp->left;
42 assert(_screen.pitch == this->anim_buf_pitch); // precondition for translating 'bp->dst' into an 'anim_buf' offset below.
43 uint16 *anim = this->anim_buf + ((uint32 *)bp->dst - (uint32 *)_screen.dst_ptr) + bp->top * this->anim_buf_pitch + bp->left;
44
45 const byte *remap = bp->remap; // store so we don't have to access it via bp everytime
46
47 for (int y = 0; y < bp->height; y++) {
48 Colour *dst_ln = dst + bp->pitch;
49 uint16 *anim_ln = anim + this->anim_buf_pitch;
50
51 const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
52 src_px++;
53
54 const uint16 *src_n_ln = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
55 src_n += 2;
56
57 Colour *dst_end = dst + bp->skip_left;
58
59 uint n;
60
61 while (dst < dst_end) {
62 n = *src_n++;
63
64 if (src_px->a == 0) {
65 dst += n;
66 src_px ++;
67 src_n++;
68
69 if (dst > dst_end) anim += dst - dst_end;
70 } else {
71 if (dst + n > dst_end) {
72 uint d = dst_end - dst;
73 src_px += d;
74 src_n += d;
75
76 dst = dst_end - bp->skip_left;
77 dst_end = dst + bp->width;
78
79 n = min<uint>(n - d, (uint)bp->width);
80 goto draw;
81 }
82 dst += n;
83 src_px += n;
84 src_n += n;
85 }
86 }
87
88 dst -= bp->skip_left;
89 dst_end -= bp->skip_left;
90
91 dst_end += bp->width;
92
93 while (dst < dst_end) {
94 n = min<uint>(*src_n++, (uint)(dst_end - dst));
95
96 if (src_px->a == 0) {
97 anim += n;
98 dst += n;
99 src_px++;
100 src_n++;
101 continue;
102 }
103
104 draw:;
105
106 switch (mode) {
107 case BM_COLOUR_REMAP:
108 if (src_px->a == 255) {
109 do {
110 uint m = *src_n;
111 /* In case the m-channel is zero, do not remap this pixel in any way */
112 if (m == 0) {
113 *dst = src_px->data;
114 *anim = 0;
115 } else {
116 uint r = remap[GB(m, 0, 8)];
117 *anim = r | (m & 0xFF00);
118 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
119 }
120 anim++;
121 dst++;
122 src_px++;
123 src_n++;
124 } while (--n != 0);
125 } else {
126 do {
127 uint m = *src_n;
128 if (m == 0) {
129 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
130 *anim = 0;
131 } else {
132 uint r = remap[GB(m, 0, 8)];
133 *anim = 0;
134 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
135 }
136 anim++;
137 dst++;
138 src_px++;
139 src_n++;
140 } while (--n != 0);
141 }
142 break;
143
144 case BM_CRASH_REMAP:
145 if (src_px->a == 255) {
146 do {
147 uint m = *src_n;
148 if (m == 0) {
149 uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
150 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
151 *anim = 0;
152 } else {
153 uint r = remap[GB(m, 0, 8)];
154 *anim = r | (m & 0xFF00);
155 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
156 }
157 anim++;
158 dst++;
159 src_px++;
160 src_n++;
161 } while (--n != 0);
162 } else {
163 do {
164 uint m = *src_n;
165 if (m == 0) {
166 if (src_px->a != 0) {
167 uint8 g = MakeDark(src_px->r, src_px->g, src_px->b);
168 *dst = ComposeColourRGBA(g, g, g, src_px->a, *dst);
169 *anim = 0;
170 }
171 } else {
172 uint r = remap[GB(m, 0, 8)];
173 *anim = 0;
174 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), src_px->a, *dst);
175 }
176 anim++;
177 dst++;
178 src_px++;
179 src_n++;
180 } while (--n != 0);
181 }
182 break;
183
184
185 case BM_BLACK_REMAP:
186 do {
187 *dst++ = Colour(0, 0, 0);
188 *anim++ = 0;
189 src_px++;
190 src_n++;
191 } while (--n != 0);
192 break;
193
194 case BM_TRANSPARENT:
195 /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
196 * This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
197 * we produce a result the newgrf maker didn't expect ;) */
198
199 /* Make the current colour a bit more black, so it looks like this image is transparent */
200 src_n += n;
201 if (src_px->a == 255) {
202 src_px += n;
203 do {
204 *dst = MakeTransparent(*dst, 3, 4);
205 *anim = 0;
206 anim++;
207 dst++;
208 } while (--n != 0);
209 } else {
210 do {
211 *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4);
212 *anim = 0;
213 anim++;
214 dst++;
215 src_px++;
216 } while (--n != 0);
217 }
218 break;
219
220 default:
221 if (src_px->a == 255) {
222 do {
223 /* Compiler assumes pointer aliasing, can't optimise this on its own */
224 uint m = GB(*src_n, 0, 8);
225 /* Above PALETTE_ANIM_START is palette animation */
226 *anim++ = *src_n;
227 *dst++ = (m >= PALETTE_ANIM_START) ? this->AdjustBrightness(this->LookupColourInPalette(m), GB(*src_n, 8, 8)) : src_px->data;
228 src_px++;
229 src_n++;
230 } while (--n != 0);
231 } else {
232 do {
233 uint m = GB(*src_n, 0, 8);
234 *anim++ = 0;
235 if (m >= PALETTE_ANIM_START) {
236 *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(m), GB(*src_n, 8, 8)), src_px->a, *dst);
237 } else {
238 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
239 }
240 dst++;
241 src_px++;
242 src_n++;
243 } while (--n != 0);
244 }
245 break;
246 }
247 }
248
249 anim = anim_ln;
250 dst = dst_ln;
251 src_px = src_px_ln;
252 src_n = src_n_ln;
253 }
254 }
255
256 void Blitter_32bppAnim::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
257 {
258 if (_screen_disable_anim) {
259 /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent Draw() */
260 Blitter_32bppOptimized::Draw(bp, mode, zoom);
261 return;
262 }
263
264 switch (mode) {
265 default: NOT_REACHED();
266 case BM_NORMAL: Draw<BM_NORMAL> (bp, zoom); return;
267 case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
268 case BM_TRANSPARENT: Draw<BM_TRANSPARENT> (bp, zoom); return;
269 case BM_CRASH_REMAP: Draw<BM_CRASH_REMAP> (bp, zoom); return;
270 case BM_BLACK_REMAP: Draw<BM_BLACK_REMAP> (bp, zoom); return;
271 }
272 }
273
274 void Blitter_32bppAnim::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
275 {
276 if (_screen_disable_anim) {
277 /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawColourMappingRect() */
278 Blitter_32bppOptimized::DrawColourMappingRect(dst, width, height, pal);
279 return;
280 }
281
282 Colour *udst = (Colour *)dst;
283 assert(_screen.pitch == this->anim_buf_pitch); // precondition for translating 'dst' into an 'anim_buf' offset below.
284 uint16 *anim = this->anim_buf + ((uint32 *)dst - (uint32 *)_screen.dst_ptr);
285
286 if (pal == PALETTE_TO_TRANSPARENT) {
287 do {
288 for (int i = 0; i != width; i++) {
289 *udst = MakeTransparent(*udst, 154);
290 *anim = 0;
291 udst++;
292 anim++;
293 }
294 udst = udst - width + _screen.pitch;
295 anim = anim - width + this->anim_buf_pitch;
296 } while (--height);
297 return;
298 }
299 if (pal == PALETTE_NEWSPAPER) {
300 do {
301 for (int i = 0; i != width; i++) {
302 *udst = MakeGrey(*udst);
303 *anim = 0;
304 udst++;
305 anim++;
306 }
307 udst = udst - width + _screen.pitch;
308 anim = anim - width + this->anim_buf_pitch;
309 } while (--height);
310 return;
311 }
312
313 DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
314 }
315
316 void Blitter_32bppAnim::SetPixel(void *video, int x, int y, uint8 colour)
317 {
318 *((Colour *)video + x + y * _screen.pitch) = LookupColourInPalette(colour);
319
320 /* Set the colour in the anim-buffer too, if we are rendering to the screen */
321 if (_screen_disable_anim) return;
322 assert(_screen.pitch == this->anim_buf_pitch); // precondition for translating 'video' into an 'anim_buf' offset below.
323 this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_pitch] = colour | (DEFAULT_BRIGHTNESS << 8);
324 }
325
326 void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 colour)
327 {
328 if (_screen_disable_anim) {
329 /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawRect() */
330 Blitter_32bppOptimized::DrawRect(video, width, height, colour);
331 return;
332 }
333
334 Colour colour32 = LookupColourInPalette(colour);
335 assert(_screen.pitch == this->anim_buf_pitch); // precondition for translating 'video' into an 'anim_buf' offset below.
336 uint16 *anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
337
338 do {
339 Colour *dst = (Colour *)video;
340 uint16 *anim = anim_line;
341
342 for (int i = width; i > 0; i--) {
343 *dst = colour32;
344 /* Set the colour in the anim-buffer too */
345 *anim = colour | (DEFAULT_BRIGHTNESS << 8);
346 dst++;
347 anim++;
348 }
349 video = (uint32 *)video + _screen.pitch;
350 anim_line += this->anim_buf_pitch;
351 } while (--height);
352 }
353
354 void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, int height)
355 {
356 assert(!_screen_disable_anim);
357 assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
358 Colour *dst = (Colour *)video;
359 const uint32 *usrc = (const uint32 *)src;
360 assert(_screen.pitch == this->anim_buf_pitch); // precondition for translating 'video' into an 'anim_buf' offset below.
361 uint16 *anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
362
363 for (; height > 0; height--) {
364 /* We need to keep those for palette animation. */
365 Colour *dst_pal = dst;
366 uint16 *anim_pal = anim_line;
367
368 memcpy(dst, usrc, width * sizeof(uint32));
369 usrc += width;
370 dst += _screen.pitch;
371 /* Copy back the anim-buffer */
372 memcpy(anim_line, usrc, width * sizeof(uint16));
373 usrc = (const uint32 *)((const uint16 *)usrc + width);
374 anim_line += this->anim_buf_pitch;
375
376 /* Okay, it is *very* likely that the image we stored is using
377 * the wrong palette animated colours. There are two things we
378 * can do to fix this. The first is simply reviewing the whole
379 * screen after we copied the buffer, i.e. run PaletteAnimate,
380 * however that forces a full screen redraw which is expensive
381 * for just the cursor. This just copies the implementation of
382 * palette animation, much cheaper though slightly nastier. */
383 for (int i = 0; i < width; i++) {
384 uint colour = GB(*anim_pal, 0, 8);
385 if (colour >= PALETTE_ANIM_START) {
386 /* Update this pixel */
387 *dst_pal = this->AdjustBrightness(LookupColourInPalette(colour), GB(*anim_pal, 8, 8));
388 }
389 dst_pal++;
390 anim_pal++;
391 }
392 }
393 }
394
395 void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, int height)
396 {
397 assert(!_screen_disable_anim);
398 assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
399 uint32 *udst = (uint32 *)dst;
400 const uint32 *src = (const uint32 *)video;
401
402 if (this->anim_buf == NULL) return;
403
404 assert(_screen.pitch == this->anim_buf_pitch); // precondition for translating 'video' into an 'anim_buf' offset below.
405 const uint16 *anim_line = ((const uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
406
407 for (; height > 0; height--) {
408 memcpy(udst, src, width * sizeof(uint32));
409 src += _screen.pitch;
410 udst += width;
411 /* Copy the anim-buffer */
412 memcpy(udst, anim_line, width * sizeof(uint16));
413 udst = (uint32 *)((uint16 *)udst + width);
414 anim_line += this->anim_buf_pitch;
415 }
416 }
417
418 void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
419 {
420 assert(!_screen_disable_anim);
421 assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
422 uint16 *dst, *src;
423
424 /* We need to scroll the anim-buffer too */
425 if (scroll_y > 0) {
426 dst = this->anim_buf + left + (top + height - 1) * this->anim_buf_pitch;
427 src = dst - scroll_y * this->anim_buf_pitch;
428
429 /* Adjust left & width */
430 if (scroll_x >= 0) {
431 dst += scroll_x;
432 } else {
433 src -= scroll_x;
434 }
435
436 uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
437 uint th = height - scroll_y;
438 for (; th > 0; th--) {
439 memcpy(dst, src, tw * sizeof(uint16));
440 src -= this->anim_buf_pitch;
441 dst -= this->anim_buf_pitch;
442 }
443 } else {
444 /* Calculate pointers */
445 dst = this->anim_buf + left + top * this->anim_buf_pitch;
446 src = dst - scroll_y * this->anim_buf_pitch;
447
448 /* Adjust left & width */
449 if (scroll_x >= 0) {
450 dst += scroll_x;
451 } else {
452 src -= scroll_x;
453 }
454
455 /* the y-displacement may be 0 therefore we have to use memmove,
456 * because source and destination may overlap */
457 uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
458 uint th = height + scroll_y;
459 for (; th > 0; th--) {
460 memmove(dst, src, tw * sizeof(uint16));
461 src += this->anim_buf_pitch;
462 dst += this->anim_buf_pitch;
463 }
464 }
465
466 Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y);
467 }
468
469 int Blitter_32bppAnim::BufferSize(int width, int height)
470 {
471 return width * height * (sizeof(uint32) + sizeof(uint16));
472 }
473
474 void Blitter_32bppAnim::PaletteAnimate(const Palette &palette)
475 {
476 assert(!_screen_disable_anim);
477
478 this->palette = palette;
479 /* If first_dirty is 0, it is for 8bpp indication to send the new
480 * palette. However, only the animation colours might possibly change.
481 * Especially when going between toyland and non-toyland. */
482 assert(this->palette.first_dirty == PALETTE_ANIM_START || this->palette.first_dirty == 0);
483
484 const uint16 *anim = this->anim_buf;
485 Colour *dst = (Colour *)_screen.dst_ptr;
486
487 /* Let's walk the anim buffer and try to find the pixels */
488 for (int y = this->anim_buf_height; y != 0 ; y--) {
489 for (int x = this->anim_buf_width; x != 0 ; x--) {
490 uint colour = GB(*anim, 0, 8);
491 if (colour >= PALETTE_ANIM_START) {
492 /* Update this pixel */
493 *dst = this->AdjustBrightness(LookupColourInPalette(colour), GB(*anim, 8, 8));
494 }
495 dst++;
496 anim++;
497 }
498 dst += _screen.pitch - this->anim_buf_width;
499 anim += this->anim_buf_pitch - this->anim_buf_width;
500 }
501
502 /* Make sure the backend redraws the whole screen */
503 VideoDriver::GetInstance()->MakeDirty(0, 0, _screen.width, _screen.height);
504 }
505
506 Blitter::PaletteAnimation Blitter_32bppAnim::UsePaletteAnimation()
507 {
508 return Blitter::PALETTE_ANIMATION_BLITTER;
509 }
510
511 void Blitter_32bppAnim::PostResize()
512 {
513 if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height ||
514 _screen.pitch != this->anim_buf_pitch) {
515 /* The size of the screen changed; we can assume we can wipe all data from our buffer */
516 free(this->anim_buf);
517 this->anim_buf_width = _screen.width;
518 this->anim_buf_height = _screen.height;
519 this->anim_buf_pitch = _screen.pitch;
520 this->anim_buf = CallocT<uint16>(this->anim_buf_height * this->anim_buf_pitch);
521 }
522 }