shithub: musw

Download patch

ref: 00f7d8dd3dc47ed3cfa951325e809a92c37341b7
parent: 42ea627853d79a54ef956a831e2f8badff5bfdb8
author: rodri <rgl@antares-labs.eu>
date: Thu Apr 13 07:09:13 EDT 2023

now using PNG files instead of image(6)s. bullet TTL taken into account during stepping.

binary files a/assets/spr/earth.pic /dev/null differ
binary files /dev/null b/assets/spr/earth.png differ
binary files a/assets/spr/intro.pic /dev/null differ
binary files /dev/null b/assets/spr/intro.png differ
binary files a/assets/spr/pulsar.pic /dev/null differ
binary files /dev/null b/assets/spr/pulsar.png differ
--- a/dat.h
+++ b/dat.h
@@ -124,7 +124,7 @@
 struct Bullet
 {
 	Particle;
-	ulong ttl; /* in s */
+	double ttl; /* in s */
 	int fired; /* XXX: |v| != 0 */
 };
 
--- a/fns.h
+++ b/fns.h
@@ -46,6 +46,7 @@
  */
 Sprite *newsprite(Image*, Point, Rectangle, int, ulong);
 Sprite *readsprite(char*, Point, Rectangle, int, ulong);
+Sprite *readpngsprite(char*, Point, Rectangle, int, ulong);
 void delsprite(Sprite*);
 
 /*
--- a/musw.c
+++ b/musw.c
@@ -526,7 +526,7 @@
 	case GSPlaying:
 		drawship(&universe->ships[0], screenb);
 		drawship(&universe->ships[1], screenb);
-		universe->star.spr->draw(universe->star.spr, screenb, subpt(toscreen(universe->star.p), Pt(16,16)));
+		universe->star.spr->draw(universe->star.spr, screenb, subpt(toscreen(universe->star.p), divpt(universe->star.spr->r.max, 2)));
 		break;
 	}
 
@@ -659,9 +659,9 @@
 		sysfatal("readvmodel: %r");
 	universe->ships[0].mdl = needlemdl;
 	universe->ships[1].mdl = wedgemdl;
-	universe->star.spr = readsprite("assets/spr/pulsar.pic", ZP, Rect(0,0,64,64), 9, 50);
+	universe->star.spr = readpngsprite("assets/spr/pulsar.png", ZP, Rect(0,0,64,64), 9, 50);
 
-	intro = readsprite("assets/spr/intro.pic", ZP, Rect(0,0,640,480), 1, 0);
+	intro = readpngsprite("assets/spr/intro.png", ZP, Rect(0,0,640,480), 28, 100);
 
 	gamestates[GSIntro].δ = intro_δ;
 	gamestates[GSConnecting].δ = connecting_δ;
@@ -697,6 +697,7 @@
 
 		gamestate = gamestate->δ(gamestate, &frametime);
 		universe->star.spr->step(universe->star.spr, frametime/1e6);
+		intro->step(intro, frametime/1e6);
 
 		redraw();
 
--- a/sprite.c
+++ b/sprite.c
@@ -3,6 +3,7 @@
 #include <ip.h>
 #include <mp.h>
 #include <libsec.h>
+#include <thread.h>
 #include <draw.h>
 #include <geometry.h>
 #include "dat.h"
@@ -63,6 +64,45 @@
 	if(fd < 0)
 		sysfatal("readsprite: %r");
 	sheet = readimage(display, fd, 1);
+	close(fd);
+
+	return newsprite(sheet, sp, r, nframes, period);
+}
+
+static void
+decproc(void *arg)
+{
+	int fd, *pfd;
+
+	pfd = arg;
+	fd = pfd[2];
+
+	close(pfd[0]);
+	dup(fd, 0);
+	close(fd);
+	dup(pfd[1], 1);
+	close(pfd[1]);
+
+	execl("/bin/png", "png", "-t9", nil);
+	threadexitsall("execl: %r");
+}
+
+Sprite *
+readpngsprite(char *sheetfile, Point sp, Rectangle r, int nframes, ulong period)
+{
+	Image *sheet;
+	int fd, pfd[3];
+
+	if(pipe(pfd) < 0)
+		sysfatal("pipe: %r");
+	fd = open(sheetfile, OREAD);
+	if(fd < 0)
+		sysfatal("readpngsprite: %r");
+	pfd[2] = fd;
+	procrfork(decproc, pfd, mainstacksize, RFFDG|RFNAMEG|RFNOTEG);
+	close(pfd[1]);
+	sheet = readimage(display, pfd[0], 1);
+	close(pfd[0]);
 	close(fd);
 
 	return newsprite(sheet, sp, r, nframes, period);
--- a/todo
+++ b/todo
@@ -1,6 +1,6 @@
 [ ] collision detection
 [✓] toroidal warping
-[ ] respect bullets's ttl
+[✓] respect bullets's ttl
 	[ ] communicate this event to the clients
 	[ ] explode when the time comes
 [ ] fuel consumption
@@ -18,3 +18,5 @@
 [ ] more realistic DEC Type 30 CRT emulation
 	[ ] the right colors (fg: 0x3daaf7, fgblur: 0x0063eb, bg0: 0x79cc3e, bg1: 0x7eba1e)
 	[ ] the right decay function
+[✓] work with PNG files instead of 9 pics
+	> big compressed image(6) files can't be used because of the iounit limits.
--- a/universe.c
+++ b/universe.c
@@ -55,6 +55,7 @@
 			s->rounds[i].p = s->p;
 			s->rounds[i].v = addpt2(s->v, bv);
 			s->rounds[i].θ = s->θ;
+			s->rounds[i].ttl = 5;
 			s->rounds[i].fired++;
 			break;
 		}
@@ -65,7 +66,14 @@
 static void
 universe_step(Universe *u, double Δt)
 {
+	Ship *s;
+	Bullet *b;
+
 	integrate(u, u->t, Δt);
+	for(s = u->ships; s < u->ships+nelem(u->ships); s++)
+		for(b = s->rounds; b < s->rounds+nelem(s->rounds); b++)
+			if(b->fired)
+				b->ttl -= Δt;
 	u->timeacc -= Δt;
 	u->t += Δt;
 }
@@ -96,7 +104,10 @@
 
 	for(s = u->ships; s < u->ships+nelem(u->ships); s++){
 		for(b = s->rounds; b < s->rounds+nelem(s->rounds); b++){
-			warp(b);
+			if(b->fired && b->ttl <= 0)
+				b->fired = 0;
+			if(b->fired)
+				warp(b);
 		}
 		warp(s);
 	}