From 2097ae3ab95b14a97dda267f80a69efe71518d30 Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Tue, 27 Mar 2018 17:45:56 +0200 Subject: [PATCH] Add code to measure frame rate; required when we want to interpolate motion --- .gitignore | 1 + configure.ac | 1 + src/Makefile.am | 1 + src/libframerate/Makefile.am | 7 +++++++ src/libframerate/framerate.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/libframerate/framerate.h | 5 +++++ src/libsdl/sdl.c | 5 +++++ src/libsdl/sdl.h | 1 + src/mercenary/Makefile.am | 2 ++ src/mercenary/main.c | 5 +++++ 10 files changed, 74 insertions(+) create mode 100644 src/libframerate/Makefile.am create mode 100644 src/libframerate/framerate.c create mode 100644 src/libframerate/framerate.h diff --git a/.gitignore b/.gitignore index 0f4a8f2..e78ba40 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/configure.ac b/configure.ac index 0c82872..656d293 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/src/Makefile.am b/src/Makefile.am index 3205afa..5fe9513 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 0000000..40f34c8 --- /dev/null +++ b/src/libframerate/Makefile.am @@ -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 index 0000000..8a79cbf --- /dev/null +++ b/src/libframerate/framerate.c @@ -0,0 +1,46 @@ +/* frame rate measurement + * + * (C) 2018 by Andreas Eversberg + * 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 . + */ + +#include +#include +#include +#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 index 0000000..9d96ee6 --- /dev/null +++ b/src/libframerate/framerate.h @@ -0,0 +1,5 @@ + +extern double vbl_duration; + +void framerate_measure(void); + diff --git a/src/libsdl/sdl.c b/src/libsdl/sdl.c index b0f464b..c3e07af 100644 --- a/src/libsdl/sdl.c +++ b/src/libsdl/sdl.c @@ -24,6 +24,7 @@ #include "sdl.h" #include "opengl.h" +#include #define GL3_PROTOTYPES 1 #include @@ -247,3 +248,7 @@ void exit_sdl(void) } } +uint32_t ticks_sdl(void) +{ + return SDL_GetTicks(); +} diff --git a/src/libsdl/sdl.h b/src/libsdl/sdl.h index f7e0423..79a9cba 100644 --- a/src/libsdl/sdl.h +++ b/src/libsdl/sdl.h @@ -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); diff --git a/src/mercenary/Makefile.am b/src/mercenary/Makefile.am index 6bd8075..621b34c 100644 --- a/src/mercenary/Makefile.am +++ b/src/mercenary/Makefile.am @@ -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 \ diff --git a/src/mercenary/main.c b/src/mercenary/main.c index 553bae0..adc53aa 100644 --- a/src/mercenary/main.c +++ b/src/mercenary/main.c @@ -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(); -- 2.13.6