Initial import
authorAntonio Ospite <ospite@studenti.unina.it>
Thu, 21 Mar 2013 15:14:50 +0000 (16:14 +0100)
committerAntonio Ospite <ospite@studenti.unina.it>
Thu, 21 Mar 2013 15:48:04 +0000 (16:48 +0100)
.gitignore [new file with mode: 0644]
Makefile [new file with mode: 0644]
fps-limit.c [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..ef7def1
--- /dev/null
@@ -0,0 +1,2 @@
+*.o
+fps-limit
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..961ef03
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,41 @@
+CFLAGS = -std=c99 -pedantic -pedantic-errors -Wall -g3 -O2 -D_ANSI_SOURCE_
+CFLAGS += -fno-common \
+         -Wall \
+         -Wdeclaration-after-statement \
+         -Wextra \
+         -Wformat=2 \
+         -Winit-self \
+         -Winline \
+         -Wpacked \
+         -Wp,-D_FORTIFY_SOURCE=2 \
+         -Wpointer-arith \
+         -Wlarger-than-65500 \
+         -Wmissing-declarations \
+         -Wmissing-format-attribute \
+         -Wmissing-noreturn \
+         -Wmissing-prototypes \
+         -Wnested-externs \
+         -Wold-style-definition \
+         -Wredundant-decls \
+         -Wsign-compare \
+         -Wstrict-aliasing=2 \
+         -Wstrict-prototypes \
+         -Wswitch-enum \
+         -Wundef \
+         -Wunreachable-code \
+         -Wunsafe-loop-optimizations \
+         -Wunused-but-set-variable \
+         -Wwrite-strings
+
+CFLAGS += -DDEBUG
+
+# For clock_nanosleep()
+CFLAGS += -D_POSIX_C_SOURCE=200112L
+
+# for clock_gettime()
+LDLIBS += -lrt
+
+fps-limit: fps-limit.o
+
+clean:
+       rm -rf *~ *.o fps-limit
diff --git a/fps-limit.c b/fps-limit.c
new file mode 100644 (file)
index 0000000..746c957
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * fps-limit - Example program to limit frames per seconds
+ *
+ * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <time.h>
+
+#ifdef DEBUG
+#define dbg(...)                     \
+       do {                         \
+               printf(__VA_ARGS__); \
+               printf("\n");        \
+               fflush(stdout);      \
+       } while(0)
+#else
+#define dbg(...) do {} while(0)
+#endif
+
+#define FPS 60
+
+#define NSEC_PER_SEC 1000000000
+
+#define timespecsub(a, b, result)                                \
+       do {                                                     \
+               (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;    \
+               (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
+               if ((result)->tv_nsec < 0) {                     \
+                       --(result)->tv_sec;                      \
+                       (result)->tv_nsec += 1000000000;         \
+               }                                                \
+       } while(0)
+
+int main(void)
+{
+       struct timespec time_start;
+       struct timespec time_end;
+       struct timespec elapsed;
+       struct timespec remaining = {0, 0};
+
+       struct timespec tmp;
+
+       while (1) {
+               clock_gettime(CLOCK_MONOTONIC, &time_start);
+               dbg("Start time: s: %ld, ns: %ld", time_start.tv_sec, time_start.tv_nsec);
+
+               dbg("Doing some work, max: %d", NSEC_PER_SEC / FPS);
+               tmp.tv_sec = 0;
+               tmp.tv_nsec = (NSEC_PER_SEC / FPS) * 2 / 3;
+               nanosleep(&tmp, NULL);
+
+               clock_gettime(CLOCK_MONOTONIC, &time_end);
+               dbg("End time: s: %ld, ns: %ld", time_end.tv_sec, time_end.tv_nsec);
+               timespecsub(&time_end, &time_start, &elapsed);
+               dbg("Elapsed s: %ld ns: %ld", elapsed.tv_sec, elapsed.tv_nsec);
+               if (elapsed.tv_sec == 0 && elapsed.tv_nsec < NSEC_PER_SEC / FPS) {
+                       remaining.tv_nsec = (NSEC_PER_SEC / FPS) - elapsed.tv_nsec;
+                       dbg("Sleeping for: ns: %ld", remaining.tv_nsec);
+                       clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &remaining, NULL);
+               }
+       }
+
+       return 0;
+}