implemented fps counter

This commit is contained in:
georgemoralis 2023-10-29 23:46:34 +02:00
parent ce2c7a7d01
commit f732704b83
1 changed files with 60 additions and 4 deletions

View File

@ -1,17 +1,29 @@
#include "emulator.h"
#include <fmt/core.h>
#include <Core/PS4/HLE/Graphics/graphics_render.h>
#include <Emulator/Host/controller.h>
#include "Emulator/Util/singleton.h"
#include <Lib/Timer.h>
#include <fmt/core.h>
#include <vulkan_util.h>
#include "Core/PS4/HLE/Graphics/video_out.h"
#include "Emulator/HLE/Libraries/LibPad/pad.h"
#include "Emulator/Util/singleton.h"
#include "version.h"
namespace Emu {
bool m_emu_needs_exit = false;
double m_current_time_seconds = {0.0};
double m_previous_time_seconds = {0.0};
int m_update_num = {0};
int m_frame_num = {0};
double m_update_time_seconds = {0.0};
double m_current_fps = {0.0};
int m_max_updates_per_frame = {4};
double m_update_fixed_time = 1.0 / 60.0;
int m_fps_frames_num = {0};
double m_fps_start_time = {0};
void emuInit(u32 width, u32 height) {
auto* window_ctx = singleton<Emu::WindowCtx>::instance();
@ -49,7 +61,39 @@ static void CreateSdlWindow(WindowCtx* ctx) {
SDL_SetWindowResizable(ctx->m_window, SDL_FALSE); // we don't support resizable atm
}
static void update() {
static double lag = 0.0;
lag += m_current_time_seconds - m_previous_time_seconds;
int num = 0;
while (lag >= m_update_fixed_time) {
if (num < m_max_updates_per_frame) {
m_update_num++;
num++;
m_update_time_seconds = m_update_num * m_update_fixed_time;
}
lag -= m_update_fixed_time;
}
}
static void calculateFps(double game_time_s) {
m_previous_time_seconds = m_current_time_seconds;
m_current_time_seconds = game_time_s;
m_frame_num++;
m_fps_frames_num++;
if (m_current_time_seconds - m_fps_start_time > 0.25f) {
m_current_fps = static_cast<double>(m_fps_frames_num) / (m_current_time_seconds - m_fps_start_time);
m_fps_frames_num = 0;
m_fps_start_time = m_current_time_seconds;
}
}
void emuRun() {
Lib::Timer timer;
timer.Start();
auto* window_ctx = singleton<Emu::WindowCtx>::instance();
{
// init window and wait until init finishes
@ -58,6 +102,7 @@ void emuRun() {
Graphics::Vulkan::vulkanCreate(window_ctx);
window_ctx->m_is_graphic_initialized = true;
window_ctx->m_graphic_initialized_cond.notify_one();
calculateFps(timer.GetTimeSec());
}
bool exit_loop = false;
@ -88,9 +133,13 @@ void emuRun() {
continue;
}
exit_loop = m_emu_needs_exit;
if (!exit_loop) {
HLE::Libs::Graphics::VideoOut::videoOutFlip(100000); // flip every 0.1 sec
update();
}
if (!exit_loop) {
if (HLE::Libs::Graphics::VideoOut::videoOutFlip(100000)) { // flip every 0.1 sec
calculateFps(timer.GetTimeSec());
}
}
}
std::exit(0);
@ -102,6 +151,12 @@ HLE::Libs::Graphics::GraphicCtx* getGraphicCtx() {
return &window_ctx->m_graphic_ctx;
}
void updateSDLTitle() {
char title[256];
sprintf(title, "shadps4 v %s FPS: %f", Emulator::VERSION, m_current_fps);
auto* window_ctx = singleton<Emu::WindowCtx>::instance();
SDL_SetWindowTitle(window_ctx->m_window, title);
}
void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) {
auto* window_ctx = singleton<Emu::WindowCtx>::instance();
if (window_ctx->is_window_hidden) {
@ -192,6 +247,7 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) {
fmt::print("vkQueuePresentKHR failed\n");
std::exit(0);
}
updateSDLTitle();
}
void keyboardEvent(SDL_Event* event) {