aboutsummaryrefslogtreecommitdiff
path: root/muswd.c
diff options
context:
space:
mode:
Diffstat (limited to 'muswd.c')
-rw-r--r--muswd.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/muswd.c b/muswd.c
index 983470b..4ebdb5e 100644
--- a/muswd.c
+++ b/muswd.c
@@ -6,8 +6,12 @@
int debug;
+GameState state;
double t, Δt;
+int *conns;
+int nconns;
+
static long
_iolisten(va_list *arg)
{
@@ -28,7 +32,7 @@ iolisten(Ioproc *io, char *adir, char *ldir)
void
threadlisten(void *arg)
{
- int lcfd;
+ int lcfd, dfd;
char *adir, ldir[40];
Ioproc *io;
@@ -45,27 +49,37 @@ threadlisten(void *arg)
* handle connection and allocate user on a seat, ready
* to play
*/
+ dfd = accept(lcfd, ldir);
+ if(dfd < 0){
+ fprint(2, "accept: %r\n");
+ continue;
+ }
+
+ conns = erealloc(conns, ++nconns*sizeof(*conns));
+ conns[nconns-1] = dfd;
}
}
void
resetsim(void)
{
- memset(&state, 0, sizeof(GameState));
- state.x = 100;
- state.stats.update = statsupdate;
t = 0;
+ memset(&state, 0, sizeof state);
+ state.x = 100;
}
void
threadsim(void *)
{
+ int i;
uvlong then, now;
double frametime, timeacc;
+ Ioproc *io;
Δt = 0.01;
then = nanosec();
timeacc = 0;
+ io = ioproc();
resetsim();
@@ -75,13 +89,16 @@ threadsim(void *)
then = now;
timeacc += frametime/1e9;
+ for(i = 0; i < nconns; i++)
+ fprint(conns[i], "state: x=%g v=%g\n", state.x, state.v);
+
while(timeacc >= Δt){
integrate(&state, t, Δt);
timeacc -= Δt;
t += Δt;
}
- sleep(66);
+ iosleep(io, FPS2MS(1));
}
}
@@ -112,7 +129,7 @@ threadmain(int argc, char *argv[])
if(acfd < 0)
sysfatal("announce: %r");
- threadcreate(threadlisten, adir, 1024);
- threadcreate(threadsim, nil, 8192);
+ threadcreate(threadlisten, adir, 4096);
+ threadcreate(threadsim, nil, 4096);
threadexits(nil);
}