SVN_SILENT made messages (.desktop file)
[kdeartwork.git] / kscreensaver / kdesavers / gravity.cpp
blobe0a579676d959206e044b4f1e0865fa66403cb35
1 //-----------------------------------------------------------------------------
2 //
3 // kgravity - Partical Gravity Screen Saver for KDE 2
4 //
5 // Copyright (c) Ian Reinhart Geiser 2001
6 //
7 // KConfig code and KScreenSaver "Setup..." improvements by
8 // Nick Betcher <nbetcher@usinternet.com> 2001
9 //
10 #include <stdlib.h>
11 #include <qlabel.h>
12 #include <qlayout.h>
13 #include <kapplication.h>
14 #include <klocale.h>
15 #include <kconfig.h>
16 #include <kcolordialog.h>
17 #include <kcolorbutton.h>
18 #include "gravity.h"
19 #include "gravity.moc"
20 #ifdef Q_WS_MACX
21 #include <OpenGL/glu.h>
22 #include <OpenGL/gl.h>
23 #else
24 #include <GL/glu.h>
25 #include <GL/gl.h>
26 #endif
27 #include <qimage.h>
28 #include <kdebug.h>
29 #include <qpainter.h>
30 #include <qradiobutton.h>
31 #include <qspinbox.h>
32 #include <kstandarddirs.h>
33 #include <math.h>
34 #include <kmessagebox.h>
35 #include <krandom.h>
36 // libkscreensaver interface
37 class KGravitySaverInterface : public KScreenSaverInterface
41 public:
42 virtual KAboutData* aboutData() {
43 return new KAboutData( "kgravity.kss", 0, ki18n( "Particle Gravity Screen Saver" ), "2.2.0", ki18n( "Particle Gravity Screen Saver" ) );
47 virtual KScreenSaver* create( WId id )
49 return new KGravitySaver( id );
52 virtual QDialog* setup()
54 return new KGravitySetup();
58 int main( int argc, char *argv[] )
60 KGravitySaverInterface kss;
61 return kScreenSaverMain( argc, argv, kss );
64 //-----------------------------------------------------------------------------
65 // dialog to setup screen saver parameters
67 KGravitySetup::KGravitySetup( QWidget *parent )
68 : QDialog(parent)
70 setupUi(this);
71 readSettings();
73 preview->setFixedSize( 220, 170 );
75 QPalette palette;
76 palette.setColor( preview->backgroundRole(), Qt::black );
77 preview->setPalette( palette );
79 #ifdef Q_WS_X11
80 preview->show(); // otherwise saver does not get correct size
81 #endif
82 saver = new KGravitySaver( preview->winId() );
84 connect( PushButton1, SIGNAL( clicked() ), SLOT( slotOkPressed() ) );
85 connect( PushButton2, SIGNAL( clicked() ), SLOT( reject() ) );
86 connect( PushButton3, SIGNAL( clicked() ), SLOT( aboutPressed() ) );
87 connect( SpinBox1, SIGNAL( valueChanged(int)), saver, SLOT( updateSize(int)));
88 connect( RadioButton1, SIGNAL( toggled(bool)), saver, SLOT( doStars(bool)));
92 KGravitySetup::~KGravitySetup()
94 delete saver;
97 // read settings from config file
98 void KGravitySetup::readSettings()
100 KConfigGroup config = KConfig("kssgravityrc", KConfig::NoGlobals).group("Settings");
102 QString boolval = config.readEntry( "Stars", "false" );
103 if (boolval == "true") {
104 RadioButton1->setDown(true);
105 RadioButton1_2->setDown(false);
106 } else {
107 if (boolval == "false")
109 RadioButton1->setDown(false);
110 RadioButton1_2->setDown(true);
114 QString starammount = config.readEntry("StarSize", "75");
115 SpinBox1->setValue(starammount.toInt());
119 // Ok pressed - save settings and exit
120 void KGravitySetup::slotOkPressed()
122 KConfig _config("kssgravityrc", KConfig::NoGlobals);
123 KConfigGroup config(&_config, "Settings" );
125 if (RadioButton1->isChecked() == true)
127 config.writeEntry( "Stars", "true" );
128 } else {
129 if (RadioButton1_2->isChecked() == true)
131 config.writeEntry( "Stars", "false" );
134 config.writeEntry( "StarSize", QString::number(SpinBox1->value()) );
136 config.sync();
138 accept();
141 void KGravitySetup::aboutPressed()
143 KMessageBox::about(this,
144 i18n("<h3>Gravity</h3>\n<p>Particle Gravity Screen Saver for KDE</p>\nCopyright (c) Ian Reinhart Geiser 2001<br>\n\n<p>KConfig code and KScreenSaver \"Setup...\" improvements by Nick Betcher <nbetcher@usinternet.com> 2001</p>"));
146 //-----------------------------------------------------------------------------
149 KGravitySaver::KGravitySaver( WId id ) : KScreenSaver( id )
152 kDebug() << "Blank";
154 timer = new QTimer( this );
155 timer->setSingleShot(true);
156 timer->start( 25);
158 QPalette palette;
159 palette.setColor( backgroundRole(), Qt::black );
160 setPalette( palette );
162 update();
163 gravity = new Gravity();
164 embed(gravity);
165 #ifdef Q_WS_X11
166 gravity->show();
167 #endif
168 connect( timer, SIGNAL(timeout()), this, SLOT(blank()) );
171 KGravitySaver::~KGravitySaver()
176 // read configuration settings from config file
177 void KGravitySaver::readSettings()
179 // Please remove me
183 void KGravitySaver::blank()
185 // Play gravity
187 gravity->updateGL();
188 timer->setSingleShot(true);
189 timer->start( 25);
192 Gravity::Gravity( QWidget * parent ) : QGLWidget (parent)
194 rainbow=true;
195 slowdown=2.0f;
196 zoom=-50.0f;
197 index=0;
198 size = 3.95f;
199 // obj = gluNewQuadric();
201 // This has to be here because you can't update the gravity until 'gravity' is created!
202 KConfig _config("kssgravityrc", KConfig::NoGlobals);
203 KConfigGroup config(&_config, "Settings" );
204 QString boolval = config.readEntry( "Stars", "false" );
205 if (boolval == "true") {
206 setStars(true);
207 } else {
208 if (boolval == "false")
210 setStars(false);
214 QString starammount = config.readEntry("StarSize", "75");
215 float passvalue = (starammount.toInt() / 100.0);
216 setSize(passvalue);
220 Gravity::~Gravity()
222 glDeleteTextures( 1, &texture[0] );
223 gluDeleteQuadric(obj);
226 /** load the particle file */
227 bool Gravity::loadParticle()
229 /* Status indicator */
230 bool Status = true;
231 QImage buf;
233 kDebug() << "Loading: " << KStandardDirs::locate("data", "kscreensaver/particle.png");
234 if (buf.load( KStandardDirs::locate("data", "kscreensaver/particle.png") ) )
237 tex = convertToGLFormat(buf); // flipped 32bit RGBA
238 kDebug() << "Texture loaded: " << tex.numBytes ();
240 else
242 QImage dummy( 32, 32, QImage::Format_RGB32 );
243 dummy.fill( Qt::white );
244 buf = dummy;
245 tex = convertToGLFormat( buf );
248 /* Set the status to true */
249 //Status = true;
250 glGenTextures(1, &texture[0]); /* create three textures */
251 glBindTexture(GL_TEXTURE_2D, texture[0]);
252 /* use linear filtering */
253 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
254 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
255 /* actually generate the texture */
256 glTexImage2D(GL_TEXTURE_2D, 0, 4, tex.width(), tex.height(), 0,
257 GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
261 return Status;
263 /** setup the GL environment */
264 void Gravity::initializeGL ()
267 kDebug() << "InitGL";
269 if (loadParticle()) // Jump To Texture Loading Routine
271 /* Enable smooth shading */
272 glShadeModel( GL_SMOOTH );
274 /* Set the background black */
275 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
277 /* Depth buffer setup */
278 glClearDepth( 1.0f );
280 /* Enables Depth Testing */
281 glDisable( GL_DEPTH_TEST );
283 /* Enable Blending */
284 glEnable( GL_BLEND );
285 /* Type Of Blending To Perform */
286 glBlendFunc( GL_SRC_ALPHA, GL_ONE );
289 /* Really Nice Perspective Calculations */
290 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
291 /* Really Nice Point Smoothing */
292 glHint( GL_POINT_SMOOTH_HINT, GL_NICEST );
294 /* Enable Texture Mapping */
295 glEnable( GL_TEXTURE_2D );
296 /* Select Our Texture */
297 glBindTexture( GL_TEXTURE_2D, texture[0] );
299 for (loop=0;loop<MAX_PARTICLES;loop++) // Initials All The Textures
301 buildParticle(loop);
304 else
305 exit(0);
307 /** resize the gl view */
308 void Gravity::resizeGL ( int width, int height )
310 kDebug() << "ResizeGL " << width << "," <<height;
311 if (height==0) // Prevent A Divide By Zero By
313 height=1; // Making Height Equal One
316 glViewport(0,0,width,height); // Reset The Current Viewport
318 glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
319 glLoadIdentity(); // Reset The Projection Matrix
321 // Calculate The Aspect Ratio Of The Window
322 gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
324 glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
325 glLoadIdentity();
327 /** paint the GL view */
328 void Gravity::paintGL ()
330 //kDebug() << "PaintGL";
331 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
332 glLoadIdentity();
333 // Reset The ModelView Matrix
334 transIndex++;
335 //glRotatef(transIndex, 1,0,0);
336 //glRotatef(transIndex, 0,1,0);
337 //glRotatef(transIndex, 0,0,1);
338 float xmax = 5.0;
339 float ymax = 5.0;
340 glTranslatef( GLfloat(xmax*sin(3.14*transIndex/360)-xmax),
341 GLfloat(ymax*cos(3.14*transIndex/360)-ymax),
342 0.0 );
343 //glRotatef(transIndex, 0,GLfloat(zmax*cos(3.14*transIndex/360000)), GLfloat(zmax*cos(3.14*transIndex/360000)));
345 for (loop=0;loop<MAX_PARTICLES;loop++) // Loop Through All The Particles
347 if (particle[loop].active) // If The Particle Is Active
349 float x=particle[loop].x; // Grab Our Particle X Position
350 float y=particle[loop].y; // Grab Our Particle Y Position
351 float z=particle[loop].z+zoom; // Particle Z Pos + Zoom
352 /* Select Our Texture */
354 /* Draw The Particle Using Our RGB Values,
355 * Fade The Particle Based On It's Life
357 particle[loop].life=(particle[loop].index/particle[loop].indexo)*2.0f;
358 glColor4f( particle[loop].r,
359 particle[loop].g,
360 particle[loop].b,
361 particle[loop].life );
363 /* Build Quad From A Triangle Strip */
364 if( !stars )
365 glBegin( GL_TRIANGLE_STRIP );
366 else
367 glBegin( GL_TRIANGLE_FAN );
368 /* Top Right */
369 glTexCoord2d( 1, 1 );
370 glVertex3f( x + particle[loop].size, y + particle[loop].size, z );
371 /* Top Left */
372 glTexCoord2d( 0, 1 );
373 glVertex3f( x - particle[loop].size, y + particle[loop].size, z );
374 /* Bottom Right */
375 glTexCoord2d( 1, 0 );
376 glVertex3f( x + particle[loop].size, y - particle[loop].size, z );
377 /* Bottom Left */
378 glTexCoord2d( 0, 0 );
379 glVertex3f( x - particle[loop].size, y - particle[loop].size, z );
380 glEnd( );
381 particle[loop].x=(particle[loop].xo*sin(particle[loop].index))*pow((double) particle[loop].index/particle[loop].indexo,(double) 8.0);
382 particle[loop].y=(particle[loop].yo*sin(particle[loop].index))*pow((double) particle[loop].index/particle[loop].indexo,(double) 8.0);
383 particle[loop].z=(particle[loop].zo*sin(particle[loop].index))*pow((double) particle[loop].index/particle[loop].indexo,(double) 8.0);
384 particle[loop].index-=0.05;
385 if (particle[loop].index<0.0f ) // If Particle Is Burned Out
387 buildParticle(loop);
389 // Lets stir some things up
393 glFlush();
395 void Gravity::setSize( float newSize )
397 size = newSize;
399 void Gravity::setStars( bool doStars )
401 stars = doStars;
404 void KGravitySaver::updateSize(int newSize)
406 gravity->setSize(newSize/100);
408 void KGravitySaver::doStars(bool starState)
410 gravity->setStars(starState);
413 void Gravity::buildParticle(int loop)
415 GLfloat colors[12][3]=
416 {{1.0f,0.5f,0.5f},{1.0f,0.75f,0.5f},{1.0f,1.0f,0.5f},{0.75f,1.0f,0.5f},
417 {0.5f,1.0f,0.5f},{0.5f,1.0f,0.75f},{0.5f,1.0f,1.0f},{0.5f,0.75f,1.0f},
418 {0.5f,0.5f,1.0f},{0.75f,0.5f,1.0f},{1.0f,0.5f,1.0f},{1.0f,0.5f,0.75f}};
419 col = ( ++col ) % 12;
420 particle[loop].active=true;
421 particle[loop].index=KRandom::random()%100;
422 particle[loop].indexo=particle[loop].index;
423 particle[loop].fade=float(KRandom::random()%100)/1000.0f+0.003f; // Random Fade Value
424 particle[loop].r=colors[col][0]; // Select Red From Color Table
425 particle[loop].g=colors[col][1]; // Select Green From Color Table
426 particle[loop].b=colors[col][2]; // Select Blue From Color Table
427 particle[loop].size=size;
428 particle[loop].x = float(KRandom::random()%100-50)*4.0;
429 particle[loop].y = float(KRandom::random()%20-10)*4.0;
430 particle[loop].z = float(KRandom::random()%100-50)*4.0;
431 particle[loop].xo = particle[loop].x;
432 if ((1+(KRandom::random() % 10) > 5))
433 particle[loop].yo = particle[loop].y;
434 else
435 particle[loop].yo = 0.0;
436 particle[loop].zo = particle[loop].z;