Permalink
Browse files

-CodeChange: Display FPS with SDL and SDL2

Display Frame rate (average on last 16 frames) on upper right of the screen.
Toggle with F8
  • Loading branch information...
1 parent d722fbe commit 9a2f1b667b98f3718aff29d4867173f0c2ea1bb3 @miniupnp miniupnp committed Jan 30, 2016
Showing with 70 additions and 1 deletion.
  1. +2 −0 source.list
  2. +1 −1 src/timer.c
  3. +2 −0 src/timer.h
  4. +37 −0 src/video/video_fps.c
  5. +8 −0 src/video/video_fps.h
  6. +10 −0 src/video/video_sdl.c
  7. +10 −0 src/video/video_sdl2.c
View
@@ -152,6 +152,7 @@ unit.c
#endif
#endif
#endif
+video/video_fps.c
#if TOS
video/c2p1x1_8.s
#else
@@ -223,6 +224,7 @@ timer.h
tools.h
unit.h
video/video.h
+video/video_fps.h
video/hqx_common.h
video/hqx.h
video/scale2x.h
View
@@ -63,7 +63,7 @@ static uint32 s_timerLastTime;
static const uint32 s_timerSpeed = 1000000 / 120; /* Our timer runs at 120Hz */
-static uint32 Timer_GetTime(void)
+uint32 Timer_GetTime(void)
{
#if defined(_MSC_VER)
DWORD t;
View
@@ -14,6 +14,8 @@ extern uint32 g_timerInput;
extern uint32 g_timerSleep;
extern uint32 g_timerTimeout;
+extern uint32 Timer_GetTime(void);
+
extern void Timer_Sleep(uint16 ticks);
extern bool Timer_SetTimer(TimerType timer, bool set);
View
@@ -0,0 +1,37 @@
+#include "types.h"
+#include "../timer.h"
+
+#include "video_fps.h"
+
+void Video_ShowFPS(uint8 *screen)
+{
+ uint32 timeStamp;
+ static uint32 s_previousTimeStamps[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+ static uint8 s_previousTimeStampsIndex = 0;
+
+ timeStamp = Timer_GetTime();
+ if(s_previousTimeStamps[s_previousTimeStampsIndex] > 0
+ && timeStamp != s_previousTimeStamps[s_previousTimeStampsIndex]) {
+ int x, i;
+ /* calculate average frames per 1000sec on the 16 last time measures */
+ uint32 kfps = 16000000 / (timeStamp - s_previousTimeStamps[s_previousTimeStampsIndex]);
+ for(x = 320 - 4; kfps > 0; kfps /= 10, x -= 4) {
+ /* draw the digits */
+ static const uint8 fontdigits[10] = {0167,044,0135,0155,056,0153,0173,045,0177,0157};
+ static const uint8 fonttestsegments[15] = {03,01,05, 02,0,04, 032,010,054, 020,0,040, 0120,0100,0140};
+ uint8 segments = fontdigits[kfps % 10];
+ int offset = 0;
+ for(i=0; i<15; i++) {
+ screen[x+offset] = (segments & fonttestsegments[i]) ? 15 : 0;
+ offset++;
+ if((i % 3) == 2) {
+ screen[x+offset] = 0;
+ offset += 317;
+ }
+ }
+ }
+ for (i=0; i<5; i++) screen[x+2+i*320] = 0;
+ }
+ s_previousTimeStamps[s_previousTimeStampsIndex] = timeStamp;
+ s_previousTimeStampsIndex = (s_previousTimeStampsIndex + 1) & 0x0f;
+}
@@ -0,0 +1,8 @@
+/** @file src/video/video_fps.h display FPS in top right of the screen */
+
+#ifndef VIDEO_VIDEO_FPS_H
+#define VIDEO_VIDEO_FPS_H
+
+void Video_ShowFPS(uint8 *screen);
+
+#endif /* VIDEO_VIDEO_FPS_H */
View
@@ -27,6 +27,7 @@
#include "../input/mouse.h"
#include "../opendune.h"
+#include "video_fps.h"
#include "scalebit.h"
#include "hqx.h"
@@ -475,6 +476,7 @@ static void Video_DrawScreen(void)
void Video_Tick(void)
{
SDL_Event event;
+ static bool s_showFPS = false;
if (!s_video_initialized) return;
@@ -483,6 +485,10 @@ void Video_Tick(void)
if (s_video_lock) return;
s_video_lock = true;
+ if (s_showFPS) {
+ Video_ShowFPS(GFX_Screen_Get_ByIndex(SCREEN_0));
+ }
+
while (SDL_PollEvent(&event)) {
uint8 keyup = 1;
@@ -521,6 +527,10 @@ void Video_Tick(void)
}
continue;
}
+ if (sym == SDLK_F8 && !keyup) {
+ s_showFPS = !s_showFPS;
+ continue;
+ }
/* Mac keyboard scancodes are very different from what
* they are on a PC : we need a translation table. */
#if defined(__APPLE__)
@@ -13,6 +13,7 @@
#include "../input/mouse.h"
#include "../opendune.h"
+#include "video_fps.h"
#include "scalebit.h"
#include "hqx.h"
@@ -461,13 +462,18 @@ static void Video_DrawScreen(void)
void Video_Tick(void)
{
SDL_Event event;
+ static bool s_showFPS = false;
if (!s_video_initialized) return;
if (g_fileOperation != 0) return;
if (s_video_lock) return;
s_video_lock = true;
+ if (s_showFPS) {
+ Video_ShowFPS(GFX_Screen_Get_ByIndex(SCREEN_0));
+ }
+
while (SDL_PollEvent(&event)) {
uint8 keyup = 1;
@@ -507,6 +513,10 @@ void Video_Tick(void)
s_full_screen = !s_full_screen;
continue;
}
+ if (sym == SDLK_F8) {
+ if (keyup) s_showFPS = !s_showFPS;
+ continue;
+ }
if (sym >= SDLK_CAPSLOCK) {
sym -= SDLK_CAPSLOCK;
if (sym < sizeof(s_SDL_hikeymap)) code = s_SDL_hikeymap[sym];

0 comments on commit 9a2f1b6

Please sign in to comment.