32bpp_anim.cpp

00001 /* $Id: 32bpp_anim.cpp 11828 2008-01-13 01:21:35Z rubidium $ */
00002 
00003 #include "../stdafx.h"
00004 #include "../core/alloc_func.hpp"
00005 #include "../gfx_func.h"
00006 #include "../zoom_func.h"
00007 #include "../debug.h"
00008 #include "../video/video_driver.hpp"
00009 #include "32bpp_anim.hpp"
00010 
00011 #include "../table/sprites.h"
00012 
00013 static FBlitter_32bppAnim iFBlitter_32bppAnim;
00014 
00015 void Blitter_32bppAnim::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
00016 {
00017   if (_screen_disable_anim) {
00018     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent Draw() */
00019     Blitter_32bppOptimized::Draw(bp, mode, zoom);
00020     return;
00021   }
00022 
00023   const SpriteLoader::CommonPixel *src, *src_line;
00024   uint32 *dst, *dst_line;
00025   uint8 *anim, *anim_line;
00026 
00027   if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height) {
00028     /* The size of the screen changed; we can assume we can wipe all data from our buffer */
00029     free(this->anim_buf);
00030     this->anim_buf = CallocT<uint8>(_screen.width * _screen.height);
00031     this->anim_buf_width = _screen.width;
00032     this->anim_buf_height = _screen.height;
00033   }
00034 
00035   /* Find where to start reading in the source sprite */
00036   src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
00037   dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left;
00038   anim_line = this->anim_buf + ((uint32 *)bp->dst - (uint32 *)_screen.dst_ptr) + bp->top * this->anim_buf_width + bp->left;
00039 
00040   for (int y = 0; y < bp->height; y++) {
00041     dst = dst_line;
00042     dst_line += bp->pitch;
00043 
00044     src = src_line;
00045     src_line += bp->sprite_width * ScaleByZoom(1, zoom);
00046 
00047     anim = anim_line;
00048     anim_line += this->anim_buf_width;
00049 
00050     for (int x = 0; x < bp->width; x++) {
00051       if (src->a == 0) {
00052         /* src->r is 'misused' here to indicate how much more pixels are following with an alpha of 0 */
00053         int skip = UnScaleByZoom(src->r, zoom);
00054 
00055         dst  += skip;
00056         anim += skip;
00057         x    += skip - 1;
00058         src  += ScaleByZoom(1, zoom) * skip;
00059         continue;
00060       }
00061 
00062       switch (mode) {
00063         case BM_COLOUR_REMAP:
00064           /* In case the m-channel is zero, do not remap this pixel in any way */
00065           if (src->m == 0) {
00066             *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
00067             *anim = 0;
00068           } else {
00069             if (bp->remap[src->m] != 0) {
00070               *dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst);
00071               *anim = bp->remap[src->m];
00072             }
00073           }
00074           break;
00075 
00076         case BM_TRANSPARENT:
00077           /* TODO -- We make an assumption here that the remap in fact is transparency, not some color.
00078            *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
00079            *  we produce a result the newgrf maker didn't expect ;) */
00080 
00081           /* Make the current color a bit more black, so it looks like this image is transparent */
00082           *dst = MakeTransparent(*dst, 192);
00083           *anim = bp->remap[*anim];
00084           break;
00085 
00086         default:
00087           /* Above 217 is palette animation */
00088           if (src->m >= 217) *dst = ComposeColourPA(this->LookupColourInPalette(src->m), src->a, *dst);
00089           else               *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
00090           *anim = src->m;
00091           break;
00092       }
00093       dst++;
00094       anim++;
00095       src += ScaleByZoom(1, zoom);
00096     }
00097   }
00098 }
00099 
00100 void Blitter_32bppAnim::DrawColorMappingRect(void *dst, int width, int height, int pal)
00101 {
00102   if (_screen_disable_anim) {
00103     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawColorMappingRect() */
00104     Blitter_32bppOptimized::DrawColorMappingRect(dst, width, height, pal);
00105     return;
00106   }
00107 
00108   uint32 *udst = (uint32 *)dst;
00109   uint8 *anim;
00110 
00111   anim = this->anim_buf + ((uint32 *)dst - (uint32 *)_screen.dst_ptr);
00112 
00113   if (pal == PALETTE_TO_TRANSPARENT) {
00114     do {
00115       for (int i = 0; i != width; i++) {
00116         *udst = MakeTransparent(*udst, 154);
00117         *anim = 0;
00118         udst++;
00119         anim++;
00120       }
00121       udst = udst - width + _screen.pitch;
00122       anim = anim - width + this->anim_buf_width;
00123     } while (--height);
00124     return;
00125   }
00126   if (pal == PALETTE_TO_STRUCT_GREY) {
00127     do {
00128       for (int i = 0; i != width; i++) {
00129         *udst = MakeGrey(*udst);
00130         *anim = 0;
00131         udst++;
00132         anim++;
00133       }
00134       udst = udst - width + _screen.pitch;
00135       anim = anim - width + this->anim_buf_width;
00136     } while (--height);
00137     return;
00138   }
00139 
00140   DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this color table ('%d')", pal);
00141 }
00142 
00143 void Blitter_32bppAnim::SetPixel(void *video, int x, int y, uint8 color)
00144 {
00145   *((uint32 *)video + x + y * _screen.pitch) = LookupColourInPalette(color);
00146 
00147   /* Set the color in the anim-buffer too, if we are rendering to the screen */
00148   if (_screen_disable_anim) return;
00149   this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = color;
00150 }
00151 
00152 void Blitter_32bppAnim::SetPixelIfEmpty(void *video, int x, int y, uint8 color)
00153 {
00154   uint32 *dst = (uint32 *)video + x + y * _screen.pitch;
00155   if (*dst == 0) {
00156     *dst = LookupColourInPalette(color);
00157     /* Set the color in the anim-buffer too, if we are rendering to the screen */
00158     if (_screen_disable_anim) return;
00159     this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = color;
00160   }
00161 }
00162 
00163 void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 color)
00164 {
00165   if (_screen_disable_anim) {
00166     /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawRect() */
00167     Blitter_32bppOptimized::DrawRect(video, width, height, color);
00168     return;
00169   }
00170 
00171   uint32 color32 = LookupColourInPalette(color);
00172   uint8 *anim_line;
00173 
00174   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00175 
00176   do {
00177     uint32 *dst = (uint32 *)video;
00178     uint8 *anim = anim_line;
00179 
00180     for (int i = width; i > 0; i--) {
00181       *dst = color32;
00182       /* Set the color in the anim-buffer too */
00183       *anim = color;
00184       dst++;
00185       anim++;
00186     }
00187     video = (uint32 *)video + _screen.pitch;
00188     anim_line += this->anim_buf_width;
00189   } while (--height);
00190 }
00191 
00192 void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, int height)
00193 {
00194   assert(!_screen_disable_anim);
00195   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00196   uint32 *dst = (uint32 *)video;
00197   uint32 *usrc = (uint32 *)src;
00198   uint8 *anim_line;
00199 
00200   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00201 
00202   for (; height > 0; height--) {
00203     memcpy(dst, usrc, width * sizeof(uint32));
00204     usrc += width;
00205     dst += _screen.pitch;
00206     /* Copy back the anim-buffer */
00207     memcpy(anim_line, usrc, width * sizeof(uint8));
00208     usrc = (uint32 *)((uint8 *)usrc + width);
00209     anim_line += this->anim_buf_width;
00210   }
00211 
00212   /* We update the palette (or the pixels that do animation) immediatly, to avoid graphical glitches */
00213   this->PaletteAnimate(217, _use_dos_palette ? 38 : 28);
00214 }
00215 
00216 void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, int height)
00217 {
00218   assert(!_screen_disable_anim);
00219   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00220   uint32 *udst = (uint32 *)dst;
00221   uint32 *src = (uint32 *)video;
00222   uint8 *anim_line;
00223 
00224   if (this->anim_buf == NULL) return;
00225 
00226   anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf;
00227 
00228   for (; height > 0; height--) {
00229     memcpy(udst, src, width * sizeof(uint32));
00230     src += _screen.pitch;
00231     udst += width;
00232     /* Copy the anim-buffer */
00233     memcpy(udst, anim_line, width * sizeof(uint8));
00234     udst = (uint32 *)((uint8 *)udst + width);
00235     anim_line += this->anim_buf_width;
00236   }
00237 }
00238 
00239 void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
00240 {
00241   assert(!_screen_disable_anim);
00242   assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch);
00243   uint8 *dst, *src;
00244 
00245   /* We need to scroll the anim-buffer too */
00246   if (scroll_y > 0) {
00247     dst = this->anim_buf + left + (top + height - 1) * this->anim_buf_width;
00248     src = dst - scroll_y * this->anim_buf_width;
00249 
00250     /* Adjust left & width */
00251     if (scroll_x >= 0) dst += scroll_x;
00252     else               src -= scroll_x;
00253 
00254     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
00255     uint th = height - scroll_y;
00256     for (; th > 0; th--) {
00257       memcpy(dst, src, tw * sizeof(uint8));
00258       src -= this->anim_buf_width;
00259       dst -= this->anim_buf_width;
00260     }
00261   } else {
00262     /* Calculate pointers */
00263     dst = this->anim_buf + left + top * this->anim_buf_width;
00264     src = dst - scroll_y * this->anim_buf_width;
00265 
00266     /* Adjust left & width */
00267     if (scroll_x >= 0) dst += scroll_x;
00268     else               src -= scroll_x;
00269 
00270     /* the y-displacement may be 0 therefore we have to use memmove,
00271      * because source and destination may overlap */
00272     uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x);
00273     uint th = height + scroll_y;
00274     for (; th > 0; th--) {
00275       memmove(dst, src, tw * sizeof(uint8));
00276       src += this->anim_buf_width;
00277       dst += this->anim_buf_width;
00278     }
00279   }
00280 
00281   Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y);
00282 }
00283 
00284 int Blitter_32bppAnim::BufferSize(int width, int height)
00285 {
00286   return width * height * (sizeof(uint32) + sizeof(uint8));
00287 }
00288 
00289 void Blitter_32bppAnim::PaletteAnimate(uint start, uint count)
00290 {
00291   assert(!_screen_disable_anim);
00292   uint8 *anim = this->anim_buf;
00293 
00294   /* Never repaint the transparency pixel */
00295   if (start == 0) start++;
00296 
00297   /* Let's walk the anim buffer and try to find the pixels */
00298   for (int y = 0; y < this->anim_buf_height; y++) {
00299     for (int x = 0; x < this->anim_buf_width; x++) {
00300       if (*anim >= start && *anim <= start + count) {
00301         /* Update this pixel */
00302         this->SetPixel(_screen.dst_ptr, x, y, *anim);
00303       }
00304       anim++;
00305     }
00306   }
00307 
00308   /* Make sure the backend redraws the whole screen */
00309   _video_driver->MakeDirty(0, 0, _screen.width, _screen.height);
00310 }
00311 
00312 Blitter::PaletteAnimation Blitter_32bppAnim::UsePaletteAnimation()
00313 {
00314   return Blitter::PALETTE_ANIMATION_BLITTER;
00315 }

Generated on Mon Sep 22 20:34:14 2008 for openttd by  doxygen 1.5.6