Skip to content

Commit abe38e0

Browse files
committed
implement screenshot capturing
1 parent a9062fc commit abe38e0

File tree

4 files changed

+76
-13
lines changed

4 files changed

+76
-13
lines changed

src/age_qt_gui/age_ui_qt_emulation_runner.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ void age::qt_emulation_runner::set_audio_downsampler_quality(age::qt_downsampler
218218

219219

220220

221+
void age::qt_emulation_runner::capture_emulator_screen()
222+
{
223+
if (m_emulator != nullptr)
224+
{
225+
const auto emulator = m_emulator->get_emulator();
226+
227+
pixel_vector screen = emulator->get_screen_front_buffer();
228+
int screen_width = emulator->get_screen_width();
229+
int screen_height = emulator->get_screen_height();
230+
231+
emit captured_emulator_screen(screen, screen_width, screen_height);
232+
}
233+
}
234+
235+
236+
221237
//---------------------------------------------------------
222238
//
223239
// private slots

src/age_qt_gui/age_ui_qt_emulation_runner.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace age
5858
void emulator_speed(int speed_percent);
5959
void emulator_milliseconds(qint64 emulated_milliseconds);
6060

61+
void captured_emulator_screen(pixel_vector screen, int screen_width, int screen_height);
62+
6163
public slots:
6264

6365
void initialize();
@@ -74,6 +76,7 @@ namespace age
7476
void set_audio_latency(int latency_milliseconds);
7577
void set_audio_downsampler_quality(age::qt_downsampler_quality quality);
7678

79+
void capture_emulator_screen();
7780

7881

7982
private slots:

src/age_qt_gui/age_ui_qt_main_window.cpp

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#include <QString>
2424
#include <QStringList>
2525

26+
#include <gfx/age_png.hpp>
2627
#include <emulator/age_gb_emulator.hpp> // gb buttons
28+
#include <QStandardPaths>
2729

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

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

55-
m_action_open = new QAction("open file", this);
56-
m_action_open_dmg = new QAction("open file as DMG", this);
57-
m_action_open_cgb_abcd = new QAction("open file as CGB A/B/C/D", this);
58-
m_action_open_cgb_e = new QAction("open file as CGB E", this);
59-
m_action_settings = new QAction("settings", this);
60-
m_action_fullscreen = new QAction("fullscreen", this);
57+
m_action_open = new QAction("open file", this);
58+
m_action_open_dmg = new QAction("open file as DMG", this);
59+
m_action_open_cgb_abcd = new QAction("open file as CGB A/B/C/D", this);
60+
m_action_open_cgb_e = new QAction("open file as CGB E", this);
61+
m_action_capture_gb_screen = new QAction("capture gb screen", this);
62+
m_action_settings = new QAction("settings", this);
63+
m_action_fullscreen = new QAction("fullscreen", this);
6164
m_action_fullscreen->setCheckable(true);
6265
m_action_fullscreen->setChecked(false);
6366
m_action_exit = new QAction("exit", this);
@@ -92,6 +95,8 @@ age::qt_main_window::qt_main_window(QWidget* parent, Qt::WindowFlags flags)
9295
connect(emulation_runner, &qt_emulation_runner::emulator_speed, this, &qt_main_window::emulator_speed);
9396
connect(emulation_runner, &qt_emulation_runner::emulator_milliseconds, this, &qt_main_window::emulator_milliseconds);
9497

98+
connect(emulation_runner, &qt_emulation_runner::captured_emulator_screen, this, &qt_main_window::menu_emulator_captured_emulator_screen);
99+
95100
// connect video output signals
96101

97102
connect(video_output, &qt_video_output::fps, this, &qt_main_window::fps);
@@ -120,6 +125,7 @@ age::qt_main_window::qt_main_window(QWidget* parent, Qt::WindowFlags flags)
120125
connect(m_action_open_dmg, &QAction::triggered, this, &qt_main_window::menu_emulator_open_dmg);
121126
connect(m_action_open_cgb_abcd, &QAction::triggered, this, &qt_main_window::menu_emulator_open_cgb_abcd);
122127
connect(m_action_open_cgb_e, &QAction::triggered, this, &qt_main_window::menu_emulator_open_cgb_e);
128+
connect(m_action_capture_gb_screen, &QAction::triggered, emulation_runner, &qt_emulation_runner::capture_emulator_screen);
123129
connect(m_action_settings, &QAction::triggered, this, &qt_main_window::menu_emulator_settings);
124130
connect(m_action_fullscreen, &QAction::triggered, this, &qt_main_window::menu_emulator_fullscreen);
125131
connect(m_action_exit, &QAction::triggered, this, &qt_main_window::menu_emulator_exit);
@@ -257,6 +263,8 @@ void age::qt_main_window::fill_menu(QMenu* menu)
257263
menu->addAction(m_action_open_cgb_abcd);
258264
menu->addAction(m_action_open_cgb_e);
259265
menu->addSeparator();
266+
menu->addAction(m_action_capture_gb_screen);
267+
menu->addSeparator();
260268
menu->addAction(m_action_settings);
261269
menu->addAction(m_action_fullscreen);
262270
menu->addSeparator();
@@ -390,6 +398,40 @@ void age::qt_main_window::menu_emulator_open_cgb_e()
390398
m_settings->set_pause_emulator(false);
391399
}
392400

401+
void age::qt_main_window::menu_emulator_captured_emulator_screen(pixel_vector captured_screen,
402+
int screen_width,
403+
int screen_height)
404+
{
405+
// no screen captured -> do nothing
406+
if (captured_screen.empty())
407+
{
408+
return;
409+
}
410+
411+
// screen captured -> select file to write
412+
QString file_name;
413+
QFileDialog dialog(this,
414+
"save screenshot",
415+
QStandardPaths::writableLocation(QStandardPaths::HomeLocation),
416+
"PNG files (*.png)");
417+
dialog.setFileMode(QFileDialog::AnyFile);
418+
dialog.setAcceptMode(QFileDialog::AcceptSave);
419+
if (dialog.exec())
420+
{
421+
QStringList files = dialog.selectedFiles();
422+
if (!files.empty())
423+
{
424+
file_name = files.at(0);
425+
}
426+
}
427+
428+
// file selected -> write
429+
if (file_name.length() > 0)
430+
{
431+
write_png_file(captured_screen, screen_width, screen_height, file_name.toStdString());
432+
}
433+
}
434+
393435
void age::qt_main_window::menu_emulator_settings()
394436
{
395437
m_settings->show();

src/age_qt_gui/age_ui_qt_main_window.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,14 @@ namespace age
7575

7676
QSharedPointer<qt_user_value_store> m_user_value_store = nullptr;
7777

78-
QAction* m_action_open = nullptr;
79-
QAction* m_action_open_dmg = nullptr;
80-
QAction* m_action_open_cgb_abcd = nullptr;
81-
QAction* m_action_open_cgb_e = nullptr;
82-
QAction* m_action_settings = nullptr;
83-
QAction* m_action_fullscreen = nullptr;
84-
QAction* m_action_exit = nullptr;
78+
QAction* m_action_open = nullptr;
79+
QAction* m_action_open_dmg = nullptr;
80+
QAction* m_action_open_cgb_abcd = nullptr;
81+
QAction* m_action_open_cgb_e = nullptr;
82+
QAction* m_action_capture_gb_screen = nullptr;
83+
QAction* m_action_settings = nullptr;
84+
QAction* m_action_fullscreen = nullptr;
85+
QAction* m_action_exit = nullptr;
8586

8687
qt_settings_dialog* m_settings = nullptr;
8788

@@ -102,6 +103,7 @@ namespace age
102103
void menu_emulator_open_dmg();
103104
void menu_emulator_open_cgb_abcd();
104105
void menu_emulator_open_cgb_e();
106+
void menu_emulator_captured_emulator_screen(pixel_vector captured_screen, int screen_width, int screen_height);
105107
void menu_emulator_settings();
106108
void menu_emulator_fullscreen();
107109
void menu_emulator_exit();

0 commit comments

Comments
 (0)