diff options
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | README | 1 | ||||
-rw-r--r-- | dash.c | 99 |
3 files changed, 65 insertions, 41 deletions
@@ -5,10 +5,6 @@ OPTIM=-g OBJ=dash CC=cc -# Uncomment the following line if your system does not support usleep() -# -# USLEEP=-DNOUSLEEP - # For Suns - edit if your system is awkward too # INCLUDE_PATH=-I/usr/openwin/include -I/usr/5include @@ -16,7 +12,7 @@ LIB_PATH=-L/usr/openwin/lib -L/usr/5lib # Shouldn't need to edit below here # -OPTS=$(OPTIM) $(USLEEP) +OPTS=$(OPTIM) ALL=dash spred @@ -43,7 +43,6 @@ USAGE dash [switches] -s Use small version -b Use black and white sprites --d delay usleep() by delay for fast servers (eg, 90000 on Sun4) -l lev start from level lev -c use curses on tty (not yet perfect) -h Dump hi-score table @@ -43,13 +43,8 @@ #define RND(x) ((rand())%(x)) -/* Slightly roundabout, but allows us to test the op -*/ -#ifdef NOUSLEEP -# define USLEEP(x) LocalUsleep(x) -#else -# define USLEEP(x) usleep(x) -#endif +#define PLAY_HERTZ 10 +#define OTHER_HERTZ 25 static XSizeHints size_hints; static ulong black,white; @@ -293,6 +288,8 @@ static int cursesmode=False; static double si[360]; static double co[360]; +static struct timespec frame_start; + static int level=1; static int score=0; static int no_levels=0; @@ -365,6 +362,8 @@ static void EnterHiscore(void); static void DrawMess(const char *s); static void Gravity(void); static void DoPause(void); +static void StartFrame(void); +static void EndFrame(int hertz); /* ------------------------------------------------------------ MAIN @@ -392,11 +391,6 @@ int main(int argc, char *argv[]) chsz=8; f++; } - else if (!strcmp(argv[f],"-d")) - { - slp=atoi(argv[f+1]); - f+=2; - } else if (!strcmp(argv[f],"-l")) { startlev=atoi(argv[f+1]); @@ -1062,8 +1056,8 @@ static void DoTillPress(void (*f)(void),int s,int qf) while(1) { - if ((slp>-1)&&(s)) - USLEEP(slp); + if (s) + StartFrame(); if(XPending(disp)) { @@ -1100,6 +1094,9 @@ static void DoTillPress(void (*f)(void),int s,int qf) } (*f)(); + + if (s) + EndFrame(OTHER_HERTZ); } } @@ -1175,6 +1172,8 @@ static void XTitle(void) char s[128]; int f; + StartFrame(); + if (title_fr) { x=title_bl[0].x; @@ -1239,6 +1238,7 @@ static void XTitle(void) } (*Update)(); + EndFrame(OTHER_HERTZ); } /* --------------------------------------------------------------------- */ @@ -1267,6 +1267,7 @@ static void XIntroLevel(void) fr=False; } + StartFrame(); (*Cls)(black); for(f=0;f<INTROSPR;f++) @@ -1288,9 +1289,8 @@ static void XIntroLevel(void) (*Centre)("Press Space to continue",Y(SCRY-2),white); - USLEEP(10000); - (*Update)(); + EndFrame(OTHER_HERTZ); } /* --------------------------------------------------------------------- */ @@ -2387,8 +2387,7 @@ static int XPlayLevel(void) while(dt) { - if (slp>-1) - USLEEP(slp); + StartFrame(); for(f=0;f<4;f++) if (released[f]) @@ -2582,6 +2581,7 @@ static int XPlayLevel(void) (*Repaint)(False); DrawScore(); (*Update)(); + EndFrame(PLAY_HERTZ); if (!ok) { @@ -2762,6 +2762,7 @@ static void DoXGameOverWonSmallScale(void) double dy; int f; + StartFrame(); (*Cls)(black); (*Centre)(gw_text[gw_text_no],Y(3),white); @@ -2802,9 +2803,8 @@ static void DoXGameOverWonSmallScale(void) gw_cast_no=0; } - USLEEP(10000); - (*Update)(); + EndFrame(OTHER_HERTZ); } @@ -2816,6 +2816,8 @@ static void DoXGameOverWonLargeScale(void) double dx,dy; int f; + StartFrame(); + XFillBox(0,Y(2),X(SCRX),Y(2),black); XFillBox(0,Y(SCRY-2),X(SCRX),Y(2),black); @@ -2869,9 +2871,8 @@ static void DoXGameOverWonLargeScale(void) gw_cast_no=0; } - USLEEP(10000); - (*Update)(); + EndFrame(OTHER_HERTZ); } @@ -3065,8 +3066,7 @@ static int CursesPlayLevel(void) while(dt) { - if (slp>-1) - USLEEP(slp); + StartFrame(); shiftmode=False; @@ -3185,6 +3185,7 @@ static int CursesPlayLevel(void) (*Repaint)(False); DrawScore(); (*Update)(); + EndFrame(PLAY_HERTZ); if (!ok) { @@ -3220,6 +3221,7 @@ static void SubCursesGameOverWon(void) { static unsigned int c; + StartFrame(); (*Cls)(black); c++; @@ -3230,12 +3232,13 @@ static void SubCursesGameOverWon(void) (*Centre)("A feat I never thought possible...",Y(12),white); (*Centre)("Noddybox salutes you!",Y(13),white); - (*Centre)("www.noddybox.demon.co.uk",Y(15),white); + (*Centre)("noddybox.co.uk",Y(15),white); if ((c/10)%2) (*Centre)("Press Space to continue",Y(SCRY-2),white); (*Update)(); + EndFrame(OTHER_HERTZ); } @@ -3249,6 +3252,7 @@ static void SubCursesGameOverLost(void) { static unsigned int c; + StartFrame(); (*Cls)(black); c++; @@ -3259,6 +3263,7 @@ static void SubCursesGameOverLost(void) (*Centre)("Press Space to continue",Y(SCRY-2),white); (*Update)(); + EndFrame(OTHER_HERTZ); } @@ -3274,6 +3279,8 @@ static void CursesDoTillPress(void (*f)(void), int s, int q) while(1) { + StartFrame(); + switch(getch()) { case ' ': @@ -3289,8 +3296,7 @@ static void CursesDoTillPress(void (*f)(void), int s, int q) (*f)(); - if /* (s) */ (True) - USLEEP(slp); + EndFrame(OTHER_HERTZ); } } @@ -3314,16 +3320,39 @@ static void Exit(int n) /* --------------------------------------------------------------------- */ -#ifdef NOUSLEEP +static void StartFrame(void) +{ + clock_gettime(CLOCK_REALTIME, &frame_start); +} + -LocalUsleep(int u) +static void EndFrame(int hertz) { - struct timeval tv; + long FRAME = 1000000000 / hertz; + struct timespec now; + struct timespec diff; - tv.tv_sec=0; - tv.tv_usec=u; + clock_gettime(CLOCK_REALTIME, &now); - (void)select(0,NULL,NULL,NULL,&tv); -} + diff.tv_sec = now.tv_sec - frame_start.tv_sec; + diff.tv_nsec = now.tv_nsec - frame_start.tv_nsec; + + if (diff.tv_sec > 0) + { + diff.tv_sec--; + diff.tv_nsec += 1000000000; + } -#endif /* NOUSLEEP */ + if (diff.tv_nsec >= 1000000000) + { + diff.tv_sec++; + diff.tv_nsec -= 1000000000; + } + + if (diff.tv_sec == 0 && diff.tv_nsec < FRAME) + { + diff.tv_nsec = FRAME - diff.tv_nsec; + + nanosleep(&diff, NULL); + } +} |