/***************************************************************************
 *   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 <list>
#include <qgl.h>
#include "rsMath.h"
#include "skyrocketsetup.h"

class particle;

#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 MAXFLARES 110    // Required 100 and 10 extra for good measure
#define MINDEPTH -1000000.0f  // particle depth for inactive particles


struct SkyrocketParams
{
	int dMaxrockets;
	int dSmoke;
	int dExplosionsmoke;
	int dWind;
	int dAmbient;
	int dStardensity;
	int dFlare;
	int dMoonglow;
	int dMoon;
	int dClouds;
	int dEarth;
	int dIllumination;
	int dSound;
};


class SkyrocketWidget : public QGLWidget
{
	Q_OBJECT

public:
	SkyrocketWidget( QWidget* parent, const char* name, SkyrocketParams(p) );
	~SkyrocketWidget();

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

private slots:
	void nextFrame();

public:
	particle* addParticle();
	void removeParticle(std::list<particle>::iterator curpart);
	void sortParticles();
	void illuminate(particle* ill);
	void pulling(particle* suck);
	void pushing(particle* shock);
	void stretching(particle* stretch);
	void makeFlareList();
	
	int _frameTime;
	QTimer* _timer;
	
	std::list<particle> particles;
	std::list<particle>::iterator currentParticle;
	int numRockets;
	int numFlares;
	int xsize, ysize;
	// Time from one frame to the next
	float elapsedTime;
	// Window variables
	int centerx, centery;
	float aspectRatio;
	// Camera variables
	rsVec lookFrom[3];  // 3 = position, target position, last position
	rsVec lookAt[3];  // 3 = position, target position, last position
	rsVec cameraPos;  // used for positioning sounds (same as lookFrom[0])
	rsVec cameraVel;  // used for doppler shift
	
	float allflares[MAXFLARES][7];  // type, x, y, r, g, b, alpha
	// Parameters edited in the dialog box
	SkyrocketParams params;
};

SkyrocketWidget* currentWidget;



class KSkyrocketScreenSaver : public KScreenSaver
{
	Q_OBJECT
	
public:
	KSkyrocketScreenSaver( WId id );
	KSkyrocketScreenSaver( WId id, SkyrocketParams p );
	virtual ~KSkyrocketScreenSaver();
	
	SkyrocketParams params;
	
private:
	void readSettings();
	
	SkyrocketWidget* _effect;
};



class KSkyrocketSetup : public KDialogBase
{
	Q_OBJECT

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

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

private:
	void saveSettings();
	
	SkyrocketSetup* setupWidget;
	QWidget* preview;
	KSkyrocketScreenSaver* _saver;
	QTimer* timer;
};

#endif
