32bpp_anim.cpp

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

Generated on Sat Jun 5 21:52:03 2010 for OpenTTD by  doxygen 1.6.1