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