shithub: dmap

Download patch

ref: 50673d39401ce2e58b03f11dfde9b6759655e764
parent: c183f656495c07f3c600a09185073a00076c2e62
author: qwx <qwx@sciops.net>
date: Sun Aug 29 15:36:19 EDT 2021

spawn dbsp from dmap, move common code

--- a/bsp.c
+++ b/bsp.c
@@ -40,14 +40,6 @@
 
 static float bbox[4];
 
-void *
-erealloc(void *p, ulong n)
-{
-	if((p = realloc(p, n)) == nil)
-		sysfatal("realloc: %r");
-	return p;
-}
-
 int
 subsect(Seg *s)
 {
@@ -71,30 +63,6 @@
 	return 0;
 }
 
-static int
-fsign(float f)
-{
-	return f < 0. ? -1 : f > 0.;
-}
-
-static float
-fround(float x)
-{
-	if(x > 0.){
-		if(x - (int)x < 0.1)
-			return (int)x;
-		else if(x - (int)x > 0.9)
-			return (int)x + 1;
-		else
-			return x;
-	}
-	if((int)x - x < 0.1)
-		return (int)x;
-	else if((int)x - x > 0.9)
-		return (int)x - 1;
-	return x;
-}
-
 static void
 divline(Divline *d, Seg *s)
 {
@@ -220,8 +188,8 @@
 addnode(Seg **ss, int *n, Seg *s, Seg *p)
 {
 	if(s - *ss >= *n){
+		*ss = erealloc(*ss, (*n+64) * sizeof **ss, *n * sizeof **ss);
 		*n += 64;
-		*ss = erealloc(*ss, *n * sizeof **ss);
 	}
 	*s = *p;
 }
--- /dev/null
+++ b/dbsp.c
@@ -1,0 +1,30 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include "dat.h"
+#include "fns.h"
+
+static void
+usage(void)
+{
+	fprint(2, "%s [mapdir]\n", argv0);
+	threadexitsall("usage");
+}
+
+void
+threadmain(int argc, char *argv[])
+{
+	Mouse m;
+	Point mo;
+	double f, vx, vy;
+	Rune r;
+
+	ARGBEGIN{
+	default:
+		usage();
+	}ARGEND
+	load(*argv);
+	buildnodes();
+	threadexitsall(nil);
+}
--- a/dmap.c
+++ b/dmap.c
@@ -32,17 +32,6 @@
 static Keyboardctl *kc;
 static Mousectl *mc;
 
-void *
-emalloc(ulong n)
-{
-	void *p;
-
-	if((p = mallocz(n, 1)) == nil)
-		sysfatal("mallocz: %r");
-	setmalloctag(p, getcallerpc(&n));
-	return p;
-}
-
 Image *
 eallocimage(Rectangle r, ulong chan, int repl, ulong c)
 {
--- a/fns.h
+++ b/fns.h
@@ -1,3 +1,7 @@
 void	buildnodes(void);
 void	load(char*);
+int	fsign(float);
+float	fround(float);
 void*	emalloc(ulong);
+void*	erealloc(void*, ulong, ulong);
+vlong	filelen(int);
--- a/fs.c
+++ b/fs.c
@@ -26,20 +26,6 @@
 
 #define	PBIT32(p,v)	(p)[0]=(v);(p)[1]=(v)>>8;(p)[2]=(v)>>16;(p)[3]=(v)>>24
 
-static vlong
-filelen(int fd)
-{
-	vlong l;
-	Dir *d;
-
-	d = dirfstat(fd);
-	if(d == nil)
-		sysfatal("filelen: %r");
-	l = d->length;
-	free(d);
-	return l;
-}
-
 static void
 loadthings(char *f)
 {
--- a/mkfile
+++ b/mkfile
@@ -1,12 +1,16 @@
 </$objtype/mkfile
 
-BIN=$home/bin/$objtype
-TARG=dmap
-OFILES=\
-	bsp.$O\
-	dmap.$O\
-	fs.$O\
+BIN=/$objtype/bin/games
+TARG=\
+	dbsp\
+	dmap\
 
 HFILES=dat.h fns.h
 
-</sys/src/cmd/mkone
+</sys/src/cmd/mkmany
+
+$O.dbsp: dbsp.$O bsp.$O fs.$O util.$O
+	$LD -o $target $prereq
+
+$O.dmap: dmap.$O fs.$O util.$O
+	$LD -o $target $prereq
--- /dev/null
+++ b/util.c
@@ -1,0 +1,65 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include "dat.h"
+#include "fns.h"
+
+int
+fsign(float f)
+{
+	return f < 0. ? -1 : f > 0.;
+}
+
+float
+fround(float x)
+{
+	if(x > 0.){
+		if(x - (int)x < 0.1)
+			return (int)x;
+		else if(x - (int)x > 0.9)
+			return (int)x + 1;
+		else
+			return x;
+	}
+	if((int)x - x < 0.1)
+		return (int)x;
+	else if((int)x - x > 0.9)
+		return (int)x - 1;
+	return x;
+}
+
+vlong
+filelen(int fd)
+{
+	vlong l;
+	Dir *d;
+
+	d = dirfstat(fd);
+	if(d == nil)
+		sysfatal("filelen: %r");
+	l = d->length;
+	free(d);
+	return l;
+}
+
+void *
+erealloc(void *p, ulong n, ulong oldn)
+{
+	if((p = realloc(p, n)) == nil)
+		sysfatal("realloc: %r");
+	setrealloctag(p, getcallerpc(&p));
+	if(n > oldn)
+		memset((uchar *)p + oldn, 0, n - oldn);
+	return p;
+}
+
+void *
+emalloc(ulong n)
+{
+	void *p;
+
+	if((p = mallocz(n, 1)) == nil)
+		sysfatal("emalloc: %r");
+	setmalloctag(p, getcallerpc(&n));
+	return p;
+}