File indexing completed on 2025-01-05 04:25:40

0001 /****************************************************************************************
0002  * Copyright (c) 2014 Matej Repinc <mrepinc@gmail.com>                                  *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Pulic License for more details.              *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef ASCIIANALYZER_H
0018 #define ASCIIANALYZER_H
0019 
0020 #include "AnalyzerBase.h"
0021 
0022 #include <QImage>
0023 #include <QPixmap>
0024 #include <QSharedPointer>
0025 #include <QSize>
0026 
0027 class QPalette;
0028 
0029 class ASCIIAnalyzer : public Analyzer::Base
0030 {
0031 public:
0032     explicit ASCIIAnalyzer( QWidget* );
0033 
0034     static GLuint createTexture( const QImage &image ) { return instance->bindTexture( image ); }
0035     static void freeTexture( GLuint id ) { instance->deleteTexture( id ); }
0036 
0037     // Signed ints because most of what we compare them against are ints
0038     static const int BLOCK_HEIGHT = 12;
0039     static const int BLOCK_WIDTH  = 12;
0040     static const int MIN_ROWS     = 30;  //arbitrary
0041     static const int MIN_COLUMNS  = 32;  //arbitrary
0042     static const int MAX_COLUMNS  = 128; //must be 2**n
0043     static const int FADE_SIZE    = 90;
0044 
0045 protected:
0046     virtual void initializeGL();
0047     virtual void paintGL();
0048     virtual void resizeGL( int w, int h );
0049     virtual void transform( QVector<float>& );
0050     virtual void analyze( const QVector<float>& );
0051     virtual void paletteChange( const QPalette& );
0052 
0053     void drawBackground();
0054     void determineStep();
0055 
0056 private:
0057     struct Texture
0058     {
0059         Texture( const QPixmap &pixmap ) :
0060             id( ASCIIAnalyzer::createTexture( pixmap.toImage().mirrored() ) ), // Flip texture vertically for OpenGL bottom-left coordinate system
0061             size( pixmap.size() )
0062         {}
0063         Texture( const Texture& texture )
0064         {
0065             id = texture.id;
0066             size = texture.size;
0067         }
0068         ~Texture()
0069         {
0070             ASCIIAnalyzer::freeTexture( id );
0071         }
0072 
0073         GLuint id;
0074         QSize size;
0075     };
0076 
0077     void drawTexture( Texture* texture, int x, int y, int sx, int sy );
0078 
0079     static ASCIIAnalyzer* instance;
0080 
0081     int m_columns, m_rows;      //number of rows and columns of blocks
0082     QPixmap m_barPixmap;
0083     QVector<float> m_scope;      //so we don't create a vector every frame
0084     QVector<float> m_store;  //current bar heights
0085     QVector<float> m_yscale;
0086 
0087     QSharedPointer<Texture> m_barTexture;
0088     QSharedPointer<Texture> m_topBarTexture;
0089     QSharedPointer<Texture> m_topSecondBarTexture;
0090     QSharedPointer<Texture> m_background;
0091 
0092     float m_step; //rows to fall per frame
0093 };
0094 
0095 #endif