shithub: npe

Download patch

ref: f6fe1a93aa38f1bc72630d8514cacfb86f33e18b
parent: 4c064ed6565ece34dd6d5194afec9a5f715a301f
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Tue May 18 09:15:11 EDT 2021

sdl2: fix scaling when logical render size has not been set

--- a/libnpe_sdl2/sdl2.c
+++ b/libnpe_sdl2/sdl2.c
@@ -5,7 +5,8 @@
 };
 
 struct SDL_Renderer {
-	int dummy;
+	int logiw;
+	int logih;
 };
 
 struct SDL_Texture {
@@ -31,7 +32,6 @@
 static Memimage *back;
 static u8int *backcopy;
 static Image *front;
-static int logiw, logih;
 static int physw, physh;
 static SDL_Cursor *oldcursor, *cursor;
 static int showcursor = SDL_ENABLE;
@@ -465,9 +465,9 @@
 
 	b = SDL_GetGlobalMouseState(nil, nil);
 	if(x != nil)
-		*x = (npe_sdl.m.xy.x - screen->r.min.x) * logiw / physw;
+		*x = npe_sdl.m.xy.x - screen->r.min.x;
 	if(y != nil)
-		*y = (npe_sdl.m.xy.y - screen->r.min.y) * logih / physh;
+		*y = npe_sdl.m.xy.y - screen->r.min.y;
 
 	return b;
 }
@@ -527,10 +527,19 @@
 }
 
 int
-SDL_RenderCopy(SDL_Renderer *, SDL_Texture *t, SDL_Rect *sre, SDL_Rect *dre)
+SDL_RenderCopy(SDL_Renderer *rend, SDL_Texture *t, SDL_Rect *sre, SDL_Rect *dre)
 {
 	Rectangle sr, dr;
+	int logiw, logih;
 
+	if(rend->logiw > 0 && rend->logih > 0){
+		logiw = rend->logiw;
+		logih = rend->logih;
+	}else{
+		logiw = physw;
+		logih = physh;
+	}
+
 	if(sre != nil){
 		sr.min = Pt(sre->x, sre->y);
 		sr.max = addpt(sr.min, Pt(sre->w, sre->h));
@@ -563,12 +572,21 @@
 }
 
 void
-SDL_RenderPresent(SDL_Renderer *)
+SDL_RenderPresent(SDL_Renderer *rend)
 {
 	Rectangle r, clipr;
 	static u32int *b;
 	uchar *rb;
+	int logiw, logih;
 
+	if(rend->logiw > 0 && rend->logih > 0){
+		logiw = rend->logiw;
+		logih = rend->logih;
+	}else{
+		logiw = physw;
+		logih = physh;
+	}
+
 	if(!npe_sdl.fullredraw && (npe_sdl.fullredraw = memcmp(backcopy, byteaddr(back, ZP), logiw*logih*4)) == 0 && !npe_sdl.mredraw)
 		return;
 
@@ -629,11 +647,11 @@
 }
 
 int
-SDL_RenderSetLogicalSize(SDL_Renderer *, int w, int h)
+SDL_RenderSetLogicalSize(SDL_Renderer *r, int w, int h)
 {
-	if(logiw != w || logih != h){
-		logiw = w;
-		logih = h;
+	if(r->logiw != w || r->logih != h){
+		r->logiw = w;
+		r->logih = h;
 		npe_sdl.fullredraw = 1;
 	}
 
@@ -641,12 +659,12 @@
 }
 
 int
-SDL_GetRendererOutputSize(SDL_Renderer *, int *w, int *h)
+SDL_GetRendererOutputSize(SDL_Renderer *r, int *w, int *h)
 {
 	if(w != nil)
-		*w = logiw;
+		*w = r->logiw;
 	if(h != nil)
-		*h = logih;
+		*h = r->logih;
 
 	return 0;
 }
@@ -788,7 +806,6 @@
 SDL_Renderer *
 SDL_CreateRenderer(SDL_Window *, int, Uint32)
 {
-	SDL_RenderSetLogicalSize(nil, physw, physh);
 	return &oneren;
 }