shithub: qk1

Download patch

ref: 612fcd2a36544c584e9933ccdcc04407edaa3485
parent: 9c1b473ae274c097e81c1dff285e089edafddc5c
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Sun Dec 31 11:50:08 EST 2023

sky: make it work with whichever sizes; replace absent sky with notexture

--- a/d_local.h
+++ b/d_local.h
@@ -1,9 +1,6 @@
 // d_local.h:  private rasterization driver defs
 
 enum {
-	R_SKY_SMASK = 0x007F0000,
-	R_SKY_TMASK	= 0x007F0000,
-
 	DS_SPAN_LIST_END = -128,
 
 	SURFCACHE_SIZE_AT_320X200 = 600*1024,
@@ -22,6 +19,14 @@
 	float sdivzorigin, tdivzorigin, ziorigin;
 }dvars_t;
 
+typedef struct {
+	pixel_t *source[2];
+	int w, h;
+	int smask, tmask;
+	int tshift;
+	float speed, time;
+}skyvars_t;
+
 typedef struct surfcache_s
 {
 	struct surfcache_s	*next;
@@ -51,6 +56,7 @@
 extern surfcache_t	*d_initial_rover;
 
 extern dvars_t dvars;
+extern skyvars_t skyvars;
 
 void D_DrawSpans16 (espan_t *pspans, bool blend, byte alpha);
 void D_DrawZSpans (espan_t *pspans);
--- a/d_sky.c
+++ b/d_sky.c
@@ -3,7 +3,7 @@
 #define SKY_SPAN_SHIFT	5
 #define SKY_SPAN_MAX	(1 << SKY_SPAN_SHIFT)
 
-extern int skyw, skyh;
+skyvars_t skyvars;
 
 /*
 =================
@@ -24,10 +24,10 @@
 	end[2] *= 3;
 	VectorNormalize(end);
 
-	s[0] = (int)((skydist + 4*(skyw-1)*end[0]) * 0x10000);
-	t[0] = (int)((skydist + 4*(skyh-1)*end[1]) * 0x10000);
-	s[1] = (int)((skydist*2.0 + 4*(skyw-1)*end[0]) * 0x10000);
-	t[1] = (int)((skydist*2.0 + 4*(skyh-1)*end[1]) * 0x10000);
+	s[0] = (int)((skydist + 4*(skyvars.w-1)*end[0]) * 0x10000);
+	t[0] = (int)((skydist + 4*(skyvars.h-1)*end[1]) * 0x10000);
+	s[1] = (int)((skydist*2.0 + 4*(skyvars.w-1)*end[0]) * 0x10000);
+	t[1] = (int)((skydist*2.0 + 4*(skyvars.h-1)*end[1]) * 0x10000);
 }
 
 
@@ -43,16 +43,16 @@
 	fixed16_t s[2], t[2], snext[2], tnext[2], sstep[2], tstep[2];
 	float skydist;
 
-	if(r_skysource[0] == nil || r_skysource[1] == nil)
+	if(skyvars.source[0] == nil || skyvars.source[1] == nil)
 		return;
 
 	sstep[0] = sstep[1] = 0;	// keep compiler happy
 	tstep[0] = tstep[1] = 0;	// ditto
-	skydist = skytime*skyspeed;	// TODO: add D_SetupFrame & set this there
+	skydist = skyvars.time*skyvars.speed;	// TODO: add D_SetupFrame & set this there
 
 	do
 	{
-		pdest = dvars.viewbuffer + pspan->v*dvars.width + pspan->u;
+		pdest = dvars.viewbuffer + (dvars.width * pspan->v) + pspan->u;
 		count = pspan->count;
 
 		// calculate the initial s & t
@@ -102,13 +102,11 @@
 
 			do
 			{
-				m = r_skysource[1][((t[1] & R_SKY_TMASK) >> 9) +
-						((s[1] & R_SKY_SMASK) >> 16)];
-				if((m & 0xffffff) == 0)
-					*pdest = r_skysource[0][((t[0] & R_SKY_TMASK) >> 9) +
-							((s[0] & R_SKY_SMASK) >> 16)];
-				else
+				m = skyvars.source[1][((t[1] & skyvars.tmask) >> skyvars.tshift) + ((s[1] & skyvars.smask) >> 16)];
+				if(opaque(m))
 					*pdest = m;
+				else
+					*pdest = skyvars.source[0][((t[0] & skyvars.tmask) >> skyvars.tshift) + ((s[0] & skyvars.smask) >> 16)];
 				pdest++;
 				s[0] += sstep[0];
 				t[0] += tstep[0];
--- a/plan9/platform.h
+++ b/plan9/platform.h
@@ -43,3 +43,5 @@
 
 static double ln2c;
 #define exp2(x) (exp((x) * (ln2c ? ln2c : (ln2c = log(2.0)))))
+
+int qctz(unsigned);
--- a/r_sky.c
+++ b/r_sky.c
@@ -3,14 +3,16 @@
 static int iskyspeed = 8;
 static int iskyspeed2 = 2;
 static float skyspeed2;
-
-float skyspeed, skytime;
-
-pixel_t *r_skysource[2];
-
 static int r_skymade;
-int skyw, skyh;
 
+static void
+skyrecalc(void)
+{
+	skyvars.smask = (skyvars.w - 1)<<16;
+	skyvars.tmask = (skyvars.h - 1)<<16;
+	skyvars.tshift = 16 - qctz(skyvars.w);
+}
+
 /*
 =============
 R_InitSky
@@ -19,34 +21,29 @@
 void R_InitSky (texture_t *mt)
 {
 	int x, y, w, n;
-	pixel_t *src;
+	pixel_t *src, p;
 
 	if(mt == nil){
-		r_skysource[0] = r_skysource[1] = nil;
-		return;
-	}
-
-	src = mt->pixels + mt->offsets[0];
-	w = mt->width;
-	skyh = mt->height;
-	if(w == skyh){ // probably without a mask?
-		skyw = w;
-		n = skyw*skyh;
-		r_skysource[0] = Hunk_Alloc(n*sizeof(pixel_t));
-		r_skysource[1] = r_skysource[0];
-		memmove(r_skysource[0], src, n*sizeof(pixel_t));
-		return;
-	}
-	skyw = w/2;
-	n = skyw*skyh;
-	r_skysource[0] = Hunk_Alloc(n*sizeof(pixel_t)*2);
-	r_skysource[1] = r_skysource[0] + n;
-	for(y=0 ; y<skyh; y++){
-		for(x=0 ; x<skyw; x++){
-			r_skysource[0][y*skyw + x] = src[y*w + skyw + x];
-			r_skysource[1][y*skyw + x] = src[y*w + x];
+		skyvars.w = r_notexture_mip->width;
+		skyvars.h = r_notexture_mip->height;
+		skyvars.source[0] = skyvars.source[1] = r_notexture_mip->pixels;
+	}else{
+		src = mt->pixels + mt->offsets[0];
+		w = mt->width;
+		skyvars.h = mt->height;
+		skyvars.w = w/2;
+		n = skyvars.w*skyvars.h;
+		skyvars.source[0] = Hunk_Alloc(n*sizeof(pixel_t)*2);
+		skyvars.source[1] = skyvars.source[0] + n;
+		for(y=0 ; y<skyvars.h; y++){
+			for(x=0 ; x<skyvars.w; x++){
+				skyvars.source[0][y*skyvars.w + x] = src[y*w + skyvars.w + x];
+				if((p = src[y*w + x]) & 0xffffff)
+					skyvars.source[1][y*skyvars.w + x] = p;
+			}
 		}
 	}
+	skyrecalc();
 }
 
 /*
@@ -59,15 +56,15 @@
 	int		g, s1, s2;
 	float	temp;
 
-	skyspeed = iskyspeed;
+	skyvars.speed = iskyspeed;
 	skyspeed2 = iskyspeed2;
 
 	g = GreatestCommonDivisor (iskyspeed, iskyspeed2);
 	s1 = iskyspeed / g;
 	s2 = iskyspeed2 / g;
-	temp = skyh * s1 * s2;
+	temp = skyvars.h * s1 * s2;
 
-	skytime = cl.time - ((int)(cl.time / temp) * temp);
+	skyvars.time = cl.time - ((int)(cl.time / temp) * temp);
 
 	r_skymade = 0;
 }
--- a/sys_plan9.c
+++ b/sys_plan9.c
@@ -8,6 +8,19 @@
 static const char *disabled[32];
 static int ndisabled;
 
+int
+qctz(unsigned x)
+{
+	unsigned r;
+
+	if(x == 0)
+		r = 32;
+	else
+		for(r = 0; (x & 1) == 0; x >>= 1, r++);
+
+	return r;
+}
+
 bool
 isdisabled(char *s)
 {
--- a/unix/platform.h
+++ b/unix/platform.h
@@ -42,6 +42,8 @@
 #define setmalloctag(p, t) do{USED(p); USED(t);}while(0)
 #define setrealloctag(p, t) do{USED(p); USED(t);}while(0)
 
+#define qctz(x) __builtin_ctz(x)
+
 #ifndef BYTE_ORDER
 #define LITTLE_ENDIAN __LITTLE_ENDIAN
 #define BIG_ENDIAN __BIG_ENDIAN