all repos — mgba @ 8219b70c2eb34cd6eb3dbe6e1ccaea7f4d01f81f

mGBA Game Boy Advance Emulator

src/platform/qt/GIFView.cpp (view raw)

 1/* Copyright (c) 2013-2014 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "GIFView.h"
 7
 8#ifdef USE_MAGICK
 9
10#include "CoreController.h"
11#include "GBAApp.h"
12#include "LogController.h"
13
14#include <QMap>
15
16#include <mgba/internal/gba/gba.h>
17#include <mgba/internal/gba/video.h>
18
19using namespace QGBA;
20
21GIFView::GIFView(QWidget* parent)
22	: QWidget(parent)
23{
24	m_ui.setupUi(this);
25
26	connect(m_ui.start, &QAbstractButton::clicked, this, &GIFView::startRecording);
27	connect(m_ui.stop, &QAbstractButton::clicked, this, &GIFView::stopRecording);
28
29	connect(m_ui.selectFile, &QAbstractButton::clicked, this, &GIFView::selectFile);
30	connect(m_ui.filename, &QLineEdit::textChanged, this, &GIFView::setFilename);
31
32	connect(m_ui.frameskip, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
33	        this, &GIFView::updateDelay);
34	connect(m_ui.delayAuto, &QAbstractButton::clicked, this, &GIFView::updateDelay);
35
36	ImageMagickGIFEncoderInit(&m_encoder);
37}
38
39GIFView::~GIFView() {
40	stopRecording();
41}
42
43void GIFView::setController(std::shared_ptr<CoreController> controller) {
44	connect(controller.get(), &CoreController::stopping, this, &GIFView::stopRecording);
45	connect(this, &GIFView::recordingStarted, controller.get(), &CoreController::setAVStream);
46	connect(this, &GIFView::recordingStopped, controller.get(), &CoreController::clearAVStream, Qt::DirectConnection);
47}
48
49void GIFView::startRecording() {
50	int delayMs = m_ui.delayAuto->isChecked() ? -1 : m_ui.delayMs->value();
51	ImageMagickGIFEncoderSetParams(&m_encoder, m_ui.frameskip->value(), delayMs);
52	if (!ImageMagickGIFEncoderOpen(&m_encoder, m_filename.toUtf8().constData())) {
53		LOG(QT, ERROR) << tr("Failed to open output GIF file: %1").arg(m_filename);
54		return;
55	}
56	m_ui.start->setEnabled(false);
57	m_ui.stop->setEnabled(true);
58	m_ui.groupBox->setEnabled(false);
59	emit recordingStarted(&m_encoder.d);
60}
61
62void GIFView::stopRecording() {
63	emit recordingStopped();
64	ImageMagickGIFEncoderClose(&m_encoder);
65	m_ui.stop->setEnabled(false);
66	m_ui.start->setEnabled(true);
67	m_ui.groupBox->setEnabled(true);
68}
69
70void GIFView::selectFile() {
71	QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"), tr("Graphics Interchange Format (*.gif)"));
72	if (!filename.isEmpty()) {
73		m_ui.filename->setText(filename);
74		if (!ImageMagickGIFEncoderIsOpen(&m_encoder)) {
75			m_ui.start->setEnabled(true);
76		}
77	}
78}
79
80void GIFView::setFilename(const QString& fname) {
81	m_filename = fname;
82}
83
84void GIFView::updateDelay() {
85	if (!m_ui.delayAuto->isChecked()) {
86		return;
87	}
88
89	uint64_t s = (m_ui.frameskip->value() + 1);
90	s *= VIDEO_TOTAL_LENGTH * 1000;
91	s /= GBA_ARM7TDMI_FREQUENCY;
92	m_ui.delayMs->setValue(s);
93}
94
95#endif