More Related Content Similar to Media Art II openFrameworks 複数のシーンの管理・切替え (20) More from Atsushi Tadokoro (20) Media Art II openFrameworks 複数のシーンの管理・切替え3. シーンを切り替える方法
‣ 2つの方法を紹介
!
‣ 1: ベースとなるシーンを継承
‣ ofBaseAppを継承したクラスbaseSceneを作成
‣ testAppに、baseSceneの配列を用意
‣ baseSceneを継承したクラスを複数作成し配列に格納
!
‣ 2: ofxStateMachineを使用する
‣ ステイトデザインを利用した複数の状態を管理するためのア
ドオン
6. シーンのテンプレート:baseSceneクラス
‣ baseScene.h
#pragma once
#include "ofMain.h"
!
class baseScene {
!
public:
//仮想関数 (virtual) として定義する
virtual
virtual
virtual
virtual
virtual
virtual
virtual
virtual
virtual
virtual
};
void
void
void
void
void
void
void
void
void
void
setup(){};
update(){};
draw(){};
keyPressed (int key){};
keyReleased(int key){};
mouseMoved(int x, int y ){};
mouseDragged(int x, int y, int button){};
mousePressed(int x, int y, int button){};
mouseReleased(int x, int y, int button){};
windowResized(int w, int h){};
10. シーン切替え:testApp
‣ testApp.h
#pragma once
#include "ofMain.h"
#include "baseScene.h"
#include "SceneA.h"
#include "SceneB.h"
#include "SceneC.h"
!
class testApp : public ofBaseApp {
public:
void
void
void
void
void
void
void
void
void
void
setup();
update();
draw();
keyPressed (int key);
keyReleased(int key);
mouseMoved(int x, int y );
mouseDragged(int x, int y, int button);
mousePressed(int x, int y, int button);
mouseReleased(int x, int y, int button);
windowResized(int w, int h);
vector <baseScene *> scenes; //複数のシーンを格納する動的配列
int currentScene; //現在のシーン番号
};
11. シーン切替え:testApp
‣ testApp.cpp (1 of 3)
#include "testApp.h"
!
void testApp::setup(){
ofSetFrameRate(60);
//シーンを新規の生成し、配列に追加
baseScene * sa = new SceneA();
scenes.push_back(sa);
baseScene * sb = new SceneB();
scenes.push_back(sb);
baseScene * sc = new SceneC();
scenes.push_back(sc);
//現在のシーンを0に
currentScene = 0;
scenes[currentScene]->setup();
}
!
void testApp::update(){
//現在表示しているシーンを更新
scenes[currentScene]->update();
}
12. シーン切替え:testApp
‣ testApp.cpp (2 of 3)
void testApp::draw(){
//現在表示しているシーンを描画
scenes[currentScene]->draw();
}
!
void testApp::keyPressed(int key){
!
!
switch (key) {
case ' ':
//シーンの切り替え
currentScene++;
currentScene %= scenes.size();
scenes[currentScene]->setup();
break;
!
}
}
case 'f':
ofToggleFullscreen();
break;
13. シーン切替え:testApp
‣ testApp.cpp (3 of 3)
!
void testApp::keyReleased(int key){
scenes[currentScene]->keyReleased(key);
}
!
void testApp::mouseMoved(int x, int y ){
scenes[currentScene]->mouseMoved(x, y);
}
!
void testApp::mouseDragged(int x, int y, int button){
scenes[currentScene]->mouseDragged(x, y, button);
}
!
void testApp::mousePressed(int x, int y, int button){
scenes[currentScene]->mousePressed(x, y, button);
}
!
void testApp::mouseReleased(int x, int y, int button){
scenes[currentScene]->mouseReleased(x, y, button);
}
!
void testApp::windowResized(int w, int h){
scenes[currentScene]->windowResized(w, h);
}
17. ofxStateMachine を使用
‣ State Machine (Finite-state Machine)
‣ 状態と遷移と動作の組み合わせからなる数学的なモデル
‣ (有限個の)状態と、入力による状態の遷移を記述する
!
状態
1
遷移1
!
状態
2
遷移2
遷移3
遷移4
!
状態
3
23. ofxStateMachine を使用
‣ 作成したシーンを、testAppから呼びだす
‣ testApp.h
#pragma once
!
#include "ofMain.h"
#include "ofxStateMachine.h"
!
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
!
(中略)
!
};
itg::ofxStateMachine<> stateMachine;
24. ofxStateMachine を使用
‣ 作成したシーンを、testAppから呼びだす
‣ testApp.h
#pragma once
!
#include "ofMain.h"
#include "ofxStateMachine.h"
!
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
!
(中略)
!
};
itg::ofxStateMachine<> stateMachine;
ofxStateMachineを
インスタンス化
25. ofxStateMachine を使用
‣ 作成したシーンを、testAppから呼びだす
‣ testApp.cpp
#include "testApp.h"
#include "TemplateState.h"
!
//-------------------------------------------------------------void testApp::setup(){
// initialise state machine
!
!
!
stateMachine.addState<TemplateState>();
stateMachine.changeState("template");
}
!
(後略)
26. ofxStateMachine を使用
‣ 作成したシーンを、testAppから呼びだす
‣ testApp.cpp
#include "testApp.h"
#include "TemplateState.h"
!
読み込む状態をインクルード
//-------------------------------------------------------------void testApp::setup(){
// initialise state machine
!
!
!
stateMachine.addState<TemplateState>();
stateMachine.changeState("template");
}
!
(後略)
27. ofxStateMachine を使用
‣ 作成したシーンを、testAppから呼びだす
‣ testApp.cpp
#include "testApp.h"
#include "TemplateState.h"
!
//-------------------------------------------------------------void testApp::setup(){
// initialise state machine
!
!
!
stateMachine.addState<TemplateState>();
stateMachine.changeState("template");
}
!
(後略)
状態をState Machineに追加
28. ofxStateMachine を使用
‣ 作成したシーンを、testAppから呼びだす
‣ testApp.cpp
#include "testApp.h"
#include "TemplateState.h"
!
//-------------------------------------------------------------void testApp::setup(){
// initialise state machine
!
!
!
stateMachine.addState<TemplateState>();
stateMachine.changeState("template");
}
!
(後略)
名前を指定して、状態へ遷移
31. ofxStateMachine を使用
‣ SharedDataを読み込めるように、testApp.hを変更
‣ testApp.h
#pragma once
!
#include "ofMain.h"
#include “ofxStateMachine.h"
#include "SharedData.h"
!
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
!
(中略)
!
};
itg::ofxStateMachine<SharedData> stateMachine;
32. ofxStateMachine を使用
‣ SharedData.hをtestAppにインクルード
‣ testApp.h
#pragma once
!
#include "ofMain.h"
#include “ofxStateMachine.h"
#include "SharedData.h"
!
SharedState.h
をインクルード
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
!
(中略)
!
};
itg::ofxStateMachine<SharedData> stateMachine;
33. ofxStateMachine を使用
‣ ofxStateMachineのインスタンス化を変更
‣ testApp.h
#pragma once
!
#include "ofMain.h"
#include “ofxStateMachine.h"
#include "SharedData.h"
!
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
!
(中略)
!
};
itg::ofxStateMachine<SharedData> stateMachine;
ofxStateMachineを
インスタンス化
34. ofxStateMachine を使用
‣ testApp.cppで、SharedDataにアクセス
‣ testApp.h
#include "testApp.h"
#include "TemplateState.h"
!
//-------------------------------------------------------------void testApp::setup(){
!
// SharedData
stateMachine.getSharedData().sharedMsg = "Hello State Machine!";
// initialise state machine
stateMachine.addState<TemplateState>();
stateMachine.changeState("template");
}
35. ofxStateMachine を使用
‣ testApp.cppで、SharedDataにアクセス
‣ testApp.h
#include "testApp.h"
#include "TemplateState.h"
!
//-------------------------------------------------------------void testApp::setup(){
!
// SharedData
stateMachine.getSharedData().sharedMsg = "Hello State Machine!";
// initialise state machine
stateMachine.addState<TemplateState>();
stateMachine.changeState("template");
}
SharedData内のsharedMsgに
文字列を代入