This problem is driving me crazy - I've just spent the past hour trying to figure out a problem that shouldn't be happening. I've narrowed it down to the z variable in the struct not being updated by the Update() function. I do know the function is being called because I put a printf command in there and it produced results. Any help would be great.
Edit: Tried having the compiler check for all warnings. Fixed a few things it reported but the problem remains.
Code:
/*
* Starfield screensaver
*/
#include <stdio.h>
#include <time.h>
#include <vector>
using namespace std;
#include <GL/gl.h>
#include <GL/glu.h>
#include "SDL.h"
#define STARS_COUNT 1000
// Star
struct Star {
float x;
float y;
float z;
float brightness;
Star() { Randomize(); };
void Randomize() {
x = (rand() % 10000 - 5000) / 1000.0;
y = (rand() % 10000 - 5000) / 1000.0;
z = (rand() % 100) / -100.0;
brightness = (rand() % 10) / 7.5;
};
void Render() {
glColor3f(brightness, brightness, brightness);
glVertex3f(x, y, z);
};
void Update() {
z += 0.1;
};
};
// Initialises SDL and OpenGL
int InitVideo() {
// SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) { return 1; }
SDL_WM_SetCaption("Starfield Screensaver", "Starfield Screensaver");
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
const SDL_VideoInfo *videoinfo = SDL_GetVideoInfo();
if (!videoinfo) { return 1; }
if (SDL_SetVideoMode(0, 0, 0, SDL_OPENGL | SDL_FULLSCREEN) == NULL) { return 1; }
// OpenGL
glViewport(0, 0, videoinfo->current_w, videoinfo->current_h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)videoinfo->current_w / (GLfloat)videoinfo->current_h, 1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return 0;
}
// Handles the events from the keyboard and mouse
int ProcessEvents() {
static Uint32 movement;
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
case SDL_KEYUP:
case SDL_KEYDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEBUTTONDOWN:
return 1;
case SDL_MOUSEMOTION:
if (movement > 5) { return 1; } else { movement++; }
break;
default:
break;
}
}
return 0;
}
// Renders the stars
void Render(vector <Star> stars) {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Camera
gluLookAt(0.0, 0.0, 5.0,
0.0, 0.0,-5.0,
0.0, 1.0, 0.0);
// Render the stars
glBegin(GL_POINTS);
for (unsigned int index = 0; index < stars.size(); index++) {
stars[index].Render();
}
glEnd();
SDL_GL_SwapBuffers();
}
// Updates the stars
void Update(vector <Star> stars) {
for (unsigned int index = 0; index < stars.size(); index++) {
stars[index].Update();
}
}
int main(int argc, char *argv[]) {
if (InitVideo()) { return 1; }
vector <Star> stars;
for (int index = 0; index < STARS_COUNT; index++) { Star tempstar; stars.push_back(tempstar); }
Uint8 running = true;
while (running) {
Render(stars);
Update(stars);
if (ProcessEvents() == 1) { running = false; }
SDL_Delay(10);
}
SDL_Quit();
return 0;
}