(svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
authortruelight
Wed, 13 Jun 2007 10:31:40 +0000
changeset 6889 2160bf28040b
parent 6888 024407b0e309
child 6890 bebf22f6c449
(svn r10132) -Codechange: split out the last direct video-buffer read access to the blitter-layer
-Add: added a new renderer and blitter to make room for some optimized bpp
-Fix: fill the alpha channel in the grf-spriteloader
projects/openttd.vcproj
projects/openttd_vs80.vcproj
source.list
src/blitter/32bpp_simple.cpp
src/blitter/32bpp_simple.hpp
src/blitter/8bpp_debug.cpp
src/blitter/8bpp_debug.hpp
src/blitter/8bpp_optimized.cpp
src/blitter/8bpp_optimized.hpp
src/blitter/8bpp_simple.cpp
src/blitter/8bpp_simple.hpp
src/blitter/blitter.hpp
src/blitter/null.cpp
src/blitter/null.hpp
src/gfx.cpp
src/renderer/32bpp.cpp
src/renderer/32bpp.hpp
src/spriteloader/grf.cpp
--- a/projects/openttd.vcproj	Wed Jun 13 09:12:10 2007 +0000
+++ b/projects/openttd.vcproj	Wed Jun 13 10:31:40 2007 +0000
@@ -965,6 +965,12 @@
 			Name="Blitters"
 			Filter="">
 			<File
+				RelativePath=".\..\src\blitter\32bpp_simple.cpp">
+			</File>
+			<File
+				RelativePath=".\..\src\blitter\32bpp_simple.hpp">
+			</File>
+			<File
 				RelativePath=".\..\src\blitter\8bpp_debug.cpp">
 			</File>
 			<File
@@ -1009,6 +1015,12 @@
 			Name="Renderer"
 			Filter="">
 			<File
+				RelativePath=".\..\src\renderer\32bpp.cpp">
+			</File>
+			<File
+				RelativePath=".\..\src\renderer\32bpp.hpp">
+			</File>
+			<File
 				RelativePath=".\..\src\renderer\8bpp.cpp">
 			</File>
 			<File
--- a/projects/openttd_vs80.vcproj	Wed Jun 13 09:12:10 2007 +0000
+++ b/projects/openttd_vs80.vcproj	Wed Jun 13 10:31:40 2007 +0000
@@ -1508,6 +1508,14 @@
 			Name="Blitters"
 			>
 			<File
+				RelativePath=".\..\src\blitter\32bpp_simple.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\blitter\32bpp_simple.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\blitter\8bpp_debug.cpp"
 				>
 			</File>
@@ -1564,6 +1572,14 @@
 			Name="Renderer"
 			>
 			<File
+				RelativePath=".\..\src\renderer\32bpp.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\..\src\renderer\32bpp.hpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\renderer\8bpp.cpp"
 				>
 			</File>
--- a/source.list	Wed Jun 13 09:12:10 2007 +0000
+++ b/source.list	Wed Jun 13 10:31:40 2007 +0000
@@ -292,6 +292,8 @@
 ai/trolly/trolly.cpp
 
 # Blitters
+blitter/32bpp_simple.cpp
+blitter/32bpp_simple.hpp
 blitter/8bpp_debug.cpp
 blitter/8bpp_debug.hpp
 blitter/8bpp_optimized.cpp
@@ -308,6 +310,8 @@
 spriteloader/spriteloader.hpp
 
 # Renderer
+renderer/32bpp.cpp
+renderer/32bpp.hpp
 renderer/8bpp.cpp
 renderer/8bpp.hpp
 renderer/null.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/blitter/32bpp_simple.cpp	Wed Jun 13 10:31:40 2007 +0000
@@ -0,0 +1,155 @@
+#include "../stdafx.h"
+#include "../zoom.hpp"
+#include "../gfx.h"
+#include "../debug.h"
+#include "../table/sprites.h"
+#include "../renderer/32bpp.hpp"
+#include "32bpp_simple.hpp"
+
+static FBlitter_32bppSimple iFBlitter_32bppSimple;
+
+/**
+ * Compose a color based on RGB values.
+ */
+static inline uint ComposeColor(uint r, uint g, uint b)
+{
+	return (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF) << 0;
+}
+
+/**
+ * Make a pixel looks like it is transparent.
+ * @param color the color already on the screen.
+ * @param amount the amount of transparency, times 100.
+ * @return the new color for the screen.
+ */
+static inline uint MakeTransparent(uint color, uint amount)
+{
+	uint r, g, b;
+	r = GB(color, 16, 8);
+	g = GB(color, 8,  8);
+	b = GB(color, 0,  8);
+
+	return ComposeColor(r * amount / 100, g * amount / 100, b * amount / 100);
+}
+
+/**
+ * Make a color grey-based.
+ * @param color the color to make grey.
+ * @return the new color, now grey.
+ */
+static inline uint MakeGrey(uint color)
+{
+	uint r, g, b;
+	r = GB(color, 16, 8);
+	g = GB(color, 8,  8);
+	b = GB(color, 0,  8);
+
+	/* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
+	 *  divide by it to normalize the value to a byte again. See heightmap.cpp for
+	 *  information about the formula. */
+	color = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
+
+	return ComposeColor(color, color, color);
+}
+
+void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
+{
+	const SpriteLoader::CommonPixel *src, *src_line;
+	uint32 *dst, *dst_line;
+
+	/* Find where to start reading in the source sprite */
+	src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
+	dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left;
+
+	for (int y = 0; y < bp->height; y++) {
+		dst = dst_line;
+		dst_line += bp->pitch;
+
+		src = src_line;
+		src_line += bp->sprite_width * ScaleByZoom(1, zoom);
+
+		for (int x = 0; x < bp->width; x++) {
+
+			switch (mode) {
+				case BM_COLOUR_REMAP:
+					/* In case the m-channel is zero, do not remap this pixel in any way */
+					if (src->m == 0) {
+						if (src->a != 0) *dst = ComposeColor(src->r, src->g, src->b);
+					} else {
+						if (bp->remap[src->m] != 0) *dst = Renderer_32bpp::LookupColourInPalette(bp->remap[src->m]);
+					}
+					break;
+
+				case BM_TRANSPARENT:
+					/* TODO -- We make an assumption here that the remap in fact is transparency, not some color.
+					 *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
+					 *  we produce a result the newgrf maker didn't expect ;) */
+
+					/* Make the current color a bit more black, so it looks like this image is transparent */
+					if (src->a != 0) *dst = MakeTransparent(*dst, 75);
+					break;
+
+				default:
+					if (src->a != 0) *dst = ComposeColor(src->r, src->g, src->b);
+					break;
+			}
+			dst++;
+			src += ScaleByZoom(1, zoom);
+		}
+	}
+}
+
+void Blitter_32bppSimple::DrawColorMappingRect(void *dst, int width, int height, int pal)
+{
+	uint32 *udst = (uint32 *)dst;
+
+	if (pal == PALETTE_TO_TRANSPARENT) {
+		do {
+			for (int i = 0; i != width; i++) {
+				*udst = MakeTransparent(*udst, 60);
+				udst++;
+			}
+			udst = udst - width + _screen.pitch;
+		} while (height--);
+		return;
+	}
+	if (pal == PALETTE_TO_STRUCT_GREY) {
+		do {
+			for (int i = 0; i != width; i++) {
+				*udst = MakeGrey(*udst);
+				udst++;
+			}
+			udst = udst - width + _screen.pitch;
+		} while (height--);
+		return;
+	}
+
+	DEBUG(misc, 0, "32bpp blitter doesn't know how to draw this color table ('%d')", pal);
+}
+
+Sprite *Blitter_32bppSimple::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
+{
+	Sprite *dest_sprite;
+	SpriteLoader::CommonPixel *dst;
+	dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
+
+	dest_sprite->height = sprite->height;
+	dest_sprite->width  = sprite->width;
+	dest_sprite->x_offs = sprite->x_offs;
+	dest_sprite->y_offs = sprite->y_offs;
+
+	dst = (SpriteLoader::CommonPixel *)dest_sprite->data;
+
+	memcpy(dst, sprite->data, sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
+	for (int i = 0; i < sprite->height * sprite->width; i++) {
+		if (dst[i].m != 0) {
+			/* Pre-convert the mapping channel to a RGB value */
+			uint color = Renderer_32bpp::LookupColourInPalette(dst[i].m);
+			dst[i].r = GB(color, 16, 8);
+			dst[i].g = GB(color, 8,  8);
+			dst[i].b = GB(color, 0,  8);
+		}
+	}
+
+	return dest_sprite;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/blitter/32bpp_simple.hpp	Wed Jun 13 10:31:40 2007 +0000
@@ -0,0 +1,32 @@
+/* $Id$ */
+
+/** @file 32bpp_simple.hpp */
+
+#ifndef BLITTER_32BPP_SIMPLE_HPP
+#define BLITTER_32BPP_SIMPLE_HPP
+
+#include "blitter.hpp"
+
+class Blitter_32bppSimple : public Blitter {
+public:
+	/* virtual */ uint8 GetScreenDepth() { return 32; }
+
+	/* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
+
+	/* virtual */ void DrawColorMappingRect(void *dst, int width, int height, int pal);
+
+	/* virtual */ Sprite *Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator);
+
+	/* virtual */ const char *GetRenderer() { return "32bpp"; }
+};
+
+class FBlitter_32bppSimple: public BlitterFactory<FBlitter_32bppSimple> {
+public:
+	/* virtual */ const char *GetName() { return "32bpp-simple"; }
+
+	/* virtual */ const char *GetDescription() { return "32bpp Simple Blitter (no palette animation)"; }
+
+	/* virtual */ Blitter *CreateInstance() { return new Blitter_32bppSimple(); }
+};
+
+#endif /* BLITTER_32BPP_SIMPLE_HPP */
--- a/src/blitter/8bpp_debug.cpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/8bpp_debug.cpp	Wed Jun 13 10:31:40 2007 +0000
@@ -35,6 +35,16 @@
 	}
 }
 
+void Blitter_8bppDebug::DrawColorMappingRect(void *dst, int width, int height, int pal)
+{
+	const uint8 *ctab = GetNonSprite(pal) + 1;
+
+	do {
+		for (int i = 0; i != width; i++) _screen.renderer->SetPixel(dst, i, 0, ctab[((uint8 *)dst)[i]]);
+		dst = _screen.renderer->MoveTo(dst, 0, 1);
+	} while (height--);
+}
+
 Sprite *Blitter_8bppDebug::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
 {
 	Sprite *dest_sprite;
--- a/src/blitter/8bpp_debug.hpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/8bpp_debug.hpp	Wed Jun 13 10:31:40 2007 +0000
@@ -13,6 +13,8 @@
 
 	/* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
 
+	/* virtual */ void DrawColorMappingRect(void *dst, int width, int height, int pal);
+
 	/* virtual */ Sprite *Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator);
 
 	/* virtual */ const char *GetRenderer() { return "8bpp"; }
--- a/src/blitter/8bpp_optimized.cpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/8bpp_optimized.cpp	Wed Jun 13 10:31:40 2007 +0000
@@ -102,6 +102,16 @@
 	}
 }
 
+void Blitter_8bppOptimized::DrawColorMappingRect(void *dst, int width, int height, int pal)
+{
+	const uint8 *ctab = GetNonSprite(pal) + 1;
+
+	do {
+		for (int i = 0; i != width; i++) _screen.renderer->SetPixel(dst, i, 0, ctab[((uint8 *)dst)[i]]);
+		dst = _screen.renderer->MoveTo(dst, 0, 1);
+	} while (height--);
+}
+
 Sprite *Blitter_8bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
 {
 	Sprite *dest_sprite;
--- a/src/blitter/8bpp_optimized.hpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/8bpp_optimized.hpp	Wed Jun 13 10:31:40 2007 +0000
@@ -13,6 +13,8 @@
 
 	/* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
 
+	/* virtual */ void DrawColorMappingRect(void *dst, int width, int height, int pal);
+
 	/* virtual */ Sprite *Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator);
 
 	/* virtual */ const char *GetRenderer() { return "8bpp"; }
--- a/src/blitter/8bpp_simple.cpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/8bpp_simple.cpp	Wed Jun 13 10:31:40 2007 +0000
@@ -48,6 +48,16 @@
 	}
 }
 
+void Blitter_8bppSimple::DrawColorMappingRect(void *dst, int width, int height, int pal)
+{
+	const uint8 *ctab = GetNonSprite(pal) + 1;
+
+	do {
+		for (int i = 0; i != width; i++) _screen.renderer->SetPixel(dst, i, 0, ctab[((uint8 *)dst)[i]]);
+		dst = _screen.renderer->MoveTo(dst, 0, 1);
+	} while (height--);
+}
+
 Sprite *Blitter_8bppSimple::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
 {
 	Sprite *dest_sprite;
--- a/src/blitter/8bpp_simple.hpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/8bpp_simple.hpp	Wed Jun 13 10:31:40 2007 +0000
@@ -13,6 +13,8 @@
 
 	/* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
 
+	/* virtual */ void DrawColorMappingRect(void *dst, int width, int height, int pal);
+
 	/* virtual */ Sprite *Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator);
 
 	/* virtual */ const char *GetRenderer() { return "8bpp"; }
--- a/src/blitter/blitter.hpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/blitter.hpp	Wed Jun 13 10:31:40 2007 +0000
@@ -49,6 +49,17 @@
 	virtual void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) = 0;
 
 	/**
+	 * Draw a colortable to the screen. This is: the color of the screen is read
+	 *  and is looked-up in the palette to match a new color, which then is put
+	 *  on the screen again.
+	 * @param dst the destination pointer (video-buffer).
+	 * @param width the width of the buffer.
+	 * @param height the height of the buffer.
+	 * @param pal the palette to use.
+	 */
+	virtual void DrawColorMappingRect(void *dst, int width, int height, int pal) = 0;
+
+	/**
 	 * Convert a sprite from the loader to our own format.
 	 */
 	virtual Sprite *Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator) = 0;
--- a/src/blitter/null.cpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/null.cpp	Wed Jun 13 10:31:40 2007 +0000
@@ -10,6 +10,10 @@
 {
 }
 
+void Blitter_Null::DrawColorMappingRect(void *dst, int width, int height, int pal)
+{
+}
+
 Sprite *Blitter_Null::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
 {
 	Sprite *dest_sprite;
--- a/src/blitter/null.hpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/blitter/null.hpp	Wed Jun 13 10:31:40 2007 +0000
@@ -13,6 +13,8 @@
 
 	/* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
 
+	/* virtual */ void DrawColorMappingRect(void *dst, int width, int height, int pal);
+
 	/* virtual */ Sprite *Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator);
 
 	/* virtual */ const char *GetRenderer() { return "null"; }
--- a/src/gfx.cpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/gfx.cpp	Wed Jun 13 10:31:40 2007 +0000
@@ -151,13 +151,7 @@
 				dst = _screen.renderer->MoveTo(dst, 0, 1);
 			} while (--bottom);
 		} else {
-			/* use colortable mode */
-			const byte* ctab = GetNonSprite(GB(color, 0, PALETTE_WIDTH)) + 1;
-
-			do {
-				for (int i = 0; i != right; i++) _screen.renderer->SetPixel(dst, i, 0, ctab[((uint8 *)dst)[i]]);
-				dst = _screen.renderer->MoveTo(dst, 0, 1);
-			} while (--bottom);
+			BlitterFactoryBase::GetCurrentBlitter()->DrawColorMappingRect(dst, right, bottom, GB(color, 0, PALETTE_WIDTH));
 		}
 	} else {
 		byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/renderer/32bpp.cpp	Wed Jun 13 10:31:40 2007 +0000
@@ -0,0 +1,77 @@
+#include "../stdafx.h"
+#include "../gfx.h"
+#include "32bpp.hpp"
+
+static FRenderer_32bpp iFRenderer_32bpp;
+
+void *Renderer_32bpp::MoveTo(const void *video, int x, int y)
+{
+	return (uint32 *)video + x + y * _screen.pitch;
+}
+
+void Renderer_32bpp::SetPixel(void *video, int x, int y, uint8 color)
+{
+	*((uint32 *)video + x + y * _screen.pitch) = LookupColourInPalette(color);
+}
+
+void Renderer_32bpp::SetPixelIfEmpty(void *video, int x, int y, uint8 color)
+{
+	uint32 *dst = (uint32 *)video + x + y * _screen.pitch;
+	if (*dst == 0) *dst = LookupColourInPalette(color);
+}
+
+void Renderer_32bpp::SetHorizontalLine(void *video, int width, uint8 color)
+{
+	uint32 *dst = (uint32 *)video;
+	uint32 color32 = LookupColourInPalette(color);
+
+	for (; width > 0; width--) {
+		*dst = color32;
+		dst++;
+	}
+}
+
+void Renderer_32bpp::CopyFromBuffer(void *video, const void *src, int width, int height, int src_pitch)
+{
+	int direction = (height < 0) ? -1 : 1;
+	uint32 *dst = (uint32 *)video;
+	uint32 *usrc = (uint32 *)src;
+
+	height = abs(height);
+	for (; height > 0; height--) {
+		memcpy(dst, usrc, width * sizeof(uint32));
+		usrc += src_pitch * direction;
+		dst += _screen.pitch * direction;
+	}
+}
+
+void Renderer_32bpp::CopyToBuffer(const void *video, void *dst, int width, int height, int dst_pitch)
+{
+	int direction = (height < 0) ? -1 : 1;
+	uint32 *udst = (uint32 *)dst;
+	uint32 *src = (uint32 *)video;
+
+	height = abs(height);
+	for (; height > 0; height--) {
+		memcpy(udst, src, width * sizeof(uint32));
+		src += _screen.pitch * direction;
+		udst += dst_pitch * direction;
+	}
+}
+
+void Renderer_32bpp::MoveBuffer(void *video_dst, const void *video_src, int width, int height)
+{
+	uint32 *dst = (uint32 *)video_dst;
+	uint32 *src = (uint32 *)video_src;
+
+	for (; height > 0; height--) {
+		memmove(dst, src, width * sizeof(uint32));
+		src += _screen.pitch;
+		dst += _screen.pitch;
+	}
+}
+
+int Renderer_32bpp::BufferSize(int width, int height)
+{
+	return width * height * sizeof(uint32);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/renderer/32bpp.hpp	Wed Jun 13 10:31:40 2007 +0000
@@ -0,0 +1,35 @@
+/* $Id$ */
+
+/** @file 32bpp.hpp */
+
+#ifndef RENDERER_32BPP_HPP
+#define RENDERER_32BPP_HPP
+
+#include "renderer.hpp"
+
+class Renderer_32bpp : public Renderer {
+public:
+	/* virtual */ void *MoveTo(const void *video, int x, int y);
+	/* virtual */ void SetPixel(void *video, int x, int y, uint8 color);
+	/* virtual */ void SetPixelIfEmpty(void *video, int x, int y, uint8 color);
+	/* virtual */ void SetHorizontalLine(void *video, int width, uint8 color);
+	/* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height, int src_pitch);
+	/* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
+	/* virtual */ void MoveBuffer(void *video_dst, const void *video_src, int width, int height);
+	/* virtual */ int BufferSize(int width, int height);
+
+	static inline uint32 LookupColourInPalette(uint8 index) {
+		#define ARGB(a, r, g, b) ((((a) << 24) & 0xFF000000) | (((r) << 16) & 0x00FF0000) | (((g) << 8) & 0x0000FF00) | ((b) & 0x000000FF))
+		if (index == 0) return 0x00000000;
+		return ARGB(0xFF, _cur_palette[index].r, _cur_palette[index].g, _cur_palette[index].b);
+	}
+};
+
+class FRenderer_32bpp: public RendererFactory<FRenderer_32bpp> {
+public:
+	/* virtual */ const char *GetName() { return "32bpp"; }
+
+	/* virtual */ Renderer *CreateInstance() { return new Renderer_32bpp(); }
+};
+
+#endif /* RENDERER_32BPP_HPP */
--- a/src/spriteloader/grf.cpp	Wed Jun 13 09:12:10 2007 +0000
+++ b/src/spriteloader/grf.cpp	Wed Jun 13 10:31:40 2007 +0000
@@ -95,6 +95,10 @@
 			sprite->data[i].m = dest[i];
 	}
 
+	/* Make sure to mark all transparent pixels transparent on the alpha channel too */
+	for (int i = 0; i < sprite->width * sprite->height; i++)
+		if (sprite->data[i].m != 0) sprite->data[i].a = 0xFF;
+
 	free(dest_orig);
 	return true;
 }