# 32bpp_anim.cpp

Go to the documentation of this file.

    00001 /* $Id: 32bpp_anim.cpp 17693 2009-10-04 17:16:41Z rubidium $ */
    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 "../gfx_func.h"
    00015 #include "../video/video_driver.hpp"
    00016 #include "32bpp_anim.hpp"
    00017
    00018 #include "../table/sprites.h"
    00019
    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 217 (PALETTE_ANIM_SIZE_START) is palette animation */
    00169               *anim++ = m;
    00170               *dst++ = (m >= PALETTE_ANIM_SIZE_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_SIZE_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   if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height) {
    00206     /* The size of the screen changed; we can assume we can wipe all data from our buffer */
    00207     free(this->anim_buf);
    00208     this->anim_buf = CallocT<uint8>(_screen.width * _screen.height);
    00209     this->anim_buf_width = _screen.width;
    00210     this->anim_buf_height = _screen.height;
    00211   }
    00212
    00213   switch (mode) {
    00214     default: NOT_REACHED();
    00215     case BM_NORMAL:       Draw<BM_NORMAL>      (bp, zoom); return;
    00216     case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
    00217     case BM_TRANSPARENT:  Draw<BM_TRANSPARENT> (bp, zoom); return;
    00218   }
    00219 }
    00220
    00221 void Blitter_32bppAnim::DrawColourMappingRect(void *dst, int width, int height, int pal)
    00222 {
    00223   if (_screen_disable_anim) {
    00224     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawColourMappingRect() */
    00225     Blitter_32bppOptimized::DrawColourMappingRect(dst, width, height, pal);
    00226     return;
    00227   }
    00228
    00229   uint32 *udst = (uint32 *)dst;
    00230   uint8 *anim;
    00231
    00232   anim = this->anim_buf + ((uint32 *)dst - (uint32 *)_screen.dst_ptr);
    00233
    00234   if (pal == PALETTE_TO_TRANSPARENT) {
    00235     do {
    00236       for (int i = 0; i != width; i++) {
    00237         *udst = MakeTransparent(*udst, 154);
    00238         *anim = 0;
    00239         udst++;
    00240         anim++;
    00241       }
    00242       udst = udst - width + _screen.pitch;
    00243       anim = anim - width + this->anim_buf_width;
    00244     } while (--height);
    00245     return;
    00246   }
    00247   if (pal == PALETTE_TO_STRUCT_GREY) {
    00248     do {
    00249       for (int i = 0; i != width; i++) {
    00250         *udst = MakeGrey(*udst);
    00251         *anim = 0;
    00252         udst++;
    00253         anim++;
    00254       }
    00255       udst = udst - width + _screen.pitch;
    00256       anim = anim - width + this->anim_buf_width;
    00257     } while (--height);
    00258     return;
    00259   }
    00260
    00261   DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
    00262 }
    00263
    00264 void Blitter_32bppAnim::SetPixel(void *video, int x, int y, uint8 colour)
    00265 {
    00266   *((uint32 *)video + x + y * _screen.pitch) = LookupColourInPalette(colour);
    00267
    00268   /* Set the colour in the anim-buffer too, if we are rendering to the screen */
    00269   if (_screen_disable_anim) return;
    00270   this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = colour;
    00271 }
    00272
    00273 void Blitter_32bppAnim::SetPixelIfEmpty(void *video, int x, int y, uint8 colour)
    00274 {
    00275   uint32 *dst = (uint32 *)video + x + y * _screen.pitch;
    00276   if (*dst == 0) {
    00277     *dst = LookupColourInPalette(colour);
    00278     /* Set the colour in the anim-buffer too, if we are rendering to the screen */
    00279     if (_screen_disable_anim) return;
    00280     this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = colour;
    00281   }
    00282 }
    00283
    00284 void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 colour)
    00285 {
    00286   if (_screen_disable_anim) {
    00287     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawRect() */
    00288     Blitter_32bppOptimized::DrawRect(video, width, height, colour);
    00289     return;
    00290   }
    00291
    00292   uint32 colour32 = LookupColourInPalette(colour);
    00293   uint8 *anim_line;
    00294
    00295   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
    00296
    00297   do {
    00298     uint32 *dst = (uint32 *)video;
    00299     uint8 *anim = anim_line;
    00300
    00301     for (int i = width; i > 0; i--) {
    00302       *dst = colour32;
    00303       /* Set the colour in the anim-buffer too */
    00304       *anim = colour;
    00305       dst++;
    00306       anim++;
    00307     }
    00308     video = (uint32 *)video + _screen.pitch;
    00309     anim_line += this->anim_buf_width;
    00310   } while (--height);
    00311 }
    00312
    00313 void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, int height)
    00314 {
    00315   assert(!_screen_disable_anim);
    00316   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
    00317   uint32 *dst = (uint32 *)video;
    00318   uint32 *usrc = (uint32 *)src;
    00319   uint8 *anim_line;
    00320
    00321   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
    00322
    00323   for (; height > 0; height--) {
    00324     memcpy(dst, usrc, width * sizeof(uint32));
    00325     usrc += width;
    00326     dst += _screen.pitch;
    00327     /* Copy back the anim-buffer */
    00328     memcpy(anim_line, usrc, width * sizeof(uint8));
    00329     usrc = (uint32 *)((uint8 *)usrc + width);
    00330     anim_line += this->anim_buf_width;
    00331   }
    00332
    00333   /* We update the palette (or the pixels that do animation) immediatly, to avoid graphical glitches */
    00334   this->PaletteAnimate(PALETTE_ANIM_SIZE_START, (_use_palette == PAL_DOS) ? PALETTE_ANIM_SIZE_DOS : PALETTE_ANIM_SIZE_WIN);
    00335 }
    00336
    00337 void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, int height)
    00338 {
    00339   assert(!_screen_disable_anim);
    00340   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
    00341   uint32 *udst = (uint32 *)dst;
    00342   uint32 *src = (uint32 *)video;
    00343   uint8 *anim_line;
    00344
    00345   if (this->anim_buf == NULL) return;
    00346
    00347   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
    00348
    00349   for (; height > 0; height--) {
    00350     memcpy(udst, src, width * sizeof(uint32));
    00351     src += _screen.pitch;
    00352     udst += width;
    00353     /* Copy the anim-buffer */
    00354     memcpy(udst, anim_line, width * sizeof(uint8));
    00355     udst = (uint32 *)((uint8 *)udst + width);
    00356     anim_line += this->anim_buf_width;
    00357   }
    00358 }
    00359
    00360 void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
    00361 {
    00362   assert(!_screen_disable_anim);
    00363   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
    00364   uint8 *dst, *src;
    00365
    00366   /* We need to scroll the anim-buffer too */
    00367   if (scroll_y > 0) {
    00368     dst = this->anim_buf + left + (top + height - 1) * this->anim_buf_width;
    00369     src = dst - scroll_y * this->anim_buf_width;
    00370
    00371     /* Adjust left & width */
    00372     if (scroll_x >= 0) {
    00373       dst += scroll_x;
    00374     } else {
    00375       src -= scroll_x;
    00376     }
    00377
    00378     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
    00379     uint th = height - scroll_y;
    00380     for (; th > 0; th--) {
    00381       memcpy(dst, src, tw * sizeof(uint8));
    00382       src -= this->anim_buf_width;
    00383       dst -= this->anim_buf_width;
    00384     }
    00385   } else {
    00386     /* Calculate pointers */
    00387     dst = this->anim_buf + left + top * this->anim_buf_width;
    00388     src = dst - scroll_y * this->anim_buf_width;
    00389
    00390     /* Adjust left & width */
    00391     if (scroll_x >= 0) {
    00392       dst += scroll_x;
    00393     } else {
    00394       src -= scroll_x;
    00395     }
    00396
    00397     /* the y-displacement may be 0 therefore we have to use memmove,
    00398      * because source and destination may overlap */
    00399     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
    00400     uint th = height + scroll_y;
    00401     for (; th > 0; th--) {
    00402       memmove(dst, src, tw * sizeof(uint8));
    00403       src += this->anim_buf_width;
    00404       dst += this->anim_buf_width;
    00405     }
    00406   }
    00407
    00408   Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y);
    00409 }
    00410
    00411 int Blitter_32bppAnim::BufferSize(int width, int height)
    00412 {
    00413   return width * height * (sizeof(uint32) + sizeof(uint8));
    00414 }
    00415
    00416 void Blitter_32bppAnim::PaletteAnimate(uint start, uint count)
    00417 {
    00418   assert(!_screen_disable_anim);
    00419
    00420   /* Never repaint the transparency pixel */
    00421   if (start == 0) {
    00422     start++;
    00423     count--;
    00424   }
    00425
    00426   const uint8 *anim = this->anim_buf;
    00427   uint32 *dst = (uint32 *)_screen.dst_ptr;
    00428
    00429   /* Let's walk the anim buffer and try to find the pixels */
    00430   for (int y = this->anim_buf_height; y != 0 ; y--) {
    00431     for (int x = this->anim_buf_width; x != 0 ; x--) {
    00432       uint colour = *anim;
    00433       if (IsInsideBS(colour, start, count)) {
    00434         /* Update this pixel */
    00435         *dst = LookupColourInPalette(colour);
    00436       }
    00437       dst++;
    00438       anim++;
    00439     }
    00440     dst += _screen.pitch - this->anim_buf_width;
    00441   }
    00442
    00443   /* Make sure the backend redraws the whole screen */
    00444   _video_driver->MakeDirty(0, 0, _screen.width, _screen.height);
    00445 }
    00446
    00447 Blitter::PaletteAnimation Blitter_32bppAnim::UsePaletteAnimation()
    00448 {
    00449   return Blitter::PALETTE_ANIMATION_BLITTER;
    00450 }