8bpp_base.cpp

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

Generated on Wed Jul 15 20:35:57 2009 for OpenTTD by  doxygen 1.5.6