32bpp_base.cpp

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

Generated on Sat Dec 26 20:05:59 2009 for OpenTTD by  doxygen 1.5.6