SDL: Software nearest scaling: start at the middle of pixel

From ab0cf42a4b2e6f31ccefeead36dc1bda4dcb031e Mon Sep 17 00:00:00 2001
From: Sylvain <[EMAIL REDACTED]>
Date: Thu, 4 Mar 2021 14:33:56 +0100
Subject: [PATCH] Software nearest scaling: start at the middle of pixel so
 that it matches opengl GL_NEAREST mode most of the time

---
 src/video/SDL_blit_slow.c | 4 ++--
 src/video/SDL_stretch.c   | 4 ++--
 src/video/sdlgenblit.pl   | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/video/SDL_blit_slow.c b/src/video/SDL_blit_slow.c
index 3ef33197c..2429028ab 100644
--- a/src/video/SDL_blit_slow.c
+++ b/src/video/SDL_blit_slow.c
@@ -49,15 +49,15 @@ SDL_Blit_Slow(SDL_BlitInfo * info)
     Uint32 rgbmask = ~src_fmt->Amask;
     Uint32 ckey = info->colorkey & rgbmask;
 
-    posy = 0;
     incy = (info->src_h << 16) / info->dst_h;
     incx = (info->src_w << 16) / info->dst_w;
+    posy = incy / 2; /* start at the middle of pixel */
 
     while (info->dst_h--) {
         Uint8 *src = 0;
         Uint8 *dst = info->dst;
         int n = info->dst_w;
-        posx = 0;
+        posx = incx / 2; /* start at the middle of pixel */
         srcy = posy >> 16;
         while (n--) {
             srcx = posx >> 16;
diff --git a/src/video/SDL_stretch.c b/src/video/SDL_stretch.c
index a6480e995..79052c7ec 100644
--- a/src/video/SDL_stretch.c
+++ b/src/video/SDL_stretch.c
@@ -857,13 +857,13 @@ SDL_LowerSoftStretchLinear(SDL_Surface *s, const SDL_Rect *srcrect,
     incy = (src_h << 16) / dst_h;                                                       \
     incx = (src_w << 16) / dst_w;                                                       \
     dst_gap   = dst_pitch - bpp * dst_w;                                                \
-    posy = 0;                                                                           \
+    posy = incy / 2;                                                                    \
 
 #define SDL_SCALE_NEAREST__HEIGHT                                                       \
     srcy = (posy >> 16);                                                                \
     src_h0  = (const Uint32 *)((const Uint8 *)src_ptr + srcy * src_pitch);              \
     posy += incy;                                                                       \
-    posx = 0;                                                                           \
+    posx = incx / 2;                                                                    \
     n = dst_w;
 
 
diff --git a/src/video/sdlgenblit.pl b/src/video/sdlgenblit.pl
index 63d6dc9b4..1d04b7cb0 100755
--- a/src/video/sdlgenblit.pl
+++ b/src/video/sdlgenblit.pl
@@ -458,15 +458,15 @@ sub output_copyfunc
 
     print FILE <<__EOF__;
 
-    posy = 0;
     incy = (info->src_h << 16) / info->dst_h;
     incx = (info->src_w << 16) / info->dst_w;
+    posy = incy / 2;
 
     while (info->dst_h--) {
         $format_type{$src} *src = 0;
         $format_type{$dst} *dst = ($format_type{$dst} *)info->dst;
         int n = info->dst_w;
-        posx = 0;
+        posx = incx / 2;
 
         srcy = posy >> 16;
         while (n--) {