32bpp_anim.cpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_anim.cpp 18709 2010-01-04 02:32:36Z peter1138 $ */
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   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, int 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_TO_STRUCT_GREY) {
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::SetPixelIfEmpty(void *video, int x, int y, uint8 colour)
00266 {
00267   uint32 *dst = (uint32 *)video + x + y * _screen.pitch;
00268   if (*dst == 0) {
00269     *dst = LookupColourInPalette(colour);
00270     /* Set the colour in the anim-buffer too, if we are rendering to the screen */
00271     if (_screen_disable_anim) return;
00272     this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = colour;
00273   }
00274 }
00275 
00276 void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 colour)
00277 {
00278   if (_screen_disable_anim) {
00279     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawRect() */
00280     Blitter_32bppOptimized::DrawRect(video, width, height, colour);
00281     return;
00282   }
00283 
00284   uint32 colour32 = LookupColourInPalette(colour);
00285   uint8 *anim_line;
00286 
00287   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00288 
00289   do {
00290     uint32 *dst = (uint32 *)video;
00291     uint8 *anim = anim_line;
00292 
00293     for (int i = width; i > 0; i--) {
00294       *dst = colour32;
00295       /* Set the colour in the anim-buffer too */
00296       *anim = colour;
00297       dst++;
00298       anim++;
00299     }
00300     video = (uint32 *)video + _screen.pitch;
00301     anim_line += this->anim_buf_width;
00302   } while (--height);
00303 }
00304 
00305 void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, int height)
00306 {
00307   assert(!_screen_disable_anim);
00308   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00309   uint32 *dst = (uint32 *)video;
00310   uint32 *usrc = (uint32 *)src;
00311   uint8 *anim_line;
00312 
00313   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00314 
00315   for (; height > 0; height--) {
00316     memcpy(dst, usrc, width * sizeof(uint32));
00317     usrc += width;
00318     dst += _screen.pitch;
00319     /* Copy back the anim-buffer */
00320     memcpy(anim_line, usrc, width * sizeof(uint8));
00321     usrc = (uint32 *)((uint8 *)usrc + width);
00322     anim_line += this->anim_buf_width;
00323   }
00324 
00325   /* We update the palette (or the pixels that do animation) immediatly, to avoid graphical glitches */
00326   this->PaletteAnimate(PALETTE_ANIM_SIZE_START, (_use_palette == PAL_DOS) ? PALETTE_ANIM_SIZE_DOS : PALETTE_ANIM_SIZE_WIN);
00327 }
00328 
00329 void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, int height)
00330 {
00331   assert(!_screen_disable_anim);
00332   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00333   uint32 *udst = (uint32 *)dst;
00334   uint32 *src = (uint32 *)video;
00335   uint8 *anim_line;
00336 
00337   if (this->anim_buf == NULL) return;
00338 
00339   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00340 
00341   for (; height > 0; height--) {
00342     memcpy(udst, src, width * sizeof(uint32));
00343     src += _screen.pitch;
00344     udst += width;
00345     /* Copy the anim-buffer */
00346     memcpy(udst, anim_line, width * sizeof(uint8));
00347     udst = (uint32 *)((uint8 *)udst + width);
00348     anim_line += this->anim_buf_width;
00349   }
00350 }
00351 
00352 void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
00353 {
00354   assert(!_screen_disable_anim);
00355   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00356   uint8 *dst, *src;
00357 
00358   /* We need to scroll the anim-buffer too */
00359   if (scroll_y > 0) {
00360     dst = this->anim_buf + left + (top + height - 1) * this->anim_buf_width;
00361     src = dst - scroll_y * this->anim_buf_width;
00362 
00363     /* Adjust left & width */
00364     if (scroll_x >= 0) {
00365       dst += scroll_x;
00366     } else {
00367       src -= scroll_x;
00368     }
00369 
00370     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
00371     uint th = height - scroll_y;
00372     for (; th > 0; th--) {
00373       memcpy(dst, src, tw * sizeof(uint8));
00374       src -= this->anim_buf_width;
00375       dst -= this->anim_buf_width;
00376     }
00377   } else {
00378     /* Calculate pointers */
00379     dst = this->anim_buf + left + top * this->anim_buf_width;
00380     src = dst - scroll_y * this->anim_buf_width;
00381 
00382     /* Adjust left & width */
00383     if (scroll_x >= 0) {
00384       dst += scroll_x;
00385     } else {
00386       src -= scroll_x;
00387     }
00388 
00389     /* the y-displacement may be 0 therefore we have to use memmove,
00390      * because source and destination may overlap */
00391     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
00392     uint th = height + scroll_y;
00393     for (; th > 0; th--) {
00394       memmove(dst, src, tw * sizeof(uint8));
00395       src += this->anim_buf_width;
00396       dst += this->anim_buf_width;
00397     }
00398   }
00399 
00400   Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y);
00401 }
00402 
00403 int Blitter_32bppAnim::BufferSize(int width, int height)
00404 {
00405   return width * height * (sizeof(uint32) + sizeof(uint8));
00406 }
00407 
00408 void Blitter_32bppAnim::PaletteAnimate(uint start, uint count)
00409 {
00410   assert(!_screen_disable_anim);
00411 
00412   /* Never repaint the transparency pixel */
00413   if (start == 0) {
00414     start++;
00415     count--;
00416   }
00417 
00418   const uint8 *anim = this->anim_buf;
00419   uint32 *dst = (uint32 *)_screen.dst_ptr;
00420 
00421   /* Let's walk the anim buffer and try to find the pixels */
00422   for (int y = this->anim_buf_height; y != 0 ; y--) {
00423     for (int x = this->anim_buf_width; x != 0 ; x--) {
00424       uint colour = *anim;
00425       if (IsInsideBS(colour, start, count)) {
00426         /* Update this pixel */
00427         *dst = LookupColourInPalette(colour);
00428       }
00429       dst++;
00430       anim++;
00431     }
00432     dst += _screen.pitch - this->anim_buf_width;
00433   }
00434 
00435   /* Make sure the backend redraws the whole screen */
00436   _video_driver->MakeDirty(0, 0, _screen.width, _screen.height);
00437 }
00438 
00439 Blitter::PaletteAnimation Blitter_32bppAnim::UsePaletteAnimation()
00440 {
00441   return Blitter::PALETTE_ANIMATION_BLITTER;
00442 }
00443 
00444 void Blitter_32bppAnim::PostResize()
00445 {
00446   if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height) {
00447     /* The size of the screen changed; we can assume we can wipe all data from our buffer */
00448     free(this->anim_buf);
00449     this->anim_buf = CallocT<uint8>(_screen.width * _screen.height);
00450     this->anim_buf_width = _screen.width;
00451     this->anim_buf_height = _screen.height;
00452   }
00453 }

Generated on Tue Jan 5 21:02:52 2010 for OpenTTD by  doxygen 1.5.6