Skip to content

Commit

Permalink
implement screenshot capturing
Browse files Browse the repository at this point in the history
  • Loading branch information
c-sp committed Feb 14, 2024
1 parent a9062fc commit abe38e0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/age_qt_gui/age_ui_qt_emulation_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ void age::qt_emulation_runner::set_audio_downsampler_quality(age::qt_downsampler



void age::qt_emulation_runner::capture_emulator_screen()
{
if (m_emulator != nullptr)
{
const auto emulator = m_emulator->get_emulator();

pixel_vector screen = emulator->get_screen_front_buffer();
int screen_width = emulator->get_screen_width();
int screen_height = emulator->get_screen_height();

emit captured_emulator_screen(screen, screen_width, screen_height);
}
}



//---------------------------------------------------------
//
// private slots
Expand Down
3 changes: 3 additions & 0 deletions src/age_qt_gui/age_ui_qt_emulation_runner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ namespace age
void emulator_speed(int speed_percent);
void emulator_milliseconds(qint64 emulated_milliseconds);

void captured_emulator_screen(pixel_vector screen, int screen_width, int screen_height);

public slots:

void initialize();
Expand All @@ -74,6 +76,7 @@ namespace age
void set_audio_latency(int latency_milliseconds);
void set_audio_downsampler_quality(age::qt_downsampler_quality quality);

void capture_emulator_screen();


private slots:
Expand Down
54 changes: 48 additions & 6 deletions src/age_qt_gui/age_ui_qt_main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <QString>
#include <QStringList>

#include <gfx/age_png.hpp>
#include <emulator/age_gb_emulator.hpp> // gb buttons
#include <QStandardPaths>

#include "age_ui_qt_emulation_runner.hpp"
#include "age_ui_qt_main_window.hpp"
Expand Down Expand Up @@ -52,12 +54,13 @@ age::qt_main_window::qt_main_window(QWidget* parent, Qt::WindowFlags flags)

m_settings = new qt_settings_dialog(m_user_value_store, this, Qt::WindowTitleHint | Qt::WindowCloseButtonHint);

m_action_open = new QAction("open file", this);
m_action_open_dmg = new QAction("open file as DMG", this);
m_action_open_cgb_abcd = new QAction("open file as CGB A/B/C/D", this);
m_action_open_cgb_e = new QAction("open file as CGB E", this);
m_action_settings = new QAction("settings", this);
m_action_fullscreen = new QAction("fullscreen", this);
m_action_open = new QAction("open file", this);
m_action_open_dmg = new QAction("open file as DMG", this);
m_action_open_cgb_abcd = new QAction("open file as CGB A/B/C/D", this);
m_action_open_cgb_e = new QAction("open file as CGB E", this);
m_action_capture_gb_screen = new QAction("capture gb screen", this);
m_action_settings = new QAction("settings", this);
m_action_fullscreen = new QAction("fullscreen", this);
m_action_fullscreen->setCheckable(true);
m_action_fullscreen->setChecked(false);
m_action_exit = new QAction("exit", this);
Expand Down Expand Up @@ -92,6 +95,8 @@ age::qt_main_window::qt_main_window(QWidget* parent, Qt::WindowFlags flags)
connect(emulation_runner, &qt_emulation_runner::emulator_speed, this, &qt_main_window::emulator_speed);
connect(emulation_runner, &qt_emulation_runner::emulator_milliseconds, this, &qt_main_window::emulator_milliseconds);

connect(emulation_runner, &qt_emulation_runner::captured_emulator_screen, this, &qt_main_window::menu_emulator_captured_emulator_screen);

// connect video output signals

connect(video_output, &qt_video_output::fps, this, &qt_main_window::fps);
Expand Down Expand Up @@ -120,6 +125,7 @@ age::qt_main_window::qt_main_window(QWidget* parent, Qt::WindowFlags flags)
connect(m_action_open_dmg, &QAction::triggered, this, &qt_main_window::menu_emulator_open_dmg);
connect(m_action_open_cgb_abcd, &QAction::triggered, this, &qt_main_window::menu_emulator_open_cgb_abcd);
connect(m_action_open_cgb_e, &QAction::triggered, this, &qt_main_window::menu_emulator_open_cgb_e);
connect(m_action_capture_gb_screen, &QAction::triggered, emulation_runner, &qt_emulation_runner::capture_emulator_screen);
connect(m_action_settings, &QAction::triggered, this, &qt_main_window::menu_emulator_settings);
connect(m_action_fullscreen, &QAction::triggered, this, &qt_main_window::menu_emulator_fullscreen);
connect(m_action_exit, &QAction::triggered, this, &qt_main_window::menu_emulator_exit);
Expand Down Expand Up @@ -257,6 +263,8 @@ void age::qt_main_window::fill_menu(QMenu* menu)
menu->addAction(m_action_open_cgb_abcd);
menu->addAction(m_action_open_cgb_e);
menu->addSeparator();
menu->addAction(m_action_capture_gb_screen);
menu->addSeparator();
menu->addAction(m_action_settings);
menu->addAction(m_action_fullscreen);
menu->addSeparator();
Expand Down Expand Up @@ -390,6 +398,40 @@ void age::qt_main_window::menu_emulator_open_cgb_e()
m_settings->set_pause_emulator(false);
}

void age::qt_main_window::menu_emulator_captured_emulator_screen(pixel_vector captured_screen,
int screen_width,
int screen_height)
{
// no screen captured -> do nothing
if (captured_screen.empty())
{
return;
}

// screen captured -> select file to write
QString file_name;
QFileDialog dialog(this,
"save screenshot",
QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
"PNG files (*.png)");
dialog.setFileMode(QFileDialog::AnyFile);
dialog.setAcceptMode(QFileDialog::AcceptSave);
if (dialog.exec())
{
QStringList files = dialog.selectedFiles();
if (!files.empty())
{
file_name = files.at(0);
}
}

// file selected -> write
if (file_name.length() > 0)
{
write_png_file(captured_screen, screen_width, screen_height, file_name.toStdString());
}
}

void age::qt_main_window::menu_emulator_settings()
{
m_settings->show();
Expand Down
16 changes: 9 additions & 7 deletions src/age_qt_gui/age_ui_qt_main_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ namespace age

QSharedPointer<qt_user_value_store> m_user_value_store = nullptr;

QAction* m_action_open = nullptr;
QAction* m_action_open_dmg = nullptr;
QAction* m_action_open_cgb_abcd = nullptr;
QAction* m_action_open_cgb_e = nullptr;
QAction* m_action_settings = nullptr;
QAction* m_action_fullscreen = nullptr;
QAction* m_action_exit = nullptr;
QAction* m_action_open = nullptr;
QAction* m_action_open_dmg = nullptr;
QAction* m_action_open_cgb_abcd = nullptr;
QAction* m_action_open_cgb_e = nullptr;
QAction* m_action_capture_gb_screen = nullptr;
QAction* m_action_settings = nullptr;
QAction* m_action_fullscreen = nullptr;
QAction* m_action_exit = nullptr;

qt_settings_dialog* m_settings = nullptr;

Expand All @@ -102,6 +103,7 @@ namespace age
void menu_emulator_open_dmg();
void menu_emulator_open_cgb_abcd();
void menu_emulator_open_cgb_e();
void menu_emulator_captured_emulator_screen(pixel_vector captured_screen, int screen_width, int screen_height);
void menu_emulator_settings();
void menu_emulator_fullscreen();
void menu_emulator_exit();
Expand Down

0 comments on commit abe38e0

Please sign in to comment.