2007-06-13 10:31:40 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file 32bpp_base.hpp Base for all 32 bits blitters. */
|
2007-06-13 10:31:40 +00:00
|
|
|
|
2007-06-17 20:30:28 +00:00
|
|
|
#ifndef BLITTER_32BPP_BASE_HPP
|
|
|
|
#define BLITTER_32BPP_BASE_HPP
|
2007-06-13 10:31:40 +00:00
|
|
|
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "base.hpp"
|
2007-12-21 19:21:21 +00:00
|
|
|
#include "../core/bitmath_func.hpp"
|
2011-12-24 23:33:45 +00:00
|
|
|
#include "../core/math_func.hpp"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "../gfx_func.h"
|
2007-06-13 10:31:40 +00:00
|
|
|
|
2011-05-01 10:15:33 +00:00
|
|
|
/** Base for all 32bpp blitters. */
|
2007-06-17 20:30:28 +00:00
|
|
|
class Blitter_32bppBase : public Blitter {
|
2007-06-13 10:31:40 +00:00
|
|
|
public:
|
2007-06-17 20:30:28 +00:00
|
|
|
/* virtual */ uint8 GetScreenDepth() { return 32; }
|
2011-11-12 08:26:30 +00:00
|
|
|
/* virtual */ void *MoveTo(void *video, int x, int y);
|
2009-02-09 02:57:15 +00:00
|
|
|
/* virtual */ void SetPixel(void *video, int x, int y, uint8 colour);
|
|
|
|
/* virtual */ void DrawRect(void *video, int width, int height, uint8 colour);
|
2007-06-21 12:36:46 +00:00
|
|
|
/* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height);
|
|
|
|
/* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height);
|
|
|
|
/* virtual */ void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
|
2007-06-18 20:08:21 +00:00
|
|
|
/* virtual */ void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y);
|
2007-06-13 10:31:40 +00:00
|
|
|
/* virtual */ int BufferSize(int width, int height);
|
2011-12-08 19:37:33 +00:00
|
|
|
/* virtual */ void PaletteAnimate(const Palette &palette);
|
2007-06-19 15:04:08 +00:00
|
|
|
/* virtual */ Blitter::PaletteAnimation UsePaletteAnimation();
|
2008-08-15 22:06:58 +00:00
|
|
|
/* virtual */ int GetBytesPerPixel() { return 4; }
|
2007-06-13 10:31:40 +00:00
|
|
|
|
2007-06-19 17:32:01 +00:00
|
|
|
/**
|
|
|
|
* Look up the colour in the current palette.
|
2008-06-18 21:19:04 +00:00
|
|
|
*/
|
2012-04-10 20:16:51 +00:00
|
|
|
static inline Colour LookupColourInPalette(uint index)
|
2007-06-19 17:32:01 +00:00
|
|
|
{
|
2012-04-10 20:16:51 +00:00
|
|
|
return _cur_palette.palette[index];
|
2007-06-19 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compose a colour based on RGBA values and the current pixel value.
|
|
|
|
*/
|
2012-04-10 20:16:51 +00:00
|
|
|
static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
|
2008-06-26 15:46:19 +00:00
|
|
|
{
|
2012-04-10 20:16:51 +00:00
|
|
|
uint cr = current.r;
|
|
|
|
uint cg = current.g;
|
|
|
|
uint cb = current.b;
|
2008-06-26 15:46:19 +00:00
|
|
|
|
|
|
|
/* The 256 is wrong, it should be 255, but 256 is much faster... */
|
2012-04-10 20:16:51 +00:00
|
|
|
return Colour(
|
2008-07-17 23:43:47 +00:00
|
|
|
((int)(r - cr) * a) / 256 + cr,
|
|
|
|
((int)(g - cg) * a) / 256 + cg,
|
|
|
|
((int)(b - cb) * a) / 256 + cb);
|
2008-06-26 15:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compose a colour based on RGBA values and the current pixel value.
|
|
|
|
* Handles fully transparent and solid pixels in a special (faster) way.
|
|
|
|
*/
|
2012-04-10 20:16:51 +00:00
|
|
|
static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
|
2007-06-19 17:32:01 +00:00
|
|
|
{
|
2007-09-09 21:56:52 +00:00
|
|
|
if (a == 0) return current;
|
2012-04-10 20:16:51 +00:00
|
|
|
if (a >= 255) return Colour(r, g, b);
|
2007-09-09 21:56:52 +00:00
|
|
|
|
2008-06-26 15:46:19 +00:00
|
|
|
return ComposeColourRGBANoCheck(r, g, b, a, current);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compose a colour based on Pixel value, alpha value, and the current pixel value.
|
|
|
|
*/
|
2012-04-10 20:16:51 +00:00
|
|
|
static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
|
2008-06-26 15:46:19 +00:00
|
|
|
{
|
2012-04-10 20:16:51 +00:00
|
|
|
uint r = colour.r;
|
|
|
|
uint g = colour.g;
|
|
|
|
uint b = colour.b;
|
2007-06-19 17:32:01 +00:00
|
|
|
|
2008-07-17 23:43:47 +00:00
|
|
|
return ComposeColourRGBANoCheck(r, g, b, a, current);
|
2007-06-19 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-06-26 15:46:19 +00:00
|
|
|
* Compose a colour based on Pixel value, alpha value, and the current pixel value.
|
|
|
|
* Handles fully transparent and solid pixels in a special (faster) way.
|
|
|
|
*/
|
2012-04-10 20:16:51 +00:00
|
|
|
static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
|
2007-06-19 17:32:01 +00:00
|
|
|
{
|
2007-09-09 21:56:52 +00:00
|
|
|
if (a == 0) return current;
|
2012-04-10 20:16:51 +00:00
|
|
|
if (a >= 255) {
|
|
|
|
colour.a = 255;
|
|
|
|
return colour;
|
|
|
|
}
|
2007-09-09 21:56:52 +00:00
|
|
|
|
2008-06-26 15:46:19 +00:00
|
|
|
return ComposeColourPANoCheck(colour, a, current);
|
2007-06-19 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-03-14 18:16:29 +00:00
|
|
|
* Make a pixel looks like it is transparent.
|
|
|
|
* @param colour the colour already on the screen.
|
|
|
|
* @param nom the amount of transparency, nominator, makes colour lighter.
|
|
|
|
* @param denom denominator, makes colour darker.
|
|
|
|
* @return the new colour for the screen.
|
|
|
|
*/
|
2012-04-10 20:16:51 +00:00
|
|
|
static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
|
2007-06-19 17:32:01 +00:00
|
|
|
{
|
2012-04-10 20:16:51 +00:00
|
|
|
uint r = colour.r;
|
|
|
|
uint g = colour.g;
|
|
|
|
uint b = colour.b;
|
2007-06-19 17:32:01 +00:00
|
|
|
|
2012-04-10 20:16:51 +00:00
|
|
|
return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
|
2007-06-19 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-13 19:22:23 +00:00
|
|
|
/**
|
|
|
|
* Make a colour dark grey, for specialized 32bpp remapping.
|
|
|
|
* @param r red component
|
|
|
|
* @param g green component
|
|
|
|
* @param b blue component
|
|
|
|
* @return the brightness value of the new colour, now dark grey.
|
|
|
|
*/
|
|
|
|
static inline uint8 MakeDark(uint8 r, uint8 g, uint8 b)
|
|
|
|
{
|
|
|
|
/* Magic-numbers are ~66% of those used in MakeGrey() */
|
|
|
|
return ((r * 13063) + (g * 25647) + (b * 4981)) / 65536;
|
|
|
|
}
|
|
|
|
|
2007-06-19 17:32:01 +00:00
|
|
|
/**
|
2009-03-14 18:16:29 +00:00
|
|
|
* Make a colour grey - based.
|
|
|
|
* @param colour the colour to make grey.
|
|
|
|
* @return the new colour, now grey.
|
|
|
|
*/
|
2012-04-10 20:16:51 +00:00
|
|
|
static inline Colour MakeGrey(Colour colour)
|
2007-06-19 17:32:01 +00:00
|
|
|
{
|
2012-04-10 20:16:51 +00:00
|
|
|
uint r = colour.r;
|
|
|
|
uint g = colour.g;
|
|
|
|
uint b = colour.b;
|
2007-06-19 17:32:01 +00:00
|
|
|
|
|
|
|
/* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
|
2009-03-14 18:16:29 +00:00
|
|
|
* divide by it to normalize the value to a byte again. See heightmap.cpp for
|
|
|
|
* information about the formula. */
|
2012-04-10 20:16:51 +00:00
|
|
|
uint grey = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
|
2007-06-19 17:32:01 +00:00
|
|
|
|
2012-04-10 20:16:51 +00:00
|
|
|
return Colour(grey, grey, grey);
|
2007-06-13 10:31:40 +00:00
|
|
|
}
|
2011-12-24 23:33:45 +00:00
|
|
|
|
2012-10-17 20:21:43 +00:00
|
|
|
static const int DEFAULT_BRIGHTNESS = 128;
|
2011-12-24 23:33:45 +00:00
|
|
|
|
2012-04-10 20:16:51 +00:00
|
|
|
static inline Colour AdjustBrightness(Colour colour, uint8 brightness)
|
2011-12-24 23:33:45 +00:00
|
|
|
{
|
|
|
|
/* Shortcut for normal brightness */
|
|
|
|
if (brightness == DEFAULT_BRIGHTNESS) return colour;
|
|
|
|
|
|
|
|
uint16 ob = 0;
|
2012-04-10 20:16:51 +00:00
|
|
|
uint16 r = colour.r * brightness / DEFAULT_BRIGHTNESS;
|
|
|
|
uint16 g = colour.g * brightness / DEFAULT_BRIGHTNESS;
|
|
|
|
uint16 b = colour.b * brightness / DEFAULT_BRIGHTNESS;
|
2011-12-24 23:33:45 +00:00
|
|
|
|
|
|
|
/* Sum overbright */
|
|
|
|
if (r > 255) ob += r - 255;
|
|
|
|
if (g > 255) ob += g - 255;
|
|
|
|
if (b > 255) ob += b - 255;
|
|
|
|
|
2012-04-10 20:16:51 +00:00
|
|
|
if (ob == 0) return Colour(r, g, b, colour.a);
|
2011-12-24 23:33:45 +00:00
|
|
|
|
|
|
|
/* Reduce overbright strength */
|
|
|
|
ob /= 2;
|
2012-04-10 20:16:51 +00:00
|
|
|
return Colour(
|
2011-12-24 23:33:45 +00:00
|
|
|
r >= 255 ? 255 : min(r + ob * (255 - r) / 256, 255),
|
|
|
|
g >= 255 ? 255 : min(g + ob * (255 - g) / 256, 255),
|
2012-04-10 20:16:51 +00:00
|
|
|
b >= 255 ? 255 : min(b + ob * (255 - b) / 256, 255),
|
|
|
|
colour.a);
|
2011-12-24 23:33:45 +00:00
|
|
|
}
|
2007-06-13 10:31:40 +00:00
|
|
|
};
|
|
|
|
|
2007-06-17 20:30:28 +00:00
|
|
|
#endif /* BLITTER_32BPP_BASE_HPP */
|