8bpp_base.cpp

00001 /* $Id: 8bpp_base.cpp 11684 2007-12-23 10:56:02Z rubidium $ */
00002 
00003 #include "../stdafx.h"
00004 #include "../gfx_func.h"
00005 #include "8bpp_base.hpp"
00006 
00007 void Blitter_8bppBase::DrawColorMappingRect(void *dst, int width, int height, int pal)
00008 {
00009   const uint8 *ctab = GetNonSprite(pal) + 1;
00010 
00011   do {
00012     for (int i = 0; i != width; i++) *((uint8 *)dst + i) = ctab[((uint8 *)dst)[i]];
00013     dst = (uint8 *)dst + _screen.pitch;
00014   } while (--height);
00015 }
00016 
00017 void *Blitter_8bppBase::MoveTo(const void *video, int x, int y)
00018 {
00019   return (uint8 *)video + x + y * _screen.pitch;
00020 }
00021 
00022 void Blitter_8bppBase::SetPixel(void *video, int x, int y, uint8 color)
00023 {
00024   *((uint8 *)video + x + y * _screen.pitch) = color;
00025 }
00026 
00027 void Blitter_8bppBase::SetPixelIfEmpty(void *video, int x, int y, uint8 color)
00028 {
00029   uint8 *dst = (uint8 *)video + x + y * _screen.pitch;
00030   if (*dst == 0) *dst = color;
00031 }
00032 
00033 void Blitter_8bppBase::DrawRect(void *video, int width, int height, uint8 color)
00034 {
00035   do {
00036     memset(video, color, width);
00037     video = (uint8 *)video + _screen.pitch;
00038   } while (--height);
00039 }
00040 
00041 void Blitter_8bppBase::DrawLine(void *video, int x, int y, int x2, int y2, int screen_width, int screen_height, uint8 color)
00042 {
00043   int dy;
00044   int dx;
00045   int stepx;
00046   int stepy;
00047   int frac;
00048 
00049   dy = (y2 - y) * 2;
00050   if (dy < 0) {
00051     dy = -dy;
00052     stepy = -1;
00053   } else {
00054     stepy = 1;
00055   }
00056 
00057   dx = (x2 - x) * 2;
00058   if (dx < 0) {
00059     dx = -dx;
00060     stepx = -1;
00061   } else {
00062     stepx = 1;
00063   }
00064 
00065   if (x >= 0 && y >= 0 && x < screen_width && y < screen_height) this->SetPixel(video, x, y, color);
00066   if (dx > dy) {
00067     frac = dy - (dx / 2);
00068     while (x != x2) {
00069       if (frac >= 0) {
00070         y += stepy;
00071         frac -= dx;
00072       }
00073       x += stepx;
00074       frac += dy;
00075       if (x >= 0 && y >= 0 && x < screen_width && y < screen_height) this->SetPixel(video, x, y, color);
00076     }
00077   } else {
00078     frac = dx - (dy / 2);
00079     while (y != y2) {
00080       if (frac >= 0) {
00081         x += stepx;
00082         frac -= dy;
00083       }
00084       y += stepy;
00085       frac += dx;
00086       if (x >= 0 && y >= 0 && x < screen_width && y < screen_height) this->SetPixel(video, x, y, color);
00087     }
00088   }
00089 }
00090 
00091 void Blitter_8bppBase::CopyFromBuffer(void *video, const void *src, int width, int height)
00092 {
00093   uint8 *dst = (uint8 *)video;
00094   uint8 *usrc = (uint8 *)src;
00095 
00096   for (; height > 0; height--) {
00097     memcpy(dst, usrc, width * sizeof(uint8));
00098     usrc += width;
00099     dst += _screen.pitch;
00100   }
00101 }
00102 
00103 void Blitter_8bppBase::CopyToBuffer(const void *video, void *dst, int width, int height)
00104 {
00105   uint8 *udst = (uint8 *)dst;
00106   uint8 *src = (uint8 *)video;
00107 
00108   for (; height > 0; height--) {
00109     memcpy(udst, src, width * sizeof(uint8));
00110     src += _screen.pitch;
00111     udst += width;
00112   }
00113 }
00114 
00115 void Blitter_8bppBase::CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
00116 {
00117   uint8 *udst = (uint8 *)dst;
00118   uint8 *src = (uint8 *)video;
00119 
00120   for (; height > 0; height--) {
00121     memcpy(udst, src, width * sizeof(uint8));
00122     src += _screen.pitch;
00123     udst += dst_pitch;
00124   }
00125 }
00126 
00127 void Blitter_8bppBase::ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y)
00128 {
00129   const uint8 *src;
00130   uint8 *dst;
00131 
00132   if (scroll_y > 0) {
00133     /*Calculate pointers */
00134     dst = (uint8 *)video + left + (top + height - 1) * _screen.pitch;
00135     src = dst - scroll_y * _screen.pitch;
00136 
00137     /* Decrease height and increase top */
00138     top += scroll_y;
00139     height -= scroll_y;
00140     assert(height > 0);
00141 
00142     /* Adjust left & width */
00143     if (scroll_x >= 0) {
00144       dst += scroll_x;
00145       left += scroll_x;
00146       width -= scroll_x;
00147     } else {
00148       src -= scroll_x;
00149       width += scroll_x;
00150     }
00151 
00152     for (int h = height; h > 0; h--) {
00153       memcpy(dst, src, width * sizeof(uint8));
00154       src -= _screen.pitch;
00155       dst -= _screen.pitch;
00156     }
00157   } else {
00158     /* Calculate pointers */
00159     dst = (uint8 *)video + left + top * _screen.pitch;
00160     src = dst - scroll_y * _screen.pitch;
00161 
00162     /* Decrese height. (scroll_y is <=0). */
00163     height += scroll_y;
00164     assert(height > 0);
00165 
00166     /* Adjust left & width */
00167     if (scroll_x >= 0) {
00168       dst += scroll_x;
00169       left += scroll_x;
00170       width -= scroll_x;
00171     } else {
00172       src -= scroll_x;
00173       width += scroll_x;
00174     }
00175 
00176     /* the y-displacement may be 0 therefore we have to use memmove,
00177      * because source and destination may overlap */
00178     for (int h = height; h > 0; h--) {
00179       memmove(dst, src, width * sizeof(uint8));
00180       src += _screen.pitch;
00181       dst += _screen.pitch;
00182     }
00183   }
00184 }
00185 
00186 int Blitter_8bppBase::BufferSize(int width, int height)
00187 {
00188   return width * height;
00189 }
00190 
00191 void Blitter_8bppBase::PaletteAnimate(uint start, uint count)
00192 {
00193   /* Video backend takes care of the palette animation */
00194 }
00195 
00196 Blitter::PaletteAnimation Blitter_8bppBase::UsePaletteAnimation()
00197 {
00198   return Blitter::PALETTE_ANIMATION_VIDEO_BACKEND;
00199 }

Generated on Wed Oct 1 17:03:20 2008 for openttd by  doxygen 1.5.6