Sign in to follow this  
petercnm

passing arguments

Recommended Posts

petercnm    172
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

Share this post


Link to post
Share on other sites
Drew_Benton    1861
You will need to add:

#define MAX_PARTICLES 1000 // Number Of Particles To Create
extern particles particle[MAX_PARTICLES];


To your "keyboard input detect" .CPP file and remove the other #define MAX_PARTICLES.

I would suggest to a header file and include it in your "keyboard input detect" .CPP file as well as the other .cpp file.

This header file should have at least the:

#include <SDL/SDL.h>
#include "Events.h"

extern SDL_Event event;
extern int done;
extern float xspeed;
//extern struct particles particle;
extern particles particle;

#define MAX_PARTICLES 1000 // Number Of Particles To Create


Code in it. Don't forget to use include gaurds! The idea is to have anything that is being shared prototyped in one header file and using that in all the files that needs them. You should also add in your structures to the header file as well.

Share this post


Link to post
Share on other sites
petercnm    172
thanks a lot, got rid of the "invalid arguments.."
I got everything now in my eventes.h file ( also structs), and i use
#ifndef EVENTS_H
#define EVENTS_H
...
...
...
#endif

but still i'm getting "multiple definition of 'particle'", is there something wrong here in the way i'm using include guards ?
events.h is included in 'main.c' and 'events.cpp'.

Share this post


Link to post
Share on other sites
Drew_Benton    1861
What's happening is that the particles is being defined more than once.

Make sure you have: extern particles particle[MAX_PARTICLES]; in the header file, and then in one of the other files, have particles particle[MAX_PARTICLES]; I have a typo in my first post where I didn't say to use the [MAX_PARTICLES].

To do this, make sure you moved the structure to the header file as well.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this