aboutsummaryrefslogtreecommitdiff
path: root/src/aocharmovie.cpp
diff options
context:
space:
mode:
authorCrystalwarrior <varsash@gmail.com>2019-09-14 22:35:02 +0300
committerCrystalwarrior <varsash@gmail.com>2019-09-14 22:35:02 +0300
commit938f1aeea15c6b68495bb1cb0a80467f4e8cf343 (patch)
treee6fc806ed0360d8cb731acddc11faa00bb7c8bb2 /src/aocharmovie.cpp
parentab30cca586a05506c3cfb502cee53957409242e8 (diff)
Optimize the heck out of animated images. There is no lag after the image is loaded now.
HOWEVER, there's lag when the image is first loaded due to the aspect ratio and sizing (at least with .apng's).
Diffstat (limited to 'src/aocharmovie.cpp')
-rw-r--r--src/aocharmovie.cpp74
1 files changed, 40 insertions, 34 deletions
diff --git a/src/aocharmovie.cpp b/src/aocharmovie.cpp
index 8004a540..2d370ec7 100644
--- a/src/aocharmovie.cpp
+++ b/src/aocharmovie.cpp
@@ -11,7 +11,8 @@ AOCharMovie::AOCharMovie(QWidget *p_parent, AOApplication *p_ao_app) : QLabel(p_
preanim_timer->setSingleShot(true);
ticker = new QTimer(this);
- ticker->setSingleShot(true);
+ ticker->setTimerType(Qt::PreciseTimer);
+ ticker->setSingleShot(false);
connect(ticker, SIGNAL(timeout()), this, SLOT(movie_ticker()));
// connect(m_movie, SIGNAL(frameChanged(int)), this, SLOT(frame_change(int)));
@@ -20,6 +21,7 @@ AOCharMovie::AOCharMovie(QWidget *p_parent, AOApplication *p_ao_app) : QLabel(p_
void AOCharMovie::play(QString p_char, QString p_emote, QString emote_prefix)
{
+ actual_time.restart();
QString emote_path;
QList<QString> pathlist;
pathlist = {
@@ -39,24 +41,29 @@ void AOCharMovie::play(QString p_char, QString p_emote, QString emote_prefix)
}
this->clear();
- m_reader = new QImageReader(emote_path);
+ ticker->stop();
+ movie_frames.clear();
+ movie_delays.clear();
+
+ QImageReader *m_reader = new QImageReader(emote_path);
QImage f_image = m_reader->read();
-// while (!f_image.isNull())
-// {
-// if (m_flipped)
-// movie_frames.append(f_image.mirrored(true, false));
-// else
-// movie_frames.append(f_image);
-// f_image = reader->read();
-// }
-
-// delete m_reader;
+ while (!f_image.isNull())
+ {
+ movie_frames.append(this->get_pixmap(f_image));
+ movie_delays.append(m_reader->nextImageDelay());
+ f_image = m_reader->read();
+ }
+
+ delete m_reader;
+
this->show();
- qDebug() << "Setting image to " << f_image;
- this->set_frame(f_image);
- if (m_reader->supportsAnimation())
- ticker->start(m_reader->nextImageDelay());
+ qDebug() << "Setting image to " << emote_path << "Time taken to process image:" << actual_time.elapsed();
+ frame = 0;
+ actual_time.restart();
+ this->set_frame(movie_frames[frame]);
+ if (movie_frames.size() > 0)
+ ticker->start(movie_delays[frame]);
}
void AOCharMovie::play_pre(QString p_char, QString p_emote, int duration)
@@ -86,7 +93,7 @@ void AOCharMovie::stop()
this->hide();
}
-void AOCharMovie::set_frame(QImage image)
+QPixmap AOCharMovie::get_pixmap(QImage image)
{
QPixmap f_pixmap;
if(m_flipped)
@@ -99,10 +106,16 @@ void AOCharMovie::set_frame(QImage image)
aspect_ratio = Qt::KeepAspectRatioByExpanding;
if (f_pixmap.size().width() > this->size().width() || f_pixmap.size().height() > this->size().height())
- this->setPixmap(f_pixmap.scaled(this->width(), this->height(), aspect_ratio, Qt::SmoothTransformation));
+ f_pixmap = f_pixmap.scaled(this->width(), this->height(), aspect_ratio, Qt::SmoothTransformation);
else
- this->setPixmap(f_pixmap.scaled(this->width(), this->height(), aspect_ratio, Qt::FastTransformation));
+ f_pixmap = f_pixmap.scaled(this->width(), this->height(), aspect_ratio, Qt::FastTransformation);
+ return f_pixmap;
+}
+
+void AOCharMovie::set_frame(QPixmap f_pixmap)
+{
+ this->setPixmap(f_pixmap);
QLabel::move(x + (this->width() - this->pixmap()->width())/2, y);
}
@@ -122,29 +135,22 @@ void AOCharMovie::move(int ax, int ay)
void AOCharMovie::movie_ticker()
{
- if(m_reader->format() != "gif")
- ticker->start(m_reader->nextImageDelay());
- if(m_reader->currentImageNumber() == -1)
+ ++frame;
+ if(frame == movie_frames.size())
{
if(play_once)
{
preanim_done();
+ ticker->stop();
return;
}
else
- m_reader->jumpToImage(0); //Loop back
- }
- QImage f_image = m_reader->read();
- if(f_image.isNull())
- {
- qDebug() << "Animated image error on frame" << m_reader->currentImageNumber() << m_reader->errorString();
- stop();
- return;
+ frame = 0;
}
- this->set_frame(f_image);
- qDebug() << m_reader->format() << "frame" << m_reader->nextImageDelay() << m_reader->currentImageNumber() << m_reader->imageCount();
- if(m_reader->format() == "gif")
- ticker->start(m_reader->nextImageDelay());
+// qint64 difference = elapsed - movie_delays[frame];
+ qDebug() << frame << movie_delays[frame] << "actual time taken from last frame:" << actual_time.restart();
+ this->set_frame(movie_frames[frame]);
+ ticker->setInterval(movie_delays[frame]);
}
void AOCharMovie::preanim_done()