From d6be49085c76bbea87b7d7c61455b93883669bf1 Mon Sep 17 00:00:00 2001 From: rodri Date: Tue, 16 Jan 2024 17:09:14 +0000 Subject: rewrite it using libgeometry. --- main.c | 72 +++++++++++++++++++++++++++++++----------------------------------- 1 file changed, 34 insertions(+), 38 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index a66481e..1547898 100644 --- a/main.c +++ b/main.c @@ -4,18 +4,18 @@ #include #include #include +#include #include "dat.h" #include "fns.h" Mousectl *mc; Keyboardctl *kc; Channel *scrsync; -Point orig; -Vector basis; +RFrame worldrf; Projectile ball; double t0, Δt; double v0; -Vector target; +Point2 target; char stats[SLEN][64]; Image *statc; @@ -34,15 +34,16 @@ emalloc(ulong n) } Point -toscreen(Vector p) +toscreen(Point2 p) { - return addpt(orig, Pt(p.x*basis.x, p.y*basis.y)); + p = invrframexform(p, worldrf); + return Pt(p.x, p.y); } -Vector +Point2 fromscreen(Point p) { - return Vec((p.x-screen->r.min.x)*M2PIX, (screen->r.max.y-p.y)*M2PIX, 1); + return rframexform(Pt2(p.x, p.y, 1), worldrf); } void @@ -50,7 +51,7 @@ drawstats(void) { int i; - snprint(stats[Svel], sizeof(stats[Svel]), "v: %gm/s", hypot(ball.v.x, ball.v.y)); + snprint(stats[Svel], sizeof(stats[Svel]), "v: %gm/s", vec2len(ball.v)); snprint(stats[Sdeltax], sizeof(stats[Sdeltax]), "Δx: %gm", target.x-ball.p.x); for(i = 0; i < nelem(stats); i++) stringn(screen, addpt(screen->r.min, Pt(10, font->height*i+1)), statc, ZP, font, stats[i], sizeof(stats[i])); @@ -62,7 +63,7 @@ redraw(void) lockdisplay(display); draw(screen, screen->r, display->black, nil, ZP); fillellipse(screen, toscreen(ball.p), 2, 2, display->white, ZP); - line(screen, toscreen(Vec(ball.p.x, 0, 1)), toscreen(target), 0, 0, 1, statc, ZP); + line(screen, toscreen(Pt2(ball.p.x, 0, 1)), toscreen(target), 0, 0, 1, statc, ZP); drawstats(); flushimage(display, 1); unlockdisplay(display); @@ -106,8 +107,8 @@ rmb(void) switch(menuhit(3, mc, &menu, nil)){ case RST: - ball.p = Vec((2+1)*M2PIX, (2+1)*M2PIX, 1); - ball.v = Vec(0, 0, 1); + ball.p = Pt2((2+1)*M2PIX,(2+1)*M2PIX,1); + ball.v = Vec2(0,0); break; case QUIT: threadexitsall(nil); @@ -117,19 +118,19 @@ rmb(void) void mouse(void) { - Vector p; + Point2 p; double θ, dist, eta; if(ball.p.y <= (2+1)*M2PIX){ - p = subvec(fromscreen(mc->xy), ball.p); + p = subpt2(fromscreen(mc->xy), ball.p); θ = atan2(p.y, p.x); snprint(stats[Stheta], sizeof(stats[Stheta]), "θ: %g°", θ*180/PI); dist = v0*v0*sin(2*θ)/Eg; - target = Vec(ball.p.x+dist, 0, 1); + target = Pt2(ball.p.x+dist, 0, 1); eta = 2*v0*sin(θ)/Eg; snprint(stats[Seta], sizeof(stats[Seta]), "eta: %gs", eta); if((mc->buttons & 1) != 0) - ball.v = Vec(v0*cos(θ), v0*sin(θ), 1); + ball.v = Vec2(v0*cos(θ), v0*sin(θ)); } if((mc->buttons & 2) != 0) mmb(); @@ -153,7 +154,7 @@ resized(void) lockdisplay(display); if(getwindow(display, Refnone) < 0) fprint(2, "can't reattach to window\n"); - orig = Pt(screen->r.min.x, screen->r.max.y); + worldrf.p = Pt2(screen->r.min.x, screen->r.max.y, 1); unlockdisplay(display); redraw(); } @@ -163,20 +164,10 @@ scrsyncproc(void *) { for(;;){ send(scrsync, nil); - sleep(SEC/FPS); + sleep(HZ2MS(60)); } } -#pragma varargck type "V" Vector; -int -Vfmt(Fmt *f) -{ - Vector v; - - v = va_arg(f->args, Vector); - return fmtprint(f, "(%g %g)", v.x, v.y); -} - void usage(void) { @@ -189,7 +180,7 @@ threadmain(int argc, char *argv[]) { Rune r; - fmtinstall('V', Vfmt); + GEOMfmtinstall(); ARGBEGIN{ default: usage(); }ARGEND; @@ -205,18 +196,23 @@ threadmain(int argc, char *argv[]) statc = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DYellow); if(statc == nil) sysfatal("allocimage: %r"); - orig = Pt(screen->r.min.x, screen->r.max.y); - basis = Vec(PIX2M, -PIX2M, 1); - ball.p = Vec((2+1)*M2PIX, (2+1)*M2PIX, 1); - ball.v = Vec(0, 0, 1); + + worldrf.p = Pt2(screen->r.min.x, screen->r.max.y, 1); + worldrf.bx = Vec2(PIX2M,0); + worldrf.by = Vec2(0,-PIX2M); + + ball.p = Pt2((2+1)*M2PIX,(2+1)*M2PIX,1); + ball.v = Vec2(0, 0); ball.mass = 106000; v0 = 1640; /* Paris Gun's specs */ + scrsync = chancreate(1, 0); display->locking = 1; unlockdisplay(display); - proccreate(scrsyncproc, 0, STACK); - t0 = nsec(); + proccreate(scrsyncproc, 0, mainstacksize); + + t0 = nsec(); for(;;){ Alt a[] = { {mc->c, &mc->Mouse, CHANRCV}, @@ -232,12 +228,12 @@ threadmain(int argc, char *argv[]) case 3: redraw(); break; } Δt = (nsec()-t0)/1e9; - ball.v = addvec(ball.v, mulvec(Vec(0, -Eg, 1), Δt)); - ball.p = addvec(ball.p, mulvec(ball.v, Δt)); - snprint(stats[Spos], sizeof(stats[Spos]), "p: %V", ball.p); + ball.v = addpt2(ball.v, mulpt2(Vec2(0,-Eg), Δt)); + ball.p = addpt2(ball.p, mulpt2(ball.v, Δt)); + snprint(stats[Spos], sizeof(stats[Spos]), "p: %v", ball.p); if(ball.p.y <= (2+1)*M2PIX){ ball.p.y = (2+1)*M2PIX; - ball.v = Vec(0, 0, 1); + ball.v = Vec2(0,0); } t0 += Δt*1e9; } -- cgit v1.2.3