1 //-----------------------------------------------------------------------------
3 // kgravity - Partical Gravity Screen Saver for KDE 2
5 // Copyright (c) Ian Reinhart Geiser 2001
7 // KConfig code and KScreenSaver "Setup..." improvements by
8 // Nick Betcher <nbetcher@usinternet.com> 2001
13 #include <kapplication.h>
16 #include <kcolordialog.h>
17 #include <kcolorbutton.h>
19 #include "gravity.moc"
21 #include <OpenGL/glu.h>
22 #include <OpenGL/gl.h>
30 #include <qradiobutton.h>
32 #include <kstandarddirs.h>
34 #include <kmessagebox.h>
36 // libkscreensaver interface
37 class KGravitySaverInterface
: public KScreenSaverInterface
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
)
73 preview
->setFixedSize( 220, 170 );
76 palette
.setColor( preview
->backgroundRole(), Qt::black
);
77 preview
->setPalette( palette
);
80 preview
->show(); // otherwise saver does not get correct size
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()
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);
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" );
129 if (RadioButton1_2
->isChecked() == true)
131 config
.writeEntry( "Stars", "false" );
134 config
.writeEntry( "StarSize", QString::number(SpinBox1
->value()) );
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
)
154 timer
= new QTimer( this );
155 timer
->setSingleShot(true);
159 palette
.setColor( backgroundRole(), Qt::black
);
160 setPalette( palette
);
163 gravity
= new Gravity();
168 connect( timer
, SIGNAL(timeout()), this, SLOT(blank()) );
171 KGravitySaver::~KGravitySaver()
176 // read configuration settings from config file
177 void KGravitySaver::readSettings()
183 void KGravitySaver::blank()
188 timer
->setSingleShot(true);
192 Gravity::Gravity( QWidget
* parent
) : QGLWidget (parent
)
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") {
208 if (boolval
== "false")
214 QString starammount
= config
.readEntry("StarSize", "75");
215 float passvalue
= (starammount
.toInt() / 100.0);
222 glDeleteTextures( 1, &texture
[0] );
223 gluDeleteQuadric(obj
);
226 /** load the particle file */
227 bool Gravity::loadParticle()
229 /* Status indicator */
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 ();
242 QImage
dummy( 32, 32, QImage::Format_RGB32
);
243 dummy
.fill( Qt::white
);
245 tex
= convertToGLFormat( buf
);
248 /* Set the status to 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());
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
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
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
333 // Reset The ModelView Matrix
335 //glRotatef(transIndex, 1,0,0);
336 //glRotatef(transIndex, 0,1,0);
337 //glRotatef(transIndex, 0,0,1);
340 glTranslatef( GLfloat(xmax
*sin(3.14*transIndex
/360)-xmax
),
341 GLfloat(ymax
*cos(3.14*transIndex
/360)-ymax
),
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
,
361 particle
[loop
].life
);
363 /* Build Quad From A Triangle Strip */
365 glBegin( GL_TRIANGLE_STRIP
);
367 glBegin( GL_TRIANGLE_FAN
);
369 glTexCoord2d( 1, 1 );
370 glVertex3f( x
+ particle
[loop
].size
, y
+ particle
[loop
].size
, z
);
372 glTexCoord2d( 0, 1 );
373 glVertex3f( x
- particle
[loop
].size
, y
+ particle
[loop
].size
, z
);
375 glTexCoord2d( 1, 0 );
376 glVertex3f( x
+ particle
[loop
].size
, y
- particle
[loop
].size
, z
);
378 glTexCoord2d( 0, 0 );
379 glVertex3f( x
- particle
[loop
].size
, y
- particle
[loop
].size
, z
);
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
389 // Lets stir some things up
395 void Gravity::setSize( float newSize
)
399 void Gravity::setStars( bool 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
;
435 particle
[loop
].yo
= 0.0;
436 particle
[loop
].zo
= particle
[loop
].z
;