-1

I have recently decided to try to learn c++. My major road block so far is object/class declaration I am reasonably experienced in java but the way c++ works confuses me. Can someone please show me a example of using a object (like the one below if possible) inside one file or multiple files? here is my current code (object is not implemented).

//inclusions
#include <GLUT/glut.h>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//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
{
   private:
      int x;
      int y;
      int speed;

      Ant() { } // private default constructor

   public:
      Ant(int nx, int ny, int nspeed)
      {
         SetX(nx);
         SetY(ny);
         SetSpeed(nspeed);
      }
      void SetX(int nx)
      {
         x = nx;
      }
      void SetY(int ny)
      {
         y = ny;
      }
      void SetSpeed(int nspeed)
      {
         speed = nspeed;
      }
      int GetX() { return x; }
      int GetY()  { return y; }
      int GetSpeed() { return speed; }
};
//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;
   }
}


//drawing methods

void line1()
{
   if(a == 1)
   {
      glLineWidth(2.5);
      glBegin(GL_LINES);
      glVertex2i(20,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 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(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);
   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;
}
SandBag_1996
  • 1,570
  • 3
  • 20
  • 50
  • You have a whole bunch of code that uses what seems like open GL calls. I didn't see anything using `Ant`. I couldn't find anything sensible to answer your question with. – R Sahu Mar 19 '15 at 22:23
  • Suggestion to start [with a good book](http://stackoverflow.com/a/388282/332733) – Mgetz Mar 24 '15 at 16:25

1 Answers1

1

Your class definition is correct (although a bit Javaish). You can create and use an object of type Ant like this:

Ant a(3, 7, 82);
a.SetSpeed(42);
int speed = a.GetSpeed();
Emil Laine
  • 41,598
  • 9
  • 101
  • 157