32bpp_anim.cpp

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

Generated on Fri Nov 21 19:01:31 2008 for openttd by  doxygen 1.5.6