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