ref: 891e6af8da9b09659355c663e55330b2b60ef3a9
parent: 81f081e0f4d1de2bb044744c5a295871c65c296d
author: rodri <rgl@antares-labs.eu>
date: Wed Oct 11 06:23:02 EDT 2023
run the simulation in real-time.
--- a/main.c
+++ b/main.c
@@ -6,6 +6,8 @@
#include <keyboard.h>
#include <geometry.h>
+#define HZ2MS(hz) (1000/(hz))
+
enum {
TERMINALV = 40,
BORDER = 100
@@ -43,6 +45,8 @@
Image *pal[NCOLOR];
Flock *flock;
int nbirds;
+int paused;
+QLock pauselk;
void flockseparate(Flock*);
void flockalign(Flock*);
@@ -240,10 +244,10 @@
static Menu menu = { .item = items };
char buf[128];
- switch(menuhit(3, mc, &menu, nil)){
+ switch(menuhit(3, mc, &menu, _screen)){
case NBIRDS:
snprint(buf, sizeof buf, "%d", nbirds);
- enter("nbirds", buf, sizeof buf, mc, kc, nil);
+ enter("nbirds", buf, sizeof buf, mc, kc, _screen);
nbirds = strtol(buf, nil, 10);
case RESET:
srand(time(nil));
@@ -267,12 +271,32 @@
case 'q':
threadexitsall(nil);
case ' ':
- flock->step(flock);
+ if(paused)
+ qunlock(&pauselk);
+ else
+ qlock(&pauselk);
+ paused = !paused;
break;
}
}
void
+runsim(void *)
+{
+ Ioproc *io;
+
+ io = ioproc();
+
+ for(;;){
+ qlock(&pauselk);
+ flock->step(flock);
+ qunlock(&pauselk);
+ redraw();
+ iosleep(io, HZ2MS(24));
+ }
+}
+
+void
usage(void)
{
fprint(2, "usage: %s [-n nbirds]\n", argv0);
@@ -313,6 +337,7 @@
display->locking = 1;
unlockdisplay(display);
+ threadcreate(runsim, nil, mainstacksize);
redraw();
for(;;){