summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2020-05-21 20:33:44 +0000
committerIan C <ianc@noddybox.co.uk>2020-05-21 20:33:44 +0000
commit7f414f4946354a3b960b1f6a15cfdad014d7e199 (patch)
treec610cdec71dbac56bbd0bbf3326abb7c74ddf254
parenta983c1ba8c20325f9e9569530e703b8336722e95 (diff)
Added timer to control update speed.
-rw-r--r--README6
-rw-r--r--lunar.c48
2 files changed, 50 insertions, 4 deletions
diff --git a/README b/README
index 3f8f739..3ae25dc 100644
--- a/README
+++ b/README
@@ -23,10 +23,8 @@ Though written under FreeBSD, it should compile OK on most flavours of unix.
USAGE
-----
-lunar [-noshm] [scale] - Use scale to magnify and impact overly fast servers.
- Use -noshm as well if your server is still too fast
- to disable MIT-SHM usage. (Note shm usage will be
- disabled if your server doesn't support it anyway).
+lunar [-noshm] [scale] - Use scale to magnify the screen. Use -noshm to
+ disable MIT_SHM usage if it causes problems.
ledit <level_file>
diff --git a/lunar.c b/lunar.c
index f57d899..86d0619 100644
--- a/lunar.c
+++ b/lunar.c
@@ -37,6 +37,7 @@ static char rcs_id[]="$Id$";
#include <fcntl.h>
#include <string.h>
#include <errno.h>
+#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
@@ -79,6 +80,8 @@ static uchar *coll_data;
static 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);
@@ -356,6 +359,8 @@ static int Paused(int k);
static void ReadScores(void);
static void WriteScores(void);
static void ReadLevels(void);
+static void StartFrame(void);
+static void EndFrame(void);
int main(int argc,char *argv[])
@@ -1164,6 +1169,7 @@ static XFuncControl ProcessTitle(void)
ctr++;
+ StartFrame();
Cls();
for(f=0;f<10;f++)
@@ -1198,6 +1204,7 @@ static XFuncControl ProcessTitle(void)
DoDebugMenu();
Update();
+ EndFrame();
k=GetKey();
@@ -1345,6 +1352,7 @@ static XFuncControl ProcessGame(void)
if (Paused(k))
return XFUNCCONT;
+ StartFrame();
Cls();
/* Process movement
@@ -1471,6 +1479,7 @@ static XFuncControl ProcessGame(void)
CheckCollisions(&landed,&dead);
Update();
+ EndFrame();
if (k==SCALE_UP)
{
@@ -1643,6 +1652,7 @@ static XFuncControl ProcessHiScore(void)
first=False;
}
+ StartFrame();
Cls();
Centre(16,WHITE,"CONGRATULATIONS!");
@@ -1689,6 +1699,7 @@ static XFuncControl ProcessHiScore(void)
bounce--;
Update();
+ EndFrame();
if (len==3)
{
@@ -1860,3 +1871,40 @@ static void ReadLevels(void)
for(f=0;f<no_levels;f++)
free(name[f]);
}
+
+static void StartFrame(void)
+{
+ clock_gettime(CLOCK_REALTIME, &frame_start);
+}
+
+
+static void EndFrame(void)
+{
+ static const long FRAME = 1000000000 / 25;
+ 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);
+ }
+}