smatz@8091: /* $Id$ */ smatz@8091: rubidium@9111: /** @file 32bpp_anim.cpp Implementation of the optimized 32 bpp blitter with animation support. */ rubidium@9111: truelight@6971: #include "../stdafx.h" rubidium@8130: #include "../core/alloc_func.hpp" smatz@9548: #include "../core/math_func.hpp" rubidium@8123: #include "../gfx_func.h" rubidium@8123: #include "../zoom_func.h" truelight@6971: #include "../debug.h" peter1138@7170: #include "../video/video_driver.hpp" truelight@6971: #include "32bpp_anim.hpp" truelight@6971: rubidium@8264: #include "../table/sprites.h" rubidium@8264: truelight@6971: static FBlitter_32bppAnim iFBlitter_32bppAnim; truelight@6971: smatz@9597: template smatz@9597: inline void Blitter_32bppAnim::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom) smatz@9597: { smatz@9597: const SpriteData *src = (const SpriteData *)bp->sprite; smatz@9597: smatz@9597: const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]); smatz@9597: const uint8 *src_n = (const uint8 *)(src->data + src->offset[zoom][1]); smatz@9597: smatz@9597: for (uint i = bp->skip_top; i != 0; i--) { smatz@9597: src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px); smatz@9597: src_n += *(const uint32 *)src_n; smatz@9597: } smatz@9597: smatz@9597: uint32 *dst = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left; smatz@9597: uint8 *anim = this->anim_buf + ((uint32 *)bp->dst - (uint32 *)_screen.dst_ptr) + bp->top * this->anim_buf_width + bp->left; smatz@9597: smatz@9597: const byte *remap = bp->remap; // store so we don't have to access it via bp everytime smatz@9597: smatz@9597: for (int y = 0; y < bp->height; y++) { smatz@9597: uint32 *dst_ln = dst + bp->pitch; smatz@9597: uint8 *anim_ln = anim + this->anim_buf_width; smatz@9597: smatz@9597: const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px); smatz@9597: src_px++; smatz@9597: smatz@9597: const uint8 *src_n_ln = src_n + *(uint32 *)src_n; smatz@9597: src_n += 4; smatz@9597: smatz@9597: uint32 *dst_end = dst + bp->skip_left; smatz@9597: smatz@9597: uint n; smatz@9597: smatz@9597: while (dst < dst_end) { smatz@9597: n = *src_n++; smatz@9597: smatz@9597: if (src_px->a == 0) { smatz@9597: dst += n; smatz@9597: src_px ++; smatz@9597: src_n++; smatz@9597: smatz@9597: if (dst > dst_end) anim += dst - dst_end; smatz@9597: } else { smatz@9597: if (dst + n > dst_end) { smatz@9597: uint d = dst_end - dst; smatz@9597: src_px += d; smatz@9597: src_n += d; smatz@9597: smatz@9597: dst = dst_end - bp->skip_left; smatz@9597: dst_end = dst + bp->width; smatz@9597: smatz@9597: n = min(n - d, (uint)bp->width); smatz@9597: goto draw; smatz@9597: } smatz@9597: dst += n; smatz@9597: src_px += n; smatz@9597: src_n += n; smatz@9597: } smatz@9597: } smatz@9597: smatz@9597: dst -= bp->skip_left; smatz@9597: dst_end -= bp->skip_left; smatz@9597: smatz@9597: dst_end += bp->width; smatz@9597: smatz@9597: while (dst < dst_end) { smatz@9597: n = min(*src_n++, (uint)(dst_end - dst)); smatz@9597: smatz@9597: if (src_px->a == 0) { smatz@9597: anim += n; smatz@9597: dst += n; smatz@9597: src_px++; smatz@9597: src_n++; smatz@9597: continue; smatz@9597: } smatz@9597: smatz@9597: draw:; smatz@9597: smatz@9597: switch (mode) { smatz@9597: case BM_COLOUR_REMAP: smatz@9597: if (src_px->a == 255) { smatz@9597: do { smatz@9597: uint m = *src_n; smatz@9597: /* In case the m-channel is zero, do not remap this pixel in any way */ smatz@9597: if (m == 0) { smatz@9597: *dst = *src_px; smatz@9597: *anim = 0; smatz@9597: } else { smatz@9597: uint r = remap[m]; smatz@9597: *anim = r; smatz@9597: if (r != 0) *dst = this->LookupColourInPalette(r); smatz@9597: } smatz@9597: anim++; smatz@9597: dst++; smatz@9597: src_px++; smatz@9597: src_n++; smatz@9597: } while (--n != 0); smatz@9597: } else { smatz@9597: do { smatz@9597: uint m = *src_n; smatz@9597: if (m == 0) { smatz@9597: *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst); smatz@9597: *anim = 0; smatz@9597: } else { smatz@9597: uint r = remap[m]; smatz@9597: *anim = r; smatz@9597: if (r != 0) *dst = ComposeColourPANoCheck(this->LookupColourInPalette(r), src_px->a, *dst); smatz@9597: } smatz@9597: anim++; smatz@9597: dst++; smatz@9597: src_px++; smatz@9597: src_n++; smatz@9597: } while (--n != 0); smatz@9597: } smatz@9597: break; smatz@9597: smatz@9597: case BM_TRANSPARENT: smatz@9597: /* TODO -- We make an assumption here that the remap in fact is transparency, not some color. smatz@9597: * This is never a problem with the code we produce, but newgrfs can make it fail... or at least: smatz@9597: * we produce a result the newgrf maker didn't expect ;) */ smatz@9597: smatz@9597: /* Make the current color a bit more black, so it looks like this image is transparent */ smatz@9597: src_n += n; smatz@9598: if (src_px->a == 255) { smatz@9598: src_px += n; smatz@9598: do { smatz@9598: *dst = MakeTransparent(*dst, 3, 4); smatz@9598: *anim = remap[*anim]; smatz@9598: anim++; smatz@9598: dst++; smatz@9598: } while (--n != 0); smatz@9598: } else { smatz@9598: do { smatz@9598: *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4); smatz@9598: *anim = remap[*anim]; smatz@9598: anim++; smatz@9598: dst++; smatz@9598: src_px++; smatz@9598: } while (--n != 0); smatz@9598: } smatz@9597: break; smatz@9597: smatz@9597: default: smatz@9597: if (src_px->a == 255) { smatz@9597: do { smatz@9597: /* Compiler assumes pointer aliasing, can't optimise this on its own */ smatz@9597: uint m = *src_n++; belugas@9870: /* Above 217 (PALETTE_ANIM_SIZE_START) is palette animation */ smatz@9597: *anim++ = m; belugas@9870: *dst++ = (m >= PALETTE_ANIM_SIZE_START) ? this->LookupColourInPalette(m) : *src_px; smatz@9597: src_px++; smatz@9597: } while (--n != 0); smatz@9597: } else { smatz@9597: do { smatz@9597: uint m = *src_n++; smatz@9597: *anim++ = m; belugas@9870: if (m >= PALETTE_ANIM_SIZE_START) { smatz@9597: *dst = ComposeColourPANoCheck(this->LookupColourInPalette(m), src_px->a, *dst); smatz@9597: } else { smatz@9597: *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst); smatz@9597: } smatz@9597: dst++; smatz@9597: src_px++; smatz@9597: } while (--n != 0); smatz@9597: } smatz@9597: break; smatz@9597: } smatz@9597: } smatz@9597: smatz@9597: anim = anim_ln; smatz@9597: dst = dst_ln; smatz@9597: src_px = src_px_ln; smatz@9597: src_n = src_n_ln; smatz@9597: } smatz@9597: } smatz@9597: truelight@6971: void Blitter_32bppAnim::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) truelight@6971: { frosch@8249: if (_screen_disable_anim) { truelight@7556: /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent Draw() */ truelight@7556: Blitter_32bppOptimized::Draw(bp, mode, zoom); truelight@7556: return; truelight@7556: } truelight@7557: truelight@6971: if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height) { truelight@6971: /* The size of the screen changed; we can assume we can wipe all data from our buffer */ truelight@6971: free(this->anim_buf); truelight@6971: this->anim_buf = CallocT(_screen.width * _screen.height); truelight@6971: this->anim_buf_width = _screen.width; truelight@6971: this->anim_buf_height = _screen.height; truelight@6971: } truelight@6971: smatz@9597: switch (mode) { smatz@9597: default: NOT_REACHED(); smatz@9597: case BM_NORMAL: Draw (bp, zoom); return; smatz@9597: case BM_COLOUR_REMAP: Draw(bp, zoom); return; smatz@9597: case BM_TRANSPARENT: Draw (bp, zoom); return; truelight@6971: } truelight@6971: } truelight@6971: truelight@6985: void Blitter_32bppAnim::DrawColorMappingRect(void *dst, int width, int height, int pal) truelight@6985: { frosch@8249: if (_screen_disable_anim) { frosch@8249: /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawColorMappingRect() */ frosch@8249: Blitter_32bppOptimized::DrawColorMappingRect(dst, width, height, pal); frosch@8249: return; frosch@8249: } frosch@8249: truelight@6985: uint32 *udst = (uint32 *)dst; truelight@6985: uint8 *anim; truelight@6985: truelight@6985: anim = this->anim_buf + ((uint32 *)dst - (uint32 *)_screen.dst_ptr); truelight@6985: truelight@6985: if (pal == PALETTE_TO_TRANSPARENT) { truelight@6985: do { truelight@6985: for (int i = 0; i != width; i++) { truelight@7552: *udst = MakeTransparent(*udst, 154); truelight@6985: *anim = 0; truelight@6985: udst++; truelight@6985: anim++; truelight@6985: } truelight@6985: udst = udst - width + _screen.pitch; truelight@6985: anim = anim - width + this->anim_buf_width; truelight@6985: } while (--height); truelight@6985: return; truelight@6985: } truelight@6985: if (pal == PALETTE_TO_STRUCT_GREY) { truelight@6985: do { truelight@6985: for (int i = 0; i != width; i++) { truelight@6985: *udst = MakeGrey(*udst); truelight@6985: *anim = 0; truelight@6985: udst++; truelight@6985: anim++; truelight@6985: } truelight@6985: udst = udst - width + _screen.pitch; truelight@6985: anim = anim - width + this->anim_buf_width; truelight@6985: } while (--height); truelight@6985: return; truelight@6985: } truelight@6985: truelight@6985: DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this color table ('%d')", pal); truelight@6985: } truelight@6985: truelight@6971: void Blitter_32bppAnim::SetPixel(void *video, int x, int y, uint8 color) truelight@6971: { truelight@6971: *((uint32 *)video + x + y * _screen.pitch) = LookupColourInPalette(color); frosch@8249: frosch@8249: /* Set the color in the anim-buffer too, if we are rendering to the screen */ frosch@8249: if (_screen_disable_anim) return; truelight@6971: this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = color; truelight@6971: } truelight@6971: truelight@6971: void Blitter_32bppAnim::SetPixelIfEmpty(void *video, int x, int y, uint8 color) truelight@6971: { truelight@6971: uint32 *dst = (uint32 *)video + x + y * _screen.pitch; truelight@6971: if (*dst == 0) { truelight@6971: *dst = LookupColourInPalette(color); frosch@8249: /* Set the color in the anim-buffer too, if we are rendering to the screen */ frosch@8249: if (_screen_disable_anim) return; truelight@6971: this->anim_buf[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * this->anim_buf_width] = color; truelight@6971: } truelight@6971: } truelight@6971: truelight@6971: void Blitter_32bppAnim::DrawRect(void *video, int width, int height, uint8 color) truelight@6971: { frosch@8249: if (_screen_disable_anim) { frosch@8249: /* This means our output is not to the screen, so we can't be doing any animation stuff, so use our parent DrawRect() */ frosch@8249: Blitter_32bppOptimized::DrawRect(video, width, height, color); frosch@8249: return; frosch@8249: } frosch@8249: truelight@6971: uint32 color32 = LookupColourInPalette(color); truelight@6971: uint8 *anim_line; truelight@6971: truelight@6971: anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; truelight@6971: truelight@6971: do { truelight@6971: uint32 *dst = (uint32 *)video; truelight@6971: uint8 *anim = anim_line; truelight@6971: truelight@6971: for (int i = width; i > 0; i--) { truelight@6971: *dst = color32; truelight@6971: /* Set the color in the anim-buffer too */ truelight@6971: *anim = color; truelight@6971: dst++; truelight@6971: anim++; truelight@6971: } truelight@6971: video = (uint32 *)video + _screen.pitch; truelight@6971: anim_line += this->anim_buf_width; truelight@6971: } while (--height); truelight@6971: } truelight@6971: truelight@6985: void Blitter_32bppAnim::CopyFromBuffer(void *video, const void *src, int width, int height) truelight@6985: { frosch@8249: assert(!_screen_disable_anim); truelight@6985: assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch); truelight@6985: uint32 *dst = (uint32 *)video; truelight@6985: uint32 *usrc = (uint32 *)src; truelight@6985: uint8 *anim_line; truelight@6985: truelight@6985: anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; truelight@6985: truelight@6985: for (; height > 0; height--) { truelight@6985: memcpy(dst, usrc, width * sizeof(uint32)); truelight@6985: usrc += width; truelight@6985: dst += _screen.pitch; truelight@6985: /* Copy back the anim-buffer */ truelight@6985: memcpy(anim_line, usrc, width * sizeof(uint8)); truelight@6985: usrc = (uint32 *)((uint8 *)usrc + width); truelight@6985: anim_line += this->anim_buf_width; truelight@6985: } truelight@6985: truelight@6985: /* We update the palette (or the pixels that do animation) immediatly, to avoid graphical glitches */ rubidium@9989: this->PaletteAnimate(PALETTE_ANIM_SIZE_START, (_use_palette == PAL_DOS) ? PALETTE_ANIM_SIZE_DOS : PALETTE_ANIM_SIZE_WIN); truelight@6985: } truelight@6985: truelight@6985: void Blitter_32bppAnim::CopyToBuffer(const void *video, void *dst, int width, int height) truelight@6985: { frosch@8249: assert(!_screen_disable_anim); truelight@6985: assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch); truelight@6985: uint32 *udst = (uint32 *)dst; truelight@6985: uint32 *src = (uint32 *)video; truelight@6985: uint8 *anim_line; truelight@6985: truelight@6986: if (this->anim_buf == NULL) return; truelight@6986: truelight@6985: anim_line = ((uint32 *)video - (uint32 *)_screen.dst_ptr) + this->anim_buf; truelight@6985: truelight@6985: for (; height > 0; height--) { truelight@6985: memcpy(udst, src, width * sizeof(uint32)); truelight@6985: src += _screen.pitch; truelight@6985: udst += width; truelight@6985: /* Copy the anim-buffer */ truelight@6985: memcpy(udst, anim_line, width * sizeof(uint8)); truelight@6985: udst = (uint32 *)((uint8 *)udst + width); truelight@6985: anim_line += this->anim_buf_width; truelight@6985: } truelight@6985: } truelight@6985: truelight@6971: void Blitter_32bppAnim::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y) truelight@6971: { frosch@8249: assert(!_screen_disable_anim); frosch@8249: assert(video >= _screen.dst_ptr && video <= (uint32 *)_screen.dst_ptr + _screen.width + _screen.height * _screen.pitch); truelight@6971: uint8 *dst, *src; truelight@6971: truelight@6971: /* We need to scroll the anim-buffer too */ truelight@6971: if (scroll_y > 0) { truelight@6971: dst = this->anim_buf + left + (top + height - 1) * this->anim_buf_width; truelight@6971: src = dst - scroll_y * this->anim_buf_width; truelight@6971: truelight@6971: /* Adjust left & width */ truelight@6971: if (scroll_x >= 0) dst += scroll_x; truelight@6971: else src -= scroll_x; truelight@6971: truelight@6971: uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x); truelight@6971: uint th = height - scroll_y; truelight@6971: for (; th > 0; th--) { truelight@6971: memcpy(dst, src, tw * sizeof(uint8)); truelight@6971: src -= this->anim_buf_width; truelight@6971: dst -= this->anim_buf_width; truelight@6971: } truelight@6971: } else { truelight@6971: /* Calculate pointers */ truelight@6971: dst = this->anim_buf + left + top * this->anim_buf_width; truelight@6971: src = dst - scroll_y * this->anim_buf_width; truelight@6971: truelight@6971: /* Adjust left & width */ truelight@6971: if (scroll_x >= 0) dst += scroll_x; truelight@6971: else src -= scroll_x; truelight@6971: truelight@6971: /* the y-displacement may be 0 therefore we have to use memmove, truelight@6971: * because source and destination may overlap */ truelight@6971: uint tw = width + (scroll_x >= 0 ? -scroll_x : scroll_x); truelight@6971: uint th = height + scroll_y; truelight@6971: for (; th > 0; th--) { truelight@6971: memmove(dst, src, tw * sizeof(uint8)); truelight@6971: src += this->anim_buf_width; truelight@6971: dst += this->anim_buf_width; truelight@6971: } truelight@6971: } truelight@6971: truelight@6971: Blitter_32bppBase::ScrollBuffer(video, left, top, width, height, scroll_x, scroll_y); truelight@6971: } truelight@6971: truelight@6988: int Blitter_32bppAnim::BufferSize(int width, int height) truelight@6988: { truelight@6988: return width * height * (sizeof(uint32) + sizeof(uint8)); truelight@6988: } truelight@6988: truelight@6971: void Blitter_32bppAnim::PaletteAnimate(uint start, uint count) truelight@6971: { frosch@8249: assert(!_screen_disable_anim); truelight@6971: truelight@6979: /* Never repaint the transparency pixel */ smatz@9548: if (start == 0) { smatz@9548: start++; smatz@9548: count--; smatz@9548: } smatz@9548: smatz@9548: const uint8 *anim = this->anim_buf; smatz@9548: uint32 *dst = (uint32 *)_screen.dst_ptr; truelight@6979: truelight@6971: /* Let's walk the anim buffer and try to find the pixels */ smatz@9548: for (int y = this->anim_buf_height; y != 0 ; y--) { smatz@9548: for (int x = this->anim_buf_width; x != 0 ; x--) { smatz@9548: uint colour = *anim; smatz@9548: if (IsInsideBS(colour, start, count)) { truelight@6971: /* Update this pixel */ smatz@9548: *dst = LookupColourInPalette(colour); truelight@6971: } smatz@9548: dst++; truelight@6971: anim++; truelight@6971: } smatz@9550: dst += _screen.pitch - this->anim_buf_width; truelight@6971: } truelight@6971: truelight@6971: /* Make sure the backend redraws the whole screen */ peter1138@7170: _video_driver->MakeDirty(0, 0, _screen.width, _screen.height); truelight@6971: } truelight@6971: truelight@6971: Blitter::PaletteAnimation Blitter_32bppAnim::UsePaletteAnimation() truelight@6971: { truelight@6971: return Blitter::PALETTE_ANIMATION_BLITTER; truelight@6971: }