Home - m1keall1son/ofxMediaSystem GitHub Wiki
ofxMediaSystem is a flexible data structure for generically composing data and logic in openFrameworks applications that require many different states of operation.
using namespace mediasystem;
SceneManager mSceneManager;
...
//create new scene
std::shared_ptr<Scene> scene = mSceneManager.createScene<Scene>("myScene");
//create an empty entity
EntityHandle entityHandle = scene->createEntity();
//entities and their components are accessed through weak_ptr's
//the user should not store the contained shared_ptrs directly
auto entity = entityHandle.lock();
//entities have an interface for scene graphing and 3D transformations
entity->setPosition(windowCenter.x, windowCenter.y, 0.f);
//define a new component
struct SomeData {
SomeData(float _speed):hitCount(0),speed(_speed){}
int hitCount;
float speed;
};
float speed = 10.f;
//add a data component to the entity, this returns a handle to access that data
std::weak_ptr<SomeData> componentHandle = entity->createComponent<SomeData>(speed);
...
struct SayMessage {
SayMessage(Entity& ent, std::string m):myEntity(ent),message(std::move(m)){}
std::string message;
Entity& myEntity;
void setMessage(std::string m){ message = std::move(m); }
void draw(){
auto pos = ent.getPosition();
ofDrawBitMappedString(message, pos.x, pos.y);
}
};
//there's a default method for drawing things to the screen by creating a LayeredRenderer system.
//first describe what types you want to be able to draw. NOTE: these types MUST have a void draw() method
using MyRenderer = ms::LayeredRenderer<SayMessage, ofMesh, ...etc>;
scene->createSystem<MyRenderer>(*scene);
//to use the layered renderer, wrap your type in an ms::Drawable<typename T> wrapper, which priovides color, alpha, draw order, etc.
using DrawableMessage = ms::Drawable<SayMessage>;
//lets have this component put a message on screen, create the component and lock the handle to get the pointer
auto draw = entity->createComponent<DrawableMessage>(*entity, "Hello World").lock();
draw->setColor(...);
draw->setAlpha(...);
draw->setDrawOrder(...);
draw->setMessage(...); // the wrapper derives from the passed type giving access to all methods and members
...
mSceneManager.initScenes(); //will initialize ALL scenes
mSceneManager.changeSceneTo(scene); //will cause scene to 'transition in'
...
//will emit update event in the current scene
//a system could tie into that event to update the state of SomeData components
mSceneManager.update();
...
//will emit draw event in the current scene, which the LayeredRenderer will use to draw our text to screen
mSceneManager.draw();