diff --git a/src/analyzers/nyancatanalyzer.cpp b/src/analyzers/nyancatanalyzer.cpp index ae92baa78..e5b0a1912 100644 --- a/src/analyzers/nyancatanalyzer.cpp +++ b/src/analyzers/nyancatanalyzer.cpp @@ -37,7 +37,9 @@ NyanCatAnalyzer::NyanCatAnalyzer(QWidget* parent) memset(history_, 0, sizeof(history_)); for (int i=0 ; i=0 ; --band) { - p.setPen(colors_[band]); - p.drawPolyline(&polyline[band*kHistorySize], kHistorySize); + // Do we have to draw the whole rainbow into the buffer? + if (buffer_.isNull()) { + buffer_ = QPixmap(size()); + buffer_.fill(background_brush_.color()); + + QPainter buffer_painter(&buffer_); + buffer_painter.setRenderHint(QPainter::Antialiasing); + for (int band=kRainbowBands-1 ; band>=0 ; --band) { + buffer_painter.setPen(colors_[band]); + buffer_painter.drawPolyline(&polyline[band*kHistorySize], kHistorySize); + } + } else { + // We can just shuffle the buffer along a bit and draw the new frame's data. + QPainter buffer_painter(&buffer_); + buffer_painter.setRenderHint(QPainter::Antialiasing); + + buffer_painter.drawPixmap(0, 0, buffer_, + px_per_frame, 0, + buffer_.width() - px_per_frame, -1); + buffer_painter.fillRect(buffer_.width() - px_per_frame, 0, px_per_frame, height(), + background_brush_); + + for (int band=kRainbowBands-1 ; band>=0 ; --band) { + buffer_painter.setPen(colors_[band]); + buffer_painter.drawPolyline(&polyline[(band+1)*kHistorySize - 2], 2); + } } + // Draw the buffer on to the widget + p.drawPixmap(0, 0, buffer_); + + qLog(Debug) << t.elapsed(); + // Draw nyan cat (he's been waiting for this for 50 lines). // Nyan nyan nyan nyan. QRect cat_dest(width() - kCatWidth, (height() - kCatHeight) / 2, kCatWidth, kCatHeight); p.drawPixmap(cat_dest, cat_, CatSourceRect()); - } diff --git a/src/analyzers/nyancatanalyzer.h b/src/analyzers/nyancatanalyzer.h index dd7376d0c..1057190d9 100644 --- a/src/analyzers/nyancatanalyzer.h +++ b/src/analyzers/nyancatanalyzer.h @@ -30,12 +30,13 @@ public: static const char* kName; - void timerEvent(QTimerEvent* e); - protected: void transform(Scope&); void analyze(QPainter& p, const Analyzer::Scope&, bool new_frame); + void timerEvent(QTimerEvent* e); + void resizeEvent(QResizeEvent* e); + private: static const int kCatHeight = 21; static const int kCatWidth = 34; @@ -54,15 +55,22 @@ private: } private: + // "constants" that get initialised in the constructor + float band_scale_[kRainbowBands]; + QPen colors_[kRainbowBands]; + + // Nyan cat! QPixmap cat_; - float band_scale_[kRainbowBands]; - + // For the nyan cat animation int timer_id_; int frame_; + // The y positions of each point on the rainbow. float history_[kHistorySize * kRainbowBands]; - QPen colors_[kRainbowBands]; + + // A cache of the last frame's rainbow, so it can be used in the next frame. + QPixmap buffer_; QBrush background_brush_; };