initialize stars array

This commit is contained in:
Josh Holtrop 2011-03-24 13:44:08 -04:00
parent 605483b41c
commit d034c2d667
3 changed files with 25 additions and 7 deletions

View File

@ -35,7 +35,7 @@ int main (int argc, char *argv[])
GnomeScreensaver gs(&argc, &argv, configure, expose, update, 12); GnomeScreensaver gs(&argc, &argv, configure, expose, update, 12);
Mode *modes[] = { Mode *modes[] = {
new Starfield(), new Starfield(gs),
new Spin(), new Spin(),
new Flip() new Flip()
}; };

View File

@ -10,10 +10,13 @@
#include "Starfield.h" #include "Starfield.h"
Starfield::Starfield() Starfield::Starfield(GnomeScreensaver & gs)
{ {
m_last_ticks = 0; uint64_t ticks = gs.getTicks();
m_last_ticks = ticks;
srand(time(NULL) + getpid()); srand(time(NULL) + getpid());
for (int i = 0; i < NUM_STARS; i++)
newStar(i, ticks);
} }
Starfield::~Starfield() Starfield::~Starfield()
@ -22,8 +25,7 @@ Starfield::~Starfield()
bool Starfield::expose (GnomeScreensaver & gs) bool Starfield::expose (GnomeScreensaver & gs)
{ {
if (m_last_ticks == 0) uint64_t ticks = gs.getTicks();
m_last_ticks = gs.getTicks();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, gs.getWidth(), gs.getHeight()); glViewport(0, 0, gs.getWidth(), gs.getHeight());
@ -42,7 +44,7 @@ bool Starfield::configure (GnomeScreensaver & gs)
{ {
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
gluPerspective(60.0, gs.getAspectRatio(), 0.01, 1000.0); gluPerspective(60.0, gs.getAspectRatio(), 0.01, MAX_STAR_DIST);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
@ -65,3 +67,8 @@ void Starfield::getRandomAxis(float (*axis)[3])
(*axis)[2] = cos(beta); (*axis)[2] = cos(beta);
} }
void Starfield::newStar(int idx, uint64_t ticks)
{
m_stars[idx].create_time = ticks;
getRandomAxis(&m_stars[idx].axis);
}

View File

@ -6,18 +6,29 @@
#include "Mode.h" #include "Mode.h"
#include "GnomeScreensaver.h" #include "GnomeScreensaver.h"
#define NUM_STARS 1000
#define MAX_STAR_DIST 1000
class Starfield : public Mode class Starfield : public Mode
{ {
public: public:
Starfield(); Starfield(GnomeScreensaver & gs);
~Starfield(); ~Starfield();
bool expose (GnomeScreensaver & gs); bool expose (GnomeScreensaver & gs);
bool configure (GnomeScreensaver & gs); bool configure (GnomeScreensaver & gs);
bool update (GnomeScreensaver & gs); bool update (GnomeScreensaver & gs);
class Star
{
public:
float axis[3];
uint64_t create_time;
};
protected: protected:
LogoBox m_logobox; LogoBox m_logobox;
uint64_t m_last_ticks; uint64_t m_last_ticks;
Star m_stars[NUM_STARS];
void getRandomAxis(float (*axis)[3]); void getRandomAxis(float (*axis)[3]);
void newStar(int idx, uint64_t ticks);
}; };
#endif /* STARFIELD_H */ #endif /* STARFIELD_H */