ref: d85705bf67be2a23e3d928f9670732be5484f958
parent: 97ec1b6d99fa48e46d9ba15ddbf434df2008dfdb
author: rodri <rgl@antares-labs.eu>
date: Tue Jul 27 09:55:18 EDT 2021
first stages of a full-featured client.
--- 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;
--- a/fns.h
+++ b/fns.h
@@ -1,3 +1,5 @@
+#define FPS2MS(fps) (1000/(fps))
+
/*
* alloc
*/
--- a/musw.c
+++ b/musw.c
@@ -1,18 +1,97 @@
#include <u.h>
#include <libc.h>
#include <thread.h>
+#include <draw.h>
+#include <mouse.h>
+#include <keyboard.h>
#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);
@@ -19,31 +98,58 @@
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
usage(void)
{
fprint(2, "usage: %s [-d] server\n", argv0);
@@ -55,6 +161,8 @@
{
char *server;
int fd;
+ Mousectl *mc;
+ Ioproc *io;
ARGBEGIN{
case 'd':
@@ -67,11 +175,27 @@
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));
+ }
}
--- a/muswd.c
+++ b/muswd.c
@@ -169,8 +169,10 @@
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 @@
lobby = newlobby();
inittheparty();
- threadcreate(threadctl, nil, 4096);
+ threadcreate(threadC2, nil, 4096);
threadcreate(threadlisten, adir, 4096);
threadcreate(threadsim, nil, 4096);
threadexits(nil);