src/gfx.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
--- a/src/gfx.cpp	Wed Jun 13 12:05:56 2007 +0000
+++ b/src/gfx.cpp	Tue Jun 19 07:21:01 2007 +0000
@@ -19,7 +19,7 @@
 #include "genworld.h"
 #include "debug.h"
 #include "zoom.hpp"
-#include "blitter/blitter.hpp"
+#include "blitter/factory.hpp"
 
 #ifdef _DEBUG
 bool _dbg_screen_rect;
@@ -50,7 +50,7 @@
 
 FontSize _cur_fontsize;
 static FontSize _last_fontsize;
-static Pixel _cursor_backup[64 * 64];
+static uint8 _cursor_backup[64 * 64 * 4];
 static Rect _invalid_rect;
 static const byte *_color_remap_ptr;
 static byte _string_colorremap[3];
@@ -58,37 +58,21 @@
 #define DIRTY_BYTES_PER_LINE (MAX_SCREEN_WIDTH / 64)
 static byte _dirty_blocks[DIRTY_BYTES_PER_LINE * MAX_SCREEN_HEIGHT / 8];
 
-void memcpy_pitch(void *dst, void *src, int w, int h, int srcpitch, int dstpitch)
-{
-	Pixel *dstp = (Pixel *)dst;
-	Pixel *srcp = (Pixel *)src;
-
-	assert(h >= 0);
-	for (; h != 0; --h) {
-		memcpy(dstp, srcp, w * sizeof(Pixel));
-		dstp += dstpitch;
-		srcp += srcpitch;
-	}
-}
-
 void GfxScroll(int left, int top, int width, int height, int xo, int yo)
 {
-	const Pixel *src;
-	Pixel *dst;
-	int p;
-	int ht;
+	Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
+	const void *src;
+	void *dst;
 
 	if (xo == 0 && yo == 0) return;
 
 	if (_cursor.visible) UndrawMouseCursor();
 	UndrawTextMessage();
 
-	p = _screen.pitch;
-
 	if (yo > 0) {
 		/*Calculate pointers */
-		dst = _screen.dst_ptr + (top + height - 1) * p + left;
-		src = dst - yo * p;
+		dst = blitter->MoveTo(_screen.dst_ptr, left, top + height - 1);
+		src = blitter->MoveTo(dst, 0, -yo);
 
 		/* Decrease height and increase top */
 		top += yo;
@@ -97,23 +81,20 @@
 
 		/* Adjust left & width */
 		if (xo >= 0) {
-			dst += xo;
+			dst = blitter->MoveTo(dst, xo, 0);
 			left += xo;
 			width -= xo;
 		} else {
-			src -= xo;
+			src = blitter->MoveTo(src, -xo, 0);
 			width += xo;
 		}
 
-		for (ht = height; ht > 0; --ht) {
-			memcpy(dst, src, width * sizeof(Pixel));
-			src -= p;
-			dst -= p;
-		}
+		/* Negative height as we want to copy from bottom to top */
+		blitter->CopyFromBuffer(dst, src, width, -height, _screen.pitch);
 	} else {
 		/* Calculate pointers */
-		dst = _screen.dst_ptr + top * p + left;
-		src = dst - yo * p;
+		dst = blitter->MoveTo(_screen.dst_ptr, left, top);
+		src = blitter->MoveTo(dst, 0, -yo);
 
 		/* Decrese height. (yo is <=0). */
 		height += yo;
@@ -121,21 +102,17 @@
 
 		/* Adjust left & width */
 		if (xo >= 0) {
-			dst += xo;
+			dst = blitter->MoveTo(dst, xo, 0);
 			left += xo;
 			width -= xo;
 		} else {
-			src -= xo;
+			src = blitter->MoveTo(src, -xo, 0);
 			width += xo;
 		}
 
 		/* the y-displacement may be 0 therefore we have to use memmove,
 		 * because source and destination may overlap */
-		for (ht = height; ht > 0; --ht) {
-			memmove(dst, src, width * sizeof(Pixel));
-			src += p;
-			dst += p;
-		}
+		blitter->MoveBuffer(dst, src, width, height);
 	}
 	/* This part of the screen is now dirty. */
 	_video_driver->make_dirty(left, top, width, height);
@@ -144,8 +121,9 @@
 
 void GfxFillRect(int left, int top, int right, int bottom, int color)
 {
-	const DrawPixelInfo* dpi = _cur_dpi;
-	Pixel *dst;
+	Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
+	const DrawPixelInfo *dpi = _cur_dpi;
+	void *dst;
 	const int otop = top;
 	const int oleft = left;
 
@@ -166,40 +144,33 @@
 	bottom -= top;
 	assert(bottom > 0);
 
-	dst = dpi->dst_ptr + top * dpi->pitch + left;
+	dst = blitter->MoveTo(dpi->dst_ptr, left, top);
 
 	if (!HASBIT(color, PALETTE_MODIFIER_GREYOUT)) {
 		if (!HASBIT(color, USE_COLORTABLE)) {
 			do {
-				memset(dst, color, right * sizeof(Pixel));
-				dst += dpi->pitch;
+				blitter->SetHorizontalLine(dst, right, (uint8)color);
+				dst = blitter->MoveTo(dst, 0, 1);
 			} while (--bottom);
 		} else {
-			/* use colortable mode */
-			const byte* ctab = GetNonSprite(GB(color, 0, PALETTE_WIDTH)) + 1;
-
-			do {
-				int i;
-				for (i = 0; i != right; i++) dst[i] = ctab[dst[i]];
-				dst += dpi->pitch;
-			} while (--bottom);
+			blitter->DrawColorMappingRect(dst, right, bottom, GB(color, 0, PALETTE_WIDTH));
 		}
 	} else {
 		byte bo = (oleft - left + dpi->left + otop - top + dpi->top) & 1;
 		do {
-			int i;
-			for (i = (bo ^= 1); i < right; i += 2) dst[i] = (byte)color;
-			dst += dpi->pitch;
+			for (int i = (bo ^= 1); i < right; i += 2) blitter->SetPixel(dst, i, 0, (uint8)color);
+			dst = blitter->MoveTo(dst, 0, 1);
 		} while (--bottom > 0);
 	}
 }
 
 static void GfxSetPixel(int x, int y, int color)
 {
-	const DrawPixelInfo* dpi = _cur_dpi;
-	if ((x -= dpi->left) < 0 || x >= dpi->width || (y -= dpi->top)<0 || y >= dpi->height)
-		return;
-	dpi->dst_ptr[y * dpi->pitch + x] = color;
+	Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
+	const DrawPixelInfo *dpi = _cur_dpi;
+
+	if ((x -= dpi->left) < 0 || x >= dpi->width || (y -= dpi->top) < 0 || y >= dpi->height) return;
+	blitter->SetPixel(dpi->dst_ptr, x, y, color);
 }
 
 void GfxDrawLine(int x, int y, int x2, int y2, int color)
@@ -817,6 +788,13 @@
 	Colour old_val[38]; // max(38, 28)
 	uint i;
 	uint j;
+	int old_tc = _timer_counter;
+
+	/* We can only update the palette in 8bpp for now */
+	/* TODO -- We need support for other bpps too! */
+	if (BlitterFactoryBase::GetCurrentBlitter() != NULL && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 8) {
+		_timer_counter = 0;
+	}
 
 	d = &_cur_palette[217];
 	memcpy(old_val, d, c * sizeof(*old_val));
@@ -913,6 +891,8 @@
 		if (_pal_first_dirty > 217) _pal_first_dirty = 217;
 		if (_pal_last_dirty < 217 + c) _pal_last_dirty = 217 + c;
 	}
+
+	if (old_tc != _timer_counter) _timer_counter = old_tc;
 }
 
 
@@ -958,18 +938,16 @@
 void UndrawMouseCursor()
 {
 	if (_cursor.visible) {
+		Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
 		_cursor.visible = false;
-		memcpy_pitch(
-			_screen.dst_ptr + _cursor.draw_pos.x + _cursor.draw_pos.y * _screen.pitch,
-			_cursor_backup,
-			_cursor.draw_size.x, _cursor.draw_size.y, _cursor.draw_size.x, _screen.pitch);
-
+		blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), _cursor_backup, _cursor.draw_size.x, _cursor.draw_size.y, _cursor.draw_size.x);
 		_video_driver->make_dirty(_cursor.draw_pos.x, _cursor.draw_pos.y, _cursor.draw_size.x, _cursor.draw_size.y);
 	}
 }
 
 void DrawMouseCursor()
 {
+	Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
 	int x;
 	int y;
 	int w;
@@ -1006,13 +984,10 @@
 	_cursor.draw_pos.y = y;
 	_cursor.draw_size.y = h;
 
-	assert(w * h < (int)sizeof(_cursor_backup));
+	assert(blitter->BufferSize(w, h) < (int)sizeof(_cursor_backup));
 
 	/* Make backup of stuff below cursor */
-	memcpy_pitch(
-		_cursor_backup,
-		_screen.dst_ptr + _cursor.draw_pos.x + _cursor.draw_pos.y * _screen.pitch,
-		_cursor.draw_size.x, _cursor.draw_size.y, _screen.pitch, _cursor.draw_size.x);
+	blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, _cursor.draw_pos.x, _cursor.draw_pos.y), _cursor_backup, _cursor.draw_size.x, _cursor.draw_size.y, _cursor.draw_size.x);
 
 	/* Draw cursor on screen */
 	_cur_dpi = &_screen;
@@ -1202,6 +1177,7 @@
  * get some nasty results */
 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
 {
+	Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
 	const DrawPixelInfo *o = _cur_dpi;
 
 	n->zoom = ZOOM_LVL_NORMAL;
@@ -1233,7 +1209,8 @@
 		n->top = 0;
 	}
 
-	n->dst_ptr = o->dst_ptr + left + top * (n->pitch = o->pitch);
+	n->dst_ptr = blitter->MoveTo(o->dst_ptr, left, top);
+	n->pitch = o->pitch;
 
 	if (height > o->height - top) {
 		height = o->height - top;