diff options
author | Ian C <ianc@noddybox.co.uk> | 2020-05-21 20:41:12 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2020-05-21 20:41:12 +0000 |
commit | c872f619808a81d4b85784d8afcbd4ca9cec96eb (patch) | |
tree | 7c1353483f22a9d1a552394a843ea8036789c497 /galax.c | |
parent | da3dc7a1d7735c0356b90cf62d93db62588267d4 (diff) |
Added time to throttle updates.
Diffstat (limited to 'galax.c')
-rw-r--r-- | galax.c | 52 |
1 files changed, 52 insertions, 0 deletions
@@ -47,6 +47,8 @@ char *coll_data; double si[3600],co[3600]; +static struct timespec frame_start; + static XFuncControl Key(Window w, XPressRelease s, XEvent *e); static XFuncControl ProcessTitle(void),ProcessIntro(void),ProcessGame(void), ProcessGameOver(void),ProcessHiScore(void), @@ -337,6 +339,8 @@ static void ClearBullets(void); static void ClearKeys(void); static void DoDebugMenu(void); static int Paused(int k); +static void StartFrame(void); +static void EndFrame(void); int main(int argc, char *argv[]) @@ -807,6 +811,7 @@ static XFuncControl ProcessTitle(void) ctr++; + StartFrame(); Cls(True,xi[(ctr%800)/100],yi[(ctr%800)/100],True); Centre(20,YELLOW,"GALAXY INVADERS"); @@ -844,6 +849,7 @@ static XFuncControl ProcessTitle(void) DoDebugMenu(); Update(); + EndFrame(); k=GetKey(); @@ -960,6 +966,7 @@ static XFuncControl ProcessStartLevel(void) { char s[80]; + StartFrame(); Cls(True,levdata[levelndx].sx,levdata[levelndx].sy,True); sprintf(s,"LEVEL %d",level+1); @@ -968,6 +975,7 @@ static XFuncControl ProcessStartLevel(void) Centre (100,RED,levdata[levelndx].title); Update(); + EndFrame(); if (GetKey()!=NONE) return(XFUNCSTOP); @@ -994,6 +1002,7 @@ static XFuncControl ProcessGame(void) if (Paused(k)) return (XFUNCCONT); + StartFrame(); Cls(True,levdata[levelndx].sx,levdata[levelndx].sy,True); if (lives) @@ -1064,6 +1073,7 @@ static XFuncControl ProcessGame(void) } Update(); + EndFrame(); if (k==QUIT) { @@ -1146,6 +1156,7 @@ static XFuncControl ProcessGameOver(void) static int posx=32; static unsigned int ctr=0; + StartFrame(); Cls (True,0,1,True); ctr++; @@ -1163,6 +1174,7 @@ static XFuncControl ProcessGameOver(void) Centre(120,WHITE,"GAME OVER"); Update(); + EndFrame(); if (GetKey()==NONE) return(XFUNCCONT); @@ -1241,6 +1253,7 @@ static XFuncControl ProcessHiScore(void) first=False; } + StartFrame(); Cls (True,1,1,True); Centre(16,WHITE,"CONGRATULATIONS!"); @@ -1286,6 +1299,7 @@ static XFuncControl ProcessHiScore(void) bounce--; Update(); + EndFrame(); if (len==3) { @@ -1878,3 +1892,41 @@ static void DrawAndKillAliens(void) } } } + + +static void StartFrame(void) +{ + clock_gettime(CLOCK_REALTIME, &frame_start); +} + + +static void EndFrame(void) +{ + static const long FRAME = 1000000000 / 50; + struct timespec now; + struct timespec diff; + + clock_gettime(CLOCK_REALTIME, &now); + + 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; + } + + 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); + } +} |