In a normal while loop for processing events a specific structure gets
trashed.
Can anyone explain why this could be happening:--------------------------
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <stdio.h>
struct particle {
float x, vx;
float y, vy;
};
int main() {
SDL_Event event;
int done=0;
struct particle falling_obj[2];
falling_obj[0].x=falling_obj[0].vx=0.0f;
falling_obj[0].y=10.0f;
falling_obj[0].vy=-98.0f;
falling_obj[1].x=falling_obj[1].vx=0.0f;
falling_obj[1].y=10.0f;
falling_obj[1].vy=-98.0f;
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(800,600,24,SW_SURFACE | SW_FULLSCREEN | SW_OPENGL);
setup_opengl(); // Just the usual stuff…
while(!done) {
fprintf(stderr, "y:%f vy:%f\n", falling_obj[1].y, falling_obj[1].vy);
while(SDL_PollEvents(&event)) {
if(event.type==SDL_KEYDOWN) {
switch(event.key.keysym.sym) {
case SDL_ESCAPE:
fprintf(stderr, "ESC!\n\n");
done=1;
break;
}
}
}
fprintf(stderr, "y:%f vy:%f\n", falling_obj[1].y, falling_obj[1].vy);
integrate(falling_obj);
}
SDL_Quit();
}
This program prints:
y:10.000000 vy:-98.000000
y:0.000000 vy:0.000000
y:0.000000 vy:0.000000
y:0.000000 vy:0.000000
…
y:0.000000 vy:0.000000
ESC!
Looks like in the while loop of the function SDL_PollEvent the variable
gets trashed, i can say so because i comment that and the program acts
normally (But obviously i can’t interact nor exit the program).
The integrate function takes the pointer to the structure’s array and
iterate over them taking a delta in time adding constant velocity. But the
vars get lost before that happens. And actually if i print the first
array’s structure (falling_obj[0]) everything is ok. The problem seems to
be only with the second structure.
My system is FreeBSD 7 running on a Intel Xeon 2.2 Dual CPU and the SDL
program running over Xorg. Also, my compile line is “gcc -I.
-I/usr/local/lib -lSDL -lGL -lGLU testSDL.c -o testSDL”, and compiles
without errors at all. Any help is appreciated, thanks in advance.