Skip to content

Commit

Permalink
sdl2: add a basic frame counter
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Oct 26, 2023
1 parent 81be31e commit 3f91441
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/platform/sdl/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int Application::Run(int argc, char **argv) {
LoadBootROM("boot9.bin", true);
LoadBootROM("boot7.bin", false);
if(argc < 2) {
LoadROM("Simple_Tri.nds");
LoadROM("pokeplatin.nds");
} else {
LoadROM(argv[1]);
}
Expand All @@ -44,7 +44,7 @@ void Application::CreateWindow() {
SDL_WINDOW_ALLOW_HIGHDPI
);

m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
m_renderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED);

for(auto& texture : m_textures) {
texture = SDL_CreateTexture(m_renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 256, 192);
Expand Down Expand Up @@ -138,6 +138,8 @@ void Application::MainLoop() {
SDL_RenderPresent(m_renderer);

m_emu_thread.ReleaseFrame();

UpdateFPS();
}

m_emu_thread.SetFastForward(SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_SPACE]);
Expand All @@ -155,3 +157,18 @@ void Application::MainLoop() {
}
}
}

void Application::UpdateFPS() {
const auto now = std::chrono::system_clock::now();
const auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(
now - m_last_fps_update).count();

m_fps_counter++;

if(elapsed_time >= 1000) {
const f32 fps = (f32)m_fps_counter / (f32)elapsed_time * 1000.0f;
SDL_SetWindowTitle(m_window, fmt::format("irisdual [{:.2f} fps]", fps).c_str());
m_fps_counter = 0;
m_last_fps_update = std::chrono::system_clock::now();
}
}
4 changes: 4 additions & 0 deletions src/platform/sdl/src/application.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#pragma once

#include <chrono>
#include <dual/nds/nds.hpp>
#include <memory>

Expand All @@ -20,11 +21,14 @@ class Application {
void LoadROM(const char* path);
void LoadBootROM(const char* path, bool arm9);
void MainLoop();
void UpdateFPS();

SDL_Window* m_window;
SDL_Renderer* m_renderer;
SDL_Texture* m_textures[2];

std::unique_ptr<dual::nds::NDS> m_nds{};
EmulatorThread m_emu_thread{};
int m_fps_counter{};
std::chrono::time_point<std::chrono::system_clock> m_last_fps_update{};
};

0 comments on commit 3f91441

Please sign in to comment.