From d85705bf67be2a23e3d928f9670732be5484f958 Mon Sep 17 00:00:00 2001 From: rodri Date: Tue, 27 Jul 2021 13:55:18 +0000 Subject: first stages of a full-featured client. --- dat.h | 2 - fns.h | 2 + musw.c | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- muswd.c | 6 ++- 4 files changed, 149 insertions(+), 23 deletions(-) diff --git a/dat.h b/dat.h index 9ea1f7f..643216e 100644 --- a/dat.h +++ b/dat.h @@ -1,5 +1,3 @@ -#define FPS2MS(fps) (1000/(fps)) - typedef struct GameState GameState; typedef struct Derivative Derivative; typedef struct Conn Conn; diff --git a/fns.h b/fns.h index a925117..38ce644 100644 --- a/fns.h +++ b/fns.h @@ -1,3 +1,5 @@ +#define FPS2MS(fps) (1000/(fps)) + /* * alloc */ diff --git a/musw.c b/musw.c index 1b40264..feba719 100644 --- a/musw.c +++ b/musw.c @@ -1,46 +1,152 @@ #include #include #include +#include +#include +#include #include "dat.h" #include "fns.h" +enum { + K↑, + K←, + K→, + Kfire, + Khyper, + Kquit, + NKEYS +}; + +Rune keys[NKEYS] = { + [K↑] Kup, + [K←] Kleft, + [K→] Kright, + [Kfire] ' ', + [Khyper] 'h', + [Kquit] 'q' +}; +ulong kup, kdown; + +typedef struct Ball Ball; +struct Ball +{ + double x, v; +}; + +Ball bouncer; int debug; void -threadnetin(void *arg) +kbdproc(void *) +{ + Rune r, *k; + char buf[128], *s; + int fd, n; + + threadsetname("kbdproc"); + + if((fd = open("/dev/kbd", OREAD)) < 0) + sysfatal("kbdproc: %r"); + + memset(buf, 0, sizeof buf); + + for(;;){ + if(buf[0] != 0){ + n = strlen(buf)+1; + memmove(buf, buf+n, sizeof(buf)-n); + } + if(buf[0] == 0){ + if((n = read(fd, buf, sizeof(buf)-1)) <= 0) + break; + buf[n-1] = 0; + buf[n] = 0; + } + if(buf[0] == 'c'){ + if(utfrune(buf, Kdel)){ + close(fd); + threadexitsall(nil); + } + } + if(buf[0] != 'k' && buf[0] != 'K') + continue; + s = buf+1; + kdown = 0; + while(*s){ + s += chartorune(&r, s); + for(k = keys; k < keys+NKEYS; k++) + if(r == *k){ + kdown |= 1 << k-keys; + break; + } + } + kup = ~kdown; + + if(debug) + fprint(2, "kup\t%lub\nkdown\t%lub\n", kup, kdown); + } +} + +void +threadnetrecv(void *arg) { uchar buf[256]; int fd, n; - double x, v; Ioproc *io; fd = *((int*)arg); io = ioproc(); while((n = ioread(io, fd, buf, sizeof buf)) > 0){ - unpack(buf, n, "dd", &x, &v); - n = snprint((char *)buf, sizeof buf, "state: x=%g v=%g\n", x, v); - if(iowrite(io, 1, buf, n) != n) - fprint(2, "iowrite: %r\n"); + unpack(buf, n, "dd", &bouncer.x, &bouncer.v); + + if(debug) + fprint(2, "bouncer [%g %g]\n", bouncer.x, bouncer.v); } closeioproc(io); } +void resize(void); + void -threadnetout(void *arg) +threadresize(void *arg) { - char buf[256]; - int fd, n; - Ioproc *io; + Mousectl *mc; + Alt a[3]; - fd = *((int*)arg); - io = ioproc(); + mc = arg; + a[0].op = CHANRCV; a[0].c = mc->c; a[0].v = &mc->Mouse; + a[1].op = CHANRCV; a[1].c = mc->resizec; a[1].v = nil; + a[2].op = CHANEND; - while((n = ioread(io, 0, buf, sizeof buf)) > 0) - if(iowrite(io, fd, buf, n) != n) - fprint(2, "iowrite: %r\n"); - closeioproc(io); + for(;;) + if(alt(a) == 1) + resize(); +} + +void +redraw(void) +{ + lockdisplay(display); + + draw(screen, screen->r, display->black, nil, ZP); + fillellipse(screen, addpt(screen->r.min,Pt(Dx(screen->r)/2,Dy(screen->r)/2+bouncer.x)), 2, 2, display->white, ZP); + + flushimage(display, 1); + unlockdisplay(display); +} + +void +resize(void) +{ + if(debug) + fprint(2, "resizing\n"); + + lockdisplay(display); + if(getwindow(display, Refnone) < 0) + sysfatal("resize failed"); + unlockdisplay(display); + redraw(); } void @@ -55,6 +161,8 @@ threadmain(int argc, char *argv[]) { char *server; int fd; + Mousectl *mc; + Ioproc *io; ARGBEGIN{ case 'd': @@ -67,11 +175,27 @@ threadmain(int argc, char *argv[]) usage(); server = argv[0]; + if(newwindow("-dx 640 -dy 480") < 0) + sysfatal("newwindow: %r"); + if(initdraw(nil, nil, nil) < 0) + sysfatal("initdraw: %r"); + if((mc = initmouse(nil, screen)) == nil) + sysfatal("initmouse: %r"); + display->locking = 1; + unlockdisplay(display); + + proccreate(kbdproc, nil, 4096); + fd = dial(server, nil, nil, nil); if(fd < 0) sysfatal("dial: %r"); - threadcreate(threadnetin, &fd, 4096); - threadcreate(threadnetout, &fd, 4096); - threadexits(nil); + threadcreate(threadnetrecv, &fd, 4096); + threadcreate(threadresize, mc, 4096); + + io = ioproc(); + for(;;){ + redraw(); + iosleep(io, FPS2MS(30)); + } } diff --git a/muswd.c b/muswd.c index b7cbbbd..cf0009e 100644 --- a/muswd.c +++ b/muswd.c @@ -169,8 +169,10 @@ fprintstate(int fd) state.x, state.v); } + +/* Command & Control */ void -threadctl(void *) +threadC2(void *) { int fd, pfd[2], n, ncmdargs; char buf[256], *usr, *cmdargs[2]; @@ -243,7 +245,7 @@ threadmain(int argc, char *argv[]) lobby = newlobby(); inittheparty(); - threadcreate(threadctl, nil, 4096); + threadcreate(threadC2, nil, 4096); threadcreate(threadlisten, adir, 4096); threadcreate(threadsim, nil, 4096); threadexits(nil); -- cgit v1.2.3