GETTING STARTED
Download and set up OpenFrameworks as per the instructions .
During the day we are going to build some programs, either clicking along with an example on screen or labs.
HELLO WORLD
testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
};
#endif
testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
cout << "This is ground control to Major Tom, commencing countdown, engines on..." << endl; // just getting started
ofBackground(0,0,0); // background color
ofSetColor(255,255,255); // text white
}
//--------------------------------------------------------------
void testApp::update(){
cout << "Can you hear me major Tom?" << endl;
}
//--------------------------------------------------------------
void testApp::draw(){
ofDrawBitmapString("I am floating in the most peculiar way", mouseX, mouseY);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
HELLO GLOBE
testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
int diameter;
};
#endif
testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
cout << "This is ground control to Major Tom, commencing countdown, engines on..." << endl; // just getting started
diameter=50;
ofBackground(0,0,0); // background color
ofSetColor(255,255,255); // text white
}
//--------------------------------------------------------------
void testApp::update(){
// cout << "Can you hear me major Tom?" << endl;
}
//--------------------------------------------------------------
void testApp::draw(){
ofCircle(mouseX, mouseY, diameter); // draw circle
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
cout << key << endl; // just for testing purposes
if(key==97) diameter++; // a pressed
if(key==122) diameter--; // z pressed
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
ofNoFill();
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
ofFill();
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
HELLO PLANET
Note: you need to put this picture in your bin/data directory in your project directory and call it planet.png
testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
ofImage planet;
};
#endif
testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
cout << "This is ground control to Major Tom, commencing countdown, engines on..." << endl; // just getting started
planet.loadImage("planet.png");
planet.setAnchorPercent(0.5, 0.5);
ofBackground(0,0,0); // background color
ofSetColor(255,255,255); // text white
}
//--------------------------------------------------------------
void testApp::update(){
// cout << "Can you hear me major Tom?" << endl;
}
//--------------------------------------------------------------
void testApp::draw(){
planet.draw(mouseX, mouseY);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
BOUNCING PLANET
testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
int x, y, vx, vy;
ofImage planet;
};
#endif
testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
cout << "This is ground control to Major Tom, commencing countdown, engines on..." << endl; // just getting started
x=ofGetWidth()/2;
y=ofGetHeight()/2;
vx=1;
vy=1;
ofBackground(0,0,0); // background color
ofSetColor(255,255,255); // text white
planet.loadImage("planet.png");
planet.setAnchorPercent(0.5, 0.5);
}
//--------------------------------------------------------------
void testApp::update(){
x+=vx; y+=vy;
// if (x < 0) x = ofGetWidth(); // if it drops off, appear on the other side
// if (y < 0) y = ofGetHeight();
// if (x > ofGetWidth()) x=0;
// if (y > ofGetHeight()) y = 0;
if (x < 0) {
x = 0;
vx=-vx;
}
if (y < 0) {
y = 0;
vy=-vy;
}
if (x > ofGetWidth()) {
x=ofGetWidth();
vx=-vx;
}
if (y > ofGetHeight()) {
y = ofGetHeight();
vy=-vy;}
}
//--------------------------------------------------------------
void testApp::draw(){
planet.draw(x, y);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
BOUNCING PLANETS
testApp.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
int n;
ofImage planet[10];
ofPoint pos[10], speed[10];
};
#endif
testApp.cpp
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
cout << "This is ground control to Major Tom, commencing countdown, engines on..." << endl; // just getting started
n=10;
for(int i=0; i<n; i++) {
pos[i].x=ofRandom(0,ofGetWidth());
pos[i].y=ofRandom(0,ofGetHeight());;
speed[i].x=ofRandom(-0.5, 0.5);
speed[i].y=ofRandom(-0.5,0.5);
cout << pos[i].x << ", " << pos[i].y << endl;
planet[i].loadImage("planet.png");
planet[i].setAnchorPercent(0.5, 0.5);
}
}
//--------------------------------------------------------------
void testApp::update(){
for(int i=0; i<n; i++) {
pos[i].x+=speed[i].x;
pos[i].y+=speed[i].y;
if (pos[i].x < 0) {
pos[i].x = 0;
speed[i].x=-speed[i].x;
}
if (pos[i].y < 0) {
pos[i].y = 0;
speed[i].y=-speed[i].y;
}
if (pos[i].x > ofGetWidth()) {
pos[i].x=ofGetWidth();
speed[i].x=-speed[i].x;
}
if (pos[i].y > ofGetHeight()) {
pos[i].y = ofGetHeight();
speed[i].y=-speed[i].y;}
}
}
//--------------------------------------------------------------
void testApp::draw(){
ofBackground(0,0,0); // background color
ofSetColor(255,255,255); // text white
// cout << "Can you hear me major Tom?" << endl;
for (int i=0; i<n; i++) planet[i].draw(pos[i].x, pos[i].y);
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}