Register / Login

documentation : ofBaseApp.h

This document refers to version 0.06

Show advanced?
yes / no

Classes

ofBaseApp

The openframeworks engine is contained in the "app" category. The project works, similar to processing, in that you have a base class which extends a class that already exists. In the case of OF, there is a class called "ofBaseApp" which contains various event driven functions. When you create an OF project, you use a main.cpp which is the boot-strap, that kicks off the application and another class, which inherits the properties of ofBaseApp. Essentially, when you write code in the testApp, you are re-writing already defined functions that exist in OF, such as update, draw, etc.

In versions pre 0.06 this class was called ofSimpleApp

ofBaseApp Functions

ofBaseApp::setup
syntax
setup()
returns
void
description
This function gets called once, just at the start of the app. It would be a good place, for example, to allocate variables or load in any files.
ofBaseApp::update
syntax
update()
returns
void
description
This function gets called repeatedly. It gets just before draw, so it is an ideal place to do any updating of variables. For example, imagine you have a varibale already defined in your testApp.h called "xpos"


void setup(){
xpos = 0;
}

void update(){
xpos += 1;
if (xPos > ofGetWidth()) xPos = 0;
}

void draw(){
ofRect(xPos, 30,10,10);
}
ofBaseApp::draw
syntax
draw()
returns
void
description
This function gets called regularly just after update. It's where you draw things:


void draw(){
ofSetColor(255,255,255);
ofNoFill();
ofRect(20,20,100,100);
}
ofBaseApp::exit
syntax
exit()
returns
void
description
Add this function to your testApp to have it called at the moment before the app is terminated. This is useful for doing cleanup stuff or making sure files are saved before the app terminates.
ofBaseApp::windowResized
syntax
windowResized(int w, int h)
returns
void
description
This function gets called when ever we resize the application window. You receive the new width (w) and the new height (h) of the window.

ofBaseApp::keyPressed
syntax
keyPressed(int key)
returns
void
description
This function gets called when a key is pressed. The value key can be tested against:

void keyPressed(int key){

if (key == 't'){
; // do something
} else if (key == ' '){
; // do something else
}
}



There are more complicated character codes, for keys such as F1-F12, Down, Enter: OF_KEY_BACKSPACE, OF_KEY_RETURN, OF_KEY_PRINTSCR, OF_KEY_F1 - OF_KEY_F12, OF_KEY_LEFT, OF_KEY_UP, OF_KEY_RIGHT, OF_KEY_DOWN, OF_KEY_PAGE_UP, OF_KEY_PAGE_DOWN, OF_KEY_HOME, OF_KEY_END, OF_KEY_INSERT
ofBaseApp::keyReleased
syntax
keyReleased(int key)
returns
void
description
This function gets called when a key is released. The value key can be tested against:

void keyReleased(int key){

if (key == 't'){
; // do something
} else if (key == ' '){
; // do something else
}
}

There are more complicated character codes, for keys such as F1-F12, Down, Enter: OF_KEY_BACKSPACE, OF_KEY_RETURN, OF_KEY_PRINTSCR, OF_KEY_F1 - OF_KEY_F12, OF_KEY_LEFT, OF_KEY_UP, OF_KEY_RIGHT, OF_KEY_DOWN, OF_KEY_PAGE_UP, OF_KEY_PAGE_DOWN, OF_KEY_HOME, OF_KEY_END, OF_KEY_INSERT
ofBaseApp::mouseMoved
syntax
mouseMoved(int x, int y)
returns
void
description
This function gets when ever the mouse moves. You receive the x and y corrdinates of the mouse.
ofBaseApp::mouseDragged
syntax
mouseDragged(int x, int y, int button)
returns
void
description
This function gets called when the mouse is moving and the button is down. The button variable can be used to test against left or right button drags. You also receive the x and y corrdinates of the mouse.
ofBaseApp::mousePressed
syntax
mousePressed(int x, int y, int button)
returns
void
description
This function gets called when the mouse is pushed down. The button (left, right, center) is passed in, along with the x and y corrdinate.
ofBaseApp::mouseReleased
syntax
mouseReleased()
returns
void
description
This function gets called when the mouse is released.
ofBaseApp::mouseReleased
syntax
mouseReleased(int x, int y, int button)
returns
void
description
This function gets called when the mouse is released. The button (left, right, center) is passed in, along with the x and y corrdinate.
ofBaseApp::audioReceived
syntax
audioReceived(float * input, int bufferSize, int nChannels)
returns
void
description
If you have setup audio input, via ofSetupAudio, this function will be called when you have one buffers worth of audio. Since you may have requested mutli-channel audio (for example, stereo input), you get not only an array of floating point info, also the size of the buffer, and the number of channels. The size of input (the float array with audio data) is: bufferSize * nChannels; The data will come interleaved, so if, for example, you request 2 channel (left / right) input, the samples come:

l r l r l r l r l r ....
0 1 2 3 4 5 6 7 8 9 ....

so to access an individual sample "n" for channel "j" , you could write something like:

input[n*(nChannels) + j];
ofBaseApp::audioRequested
syntax
audioRequested(float * output, int bufferSize, int nChannels)
returns
void
description
If you have setup audio output, via ofSetupAudio, this function will be called when the system needs one buffers worth of audio. Since you may have requested mutli-channel audio (for example, stereo output), you get not only an array of floating point info, also the size of the buffer, and the number of channels.

ofBaseApp Variables

ofBaseApp::mouseX
type
int
description
Variable mouseX contains information about the current x coordinate of the mouse.
ofBaseApp::mouseY
type
int
description
Variable mouseY contains information about the current y coordinate of the mouse.