Skip to content

Commit

Permalink
Return of the Qt frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Sep 12, 2021
1 parent d4499d2 commit dab6754
Show file tree
Hide file tree
Showing 10 changed files with 700 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ cmake_minimum_required(VERSION 3.2)
project(NanoBoyAdvance)

option(PLATFORM_SDL2 "Build SDL2 frontend" ON)
option(PLATFORM_QT "Build Qt frontend" ON)

add_subdirectory(src/nba)
add_subdirectory(src/platform/core)

if (PLATFORM_SDL2)
add_subdirectory(src/platform/sdl ${CMAKE_CURRENT_BINARY_DIR}/bin/sdl/)
endif()

if (PLATFORM_QT)
add_subdirectory(src/platform/qt ${CMAKE_CURRENT_BINARY_DIR}/bin/qt/)
endif()
2 changes: 2 additions & 0 deletions src/platform/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SOURCES
src/emulator.cpp
src/frame_limiter.cpp
src/game_db.cpp
)

Expand All @@ -15,6 +16,7 @@ set(HEADERS

set(HEADERS_PUBLIC
include/platform/emulator.hpp
include/platform/frame_limiter.hpp
)

add_library(platform-core STATIC ${SOURCES} ${HEADERS} ${HEADERS_PUBLIC})
Expand Down
43 changes: 43 additions & 0 deletions src/platform/core/include/platform/frame_limiter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2021 fleroviux
*
* Licensed under GPLv3 or any later version.
* Refer to the included LICENSE file.
*/

#pragma once

#include <chrono>
#include <functional>
#include <thread>

namespace nba {

struct FrameLimiter {
FrameLimiter(float fps = 60.0) {
Reset(fps);
}

void Reset();
void Reset(float fps);
void SetFastForward(bool value);

void Run(
std::function<void(void)> frame_advance,
std::function<void(float)> update_fps
);

private:
static constexpr int kMillisecondsPerSecond = 1000;
static constexpr int kMicrosecondsPerSecond = 1000000;

int frame_count = 0;
int frame_duration;
float frames_per_second;
bool fast_forward = false;

std::chrono::time_point<std::chrono::steady_clock> timestamp_target;
std::chrono::time_point<std::chrono::steady_clock> timestamp_fps_update;
};

} // namespace nba
58 changes: 58 additions & 0 deletions src/platform/core/src/frame_limiter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2021 fleroviux
*
* Licensed under GPLv3 or any later version.
* Refer to the included LICENSE file.
*/

#include <platform/frame_limiter.hpp>

namespace nba {

void FrameLimiter::Reset() {
Reset(frames_per_second);
}

void FrameLimiter::Reset(float fps) {
frame_count = 0;
frame_duration = int(kMicrosecondsPerSecond / fps);
frames_per_second = fps;
fast_forward = false;
timestamp_target = std::chrono::steady_clock::now();
timestamp_fps_update = std::chrono::steady_clock::now();
}

void FrameLimiter::SetFastForward(bool value) {
if (fast_forward != value) {
fast_forward = value;
if (!fast_forward) {
timestamp_target = std::chrono::steady_clock::now();
}
}
}

void FrameLimiter::Run(
std::function<void(void)> frame_advance,
std::function<void(float)> update_fps
) {
timestamp_target += std::chrono::microseconds(frame_duration);

frame_advance();
frame_count++;

auto now = std::chrono::steady_clock::now();
auto fps_update_delta = std::chrono::duration_cast<std::chrono::milliseconds>(
now - timestamp_fps_update).count();

if (fps_update_delta >= kMillisecondsPerSecond) {
update_fps(frame_count * float(kMillisecondsPerSecond) / fps_update_delta);
frame_count = 0;
timestamp_fps_update = std::chrono::steady_clock::now();
}

if (!fast_forward) {
std::this_thread::sleep_until(timestamp_target);
}
}

} // namespace nba
34 changes: 34 additions & 0 deletions src/platform/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
project(NanoBoyAdvance-Qt CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/CMakeModules)

set(SOURCES
main.cpp
screen.cpp
main_window.cpp
)

set(HEADERS
screen.hpp
main_window.hpp
)

# CHECKME: dunno, is this required?
set(CMAKE_AUTOMOC ON)

# CHECKME: possibly we can get rid of all OpenGL dependencies here.
find_package(OpenGL REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5OpenGL REQUIRED)
set(QT_DEPS Qt5::Gui Qt5::Widgets Qt5::OpenGL)

include(FindSDL2 REQUIRED)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})

add_executable(NanoBoyAdvance-Qt ${SOURCES} ${HEADERS})
target_link_libraries(NanoBoyAdvance-Qt platform-core ${QT_DEPS} ${OPENGL_gl_LIBRARY} ${SDL2_LIBRARY})
set_target_properties(NanoBoyAdvance-Qt PROPERTIES OUTPUT_NAME "NanoBoyAdvance")
21 changes: 21 additions & 0 deletions src/platform/qt/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2021 fleroviux
*
* Licensed under GPLv3 or any later version.
* Refer to the included LICENSE file.
*/

#include <QApplication>

#include "main_window.hpp"

int main(int argc, char** argv) {
QApplication app{ argc, argv };
MainWindow window{ &app };

QCoreApplication::setOrganizationName("fleroviux");
QCoreApplication::setApplicationName("NanoboyAdvance");

window.show();
return app.exec();
}
Loading

0 comments on commit dab6754

Please sign in to comment.