Hello,
i'm having a problem passing a variable from within a for loop to another function. The problem is when i do " MyKeyboard(loop)", and in my "keyboard action" function i get :"invalid argument for particle[loop]".
Here's the (stripped) code, if someone would be so kind...
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glext.h>
#include "LoadBMP.h"
#include "Events.h"
#define MAX_PARTICLES 1000 // Number Of Particles To Create
using namespace std;
int loop;
typedef struct // Create A Structure For Particle
{
bool active; // Active (Yes/No)
float life; // Particle Life
float fade; // Fade Speed
float r; // Red Value
float g; // Green Value
float b; // Blue Value
float x; // X Position
float y; // Y Position
float z; // Z Position
float xi; // X Direction
float yi; // Y Direction
float zi; // Z Direction
float xg; // X Gravity
float yg; // Y Gravity
float zg; // Z Gravity
} particles; // Particles Structure
particles particle[MAX_PARTICLES];
int done;
SDL_Event event;
int main(int argc, char *argv[]){
//// initialize each particle ////
for (loop=0;loop<MAX_PARTICLES;loop++)
{
particle[loop].active=true; particle[loop].life=1.0f; particle[loop].fade=float(rand()%100)/1000.0f+0.003f; particle[loop].r=colors[loop*(12/MAX_PARTICLES)][0]; particle[loop].g=colors[loop*(12/MAX_PARTICLES)][1]; particle[loop].b=colors[loop*(12/MAX_PARTICLES)][2]; particle[loop].xi=float((rand()%50)-26.0f)*10.0f; particle[loop].yi=float((rand()%50)-25.0f)*10.0f; particle[loop].zi=float((rand()%50)-25.0f)*10.0f; particle[loop].xg=0.0f; particle[loop].yg=-0.8f; particle[loop].zg=0.0f;
}
///// render the whole thing /////
for(done = 0; !done;){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
/// update each particle ////
for (loop=0;loop<MAX_PARTICLES;loop++) // Loop Through All The Particles
{
MyKeyboard(loop); // look for keyboard inputs
if (particle[loop].active)
{
float x=particle[loop].x; float y=particle[loop].y; float z=particle[loop].z+zoom; glColor4f(particle[loop].r,particle[loop].g,particle[loop].b,particle[loop].life);
glBegin(GL_TRIANGLE_STRIP); glTexCoord2d(1,1); glVertex3f(x+0.5f,y+0.5f,z); glTexCoord2d(0,1); glVertex3f(x-0.5f,y+0.5f,z);
glTexCoord2d(1,0); glVertex3f(x+0.5f,y-0.5f,z); glTexCoord2d(0,0); glVertex3f(x-0.5f,y-0.5f,z); glEnd(); particle[loop].x+=particle[loop].xi/(slowdown*1000 particle[loop].y+=particle[loop].yi/(slowdown*1000 particle[loop].z+=particle[loop].zi/(slowdown*1000
}
}
SDL_GL_SwapBuffers();
} // end game loop
SDL_Quit();
return(0);
} // end of main
and my keyboard input detect:
#include <SDL/SDL.h>
#include "Events.h"
extern SDL_Event event;
extern int done;
extern float xspeed;
extern struct particles particle;
void MyKeyboard (int loop){
while( SDL_PollEvent(&event)){
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
switch (event.type){ // look for a key press
case SDL_KEYDOWN: // check key values
switch ( event.key.keysym.sym) {
case SDLK_u:
if( particle[loop].yg<1.5f)
particle[loop].yg+=0.01f;
break;
case SDLK_d:
if( particle[loop].yg>-1.5f)
particle[loop].yg-=0.01f;
break;
case SDLK_r:
if( particle[loop].xg<1.5f)
particle[loop].xg+=0.01f;
break;
case SDLK_l:
if( particle[loop].xg>-1.5f)
particle[loop].xg-=0.01f;
break;
} // switch Event type
} // poll Event loop
} // end Events
Thanks in advance