# 32bpp_anim.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../core/math_func.hpp"
    00014 #include "../video/video_driver.hpp"
    00015 #include "32bpp_anim.hpp"
    00016
    00017 #include "../table/sprites.h"
    00018
    00020 static FBlitter_32bppAnim iFBlitter_32bppAnim;
    00021
    00022 template <BlitterMode mode>
    00023 inline void Blitter_32bppAnim::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
    00024 {
    00025   const SpriteData *src = (const SpriteData *)bp->sprite;
    00026
    00027   const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]);
    00028   const uint8  *src_n  = (const uint8  *)(src->data + src->offset[zoom][1]);
    00029
    00030   for (uint i = bp->skip_top; i != 0; i--) {
    00031     src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
    00032     src_n += *(const uint32 *)src_n;
    00033   }
    00034
    00035   uint32 *dst = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left;
    00036   uint8 *anim = this->anim_buf + ((uint32 *)bp->dst - (uint32 *)_screen.dst_ptr) + bp->top * this->anim_buf_width + bp->left;
    00037
    00038   const byte *remap = bp->remap; // store so we don't have to access it via bp everytime
    00039
    00040   for (int y = 0; y < bp->height; y++) {
    00041     uint32 *dst_ln = dst + bp->pitch;
    00042     uint8 *anim_ln = anim + this->anim_buf_width;
    00043
    00044     const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px);
    00045     src_px++;
    00046
    00047     const uint8 *src_n_ln = src_n + *(uint32 *)src_n;
    00048     src_n += 4;
    00049
    00050     uint32 *dst_end = dst + bp->skip_left;
    00051
    00052     uint n;
    00053
    00054     while (dst < dst_end) {
    00055       n = *src_n++;
    00056
    00057       if (src_px->a == 0) {
    00058         dst += n;
    00059         src_px ++;
    00060         src_n++;
    00061
    00062         if (dst > dst_end) anim += dst - dst_end;
    00063       } else {
    00064         if (dst + n > dst_end) {
    00065           uint d = dst_end - dst;
    00066           src_px += d;
    00067           src_n += d;
    00068
    00069           dst = dst_end - bp->skip_left;
    00070           dst_end = dst + bp->width;
    00071
    00072           n = min<uint>(n - d, (uint)bp->width);
    00073           goto draw;
    00074         }
    00075         dst += n;
    00076         src_px += n;
    00077         src_n += n;
    00078       }
    00079     }
    00080
    00081     dst -= bp->skip_left;
    00082     dst_end -= bp->skip_left;
    00083
    00084     dst_end += bp->width;
    00085
    00086     while (dst < dst_end) {
    00087       n = min<uint>(*src_n++, (uint)(dst_end - dst));
    00088
    00089       if (src_px->a == 0) {
    00090         anim += n;
    00091         dst += n;
    00092         src_px++;
    00093         src_n++;
    00094         continue;
    00095       }
    00096
    00097       draw:;
    00098
    00099       switch (mode) {
    00100         case BM_COLOUR_REMAP:
    00101           if (src_px->a == 255) {
    00102             do {
    00103               uint m = *src_n;
    00104               /* In case the m-channel is zero, do not remap this pixel in any way */
    00105               if (m == 0) {
    00106                 *dst = src_px->data;
    00107                 *anim = 0;
    00108               } else {
    00109                 uint r = remap[m];
    00110                 *anim = r;
    00111                 if (r != 0) *dst = this->LookupColourInPalette(r);
    00112               }
    00113               anim++;
    00114               dst++;
    00115               src_px++;
    00116               src_n++;
    00117             } while (--n != 0);
    00118           } else {
    00119             do {
    00120               uint m = *src_n;
    00121               if (m == 0) {
    00122                 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
    00123                 *anim = 0;
    00124               } else {
    00125                 uint r = remap[m];
    00126                 *anim = r;
    00127                 if (r != 0) *dst = ComposeColourPANoCheck(this->LookupColourInPalette(r), src_px->a, *dst);
    00128               }
    00129               anim++;
    00130               dst++;
    00131               src_px++;
    00132               src_n++;
    00133             } while (--n != 0);
    00134           }
    00135           break;
    00136
    00137         case BM_TRANSPARENT:
    00138           /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
    00139            *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
    00140            *  we produce a result the newgrf maker didn't expect ;) */
    00141
    00142           /* Make the current colour a bit more black, so it looks like this image is transparent */
    00143           src_n += n;
    00144           if (src_px->a == 255) {
    00145             src_px += n;
    00146             do {
    00147               *dst = MakeTransparent(*dst, 3, 4);
    00148               *anim = remap[*anim];
    00149               anim++;
    00150               dst++;
    00151             } while (--n != 0);
    00152           } else {
    00153             do {
    00154               *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4);
    00155               *anim = remap[*anim];
    00156               anim++;
    00157               dst++;
    00158               src_px++;
    00159             } while (--n != 0);
    00160           }
    00161           break;
    00162
    00163         default:
    00164           if (src_px->a == 255) {
    00165             do {
    00166               /* Compiler assumes pointer aliasing, can't optimise this on its own */
    00167               uint m = *src_n++;
    00168               /* Above PALETTE_ANIM_START is palette animation */
    00169               *anim++ = m;
    00170               *dst++ = (m >= PALETTE_ANIM_START) ? this->LookupColourInPalette(m) : src_px->data;
    00171               src_px++;
    00172             } while (--n != 0);
    00173           } else {
    00174             do {
    00175               uint m = *src_n++;
    00176               *anim++ = m;
    00177               if (m >= PALETTE_ANIM_START) {
    00178                 *dst = ComposeColourPANoCheck(this->LookupColourInPalette(m), src_px->a, *dst);
    00179               } else {
    00180                 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
    00181               }
    00182               dst++;
    00183               src_px++;
    00184             } while (--n != 0);
    00185           }
    00186           break;
    00187       }
    00188     }
    00189
    00190     anim = anim_ln;
    00191     dst = dst_ln;
    00192     src_px = src_px_ln;
    00193     src_n  = src_n_ln;
    00194   }
    00195 }
    00196
    00197 void Blitter_32bppAnim::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
    00198 {
    00199   if (_screen_disable_anim) {
    00200     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent Draw() */
    00201     Blitter_32bppOptimized::Draw(bp, mode, zoom);
    00202     return;
    00203   }
    00204
    00205   switch (mode) {
    00206     default: NOT_REACHED();
    00207     case BM_NORMAL:       Draw<BM_NORMAL>      (bp, zoom); return;
    00208     case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
    00209     case BM_TRANSPARENT:  Draw<BM_TRANSPARENT> (bp, zoom); return;
    00210   }
    00211 }
    00212
    00213 void Blitter_32bppAnim::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
    00214 {
    00215   if (_screen_disable_anim) {
    00216     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawColourMappingRect() */
    00217     Blitter_32bppOptimized::DrawColourMappingRect(dst, width, height, pal);
    00218     return;
    00219   }
    00220
    00221   uint32 *udst = (uint32 *)dst;
    00222   uint8 *anim;
    00223
    00224   anim = this->anim_buf + ((uint32 *)dst - (uint32 *)_screen.dst_ptr);
    00225
    00226   if (pal == PALETTE_TO_TRANSPARENT) {
    00227     do {
    00228       for (int i = 0; i != width; i++) {
    00229         *udst = MakeTransparent(*udst, 154);
    00230         *anim = 0;
    00231         udst++;
    00232         anim++;
    00233       }
    00234       udst = udst - width + _screen.pitch;
    00235       anim = anim - width + this->anim_buf_width;
    00236     } while (--height);
    00237     return;
    00238   }
    00239   if (pal == PALETTE_NEWSPAPER) {
    00240     do {
    00241       for (int i = 0; i != width; i++) {
    00242         *udst = MakeGrey(*udst);
    00243         *anim = 0;
    00244         udst++;
    00245         anim++;
    00246       }
    00247       udst = udst - width + _screen.pitch;
    00248       anim = anim - width + this->anim_buf_width;
    00249     } while (--height);
    00250     return;
    00251   }
    00252
    00253   DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
    00254 }
    00255
    00256 void Blitter_32bppAnim::SetPixel(void *video, int x, int y, uint8 colour)
    00257 {
    00258   *((uint32 *)video + x + y * _screen.pitch) = LookupColourInPalette(colour);
    00259
    00260   /* Set the colour in the anim-buffer too, if we are rendering to the screen */
    00261   if (_screen_disable_anim) return;
    00262   this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = colour;
    00263 }
    00264
    00265 void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 colour)
    00266 {
    00267   if (_screen_disable_anim) {
    00268     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawRect() */
    00269     Blitter_32bppOptimized::DrawRect(video, width, height, colour);
    00270     return;
    00271   }
    00272
    00273   uint32 colour32 = LookupColourInPalette(colour);
    00274   uint8 *anim_line;
    00275
    00276   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
    00277
    00278   do {
    00279     uint32 *dst = (uint32 *)video;
    00280     uint8 *anim = anim_line;
    00281
    00282     for (int i = width; i > 0; i--) {
    00283       *dst = colour32;
    00284       /* Set the colour in the anim-buffer too */
    00285       *anim = colour;
    00286       dst++;
    00287       anim++;
    00288     }
    00289     video = (uint32 *)video + _screen.pitch;
    00290     anim_line += this->anim_buf_width;
    00291   } while (--height);
    00292 }
    00293
    00294 void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, int height)
    00295 {
    00296   assert(!_screen_disable_anim);
    00297   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
    00298   uint32 *dst = (uint32 *)video;
    00299   uint32 *usrc = (uint32 *)src;
    00300   uint8 *anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
    00301
    00302   for (; height > 0; height--) {
    00303     /* We need to keep those for palette animation. */
    00304     uint32 *dst_pal = dst;
    00305     uint8 *anim_pal = anim_line;
    00306
    00307     memcpy(dst, usrc, width * sizeof(uint32));
    00308     usrc += width;
    00309     dst += _screen.pitch;
    00310     /* Copy back the anim-buffer */
    00311     memcpy(anim_line, usrc, width * sizeof(uint8));
    00312     usrc = (uint32 *)((uint8 *)usrc + width);
    00313     anim_line += this->anim_buf_width;
    00314
    00315     /* Okay, it is *very* likely that the image we stored is using
    00316      * the wrong palette animated colours. There are two things we
    00317      * can do to fix this. The first is simply reviewing the whole
    00318      * screen after we copied the buffer, i.e. run PaletteAnimate,
    00319      * however that forces a full screen redraw which is expensive
    00320      * for just the cursor. This just copies the implementation of
    00321      * palette animation, much cheaper though slightly nastier. */
    00322     for (int i = 0; i < width; i++) {
    00323       uint colour = *anim_pal;
    00324       if (IsInsideBS(colour, PALETTE_ANIM_START, PALETTE_ANIM_SIZE)) {
    00325         /* Update this pixel */
    00326         *dst_pal = LookupColourInPalette(colour);
    00327       }
    00328       dst_pal++;
    00329       anim_pal++;
    00330     }
    00331   }
    00332 }
    00333
    00334 void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, int height)
    00335 {
    00336   assert(!_screen_disable_anim);
    00337   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
    00338   uint32 *udst = (uint32 *)dst;
    00339   uint32 *src = (uint32 *)video;
    00340   uint8 *anim_line;
    00341
    00342   if (this->anim_buf == NULL) return;
    00343
    00344   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
    00345
    00346   for (; height > 0; height--) {
    00347     memcpy(udst, src, width * sizeof(uint32));
    00348     src += _screen.pitch;
    00349     udst += width;
    00350     /* Copy the anim-buffer */
    00351     memcpy(udst, anim_line, width * sizeof(uint8));
    00352     udst = (uint32 *)((uint8 *)udst + width);
    00353     anim_line += this->anim_buf_width;
    00354   }
    00355 }
    00356
    00357 void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
    00358 {
    00359   assert(!_screen_disable_anim);
    00360   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
    00361   uint8 *dst, *src;
    00362
    00363   /* We need to scroll the anim-buffer too */
    00364   if (scroll_y > 0) {
    00365     dst = this->anim_buf + left + (top + height - 1) * this->anim_buf_width;
    00366     src = dst - scroll_y * this->anim_buf_width;
    00367
    00368     /* Adjust left & width */
    00369     if (scroll_x >= 0) {
    00370       dst += scroll_x;
    00371     } else {
    00372       src -= scroll_x;
    00373     }
    00374
    00375     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
    00376     uint th = height - scroll_y;
    00377     for (; th > 0; th--) {
    00378       memcpy(dst, src, tw * sizeof(uint8));
    00379       src -= this->anim_buf_width;
    00380       dst -= this->anim_buf_width;
    00381     }
    00382   } else {
    00383     /* Calculate pointers */
    00384     dst = this->anim_buf + left + top * this->anim_buf_width;
    00385     src = dst - scroll_y * this->anim_buf_width;
    00386
    00387     /* Adjust left & width */
    00388     if (scroll_x >= 0) {
    00389       dst += scroll_x;
    00390     } else {
    00391       src -= scroll_x;
    00392     }
    00393
    00394     /* the y-displacement may be 0 therefore we have to use memmove,
    00395      * because source and destination may overlap */
    00396     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
    00397     uint th = height + scroll_y;
    00398     for (; th > 0; th--) {
    00399       memmove(dst, src, tw * sizeof(uint8));
    00400       src += this->anim_buf_width;
    00401       dst += this->anim_buf_width;
    00402     }
    00403   }
    00404
    00405   Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y);
    00406 }
    00407
    00408 int Blitter_32bppAnim::BufferSize(int width, int height)
    00409 {
    00410   return width * height * (sizeof(uint32) + sizeof(uint8));
    00411 }
    00412
    00413 void Blitter_32bppAnim::PaletteAnimate(uint start, uint count)
    00414 {
    00415   assert(!_screen_disable_anim);
    00416
    00417   /* Never repaint the transparency pixel */
    00418   if (start == 0) {
    00419     start++;
    00420     count--;
    00421   }
    00422
    00423   const uint8 *anim = this->anim_buf;
    00424   uint32 *dst = (uint32 *)_screen.dst_ptr;
    00425
    00426   /* Let's walk the anim buffer and try to find the pixels */
    00427   for (int y = this->anim_buf_height; y != 0 ; y--) {
    00428     for (int x = this->anim_buf_width; x != 0 ; x--) {
    00429       uint colour = *anim;
    00430       if (IsInsideBS(colour, start, count)) {
    00431         /* Update this pixel */
    00432         *dst = LookupColourInPalette(colour);
    00433       }
    00434       dst++;
    00435       anim++;
    00436     }
    00437     dst += _screen.pitch - this->anim_buf_width;
    00438   }
    00439
    00440   /* Make sure the backend redraws the whole screen */
    00441   _video_driver->MakeDirty(0, 0, _screen.width, _screen.height);
    00442 }
    00443
    00444 Blitter::PaletteAnimation Blitter_32bppAnim::UsePaletteAnimation()
    00445 {
    00446   return Blitter::PALETTE_ANIMATION_BLITTER;
    00447 }
    00448
    00449 void Blitter_32bppAnim::PostResize()
    00450 {
    00451   if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height) {
    00452     /* The size of the screen changed; we can assume we can wipe all data from our buffer */
    00453     free(this->anim_buf);
    00454     this->anim_buf = CallocT<uint8>(_screen.width * _screen.height);
    00455     this->anim_buf_width = _screen.width;
    00456     this->anim_buf_height = _screen.height;
    00457   }
    00458 }