2. Utils
date screenshots noise
list helpers remote loading logging
string threading ļ¬le utils
manipulation system tools constants
3. date
int ofGetSeconds() Get seconds of current time
int ofGetMinutes() Get minutes of current time
int ofGetHours() Get hours of current time
int ofGetYear() Get current year
int ofGetMonth() Get current month
int ofGetDay() Get current day
int ofGetWeekDay Get current weekday
unsigned int ofGetUnixTime() Timestamp since 1970
unsigned long ofGetSystemTime() System time in millis
string ofGetSystemTimeMicros() System time in micros
string ofGetTimestampString() Timestamp since 1970 as string
4. list helpers
Remove items from a vector.
void ofRemove(vector<T>&, BoolFunction shouldErase)
Step 1. Create a custom remove functor
class MyOwnRemover{
public:
bool operator()(const string& str) {
return str == "roxlu";
}
};
5. list helpers
void ofRemove(vector<T>&, BoolFunction shouldErase)
Step 2. use ofRemove with remove functor
void testApp::setup(){
// create list with strings.
vector<string> mystrings;
mystrings.push_back("diederick");
mystrings.push_back("openframeworks");
mystrings.push_back("supercool");
mystrings.push_back("roxlu");
mystrings.push_back("do not remove");
mystrings.push_back("roxlu");
// show before removing items.
for(int i = 0; i < mystrings.size(); ++i) {
cout << "before: " << mystrings[i] << endl;
}
cout << "=======" << endl;
// remove items using my own remover!
ofRemove(mystrings, MyOwnRemover());
for(int i = 0; i < mystrings.size(); ++i) {
cout << "after: " << mystrings[i] << endl;
}
}
6. Summary ofRemove list helpers
void ofRemove(vector<T>&, BoolFunction shouldErase)
class MyOwnRemover{ void testApp::setup(){
public:
bool operator()(const string& str) {
// create list with strings.
return str == "roxlu";
vector<string> mystrings;
}
mystrings.push_back("diederick");
};
mystrings.push_back("openframeworks");
mystrings.push_back("supercool");
mystrings.push_back("roxlu");
mystrings.push_back("do not remove");
mystrings.push_back("roxlu");
// show before removing items.
for(int i = 0; i < mystrings.size(); ++i) {
Result
}
cout << "before: " << mystrings[i] << endl;
before: diederick
cout << "=======" << endl;
before: openframeworks
before: supercool
before: roxlu
before: do not remove
// remove items using my own remover!
before: roxlu
ofRemove(mystrings, MyOwnRemover());
=======
after: diederick
for(int i = 0; i < mystrings.size(); ++i) {
after: openframeworks
cout << "after: " << mystrings[i] << endl;
after: supercool
after: do not remove
}
}
7. Randomize a vector list helpers
void ofRandomize(vector<T>&)
void testApp::setup(){
// create list with strings.
vector<string> mystrings;
mystrings.push_back("diederick");
mystrings.push_back("openframeworks"); Result
mystrings.push_back("supercool");
random: roxlu
mystrings.push_back("roxlu");
random: roxlu
mystrings.push_back("do not remove");
random: supercool
mystrings.push_back("roxlu");
random: openframeworks
random: do not remove
ofRandomize(mystrings);
random: diederick
// show before removing items.
for(int i = 0; i < mystrings.size(); ++i) {
cout << "random: " << mystrings[i] << endl;
}
}
8. Sort a vector list helpers
void ofSort(vector<T>&)
void testApp::setup(){
// create list with strings.
vector<string> mystrings;
mystrings.push_back("diederick");
mystrings.push_back("openframeworks"); Result
mystrings.push_back("supercool");
sorted: diederick
mystrings.push_back("roxlu");
sorted: do not remove
mystrings.push_back("do not remove");
sorted: openframeworks
mystrings.push_back("roxlu");
sorted: roxlu
sorted: roxlu
ofSort(mystrings);
sorted: supercool
// show before removing items.
for(int i = 0; i < mystrings.size(); ++i) {
cout << "sorted: " << mystrings[i] << endl;
}
}
9. Find entry in vector list helpers
unsigned int ofFind(vector<T>&, const T& target)
void testApp::setup(){
vector<string> mystrings;
mystrings.push_back("diederick");
mystrings.push_back("openframeworks"); Result
mystrings.push_back("supercool");
mystrings.push_back("roxlu"); do not remove <--
mystrings.push_back("do not remove");
mystrings.push_back("roxlu");
string fstr = "do not remove";
unsigned int num_found = ofFind(mystrings, fstr);
if(num_found) {
cout << mystrings[num_found] << " <-- ā << endl;
}
}
10. Check if entry exists list helpers
bool ofContains(vector<T>&, const T& target)
// create list with strings.
vector<string> mystrings;
mystrings.push_back("diederick");
mystrings.push_back("openframeworks"); Result
mystrings.push_back("supercool");
mystrings.push_back("roxlu");
do not remove in mystrings
mystrings.push_back("do not remove");
mystrings.push_back("roxlu");
string fstr = "do not remove";
bool contains = ofContains(mystrings, fstr);
if(contains) {
cout << fstr << " in mystrings" << endl;
}
13. string
manipulation
Is a string in a string?
bool ofIsStringInString(string haystack, string needle)
string haystack_str = "I'm searching for a needle in this haystack";
if(ofIsStringInString(haystack_str, "searching")) {
cout << "yes, 'searching' is in the string" << endl;
}
Result
yes, 'searching' is in the string
14. string
manipulation
Case conversion
string ofToUpper(const string& src)
string ofToLower(const string& src)
string str = "Just some string";
cout << ofToLower(str) << " = ofToLower" << endl;
cout << ofToUpper(str) << " = ofToUpper" << endl;
Result
just some string = ofToLower
JUST SOME STRING = ofToUpper
15. string
manipulation
Type / hex conversion
Convert int,ļ¬oat,uint,char,??
string ofToString(const T&)
to string
string ofToHex(string|char)
int ofHexToInt(const string&)
string ofHexToChar(const string&)
ļ¬oat ofHexToFloat(const string&)
string ofHexToString(const string&)
16. string
manipulation
String conversions
Convert int,ļ¬oat,uint,char,??
string ofToString(const T&)
to string
int ofToInt(const string& str) Convert string to int
char ofToChar(const string& str) Convert string to char
ļ¬oat ofToFloat(const string& str) Convert string to ļ¬oat
bool ofToBool(const string& str) Convert string to bool
17. string
manipulation
Binary utils
string ofToBinary(const string& str) String to binary
int ofBinaryToInt(const string& str) From binary to int
char ofBinaryToChar(const string& str) From binary to char
ļ¬oat ofBinaryToFloat(const string& str) From binary to ļ¬oat
18. screenshots
Screenshots
Save whatās drawn to a ļ¬le
named by the current frame.
void ofSaveFrame()
The ļ¬le is saved into the data
directory.
Save the current screen to a
void ofSaveScreen(string ļ¬lename)
ļ¬le in the data directory.
void ofSaveViewport(string ļ¬lename) Save current viewport to ļ¬le
19. screenshots
Screenshots
void testApp::draw(){
// create filename (each call we increment the counter)
static int screen_num = 0;
++screen_num;
char buf[512];
sprintf(buf,"screen_%04d.jpg",screen_num);
// draw a simple circle at the center of screen and save it to image file.
ofCircle(ofGetWidth() *0.5,ofGetHeight()*0.5,40);
ofSaveScreen(buf);
}
20. screenshots
Screenshots
void testApp::draw(){
// create filename (each call we increment the counter)
static int screen_num = 0;
++screen_num;
char buf[512];
sprintf(buf,"screen_%04d.jpg",screen_num);
// draw a simple circle at the center of screen and save it to image file.
ofCircle(ofGetWidth() *0.5,ofGetHeight()*0.5,40);
ofSaveScreen(buf);
}
21. remote loading
Load from url (synchronous)
ofHttpResponse ofLoadURL(string url)
ofHttpResponse resp = ofLoadURL("http://roxlu.com/ofloadurl.txt");
cout << resp.data << endl;
Result
This is just a test!
22. remote loading
Load from url (async)
int ofLoadURLAsync(string url, string name)
ā¢ Loads asynchronous
ā¢ Returns ID of process
ā¢ You need to listen for URL notiļ¬cations in
testApp::urlResponse(ofHttpResponse&)
23. remote loading
Asynchronous loading
Step 1. declare urlResponse in class header which wants to get notiļ¬cations
class testApp : public ofBaseApp{
public:
void urlResponse(ofHttpResponse & response);
}
Step 2. deļ¬ne urlResponse in class which wants to get notiļ¬cations
void testApp::urlResponse(ofHttpResponse & response){
if(response.status==200 && response.request.name == "async_req"){
img.loadImage(response.data);
loading=false;
}
else{
cout << response.status << " " << response.error << endl;
if(response.status!=-1) loading=false;
}
}
25. threading
Threading in OF
ā¢ Very easy to create threads using ofThread
ā¢ Locking using lock/unlock() functions
ā¢ Easy start/stop/joining
ā¢ Just implement threadedFunction()
ā¢ I.e. used to perform background processes
26. threading
Creating a thread
Step 1. Create your thread class which extends ofThread
class MyCustomThread : public ofThread {
protected:
virtual void threadedFunction() {
while(true) {
cout << "My own custom thread!" << endl;
ofSleepMillis(3000);
}
}
};
Step 2. Create member in testApp.h
class testApp : public ofBaseApp{
public:
MyCustomThread my_thread;
...
}
27. threading
Creating a thread
Step 3. Start the thread by calling startThread()
void testApp::setup(){
// creates a non-blocking thread. (not verbose)
my_thread.startThread(false, false);
}
28. system tools
System tools
void ofSystemAlertDialog(string) Show system Alert window
ofFileDialogResult ofSystemLoadDialog(string Show system File Load
windowtitle, bool folderselection) window
ofFileDialogResult ofSystemSaveDialog(string Show system File Save
defaultname, string messagename) window
33. logging
Logging
ā¢ Simple logging to console or ļ¬le
ā¢ Support for basic log levels
ā¢ Log level ļ¬ltering
ā¢ More advanced stuff, see ofxLogger
(ļ¬le rotation)
34. logging
Logging
Set level to log. Only this
void ofSetLogLevel(ofLogLevel) level will show up in console
or ļ¬le
ofLogLevel ofGetLogLevel() Get current log level
void ofLogToFile(string path) Log to path
void ofLogToConsole() Log to console (default)
35. logging
Logging
ofSetLogLevel(OF_LOG_VERBOSE);
ofLog(OF_LOG_VERBOSE,"log something");
ofLog(OF_LOG_VERBOSE,"Something to log");
ofLog(OF_LOG_VERBOSE,"Something else to log");
Result
OF: OF_VERBOSE: log something
OF: OF_VERBOSE: Something to log
OF: OF_VERBOSE: Something else to log
36. logging
Logging
Set level to log. Only this
void ofSetLogLevel(ofLogLevel) level will show up in console
or ļ¬le
ofLogLevel ofGetLogLevel() Get current log level
void ofLogToFile(string path) Log to path
void ofLogToConsole() Log to console (default)
37. ļ¬le utils
File utils
ofBuffer Raw byte buffer class
ofFilePath A ļ¬le system path
ofFile File class; handy functions
ofDirectory Directory class; i.e. dirlists
38. ļ¬le utils
ofBuffer
ofBuffer
ā¢ Allows you to store raw data
ā¢ This is useful when working on networked
applications or hardware (arduino) when you want to
use a custom protocol.
Two handy global functions:
ofBuffer ofBufferFromFile(const string& p, bool binary = false)
bool ofBufferToFile(const string& p, ofBuffer& buf, bool bin=false);
39. ļ¬le utils
ofBuffer
ofBuffer
string data = "This isnjust a bunchnof rawndata withn somenline breaks.";
ofBuffer buffer;
buffer.set(data.c_str(), data.size());
while(!buffer.isLastLine()) {
cout << "Buffer line: " << buffer.getNextLine() << endl;
}
Result
Buffer line: This is
Buffer line: just a bunch
Buffer line: of raw
Buffer line: data with
Buffer line: some
Buffer line: line breaks.
41. ļ¬le utils
ofFilePath
File paths
ā¢ Just easy class to play with paths
ā¢ Use this for cleaning up paths, retrieving absolute
paths, retrieving basenames, ļ¬lenames, enclosing
directories, current working dir.
ā¢ All member functions are static