From 4b0f729d528061d34bbd8bbdcd7c383b8489101a Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Wed, 21 Feb 2024 17:43:35 +0700 Subject: [PATCH] bring back clipping to v_video functions (#1518) * add clipping to V_FillRect * add clipping to V_CopyRect --- src/v_video.c | 83 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 32 deletions(-) diff --git a/src/v_video.c b/src/v_video.c index d09fc122a..6bfc3df92 100644 --- a/src/v_video.c +++ b/src/v_video.c @@ -632,6 +632,35 @@ int V_ScaleY(int y) return y1lookup[y]; } +static void V_ClipRect(vrect_t *rect) +{ + // clip to left and top edges + rect->cx1 = rect->x >= 0 ? rect->x : 0; + rect->cy1 = rect->y >= 0 ? rect->y : 0; + + // determine right and bottom edges + rect->cx2 = rect->x + rect->w - 1; + rect->cy2 = rect->y + rect->h - 1; + + // clip right and bottom edges + if (rect->cx2 >= video.unscaledw) + rect->cx2 = video.unscaledw - 1; + if (rect->cy2 >= SCREENHEIGHT) + rect->cy2 = SCREENHEIGHT - 1; + + // determine clipped width and height + rect->cw = rect->cx2 - rect->cx1 + 1; + rect->ch = rect->cy2 - rect->cy1 + 1; +} + +static void V_ScaleClippedRect(vrect_t *rect) +{ + rect->sx = x1lookup[rect->cx1]; + rect->sy = y1lookup[rect->cy1]; + rect->sw = x2lookup[rect->cx2] - rect->sx + 1; + rect->sh = y2lookup[rect->cy2] - rect->sy + 1; +} + void V_FillRect(int x, int y, int width, int height, byte color) { vrect_t dstrect; @@ -641,7 +670,13 @@ void V_FillRect(int x, int y, int width, int height, byte color) dstrect.w = width; dstrect.h = height; - V_ScaleRect(&dstrect); + V_ClipRect(&dstrect); + + // clipped away completely? + if (dstrect.cw <= 0 || dstrect.ch <= 0) + return; + + V_ScaleClippedRect(&dstrect); byte* dest = V_ADDRESS(dest_screen, dstrect.sx, dstrect.sy); @@ -683,13 +718,26 @@ void V_CopyRect(int srcx, int srcy, pixel_t *source, srcrect.w = width; srcrect.h = height; + V_ClipRect(&srcrect); + + // clipped away completely? + if (srcrect.cw <= 0 || srcrect.ch <= 0) + return; + + V_ScaleClippedRect(&srcrect); + dstrect.x = destx; dstrect.y = desty; dstrect.w = width; dstrect.h = height; - V_ScaleRect(&srcrect); - V_ScaleRect(&dstrect); + V_ClipRect(&dstrect); + + // clipped away completely? + if (dstrect.cw <= 0 || dstrect.ch <= 0) + return; + + V_ScaleClippedRect(&dstrect); // use the smaller of the two scaled rect widths / heights usew = (srcrect.sw < dstrect.sw ? srcrect.sw : dstrect.sw); @@ -706,35 +754,6 @@ void V_CopyRect(int srcx, int srcy, pixel_t *source, } } -static void V_ClipRect(vrect_t *rect) -{ - // clip to left and top edges - rect->cx1 = rect->x >= 0 ? rect->x : 0; - rect->cy1 = rect->y >= 0 ? rect->y : 0; - - // determine right and bottom edges - rect->cx2 = rect->x + rect->w - 1; - rect->cy2 = rect->y + rect->h - 1; - - // clip right and bottom edges - if (rect->cx2 >= video.unscaledw) - rect->cx2 = video.unscaledw - 1; - if (rect->cy2 >= SCREENHEIGHT) - rect->cy2 = SCREENHEIGHT - 1; - - // determine clipped width and height - rect->cw = rect->cx2 - rect->cx1 + 1; - rect->ch = rect->cy2 - rect->cy1 + 1; -} - -static void V_ScaleClippedRect(vrect_t *rect) -{ - rect->sx = x1lookup[rect->cx1]; - rect->sy = y1lookup[rect->cy1]; - rect->sw = x2lookup[rect->cx2] - rect->sx + 1; - rect->sh = y2lookup[rect->cy2] - rect->sy + 1; -} - // // V_DrawBlock //