Add FPS counter to SDL title

This commit is contained in:
Maximilian Mader 2018-09-10 21:07:17 +02:00
parent 629550c30b
commit 2c58e669ef
Signed by: Max
GPG Key ID: F71D56A3151C4FB3

View File

@ -7,6 +7,17 @@
#include "gui.h"
#include "shader.h"
#define str(x) #x
#define xstr(x) str(x)
#define title "SameBoy v" xstr(VERSION)
#define FRAME_VALUES 300 // reduce to get averages above shorter time periods
Uint32 frametimes[FRAME_VALUES];
Uint32 frametime_last;
Uint32 frame_count;
float frames_per_second;
float fps_min;
float fps_max;
#ifndef _WIN32
#define AUDIO_FREQUENCY 96000
@ -42,6 +53,51 @@ static const GB_model_t sdl_to_internal_model[] =
[MODEL_AGB] = GB_MODEL_AGB
};
void fps_init() {
// Set all frame times to 0ms.
memset(frametimes, 0, sizeof(frametimes));
frame_count = 0;
frames_per_second = 0;
fps_max = 0;
fps_min = FLT_MAX;
frametime_last = SDL_GetTicks();
}
/* Source: http://sdl.beuc.net/sdl.wiki/SDL_Average_FPS_Measurement */
void fps_update() {
Uint32 frametimes_index;
Uint32 get_ticks;
Uint32 count;
Uint32 i;
frametimes_index = frame_count % FRAME_VALUES;
get_ticks = SDL_GetTicks();
frametimes[frametimes_index] = get_ticks - frametime_last;
frametime_last = get_ticks;
frame_count++;
if (frame_count < FRAME_VALUES) {
count = frame_count;
} else {
count = FRAME_VALUES;
}
frames_per_second = 0;
for (i = 0; i < count; i++) {
frames_per_second += frametimes[i];
}
frames_per_second /= count;
frames_per_second = 1000.f / frames_per_second;
if (frames_per_second < fps_min) fps_min = frames_per_second;
if (frames_per_second > fps_max) fps_max = frames_per_second;
char title_buffer[50];
snprintf(title_buffer, sizeof(title_buffer), title " - %.1f FPS (%.1f - %.1f)", frames_per_second, fps_min, fps_max);
SDL_SetWindowTitle(window, title_buffer);
}
void set_filename(const char *new_filename, bool new_should_free)
{
if (filename && should_free_filename) {
@ -224,7 +280,6 @@ static void handle_events(GB_gameboy_t *gb)
case SDL_SCANCODE_C:
if (event.type == SDL_KEYDOWN && (event.key.keysym.mod & KMOD_CTRL)) {
GB_debugger_break(gb);
}
break;
@ -269,6 +324,12 @@ static void handle_events(GB_gameboy_t *gb)
}
break;
case SDL_SCANCODE_L:
if (event.key.keysym.mod & MODIFIER) {
fps_init();
}
break;
default:
/* Save states */
if (event.key.keysym.scancode >= SDL_SCANCODE_1 && event.key.keysym.scancode <= SDL_SCANCODE_0) {
@ -324,6 +385,7 @@ static void vblank(GB_gameboy_t *gb)
clock_mutliplier += 0.1;
GB_set_clock_multiplier(gb, clock_mutliplier);
}
if (configuration.blend_frames) {
render_texture(active_pixel_buffer, previous_pixel_buffer);
uint32_t *temp = active_pixel_buffer;
@ -334,6 +396,9 @@ static void vblank(GB_gameboy_t *gb)
else {
render_texture(active_pixel_buffer, NULL);
}
fps_update();
do_rewind = rewind_down;
handle_events(gb);
}
@ -448,6 +513,8 @@ restart:
replace_extension(filename, path_length, symbols_path, ".sym");
GB_debugger_load_symbol_file(&gb, symbols_path);
fps_init();
/* Run emulation */
while (true) {
if (paused || rewind_paused) {
@ -490,9 +557,7 @@ static void save_configuration(void)
int main(int argc, char **argv)
{
#define str(x) #x
#define xstr(x) str(x)
fprintf(stderr, "SameBoy v" xstr(VERSION) "\n");
fprintf(stderr, title "\n");
if (argc > 2) {
fprintf(stderr, "Usage: %s [rom]\n", argv[0]);
@ -511,7 +576,7 @@ int main(int argc, char **argv)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
window = SDL_CreateWindow("SameBoy v" xstr(VERSION), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
160 * 2, 144 * 2, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_SetWindowMinimumSize(window, 160, 144);