Skip to content

Commit

Permalink
[cmds] Add accurate train timing to sl
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Sep 15, 2023
1 parent 69a1c08 commit f6969ae
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion elkscmd/tui/sl.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "curses.h"
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "sl.h"

void add_smoke(int y, int x);
Expand All @@ -56,6 +58,7 @@ int LOGO = 0;
int FLY = 0;
int C51 = 0;
int SLOW = 0;
int FAST = 0;

int my_mvaddstr(int y, int x, char *str)
{
Expand Down Expand Up @@ -84,11 +87,29 @@ void option(char *str)
case 'l': LOGO = 1; break;
case 'c': C51 = 1; break;
case 's': SLOW = 1; break;
case 'f': FAST = 1; break;
default: break;
}
}
}

void cycle(unsigned long usecs)
{
long elapsed;
struct timeval now;
static long start;

if (usecs == 0) {
gettimeofday(&now, NULL);
start = now.tv_sec * 1000000L + now.tv_usec;
return;
}
gettimeofday(&now, NULL);
elapsed = now.tv_sec * 1000000L + now.tv_usec - start;
if (elapsed < usecs)
usleep(usecs - elapsed);
}

int main(int argc, char *argv[])
{
int x, i;
Expand All @@ -107,6 +128,9 @@ int main(int argc, char *argv[])
scrollok(stdscr, FALSE);

for (x = COLS - 1; ; --x) {
if (!FAST && !SLOW) {
cycle(0);
}
if (LOGO == 1) {
if (add_sl(x) == ERR) break;
}
Expand All @@ -117,7 +141,9 @@ int main(int argc, char *argv[])
if (add_D51(x) == ERR) break;
}
refresh();
//usleep(40000);
if (!FAST && !SLOW) {
cycle(40000);
}
}
mvcur(0, COLS - 1, LINES - 2, 0);
endwin();
Expand Down

0 comments on commit f6969ae

Please sign in to comment.