Add code to measure frame rate; required when we want to interpolate motion
authorAndreas Eversberg <jolly@eversberg.eu>
Tue, 27 Mar 2018 15:45:56 +0000 (17:45 +0200)
committerAndreas Eversberg <jolly@eversberg.eu>
Thu, 5 Apr 2018 16:00:14 +0000 (18:00 +0200)
.gitignore
configure.ac
src/Makefile.am
src/libframerate/Makefile.am [new file with mode: 0644]
src/libframerate/framerate.c [new file with mode: 0644]
src/libframerate/framerate.h [new file with mode: 0644]
src/libsdl/sdl.c
src/libsdl/sdl.h
src/mercenary/Makefile.am
src/mercenary/main.c

index 0f4a8f2..e78ba40 100644 (file)
@@ -22,6 +22,7 @@ compile
 m4
 src/libcpu/libcpu.a
 src/libcpu/m68kmake
+src/libframerate/libframerate.a
 src/libsdl/libsdl.a
 src/libvideo/libvideo.a
 src/libsound/libsound.a
index 0c82872..656d293 100644 (file)
@@ -52,6 +52,7 @@ NOTE: This package is manatory. The games will not compile without it.
 
 AC_OUTPUT(
     src/libcpu/Makefile
+    src/libframerate/Makefile
     src/libvideo/Makefile
     src/libsound/Makefile
     src/libjoystick/Makefile
index 3205afa..5fe9513 100644 (file)
@@ -2,6 +2,7 @@ AUTOMAKE_OPTIONS = foreign
 
 SUBDIRS = \
        libcpu \
+       libframerate \
        libvideo \
        libjoystick \
        libkeyboard \
diff --git a/src/libframerate/Makefile.am b/src/libframerate/Makefile.am
new file mode 100644 (file)
index 0000000..40f34c8
--- /dev/null
@@ -0,0 +1,7 @@
+AM_CPPFLAGS = -Wall -Wextra -g $(all_includes)
+
+noinst_LIBRARIES = libframerate.a
+
+libframerate_a_SOURCES = \
+       framerate.c
+
diff --git a/src/libframerate/framerate.c b/src/libframerate/framerate.c
new file mode 100644 (file)
index 0000000..8a79cbf
--- /dev/null
@@ -0,0 +1,46 @@
+/* frame rate measurement
+ *
+ * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
+ * All Rights Reserved
+ *
+ * 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 <stdint.h>
+#include <string.h>
+#include "../libsdl/sdl.h"
+#include "framerate.h"
+
+#define MAX_HISTORY 60
+
+double vbl_duration = 1.0 / 60.0;
+static uint32_t ticks_history[MAX_HISTORY];
+static int ticks_count = 0;
+
+void framerate_measure(void)
+{
+       uint32_t ticks = ticks_sdl();
+
+       /* wait some ticks until to get better average when we start */
+       if (ticks_count >= 10 || ticks_count == MAX_HISTORY)
+               vbl_duration = (double)((uint32_t)(ticks - ticks_history[0])) / (double)ticks_count / 1000.0;
+
+       if (ticks_count == MAX_HISTORY) {
+               memmove(ticks_history, ticks_history + 1, sizeof(ticks_history[0]) * (MAX_HISTORY - 1));
+               ticks_count--;
+       }
+       ticks_history[ticks_count++] = ticks;
+}
+
diff --git a/src/libframerate/framerate.h b/src/libframerate/framerate.h
new file mode 100644 (file)
index 0000000..9d96ee6
--- /dev/null
@@ -0,0 +1,5 @@
+
+extern double vbl_duration;
+
+void framerate_measure(void);
+
index b0f464b..c3e07af 100644 (file)
@@ -24,6 +24,7 @@
 #include "sdl.h"
 #include "opengl.h"
 
+#include <SDL2/SDL.h>
 #define GL3_PROTOTYPES 1
 #include <GL/glew.h>
 
@@ -247,3 +248,7 @@ void exit_sdl(void)
        }
 }
 
+uint32_t ticks_sdl(void)
+{
+       return SDL_GetTicks();
+}
index f7e0423..79a9cba 100644 (file)
@@ -4,4 +4,5 @@ int init_sdl(const char *progname, int width, int height, int sound_samplerate,
 int event_sdl(void);
 void swap_sdl(void);
 void exit_sdl(void);
+uint32_t ticks_sdl(void);
 
index 6bd8075..621b34c 100644 (file)
@@ -19,6 +19,7 @@ mercenary2_LDADD = \
        $(COMMON_LA) \
        $(top_builddir)/src/mercenary/libmain.a \
        $(top_builddir)/src/libcpu/libcpu.a \
+       $(top_builddir)/src/libframerate/libframerate.a \
        $(top_builddir)/src/libvideo/libvideo.a \
        $(top_builddir)/src/libsound/libsound.a \
        $(top_builddir)/src/libjoystick/libjoystick.a \
@@ -36,6 +37,7 @@ mercenary3_LDADD = \
        $(COMMON_LA) \
        $(top_builddir)/src/mercenary/libmain.a \
        $(top_builddir)/src/libcpu/libcpu.a \
+       $(top_builddir)/src/libframerate/libframerate.a \
        $(top_builddir)/src/libvideo/libvideo.a \
        $(top_builddir)/src/libsound/libsound.a \
        $(top_builddir)/src/libjoystick/libjoystick.a \
index 553bae0..adc53aa 100644 (file)
@@ -27,6 +27,7 @@
 #include "../libsdl/print.h"
 #include "../libcpu/m68k.h"
 #include "../libcpu/execute.h"
+#include "../libframerate/framerate.h"
 #include "../libvideo/video.h"
 #include "../libsound/sound.h"
 #include "../libjoystick/joystick.h"
@@ -255,6 +256,7 @@ static void main_loop(void)
 
        /* render result on window */
        while (!quit) {
+printf("frame rate: %.6f\n", 1.0 / vbl_duration);
                /* handle SDL events */
                rc = event_sdl();
                if (rc)
@@ -314,7 +316,10 @@ static void main_loop(void)
                        opengl_viewport_legacy(debug_opengl);
                        opengl_blit_legacy(image, config_video_filter);
                }
+               /* wait for VBL and show */
                swap_sdl();
+               /* measure frame rate */
+               framerate_measure();
 
                /* STEP 3: execute interrupt at rate of 50Hz, render sound */
                current_time = SDL_GetTicks();