foxygit / fake-08-xbox360 Log in
commit 2b84c8dff4fa42c776219f6a7ea2724511013167
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sun Apr 26 21:09:57 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sun Apr 26 21:09:57 2020 -0600

    Fix clipping sprites and bugs with lines
---
 source/graphics.cpp | 136 +++++++++++++++++++++++++++++++++++++++++++---------
 source/graphics.h   |   1 +
 2 files changed, 115 insertions(+), 22 deletions(-)

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 54428e5..95411cd 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -78,6 +78,44 @@ void Graphics::copySpriteToScreen(
 	//note: no clipping yet
 	short scr_w = spr_w;
 	short scr_h = spr_h;
+
+	applyCameraToPoint(&scr_x, &scr_y);
+
+	// left clip
+	if (scr_x < _gfxState_clip_xb) {
+		int nclip = _gfxState_clip_xb - scr_x;
+		scr_x = _gfxState_clip_xb;
+		scr_w -= nclip;
+		if (!flip_x) {
+			spr_x += nclip;
+		} else {
+			spr_w -= nclip;
+		}
+	}
+
+	// right clip
+	if (scr_x + scr_w > _gfxState_clip_xe) {
+		int nclip = (scr_x + scr_w) - _gfxState_clip_xe;
+		scr_w -= nclip;
+	}
+
+	// top clip
+	if (scr_y < _gfxState_clip_yb) {
+		int nclip = _gfxState_clip_yb - scr_y;
+		scr_y = _gfxState_clip_yb;
+		scr_h -= nclip;
+		if (!flip_y) {
+			spr_y += nclip;
+		} else {
+			spr_h -= nclip;
+		}
+	}
+
+	// bottom clip
+	if (scr_y + scr_h > _gfxState_clip_ye) {
+		int nclip = (scr_y + scr_h) - _gfxState_clip_ye;
+		scr_h -= nclip;
+	}

 	short dy = 1;
 	if (flip_y) {
@@ -142,6 +180,8 @@ void Graphics::copyStretchSpriteToScreen(
 		return;
 	}

+	applyCameraToPoint(&scr_x, &scr_y);
+
 	//shift bits to avoid floating point math
 	spr_x = spr_x << 16;
 	spr_y = spr_y << 16;
@@ -151,6 +191,42 @@ void Graphics::copyStretchSpriteToScreen(
 	int dx = spr_w / scr_w;
 	int dy = spr_h / scr_h;

+	// left clip
+	if (scr_x < _gfxState_clip_xb) {
+		int nclip = _gfxState_clip_xb - scr_x;
+		scr_x = _gfxState_clip_xb;
+		scr_w -= nclip;
+		if (!flip_x) {
+			spr_x += nclip * dx;
+		} else {
+			spr_w -= nclip * dx;
+		}
+	}
+
+	// right clip
+	if (scr_x + scr_w > _gfxState_clip_xe) {
+		int nclip = (scr_x + scr_w) - _gfxState_clip_xe;
+		scr_w -= nclip;
+	}
+
+	// top clip
+	if (scr_y < _gfxState_clip_yb) {
+		int nclip = _gfxState_clip_yb - scr_y;
+		scr_y = _gfxState_clip_yb;
+		scr_h -= nclip;
+		if (!flip_y) {
+			spr_y += nclip * dy;
+		} else {
+			spr_h -= nclip * dy;
+		}
+	}
+
+	// bottom clip
+	if (scr_y + scr_h > _gfxState_clip_ye) {
+		int nclip = (scr_y + scr_h) - _gfxState_clip_ye;
+		scr_h -= nclip;
+	}
+
 	if (flip_y) {
 		spr_y += spr_h - 1 * dy;
 		dy = -dy;
@@ -201,6 +277,11 @@ void Graphics::applyCameraToPoint(short *x, short *y) {
 	*y -= _gfxState_camera_y;
 }

+void Graphics::applyCameraToPoint(int *x, int *y) {
+	*x -= _gfxState_camera_x;
+	*y -= _gfxState_camera_y;
+}
+
 void Graphics::sortPointsLtoR(short *x1, short *y1, short *x2, short *y2){
 	if (*x1 > *x2) {
 		swap(x1, x2);
@@ -340,7 +421,10 @@ void Graphics::_private_h_line (short x1, short x2, short y, uint8_t col){
 	short maxx = clampCoordToScreenDims(std::max(x1, x2));
 	short minx = clampCoordToScreenDims(std::min(x1, x2));

-	//possible todo: check if memset is any better here?
+
+	//possible todo: check if memset is any better here? this seems to be wrong
+	//uint8_t* fb_line = _pico8_fb + y * PicoScreenWidth;
+	//memset(fb_line + minx, col, maxx - minx);
 	for (short x = minx; x <= maxx; x++){
 		_private_pset(x, y, col);
 	}
@@ -360,35 +444,43 @@ void Graphics::_private_v_line (short y1, short y2, short x, uint8_t col){
 	}
 }

-void Graphics::line (short x1, short y1, short x2, short y2, uint8_t col) {
-	this->_gfxState_line_x = x2;
-	this->_gfxState_line_y = y2;
+void Graphics::line(short x0, short y0, short x1, short y1, uint8_t col) {
+	this->_gfxState_line_x = x1;
+	this->_gfxState_line_y = y1;
 	this->_gfxState_line_valid = true;

-	sortPointsLtoR(&x1, &y1, &x2, &y2);
-
-	//y = mx + b
+	applyCameraToPoint(&x0, &y0);
+	applyCameraToPoint(&x1, &y1);

-	float run = x2 - x1;
-	float rise = y2 - y1;
+	color(col);

 	//vertical line
-	if (run == 0) {
-		_private_v_line(y1, y2, x1, col);
+	if (x0 == x1) {
+		_private_v_line(y0, y1, x0, col);
 	}
-	else if (rise == 0) {
-		_private_h_line(x1, x2, y1, col);
+	else if (y0 == y1) {
+		_private_h_line(x0, x1, y0, col);
 	}
 	else {
-		//todo: this is broken for steep lines
-		applyCameraToPoint(&x1, &y1);
-		applyCameraToPoint(&x2, &y2);
-
-		float slope = rise / run;
-
-		for (short x = x1; x <= x2; x++){
-			short y = y1 + (short)ceil((float)x * slope);
-			_private_pset(x, y, col);
+		//tac08 line impl for diagonals (this should work for horizontal and vertical as well,
+		//but it has more branching)
+		int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
+		int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
+		int err = dx + dy, e2; /* error value e_xy */
+
+		for (;;) { /* loop */
+			_private_safe_pset(x0, y0, col);
+			if (x0 == x1 && y0 == y1)
+				break;
+			e2 = 2 * err;
+			if (e2 >= dy) {
+				err += dy;
+				x0 += sx;
+			} /* e_xy+e_x > 0 */
+			if (e2 <= dx) {
+				err += dx;
+				y0 += sy;
+			} /* e_xy+e_y < 0 */
 		}
 	}
 }
diff --git a/source/graphics.h b/source/graphics.h
index 37776a0..c192b2e 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -87,6 +87,7 @@ class Graphics {

 	void swap(short *x, short *y);
 	void applyCameraToPoint(short *x, short *y);
+	void applyCameraToPoint(int *x, int *y);

 	void sortPointsLtoR(short *x1, short *y1, short *x2, short *y2);