# 32bpp_base.cpp

Go to the documentation of this file.

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