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 uint16 *src_n  = (const uint16 *)(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 uint16 *)((const byte *)src_n  + *(const uint32 *)src_n);
00033   }
00034 
00035   uint32 *dst = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left;
00036   uint16 *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     uint16 *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 uint16 *src_n_ln = (const uint16 *)((const byte *)src_n + *(const uint32 *)src_n);
00048     src_n += 2;
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[GB(m, 0, 8)];
00110                 *anim = r | (m & 0xFF00);
00111                 if (r != 0) *dst = this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8));
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[GB(m, 0, 8)];
00126                 *anim = 0;
00127                 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), GB(m, 8, 8)), 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 = 0;
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 = 0;
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 = GB(*src_n, 0, 8);
00168               /* Above PALETTE_ANIM_START is palette animation */
00169               *anim++ = *src_n;
00170               *dst++ = (m >= PALETTE_ANIM_START) ? this->AdjustBrightness(this->LookupColourInPalette(m), GB(*src_n, 8, 8)) : src_px->data;
00171               src_px++;
00172               src_n++;
00173             } while (--n != 0);
00174           } else {
00175             do {
00176               uint m = GB(*src_n, 0, 8);
00177               *anim++ = 0;
00178               if (m >= PALETTE_ANIM_START) {
00179                 *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(m), GB(*src_n, 8, 8)), src_px->a, *dst);
00180               } else {
00181                 *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst);
00182               }
00183               dst++;
00184               src_px++;
00185               src_n++;
00186             } while (--n != 0);
00187           }
00188           break;
00189       }
00190     }
00191 
00192     anim = anim_ln;
00193     dst = dst_ln;
00194     src_px = src_px_ln;
00195     src_n  = src_n_ln;
00196   }
00197 }
00198 
00199 void Blitter_32bppAnim::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
00200 {
00201   if (_screen_disable_anim) {
00202     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent Draw() */
00203     Blitter_32bppOptimized::Draw(bp, mode, zoom);
00204     return;
00205   }
00206 
00207   switch (mode) {
00208     default: NOT_REACHED();
00209     case BM_NORMAL:       Draw<BM_NORMAL>      (bp, zoom); return;
00210     case BM_COLOUR_REMAP: Draw<BM_COLOUR_REMAP>(bp, zoom); return;
00211     case BM_TRANSPARENT:  Draw<BM_TRANSPARENT> (bp, zoom); return;
00212   }
00213 }
00214 
00215 void Blitter_32bppAnim::DrawColourMappingRect(void *dst, int width, int height, PaletteID pal)
00216 {
00217   if (_screen_disable_anim) {
00218     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawColourMappingRect() */
00219     Blitter_32bppOptimized::DrawColourMappingRect(dst, width, height, pal);
00220     return;
00221   }
00222 
00223   uint32 *udst = (uint32 *)dst;
00224   uint16 *anim;
00225 
00226   anim = this->anim_buf + ((uint32 *)dst - (uint32 *)_screen.dst_ptr);
00227 
00228   if (pal == PALETTE_TO_TRANSPARENT) {
00229     do {
00230       for (int i = 0; i != width; i++) {
00231         *udst = MakeTransparent(*udst, 154);
00232         *anim = 0;
00233         udst++;
00234         anim++;
00235       }
00236       udst = udst - width + _screen.pitch;
00237       anim = anim - width + this->anim_buf_width;
00238     } while (--height);
00239     return;
00240   }
00241   if (pal == PALETTE_NEWSPAPER) {
00242     do {
00243       for (int i = 0; i != width; i++) {
00244         *udst = MakeGrey(*udst);
00245         *anim = 0;
00246         udst++;
00247         anim++;
00248       }
00249       udst = udst - width + _screen.pitch;
00250       anim = anim - width + this->anim_buf_width;
00251     } while (--height);
00252     return;
00253   }
00254 
00255   DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this colour table ('%d')", pal);
00256 }
00257 
00258 void Blitter_32bppAnim::SetPixel(void *video, int x, int y, uint8 colour)
00259 {
00260   *((uint32 *)video + x + y * _screen.pitch) = LookupColourInPalette(colour);
00261 
00262   /* Set the colour in the anim-buffer too, if we are rendering to the screen */
00263   if (_screen_disable_anim) return;
00264   this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = colour | (DEFAULT_BRIGHTNESS << 8);
00265 }
00266 
00267 void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 colour)
00268 {
00269   if (_screen_disable_anim) {
00270     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawRect() */
00271     Blitter_32bppOptimized::DrawRect(video, width, height, colour);
00272     return;
00273   }
00274 
00275   uint32 colour32 = LookupColourInPalette(colour);
00276   uint16 *anim_line;
00277 
00278   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00279 
00280   do {
00281     uint32 *dst = (uint32 *)video;
00282     uint16 *anim = anim_line;
00283 
00284     for (int i = width; i > 0; i--) {
00285       *dst = colour32;
00286       /* Set the colour in the anim-buffer too */
00287       *anim = colour | (DEFAULT_BRIGHTNESS << 8);
00288       dst++;
00289       anim++;
00290     }
00291     video = (uint32 *)video + _screen.pitch;
00292     anim_line += this->anim_buf_width;
00293   } while (--height);
00294 }
00295 
00296 void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, int height)
00297 {
00298   assert(!_screen_disable_anim);
00299   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00300   uint32 *dst = (uint32 *)video;
00301   const uint32 *usrc = (const uint32 *)src;
00302   uint16 *anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00303 
00304   for (; height > 0; height--) {
00305     /* We need to keep those for palette animation. */
00306     uint32 *dst_pal = dst;
00307     uint16 *anim_pal = anim_line;
00308 
00309     memcpy(dst, usrc, width * sizeof(uint32));
00310     usrc += width;
00311     dst += _screen.pitch;
00312     /* Copy back the anim-buffer */
00313     memcpy(anim_line, usrc, width * sizeof(uint16));
00314     usrc = (const uint32 *)((const uint16 *)usrc + width);
00315     anim_line += this->anim_buf_width;
00316 
00317     /* Okay, it is *very* likely that the image we stored is using
00318      * the wrong palette animated colours. There are two things we
00319      * can do to fix this. The first is simply reviewing the whole
00320      * screen after we copied the buffer, i.e. run PaletteAnimate,
00321      * however that forces a full screen redraw which is expensive
00322      * for just the cursor. This just copies the implementation of
00323      * palette animation, much cheaper though slightly nastier. */
00324     for (int i = 0; i < width; i++) {
00325       uint colour = GB(*anim_pal, 0, 8);
00326       if (colour >= PALETTE_ANIM_START) {
00327         /* Update this pixel */
00328         *dst_pal = this->AdjustBrightness(LookupColourInPalette(colour), GB(*anim_pal, 8, 8));
00329       }
00330       dst_pal++;
00331       anim_pal++;
00332     }
00333   }
00334 }
00335 
00336 void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, int height)
00337 {
00338   assert(!_screen_disable_anim);
00339   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00340   uint32 *udst = (uint32 *)dst;
00341   const uint32 *src = (const uint32 *)video;
00342   const uint16 *anim_line;
00343 
00344   if (this->anim_buf == NULL) return;
00345 
00346   anim_line = ((const uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00347 
00348   for (; height > 0; height--) {
00349     memcpy(udst, src, width * sizeof(uint32));
00350     src += _screen.pitch;
00351     udst += width;
00352     /* Copy the anim-buffer */
00353     memcpy(udst, anim_line, width * sizeof(uint16));
00354     udst = (uint32 *)((uint16 *)udst + width);
00355     anim_line += this->anim_buf_width;
00356   }
00357 }
00358 
00359 void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
00360 {
00361   assert(!_screen_disable_anim);
00362   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00363   uint16 *dst, *src;
00364 
00365   /* We need to scroll the anim-buffer too */
00366   if (scroll_y > 0) {
00367     dst = this->anim_buf + left + (top + height - 1) * this->anim_buf_width;
00368     src = dst - scroll_y * this->anim_buf_width;
00369 
00370     /* Adjust left & width */
00371     if (scroll_x >= 0) {
00372       dst += scroll_x;
00373     } else {
00374       src -= scroll_x;
00375     }
00376 
00377     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
00378     uint th = height - scroll_y;
00379     for (; th > 0; th--) {
00380       memcpy(dst, src, tw * sizeof(uint16));
00381       src -= this->anim_buf_width;
00382       dst -= this->anim_buf_width;
00383     }
00384   } else {
00385     /* Calculate pointers */
00386     dst = this->anim_buf + left + top * this->anim_buf_width;
00387     src = dst - scroll_y * this->anim_buf_width;
00388 
00389     /* Adjust left & width */
00390     if (scroll_x >= 0) {
00391       dst += scroll_x;
00392     } else {
00393       src -= scroll_x;
00394     }
00395 
00396     /* the y-displacement may be 0 therefore we have to use memmove,
00397      * because source and destination may overlap */
00398     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
00399     uint th = height + scroll_y;
00400     for (; th > 0; th--) {
00401       memmove(dst, src, tw * sizeof(uint16));
00402       src += this->anim_buf_width;
00403       dst += this->anim_buf_width;
00404     }
00405   }
00406 
00407   Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y);
00408 }
00409 
00410 int Blitter_32bppAnim::BufferSize(int width, int height)
00411 {
00412   return width * height * (sizeof(uint32) + sizeof(uint16));
00413 }
00414 
00415 void Blitter_32bppAnim::PaletteAnimate(const Palette &palette)
00416 {
00417   assert(!_screen_disable_anim);
00418 
00419   this->palette = palette;
00420   /* If first_dirty is 0, it is for 8bpp indication to send the new
00421    *  palette. As we dont do that for 32bpp, ignore that request
00422    *  completely */
00423   if (this->palette.first_dirty == 0) return;
00424   assert(this->palette.first_dirty == PALETTE_ANIM_START);
00425 
00426   const uint16 *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 = GB(*anim, 0, 8);
00433       if (colour >= PALETTE_ANIM_START) {
00434         /* Update this pixel */
00435         *dst = this->AdjustBrightness(LookupColourInPalette(colour), GB(*anim, 8, 8));
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 }
00451 
00452 void Blitter_32bppAnim::PostResize()
00453 {
00454   if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height) {
00455     /* The size of the screen changed; we can assume we can wipe all data from our buffer */
00456     free(this->anim_buf);
00457     this->anim_buf = CallocT<uint16>(_screen.width * _screen.height);
00458     this->anim_buf_width = _screen.width;
00459     this->anim_buf_height = _screen.height;
00460   }
00461 }