Basic syntax for writing classes

Can someone help me understand the basic procedure / syntax for writing classes?

To make it simple, I’d like to write a small ‘ball’ class and I’m imagining something like this:

  
  
class Ball{  
  Ball(){  
    // set up some vars here  
  }  
  
  void update(){  
    // do some movement here  
  }  
  
  void draw(){  
    // render some stuff here  
  }  
}  
  

(apologies for pseudo-processing syntax).

I’ve made a ball bounce around the screen in this project (download project folder here: www.okdeluxe.co.uk/-files/-MK-ball-objects.zip ) … and I’ve had a look through the example files coming with OF 0.04, but I can’t seem to find any examples of using (silly, little) simple classes.

I’m using XCode 3.0 and I’ve found the ‘Add new file’ button, I’ve also added a new .h and .cpp file to my project … but, from here I’m stuck … can someone help me understand how to proceed?

Cheers,
Mikkel

Ha! … here I go replying to my own post.

I found a project that involved a custom class .h and .cpp file(s) here and started poking around a bit…
http://openframeworks.cc/forum/viewtopi-…-class#1501

…in particular in the ofCamera class to try and understand how to do classes and such - and I got a bit further, but still not quite there - I’m simply too new to C++ - I’m stumbling around at random! Not sure if this was a good example to copy, but at least it got me started.

Anyway, have a look at this project-folder, there are now two new files ofBall.h and ofBall.cpp: www.okdeluxe.co.uk/-files/-MK-ball-obje-…-lasses.zip

Basically what I’ve done is this:

Code for ofBall.h:

  
  
#ifndef _OF_BALL  
#define _OF_BALL  
  
#include "ofMain.h"  
  
class ofBall {  
	private:  
		// vars  
		float x;  
		float y;  
		float speedX;  
		float speedY;  
	  
	public:	  
		// methods  
		void update();  
		void draw();  
		  
	// constructor  
	ofBall();  
};  
#endif  
  

Code for ofBall.cpp

  
  
#include "ofBall.h"  
  
ofBall::ofBall(){  
	x = ofGetScreenWidth()/2;  
	y = ofGetScreenHeight()/2;  
	speedX = ofRandom(-5, 5);  
	speedY = ofRandom(-5, 5);  
};  
  
void ofBall::update(){  
	x += speedX;  
	y += speedY;  
	  
	if(x < 0 || x > ofGetWidth())		speedX *= -1;  
	if(y < 0 || y > ofGetHeight())		speedY *= -1;  
}  
  
void ofBall::draw(){  
	  
	ofSetColor(0xFFBB00);  
	ofCircle(x, y, 20);  
	  
	string pos = ofToString(x, 0) + ", " + ofToString(y, 0) ;  
	ofDrawBitmapString(pos, x-30,y+40);  
	  
	ofSetColor(0x00AEEF);  
}  
  

Then I’ve tried to instantiate and make calls to it from all the right places … I’ve managed to compile a version that runs, only problem is, the ofBall class has NO EFFECT what so ever…

Any ideas/suggestions/recommendations?

Cheers,
Mikkel[http://forum.openframeworks.cc/index.php/topic,363.msg1501.html#msg1501]http://forum.openframeworks.cc/index.php/topic,363.msg1501.html#msg1501[/url]](http://forum.openframeworks.cc/index.php/topic,363.msg1501.html#msg1501]http://forum.openframeworks.cc/index.php/topic,363.msg1501.html#msg1501[/url])

Basically what I’ve done is this:

Code for ofBall.h:

  
  
#ifndef _OF_BALL  
#define _OF_BALL  
  
#include "ofMain.h"  
  
class ofBall {  
	private:  
		// vars  
		float x;  
		float y;  
		float speedX;  
		float speedY;  
	  
	public:	  
		// methods  
		void update();  
		void draw();  
		  
	// constructor  
	ofBall();  
};  
#endif  
  

Code for ofBall.cpp

  
  
#include "ofBall.h"  
  
ofBall::ofBall(){  
	x = ofGetScreenWidth()/2;  
	y = ofGetScreenHeight()/2;  
	speedX = ofRandom(-5, 5);  
	speedY = ofRandom(-5, 5);  
};  
  
void ofBall::update(){  
	x += speedX;  
	y += speedY;  
	  
	if(x < 0 || x > ofGetWidth())		speedX *= -1;  
	if(y < 0 || y > ofGetHeight())		speedY *= -1;  
}  
  
void ofBall::draw(){  
	  
	ofSetColor(0xFFBB00);  
	ofCircle(x, y, 20);  
	  
	string pos = ofToString(x, 0) + ", " + ofToString(y, 0) ;  
	ofDrawBitmapString(pos, x-30,y+40);  
	  
	ofSetColor(0x00AEEF);  
}  
  

Then I’ve tried to instantiate and make calls to it from all the right places … I’ve managed to compile a version that runs, only problem is, the ofBall class has NO EFFECT what so ever…

Any ideas/suggestions/recommendations?

Cheers,
Mikkel

Okay I know what the problem is. The class looks good btw.

  1. ofGetScreenWidth() ofGetScreenHeight() return your monitor width and height.
    use ofGetWidth() ofGetHeight() for your window width and height.

  2. your speed is way too fast. You app will try and run as fast as it can and that means it could run up to 200-500 frames per second. if you are moving at even 3 pixels a frame you could have moved by more than a 1000 pixels in one second.
    so try putting ofSetVerticalSync(true); in your testApp::setup() this will limit your fps to your monitor refresh rate which will be aprox 60 fps. Also try reducing the speed value maybe ofRandom(-1, 1); ?

  
   if(x < 0 || x > ofGetWidth())      speedX *= -1;   
   if(y < 0 || y > ofGetHeight())      speedY *= -1;   

won’t work - this is because you are saying “if my object is off screen reverse direction” - but consider a screen that is 1024 pixels wide and your object is at an x position of 1032 - the speed is reversed and so now it will be heading back into the screen but the next frame it has only moved 4 pixels - so it is at 1028 . this still means it is off screen and greater than the screen width so the speed gets flipped again and the direction is reversed (now going away from the screen).

So what you need is:

  
  
  
if(x < 0 ){  
      x = 0;  
      speedX *= -1;   
}else if( x > ofGetWidth() ){  
      x = ofGetWidth();  
      speedX *= -1;   
}  
  
  

And then the same for y.
The even better way to do it is to move it back into the screen by the same amount it has gone passed the screen which at very high speeds will give you an accurate position of where the ball would be if it bounced off the edge.

Hope that helps!
Theo

Thanks Theo!..

I’ve changed those few bits but I’m still not seeing the ball-object on the screen - I’m thinking it’s to do with me not understanding ANYTHING about C++ (this is my first attempt!) and that I probably aren’t instantiating the ball-object and calling it’s methods correctly. Hmm. Not sure if it helps, but here’s the code from the testApp.h and testApp.cpp files:

testApp.h

  
  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
#include "ofBall.h"  
  
  
class testApp : public ofSimpleApp{  
	  
	public:  
		  
		void setup();  
		void update();  
		void draw();  
		  
  
	private:  
	  
		float speedX;  
		float speedY;  
		  
		float x;  
		float y;  
		  
		ofBall* ballA;  
		  
};  
  
#endif	  
  

testApp.cpp

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){	   
	ofBackground(0, 0, 0);  
		  
	ofSetFrameRate(30);	  
	ofSetWindowTitle("simple bouncing ball");  
		  
	ofEnableSmoothing();  
	ofNoFill();  
	ofSetColor(0x00AEEF);  
	  
	speedX = ofRandom(-5, 5);  
	speedY = ofRandom(-5, 5);  
	  
	x = ofGetWidth()/2;  
	y = ofGetHeight()/2;  
	  
	ballA = new ofBall();  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
	x += speedX;  
	y += speedY;  
	  
	if(x < 0 ){  
		x = 0;  
		speedX *= -1;  
	}else if( x > ofGetWidth() ){  
		x = ofGetWidth();  
		speedX *= -1;  
	}   
	  
	if(y < 0 ){  
		y = 0;  
		speedY *= -1;  
	}else if( y > ofGetHeight() ){  
		y = ofGetHeight();  
		speedY *= -1;  
	}   
  
	ballA->update();  
	ballA->draw();  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	ofCircle(x, y, 20);  
	  
	string pos = ofToString(x, 0) + ", " + ofToString(y, 0) ;  
	ofDrawBitmapString(pos, x-30,y+40);  
	  
}  
  

Basically I’ve copy/pasted a bunch of things in from the ofCamera example I linked to above (and changed it to ofBall) … problem is, I don’t know what all those things do (things such as “#ifndef _TEST_APP” and “#define _TEST_APP” … and “ofBall* ballA;”) … I know I need to read some C++ for dummies (and have found a couple of good links on this forum so far) … I just want to play NOW :wink: and this is usually the way I learn things.

Any ideas to what I can do to the code above?

Cheers,
Mikkel

ps - and also the code from the ‘main.cpp’ file here:

main.cpp

  
  
#include "ofMain.h"  
#include "testApp.h"  
  
//========================================================================  
int main( ){  
  
	ofSetupOpenGL(720,360, OF_WINDOW);			// <-------- setup the GL context  
	  
	// this kicks off the running of my app  
	// can be OF_WINDOW or OF_FULLSCREEN  
	// pass in width and height too:  
	  
	ofRunApp(new testApp());  
	  
}  
  

pps - and here’s the project folder: www.okdeluxe.co.uk/-files/-MK-ball-obje-…-ses-02.zip

everything looks perfect except one small thing:

  
  
//--------------------------------------------------------------  
void testApp::update(){  
   ....  
   ballA->update();  
   ballA->draw();  
}  
  

you are calling the draw of the ball in testApp’s update. Update gets called before draw, and no drawing you do will show up.

move ball->draw to testApp’s draw routine any you will be good to go !!

hope that helps!
zach

no worries - you’re pretty close

Now its easy to see.
You don’t need anything but the ball stuff in testApp.

Most importantly:

  
   ballA->draw();   

should be in the testApp::draw()
not at the end of testAPP::update()

openFrameworks will only draw stuff that is in the testApp::draw function.

so:

  
#include "testApp.h"   
  
//--------------------------------------------------------------   
void testApp::setup(){      
   ofBackground(0, 0, 0);   
         
   ofSetFrameRate(30);      
   ofSetWindowTitle("simple bouncing ball");   
      
   ballA = new ofBall();   
}   
  
//--------------------------------------------------------------   
void testApp::update(){   
     
   ballA->update();   
}   
  
//--------------------------------------------------------------   
void testApp::draw(){   
  
   ofEnableSmoothing();   
   ofNoFill();   
  
   ofSetColor(0x00AEEF);   
   ballA->draw();   
      
}  

Should do it.

make sure the ball update function looks like this

  
      ball::update(){  
   if(x < 0 ){   
      x = 0;   
      speedX *= -1;   
   }else if( x > ofGetWidth() ){   
      x = ofGetWidth();   
      speedX *= -1;   
   }   
      
   if(y < 0 ){   
      y = 0;   
      speedY *= -1;   
   }else if( y > ofGetHeight() ){   
      y = ofGetHeight();   
      speedY *= -1;   
   }   
  
   //this should be after the check  
   x += speedX;   
   y += speedY;   
  
}  
  
  

theo

managed to move this demo a little bit further … it now includes the balls drawing lines to eachother… thanks guys for helping.

here’s the source code
http://www.openframeworks.cc/tinker/mik-…-s-demo.zip

cheers,
mikkel