/***************************************************************************
 *   Copyright (C) 2004 by David Sansome                                   *
 *   me@davidsansome.com                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef __plasma_H__
#define __plasma_H__

#include <qdialog.h>
#include <kscreensaver.h>
#include <kdialogbase.h>
#include <qslider.h>
#include <qgl.h>
#include "plasmasetup.h"

#define myRandf(x) (((float)(x) * random()) / 2147483647)
#define myRandi(x) (int)(myRandf(x))
#define fabstrunc(f) (f >= 0.0f ? (f <= 1.0f ? f : 1.0f) : (f >= -1.0f ? -f : 1.0f))

#define PIx2 6.28318530718f
#define NUMCONSTS 18
#define MAXTEXSIZE 1024


class PlasmaWidget : public QGLWidget
{
	Q_OBJECT

public:
	PlasmaWidget( QWidget* parent, const char* name, int zoom, int focus, int speed, int res );
	~PlasmaWidget();

protected:
	void paintGL();
	void resizeGL( int w, int h );
	void initTexture(int w, int h);
	void initializeGL();

private slots:
	void nextFrame();

private:
	int _frameTime;
	QTimer* _timer;
	
	// Globals
	float aspectRatio;
	float wide;
	float high;
	float c[NUMCONSTS];		// constant
	float ct[NUMCONSTS];		// temporary value of constant
	float cv[NUMCONSTS];		// velocity of constant
	float ***position;
	float ***plasma;
	int texsize;
	int plasmaWidth, plasmaHeight;
	float texright, textop;
	float *plasmamap;
	
	// Parameters edited in the dialog box
	int dZoom;
	int dFocus;
	int dSpeed;
	int dResolution;
};



class KPlasmaScreenSaver : public KScreenSaver
{
	Q_OBJECT
	
public:
	KPlasmaScreenSaver( WId id );
	KPlasmaScreenSaver( WId id, int z, int f, int s, int r );
	virtual ~KPlasmaScreenSaver();
	
	int zoom() { return dZoom; }
	int focus() { return dFocus; }
	int speed() { return dSpeed; }
	int resolution() { return dResolution; }
	
	
private:
	void readSettings();
	
	PlasmaWidget* _effect;
	
	int dZoom;
	int dFocus;
	int dSpeed;
	int dResolution;
};



class KPlasmaSetup : public KDialogBase
{
	Q_OBJECT

public:
	KPlasmaSetup( QWidget* parent = 0, const char* name = 0 );
	~KPlasmaSetup();

private slots:
	void slotHelp();
	void slotOk();
	void slotDefault();
	void slotApply();
	void resetTimer();
	void updatePreview();

private:
	void saveSettings();
	
	PlasmaSetup* setupWidget;
	QWidget* preview;
	KPlasmaScreenSaver* _saver;
	QTimer* timer;
};

#endif
