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