-4

I have been trying to make a simulator which involves ants randomly running around (for now...) I want to make a array of "Ant" to contain all my ant information and later functions. How would I do that?? (example with my code if possible)(I know java fairly well so if you could relate it to java that would be nice

//inclusions
#include <GLUT/glut.h>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//declarations

//class declarations


void keyboard (unsigned char key, int x, int y);
void keyboardUp (unsigned char key, int x, int y);
void mouseLocation(int,int);
int onMouse;
int rightClick;
int a = 0;
int mx;
int my;
float red=1.0, blue=0, green=0;

class Ant {
    int x;
    int y;
    int type;

    Ant() { } // private default constructor

public:
    Ant(int nx, int ny, int ntype)
    {
        x = nx;
        y = ny;
        type = ntype;
    }
    void SetX(int swagger)
    {
        x = swagger;
    }
    void SetY(int ny)
    {
        y = ny;
    }
    void SetType(int ntype)
    {
        type = ntype;
    }
    int GetX() { return x; }
    int GetY()  { return y; }
};

//mouse listener methods

void mouseLocation(int x,int y) {
    mx = x;
    my = y;
}
void mouseClicks(int button, int state, int x, int y) {
    mx = x;
    my = y;
    if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        onMouse = 1;
    }
    if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
        onMouse = 0;
    }
    if(button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
        rightClick = 0;
    }
    if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
        rightClick = 1;
    }

}

//keylistner methods

void keyboard (unsigned char key, int x, int y)
{
    if (key == 'a')
    {
        a = 1;
    }
}

void keyboardUp (unsigned char key, int x, int y)
{
    if(key == 'a')
    {
        a = 0;
    }
}

//ant methods
int randommove(int position,int speed)
{
    speed++;
    int random = rand() %speed  - speed/2 ;
    position = position + random;
    return position;
}
//drawing methods

Ant ant1(100,100,1);
Ant ant2(50,500,1);
//Here is the where I want to try the ant array something like
//Ant ants[x] and then I would fill in the values with the SetX, SetY... methods
void ant()
{
    int a2 = randommove(ant1.GetX(),2);
    ant1.SetX(a2);
    glLineWidth(1);
    glBegin(GL_LINES);
    glColor4f(1, 0, 0, 1);
    glVertex2i(ant1.GetX(),ant1.GetY());
    glVertex2i(ant1.GetX()+5,ant1.GetY()+5);
    glEnd();
}

void line1()
{

    if(a == 1)
    {
        glLineWidth(2.5);
        glBegin(GL_LINES);
        glVertex2i(80,20);
        glVertex2i(100,400);
        glEnd();
    }
}
void line2()
{
    if(onMouse == 1)
    {
        glLineWidth(2.5);
        glBegin(GL_LINES);
        glVertex2i(200,20);
        glVertex2i(100,400);
        glEnd();
    }
}
void rect1()
{
    if(mx >= 50 && my >= 50)
    {
        int x = 100;
        int y = 100;
        int w = 100;
        int h = 100;
        unsigned int rgba = 0xff0000ff; // red, no alpha
        glBegin(GL_QUADS);
        glColor4f(((rgba>>24)&0xff)/255.0f,
                  ((rgba>>16)&0xff)/255.0f,
                  ((rgba>>8)&0xff)/255.0f,
                  (rgba&0xff)/255.0f);
        glVertex3f(x,y,0);
        glVertex3f(x+w,y,0);
        glVertex3f(x+w,y+h,0);
        glVertex3f(x,y+h,0);
        glEnd();
        glColor4f(1, 1, 1, 1);
    }
}
void rect2()
{
    if(rightClick == 1)
    {
        int w = 100;
        int h = 100;
        unsigned int rgba = 0xff0000ff; // red, no alpha
        glBegin(GL_QUADS);
        glColor4f(((rgba>>24)&0xff)/255.0f,
                  ((rgba>>16)&0xff)/255.0f,
                  ((rgba>>8)&0xff)/255.0f,
                  (rgba&0xff)/255.0f);
        glVertex3f(mx,my,0);
        glVertex3f(mx+w,my,0);
        glVertex3f(mx+w,my+h,0);
        glVertex3f(mx,my+h,0);
        glEnd();
        glColor4f(1, 1, 1, 1);
    }
}
//render drawing methods
void renderScence(void){

    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f(red, green, blue);
    ant();
    line1();
    line2();
    rect1();
    rect2();
    glFlush();

}

//graphics init method
void init(void)
{
    glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
    glutInitWindowSize (500,500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Testing");
    glClearColor (0, 0, 0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 500, 0, 500);
}

//main/graphics calls
int main(int argc,char** argv)
{
    glutInit(&argc, argv);
    init();
    glutDisplayFunc(renderScence);
    glutIdleFunc(renderScence);
    glutMouseFunc(mouseClicks);
    glutPassiveMotionFunc(mouseLocation);
    glutKeyboardUpFunc (keyboardUp);
    glutKeyboardFunc (keyboard);
    glutMainLoop();
    return 0;
}
  • Do you mean you want an array of `Ant` objects? – DigitalNinja Mar 24 '15 at 01:13
  • yes could you show me? – jimbo bob Mar 24 '15 at 01:27
  • I'm sorry I didn't see your comment yesterday. If you're still struggling please see my answer below. – DigitalNinja Mar 24 '15 at 16:18
  • Here's a tip for the future: your question is fairly simple: "how do I make an array of 'Ant'?", and yet you gave us a 226-line program that has about 225 lines that have nothing to do with arrays. It is important to learn to extract only the relevant parts of the code for a question. No one needs to read the whole program to answer your question, so why should you add it? – R. Martinho Fernandes Mar 24 '15 at 16:33

1 Answers1

0

You could create the array of Ant objects inside main, and it's the same way you'd create any other array except it's of type Ant:

 Ant ants[25]; // 25 Ant objects   

 /* to set them all the same, as an example */
 for(i=0; i<25; i++)
 { 
     ants[i].SetX(1); // x = 1
     ants[i].SetY(2); // y = 2
     ants[i].SetType(3); // type = 3
 }
DigitalNinja
  • 1,078
  • 9
  • 17
  • Thanks for the solution I tried this myself but I get (in Xcode on OS X) "Calling a private constructor of class Ant" – jimbo bob Mar 24 '15 at 17:09
  • Try moving the default constructor line `Ant() { } // private default constructor` under `public:` in your `Ant` class. – DigitalNinja Mar 24 '15 at 17:15