shithub: qk1

Download patch

ref: d64e9404b80b1428526be60cebc1987a12f65843
parent: 5432e05f810baa5a0d2ac75090345bba33d67d1c
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Thu Nov 9 14:36:42 EST 2023

fix a bunch of stuff and clean up

--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@
 BIN=${DESTDIR}${PREFIX}/bin
 SDL2_CFLAGS=$$(pkg-config --cflags sdl2)
 SDL2_LDFLAGS=$$(pkg-config --libs sdl2)
-CFLAGS?=-O2 -ggdb -Wall -Wextra -Wno-unknown-pragmas -Wno-missing-field-initializers -Wno-implicit-fallthrough
+CFLAGS?=-O2 -g -Wall -Wextra -Wno-unknown-pragmas -Wno-missing-field-initializers -Wno-implicit-fallthrough
 CFLAGS+=-fms-extensions -I3rd/parg -Iunix -I. ${SDL2_CFLAGS}
 LDFLAGS?=
 LDFLAGS+=-lm ${SDL2_LDFLAGS}
--- a/cmd.c
+++ b/cmd.c
@@ -431,7 +431,7 @@
 */
 char	*Cmd_Argv (int arg)
 {
-	if ( (unsigned)arg >= cmd_argc )
+	if (arg >= cmd_argc)
 		return cmd_null_string;
 	return cmd_argv[arg];	
 }
--- a/common.c
+++ b/common.c
@@ -281,19 +281,14 @@
 {
 	static char     string[2048];
 	int             l,c;
-	
-	l = 0;
-	do
-	{
-		c = MSG_ReadChar ();
-		if (c == -1 || c == 0)
+
+	for(l = 0; l < (int)sizeof(string)-1; l++){
+		if((c = MSG_ReadChar()) == -1 || c == 0)
 			break;
 		string[l] = c;
-		l++;
-	} while (l < sizeof(string)-1);
-	
+	}
 	string[l] = 0;
-	
+
 	return string;
 }
 
--- a/d_polyse.c
+++ b/d_polyse.c
@@ -98,12 +98,8 @@
 */
 void D_PolysetDraw (void)
 {
-	spanpackage_t	spans[DPS_MAXSPANS + 1 +
-			((CACHE_SIZE - 1) / sizeof(spanpackage_t)) + 1];
-						// one extra because of cache line pretouching
-
-	a_spans = (spanpackage_t *)
-			(((uintptr)&spans[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
+	static spanpackage_t	spans[DPS_MAXSPANS];
+	a_spans = spans;
 
 	if (r_affinetridesc.drawtype)
 	{
--- a/d_surf.c
+++ b/d_surf.c
@@ -3,8 +3,8 @@
 float           surfscale;
 qboolean        r_cache_thrash;         // set if surface cache is thrashing
 
-int                                     sc_size;
-surfcache_t                     *sc_rover, *sc_base;
+int sc_size;
+surfcache_t *sc_rover, *sc_base;
 
 #define GUARDSIZE       4
 
@@ -111,13 +111,13 @@
 	
 	size = (uintptr)&((surfcache_t *)0)->data[size];
 	size = (size + 3) & ~3;
-	if (size > sc_size)
+	if (size > (uintptr)sc_size)
 		Host_Error("D_SCAlloc: %zud > cache size",size);
 
 // if there is not size bytes after the rover, reset to the start
 	wrapped_this_time = false;
 
-	if ( !sc_rover || (byte *)sc_rover - (byte *)sc_base > sc_size - size)
+	if ( !sc_rover || (intptr)sc_rover-(intptr)sc_base > (intptr)sc_size-(intptr)size)
 	{
 		if (sc_rover)
 		{
@@ -131,7 +131,7 @@
 	if (sc_rover->owner)
 		*sc_rover->owner = nil;
 	
-	while (new->size < size)
+	while ((uintptr)new->size < size)
 	{
 	// free another
 		sc_rover = sc_rover->next;
@@ -191,36 +191,6 @@
 	}
 }
 
-//=============================================================================
-
-// if the num is not a power of 2, assume it will not repeat
-
-int     MaskForNum (int num)
-{
-	if (num==128)
-		return 127;
-	if (num==64)
-		return 63;
-	if (num==32)
-		return 31;
-	if (num==16)
-		return 15;
-	return 255;
-}
-
-int D_log2 (int num)
-{
-	int     c;
-	
-	c = 0;
-	
-	while (num>>=1)
-		c++;
-	return c;
-}
-
-//=============================================================================
-
 /*
 ================
 D_CacheSurface
@@ -244,6 +214,7 @@
 //
 	cache = surface->cachespots[miplevel];
 
+	if(currententity == cl_entities || currententity->model->name[0] == '*')
 	if (cache && !cache->dlight && surface->dlightframe != r_framecount
 			&& cache->texture == r_drawsurf.texture
 			&& cache->lightadj[0] == r_drawsurf.lightadj[0]
--- a/draw.c
+++ b/draw.c
@@ -243,11 +243,9 @@
 	byte	*dest, *source, tbyte;
 	int				v, u;
 
-	if (x < 0 || (unsigned)(x + pic->width) > vid.width || y < 0 ||
-		 (unsigned)(y + pic->height) > vid.height)
-	{
+	if (x < 0 || y < 0 || x+pic->width > vid.width || y+pic->height > vid.height)
 		fatal ("Draw_TransPic: bad coordinates");
-	}
+
 	source = pic->data;
 	dest = vid.buffer + y * vid.rowbytes + x;
 	if(pic->width & 7){	// general
@@ -295,11 +293,9 @@
 	byte	*dest, *source, tbyte;
 	int				v, u;
 
-	if (x < 0 || (unsigned)(x + pic->width) > vid.width || y < 0 ||
-		 (unsigned)(y + pic->height) > vid.height)
-	{
+	if (x < 0 || y < 0 || x+pic->width > vid.width || y+pic->height > vid.height)
 		fatal ("Draw_TransPic: bad coordinates");
-	}	
+
 	source = pic->data;
 	dest = vid.buffer + y * vid.rowbytes + x;
 	if (pic->width & 7){	// general
@@ -369,7 +365,7 @@
 */
 void Draw_ConsoleBackground (int lines)
 {
-	int				x, y, v;
+	int				x, y, v, n;
 	byte			*src, *dest;
 	int				f, fstep;
 	qpic_t			*conback;
@@ -381,7 +377,8 @@
 	sprint (ver, "(9)quake %4.2f", (float)VERSION);
 	dest = conback->data + 320*186 + 320 - 11 - 8*strlen(ver);
 
-	for (x=0 ; x<strlen(ver) ; x++)
+	n = strlen(ver);
+	for (x=0 ; x<n ; x++)
 		Draw_CharToConback (ver[x], dest+(x<<3));
 	dest = vid.conbuffer;
 	for(y=0; y<lines; y++, dest+=vid.conrowbytes){
@@ -392,7 +389,7 @@
 		else{
 			f = 0;
 			fstep = 320 * 0x10000 / vid.conwidth;
-			for(x=0; x<vid.conwidth; x+=4){
+			for(x=0; x<(int)vid.conwidth; x+=4){
 				dest[x] = src[f>>16]; f += fstep;
 				dest[x+1] = src[f>>16]; f += fstep;
 				dest[x+2] = src[f>>16]; f += fstep;
--- a/host_cmd.c
+++ b/host_cmd.c
@@ -440,7 +440,7 @@
 		sprint (text, "%c<%s> ", 1, hostname.string);
 
 	j = sizeof(text) - 2 - strlen(text);  // -2 for /n and null terminator
-	if(strlen(p) > j)
+	if(j < (int)strlen(p))
 		p[j] = 0;
 
 	strcat (text, p);
@@ -504,7 +504,7 @@
 
 // check length & truncate if necessary
 	j = sizeof(text) - 2 - strlen(text);  // -2 for /n and null terminator
-	if(strlen(p) > j)
+	if(j < (int)strlen(p))
 		p[j] = 0;
 
 	strcat (text, p);
--- a/menu.c
+++ b/menu.c
@@ -1139,8 +1139,6 @@
 {"+movedown",		"swim down"}
 };
 
-#define	NUMCOMMANDS	(sizeof(bindnames)/sizeof(bindnames[0]))
-
 int		keys_cursor;
 int		bind_grab;
 
@@ -1214,7 +1212,7 @@
 		M_Print (18, 32, "Enter to change, backspace to clear");
 
 // search for known bindings
-	for (i=0 ; i<NUMCOMMANDS ; i++)
+	for (i=0 ; i<nelem(bindnames) ; i++)
 	{
 		y = 48 + 8*i;
 
@@ -1275,7 +1273,7 @@
 		localsfx ("misc/menu1.wav");
 		keys_cursor--;
 		if (keys_cursor < 0)
-			keys_cursor = NUMCOMMANDS-1;
+			keys_cursor = nelem(bindnames)-1;
 		break;
 
 	case K_DOWNARROW:
@@ -1282,7 +1280,7 @@
 	case K_RIGHTARROW:
 		localsfx ("misc/menu1.wav");
 		keys_cursor++;
-		if (keys_cursor >= NUMCOMMANDS)
+		if (keys_cursor >= nelem(bindnames))
 			keys_cursor = 0;
 		break;
 
--- a/model.c
+++ b/model.c
@@ -1132,7 +1132,7 @@
 // swap all the lumps
 	mod_base = (byte *)header;
 
-	for (i=0 ; i<sizeof(dheader_t)/4 ; i++)
+	for (i=0 ; i<(int)sizeof(dheader_t)/4 ; i++)
 		((int *)header)[i] = LittleLong ( ((int *)header)[i]);
 
 // load into heap
--- a/net.h
+++ b/net.h
@@ -48,7 +48,6 @@
 	CPRUINFO  = 1<<7 | 5,	/* rule[s] val[s] */
 
 	MAX_NET_DRIVERS = 8,
-	HOSTCACHESIZE = 8
 };
 
 typedef struct qsocket_s
--- a/pr_edict.c
+++ b/pr_edict.c
@@ -1042,7 +1042,7 @@
 		crc (((byte *)progs)[i]);
 
 // byte swap the header
-	for (i=0 ; i<sizeof(*progs)/4 ; i++)
+	for (i=0 ; i<(int)sizeof(*progs)/4 ; i++)
 		((int *)progs)[i] = LittleLong ( ((int *)progs)[i] );		
 
 	if (progs->version != PROG_VERSION)
--- a/quakedef.h
+++ b/quakedef.h
@@ -10,9 +10,6 @@
 #define	GAMENAME	"id1"		// directory to look in by default
 
 enum {
-	// !!! if this is changed, it must be changed in d_ifacea.h too !!!
-	CACHE_SIZE = 32, // used to align key data structures
-
 	MAX_DATAGRAM = 1400,		// max length of unreliable message
 	MAX_DATAGRAM_LOCAL = 65000,	// on loopback we don't care
 
--- a/r_alias.c
+++ b/r_alias.c
@@ -673,16 +673,14 @@
 */
 void R_AliasDrawModel (alight_t *plighting)
 {
-	finalvert_t		finalverts[MAXALIASVERTS +
-						((CACHE_SIZE - 1) / sizeof(finalvert_t)) + 1];
-	auxvert_t		auxverts[MAXALIASVERTS];
+	static finalvert_t		finalverts[MAXALIASVERTS];
+	static auxvert_t		auxverts[MAXALIASVERTS];
 
 	r_amodels_drawn++;
 
 // cache align
-	pfinalverts = (finalvert_t *)
-			(((uintptr)&finalverts[0] + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
-	pauxverts = &auxverts[0];
+	pfinalverts = finalverts;
+	pauxverts = auxverts;
 
 	paliashdr = (aliashdr_t *)Mod_Extradata (currententity->model);
 	pmdl = (mdl_t *)((byte *)paliashdr + paliashdr->model);
--- a/r_bsp.c
+++ b/r_bsp.c
@@ -582,7 +582,7 @@
 	model_t *clmodel;
 	nodereject_t *rej;
 	msurface_t *surf;
-	int i;
+	unsigned i;
 
 	currententity = &cl_entities[0];
 	VectorCopy (r_origin, modelorg);
--- a/r_draw.c
+++ b/r_draw.c
@@ -107,9 +107,9 @@
 	
 	// FIXME: build x/yscale into transform?
 		u0 = xcenter + xscale*lzi0*transformed[0];
-		//u0 = clamp(u0, r_refdef.fvrectx_adj, r_refdef.fvrectright_adj);
+		u0 = clamp(u0, r_refdef.fvrectx_adj, r_refdef.fvrectright_adj);
 		v0 = ycenter - yscale*lzi0*transformed[1];
-		//v0 = clamp(v0, r_refdef.fvrecty_adj, r_refdef.fvrectbottom_adj);
+		v0 = clamp(v0, r_refdef.fvrecty_adj, r_refdef.fvrectbottom_adj);
 		ceilv0 = (int)ceil(v0);
 	}
 
@@ -125,9 +125,9 @@
 	r_lzi1 = 1.0 / transformed[2];
 
 	r_u1 = xcenter + xscale*r_lzi1*transformed[0];
-	//r_u1 = clamp(r_u1, r_refdef.fvrectx_adj, r_refdef.fvrectright_adj);
+	r_u1 = clamp(r_u1, r_refdef.fvrectx_adj, r_refdef.fvrectright_adj);
 	r_v1 = ycenter - yscale*r_lzi1*transformed[1];
-	//r_v1 = clamp(r_v1, r_refdef.fvrecty_adj, r_refdef.fvrectbottom_adj);
+	r_v1 = clamp(r_v1, r_refdef.fvrecty_adj, r_refdef.fvrectbottom_adj);
 	if (r_lzi1 > lzi0)
 		lzi0 = r_lzi1;
 	if (lzi0 > r_nearzi)	// for mipmap finding
@@ -185,15 +185,9 @@
 
 	edge->u_step = u_step*0x100000;
 	edge->u = u*0x100000 + 0xFFFFF;
+	/* with all the clamping before this one shouldn't be needed but kept to be 100% sure */
+	edge->u = clamp(edge->u, r_refdef.vrect_x_adj_shift20, r_refdef.vrectright_adj_shift20);
 
-// we need to do this to avoid stepping off the edges if a very nearly
-// horizontal edge is less than epsilon above a scan, and numeric error causes
-// it to incorrectly extend to the scan, and the extension of the line goes off
-// the edge of the screen
-// FIXME: is this actually needed?
-// FIXME(sigrid): looks like it isn't.
-	//edge->u = clamp(edge->u, r_refdef.vrect_x_adj_shift20, r_refdef.vrectright_adj_shift20);
-
 //
 // sort the edge in normally
 //
@@ -407,8 +401,7 @@
 			{
 				if (r_pedge->cachededgeoffset & FULLY_CLIPPED_CACHED)
 				{
-					if ((r_pedge->cachededgeoffset & FRAMECOUNT_MASK) ==
-						r_framecount)
+					if ((int)(r_pedge->cachededgeoffset & FRAMECOUNT_MASK) == r_framecount)
 					{
 						r_lastvertvalid = false;
 						continue;
@@ -416,10 +409,8 @@
 				}
 				else
 				{
-					if ((((uintptr)edge_p - (uintptr)r_edges) >
-						 r_pedge->cachededgeoffset) &&
-						(((edge_t *)((uintptr)r_edges +
-						 r_pedge->cachededgeoffset))->owner == r_pedge))
+					if ((((uintptr)edge_p - (uintptr)r_edges) > r_pedge->cachededgeoffset) &&
+						(((edge_t *)((uintptr)r_edges + r_pedge->cachededgeoffset))->owner == r_pedge))
 					{
 						R_EmitCachedEdge ();
 						r_lastvertvalid = false;
@@ -451,8 +442,7 @@
 			{
 				if (r_pedge->cachededgeoffset & FULLY_CLIPPED_CACHED)
 				{
-					if ((r_pedge->cachededgeoffset & FRAMECOUNT_MASK) ==
-						r_framecount)
+					if ((int)(r_pedge->cachededgeoffset & FRAMECOUNT_MASK) == r_framecount)
 					{
 						r_lastvertvalid = false;
 						continue;
@@ -462,10 +452,8 @@
 				{
 				// it's cached if the cached edge is valid and is owned
 				// by this medge_t
-					if ((((uintptr)edge_p - (uintptr)r_edges) >
-						 r_pedge->cachededgeoffset) &&
-						(((edge_t *)((uintptr)r_edges +
-						 r_pedge->cachededgeoffset))->owner == r_pedge))
+					if ((((uintptr)edge_p - (uintptr)r_edges) > r_pedge->cachededgeoffset) &&
+						(((edge_t *)((uintptr)r_edges + r_pedge->cachededgeoffset))->owner == r_pedge))
 					{
 						R_EmitCachedEdge ();
 						r_lastvertvalid = false;
@@ -555,8 +543,8 @@
 	mplane_t	*pplane;
 	float		distinv;
 	vec3_t		p_normal;
-	medge_t		tedge;
 	clipplane_t	*pclip;
+	static medge_t tedge;
 
 // skip out if no more surfs
 	if (surface_p >= surf_max)
@@ -722,6 +710,8 @@
 	while (pclip)
 	{
 		lastvert = lnumverts - 1;
+		assert(vertpage < 2);
+		assert(lastvert < 100);
 		lastdist = DotProduct (verts[vertpage][lastvert].position,
 							   pclip->normal) - pclip->dist;
 
--- a/r_edge.c
+++ b/r_edge.c
@@ -54,8 +54,6 @@
 */
 void R_BeginEdgeFrame (void)
 {
-	int		v, n;
-
 	r_edges = Arr_AllocExtra(r_edges, &r_numallocatededges, r_outofedges);
 	edge_p = r_edges;
 	edge_max = &r_edges[r_numallocatededges];
@@ -75,10 +73,8 @@
 	surfaces[1].key = 0x7FFFFFFF;
 	r_currentkey = 0;
 
-	v = r_refdef.vrect.y;
-	n = r_refdef.vrectbottom - v;
-	memset(newedges+v, 0, n*sizeof(*newedges));
-	memset(removeedges+v, 0, n*sizeof(*removeedges));
+	memset(newedges, 0, sizeof(newedges));
+	memset(removeedges, 0, sizeof(removeedges));
 }
 
 
@@ -131,7 +127,6 @@
 */
 void R_RemoveEdges (edge_t *pedge)
 {
-
 	do
 	{
 		pedge->next->prev = pedge->prev;
@@ -171,7 +166,7 @@
 		if (pedge->u < pedge->prev->u)
 			goto pushback;
 		pedge = pedge->next;
-			
+
 		goto nextedge;		
 		
 pushback:
@@ -537,8 +532,8 @@
 	surf_t	*s;
 
 	r_basespans = Arr_AllocExtra(r_basespans, &r_numallocatedbasespans, r_outofspans);
-	basespan_p = (espan_t *)
-			((uintptr)(r_basespans + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
+	basespan_p = (espan_t *)r_basespans;
+	assert(r_numallocatedbasespans > r_refdef.vrect.width);
 	max_span_p = &basespan_p[r_numallocatedbasespans - r_refdef.vrect.width];
 	span_p = basespan_p;
 	r_outofspans = 0;
@@ -567,7 +562,7 @@
 	edge_aftertail.prev = &edge_tail;
 
 // FIXME: do we need this now that we clamp x in r_draw.c?
-	edge_sentinel.u = 2000 << 24;		// make sure nothing sorts past this
+	edge_sentinel.u = 0x7d << 24;		// make sure nothing sorts past this
 	edge_sentinel.prev = &edge_aftertail;
 
 //	
--- a/r_light.c
+++ b/r_light.c
@@ -49,7 +49,7 @@
 	mplane_t	*splitplane;
 	float		dist;
 	msurface_t	*surf;
-	int			i;
+	unsigned	i;
 	
 	if (node->contents < 0)
 		return;
@@ -125,7 +125,7 @@
 	vec3_t		mid;
 	msurface_t	*surf;
 	int			s, t, ds, dt;
-	int			i;
+	unsigned	i;
 	mtexinfo_t	*tex;
 	byte		*lightmap;
 	unsigned	scale;
--- a/r_main.c
+++ b/r_main.c
@@ -217,10 +217,10 @@
 		cl.worldmodel->leafs[i].efrags = nil;
 
 	r_numallocatedbasespans = MAXSPANS;
-	r_basespans = Hunk_Alloc(r_numallocatedbasespans * sizeof(espan_t) + CACHE_SIZE);
 	r_cnumsurfs = MAXSURFACES;
-	surfaces = Hunk_Alloc(r_cnumsurfs * sizeof *surfaces);
 	r_numallocatededges = MAXEDGES;
+	r_basespans = Hunk_Alloc(r_numallocatedbasespans * sizeof(espan_t));
+	surfaces = Hunk_Alloc(r_cnumsurfs * sizeof *surfaces);
 	r_edges = Hunk_Alloc(r_numallocatededges * sizeof *r_edges);
 
 	r_viewleaf = nil;
@@ -747,7 +747,6 @@
 
 	R_DrawViewModel ();
 	R_DrawParticles ();
-
 	r_drawflags = DRAW_BLEND;
 	R_EdgeDrawing();
 	// FIXME(sigrid): these need to be sorted and drawn back to front
--- a/sv_move.c
+++ b/sv_move.c
@@ -297,7 +297,7 @@
 	}
 
 // try other directions
-	if ( ((rand()&3) & 1) ||  abs(deltay)>abs(deltax))
+	if ( ((rand()&3) & 1) ||  fabs(deltay)>fabs(deltax))
 	{
 		tdir=d[1];
 		d[1]=d[2];
--- a/unix/net_udp.c
+++ b/unix/net_udp.c
@@ -174,6 +174,7 @@
 
 int UDP_Connect (Addr *addr)
 {
+	USED(addr);
 	return 0;
 }
 
--- a/unix/qk1.c
+++ b/unix/qk1.c
@@ -2,6 +2,7 @@
 #include "parg.h"
 #include <time.h>
 #include <errno.h>
+#include <fenv.h>
 
 char *game;
 int debug;
@@ -10,9 +11,12 @@
 char *
 lerr(void)
 {
+	static char lasterrcopy[256];
+
 	if(*lasterr == 0 && errno != 0)
 		return strerror(errno);
-	return lasterr;
+	strcpy(lasterrcopy, lasterr);
+	return lasterrcopy;
 }
 
 int
@@ -121,10 +125,12 @@
 		}
 	}
 
-	srand(getpid());
+	srand(nanosec() + time(nil));
 
 	paths[1] = strdup(va("%s/.quake", getenv("HOME")));
 	Host_Init(nargs, argv, paths);
+
+	fesetround(FE_TOWARDZERO);
 
 	t = dtime() - 1.0 / Fpsmax;
 	for(;;){
--- a/unix/u.h
+++ b/unix/u.h
@@ -20,15 +20,8 @@
 typedef uint16_t u16int;
 typedef int32_t s32int;
 typedef uint32_t u32int;
+typedef intptr_t intptr;
 typedef uintptr_t uintptr;
-
-#define OREAD O_RDONLY
-#define OWRITE O_WRONLY
-#define OCEXEC O_CLOEXEC
-#define OTRUNC O_TRUNC
-#define AEXIST F_OK
-
-#define seek lseek
 
 #define nil NULL
 #define USED(x) (void)(x)
--- a/wad.c
+++ b/wad.c
@@ -48,7 +48,7 @@
 {
 	lumpinfo_t		*lump_p;
 	wadinfo_t		*header;
-	unsigned		i;
+	int				i;
 	int				infotableofs;
 
 	wad_base = loadhunklmp(filename, nil);