Posts Tagged ‘OF’

C++ Vectors [quick overview]

Saturday, July 9th, 2011

Vector is a dynamic container type in OpenFrameworks (and C++) that is able to hold multiple elements of the same type. It functions like a special type of array that can be expanded and contracted while the application runs. Vector elements are stored in contiguous locations so they can be accessed easily using operators, iterators, and pointers. You can download a pdf version of this tutorial here. Here is a link to the C++ vector reference page.

Declaration
When you declare a vector you need to specify the type of data that it will contain. This enables the compiler to know how much memory to allocate to a vector instance.

syntax: vector < data_type > vector_name;
example: vector < ofTouchEventArgs > touches;

Add Element
To add a new element to the end of a vector you use the push_back() method. This method accepts an object as an argument. This object is placed at the end of the array. The object must be of the appropriate data type, otherwise you will receive a “No matching function” error.

syntax: vector_name.push_back(new_object);
example: touches.push_back(touches);

Get Element
To access objects from an Vector you can use several different approaches. The most common ones are the operator [index] and the at(index) method. The difference between these approaches is that at() throws an exception if the index is out of range).

syntax: vector_name[index]
example: touches.at(index)

Iterators and pointer math can also be used to access objects from a Vector. To learn more about how to use these approaches check out the C++ reference website.

Remove Element
To remove elements from a Vector the best approach is to use the erase() method. This function takes an iterator as an argument. The vector methods begin() and end() both return iterators; the former returns an iterator that points to the first element in the Vector, the later returns an iterator that points to the last element in the Vector.

syntax: vector_name.erase(iterator);
example: touches.erase(touches.begin() + i);

You can also use the pop_back() function to remove the last element from a vector.

syntax: vector_name.pop_back();
example: touches.pop_back();

Vector Iterators
An iterator is an object that has the ability to iterate through the elements of a range, such as a vector or array. Vectors have a special iterator that can go through its elements using operators such as increment (++) and decrement (–). Iterators are used as input arguments for several of the modification methods in the vector class and for looping through all the elements in an array.

To get an iterator from a vector you can use the begin() and end() methods. These methods return an iterator that resolves to the first and last positions of the vector respectively. Below is sample of code that demonstrates how to use an iterator to loop through the elements in a vector.

vector my_vector;
  for (int i=1; i<=5; i++)
    my_vector.push_back(i);
vector::iterator my_iterator;
  for (my_iterator = my_vector.begin(); my_iterator < my_vector.end(); ++my_iterator)
    cout << " " << * my_iterator;

Random Stuff
Here is a brief overview of a few other vector methods worth knowing about. Check out the full reference for more information about vectors.
- empty(): checks if a vector empty, and returns true if it is.
- size(): returns the number of elements in a vector.
- capacity(): returns the amount of space allocated to the vector.

Thank You
I hope this short tutorial has been helpful since the reference pages can sometimes be a bit cryptic. If you have any comments or questions email me at julioterra at gmail dot com.


Open Frameworks [intro to building apps for iOS]

Friday, July 8th, 2011


This past weekend I attended a one-day workshop to learn how to create applications for iOS devices using Open Frameworks. During this intimate session led by ITP veterans Amit Pitaru and David Nolen we covered a lot of ground, though of course we barely scratched the surface of the iceberg (if I may mix my metaphors).

We started the day by getting an overview of Apple’s iOS Model View Controller (MVC) model, and how Open Framework applications fit into this framework. Then we dove into xCode to get OF and the iOS SDK up and running. The last half of the workshop was spend working on iOS apps, which involved little more then touch tracking.

I have created a nice 8-page presentation using my notes from the workshop. These slides focus on how to get Open Frameworks set-up so that you can create applications for iOS devices in five relatively easy (though not necessarily quick) steps. [download a pdf version here]

I plan to write a post about Apple’s implementation of the Model View Controller framework in the next couple of weeks, as soon as I have my head wrapped around it better.