openframeworks/openFrameworks — pull request #3384 by arturoc (2014-11-01 16:27:32+00:00)
Title: removes statics and globals. adds multiwindow

This removes almost all statics, singletons and global calls (like ofPushMatrix, ofGetWidth...) from the core, at least form the graphics and core events pipeline. To do that, the following function based apis have been transformed into classes:

- ofGraphics3d
- ofBitmapFont
- ofEvents -> ofCoreEvents

while keeping the api totally backwards compatible.

Windows now contain their own renderer and core events instance and notify that instead of the global one. Each renderer contains it's own ofGraphics3d and ofBitmapFont, which avoids singletons allowing multiwindow or multicontext even on different threads if everything is done through a explicit renderer.

There's a new class ofMainLoop which is the only singleton in the rendering pipeline and is responsible of creating new windows and calling them in a loop if the window doesn't provide it's own loop as is the case of glut, android and iphone.

the old syntax to create windows is still working but there's a new way which is the preferred one:

```cpp
int main(){
ofWindowSettings settings;
ofCreateWindow(settings);

return ofRunApp(new ofApp); // now returns a value that can be
// specified through ofExit(exitStatus)
}
```

where one can pass width, height and window mode (windowed, fullscreen...) to the settings like:

```cpp
int main(){
ofWindowSettings settings;
settings.width=1024;
settings.height=768;
settings.windowMode = OF_WINDOW;
ofCreateWindow(settings);

return ofRunApp(new ofApp);
}
```

or:

```cpp
int main(){
ofWindowSettings settings;
settings.windowMode = OF_FULLSCREEN;
ofCreateWindow(settings);

return ofRunApp(new ofApp);
}
```

there's also specializations of the window settings like:

```cpp
int main(){
ofGLWindowSettings settings;
settings.setGLVersion(4,4);
ofCreateWindow(settings);

return ofRunApp(new ofApp);
}
```

or

```cpp
int main(){
ofGLFWWindowSettings settings;
settings.setGLVersion(4,4);
settings.decorations = false;
ofCreateWindow(settings);

return ofRunApp(new ofApp);
}
```

egl, iOS and android should have their own window settings class although that's not implemented yet. the EGL and iOS windows indeed still need to be refactored to use the new api.

To use multiwindow the syntax is:

```cpp
int main(){
ofWindowSettings settings;
shared_ptr window1 = ofCreateWindow(settings);
shared_ptr app1(new ofApp);
ofRunApp(window1,app1);

shared_ptr window2 = ofCreateWindow(settings);
shared_ptr app2(new guiApp);
ofRunApp(window2,guiApp);

return ofRunMainLoop();
}
```

and simpler using c++11:

```cpp
int main(){
ofWindowSettings settings;
auto window1 = ofCreateWindow(settings);
auto app1 = make_shared();
ofRunApp(window1,app1);

auto window2 = ofCreateWindow(settings);
auto app2 = make_shared();
ofRunApp(window2,guiApp);

return ofRunMainLoop();
}
```

There's also a way of creating a window without associated app and just bind some of it's events to an existing app. There's a couple of examples showing both cases in the events folder.

Also every class that can be drawn or binded can be called the traditional way like:

```
tex.draw(x,y)
```

or through a renderer like:

```
shared_ptr gl;
...
gl->bind(fbo);
gl->bind(shader);
gl->bind(tex);
gl->draw(mesh);
gl->unbind(tex);
gl->unbind(shader);
gl->unbind(fbo);
```

which allows to use different gl contexts or even windows from different threads for example. this api is still experimental but there's an example on how to use it in apps/devApps/explicitRendererExample