openFrameworks 入門#01 install ~ Hello World
openFrameworks http://www.openframeworks.cc/
openFrameworks is an open source C++ toolkit for creative coding.
1. download
2. 準備
以下、v0.062 + Mac OSX 10.6 + Xcode4環境です。
プロジェクトテンプレートをインストール
Xcodeが起動している場合には、終了しxcode templates/Project Templates/openFrameworksを
Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/ に配置します。
% mv xcode\ templates/Project\ Templates/openFrameworks/ ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/.
Xcodeを起動すると新規プロジェクト作成時にテンプレートを選べるようになるらしいですが
Xcode4ではテンプレートフォーマットが異なるので使えないぽい。
ので、結局 "apps/examples/emptyExample"をコピーして使うことにします。
3.Hello World!
文字列を描画するにはdrawStringという関数を使うらしいので、apps/examples/fontsExampleを参考にhelloWorld.h/helloWorld.cpp/main.cppを編集
helloWorld.h
#ifndef _HELLO_WORLD #define _HELLO_WORLD #include "ofMain.h" class helloWorld : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed (int key); void keyReleased(int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void windowResized(int w, int h); ofTrueTypeFont myriad; }; #endif
helloWorld.cpp
void helloWorld::setup(){ myriad.loadFont("MyriadWebPro.ttf", 32); ofBackground(255, 255, 255); } void helloWorld::draw(){ ofSetColor(0, 0, 0); myriad.drawString("Hello World!", 130, 155); }
main.cpp
#include "ofMain.h" #include "helloWorld.h" #include "ofAppGlutWindow.h" //======================================================================== int main( ){ ofAppGlutWindow window; ofSetupOpenGL(&window, 500,310, OF_WINDOW); // <-------- setup the GL context // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN // pass in width and height too: ofRunApp( new helloWorld()); }
使用するフォントをhelloWorld/bin/data以下にコピー
% cp /Library/Fonts/MyriadWebPro.ttf ./helloWorld/bin/data/.
Hello World!をいじってみる
キー入力によって文字色を変える
r = 赤、g = 緑、b = 青のように文字色を変えてみる。初期文字色は白
helloWorld.cpp
#ifndef _HELLO_WORLD #define _HELLO_WORLD #include "ofMain.h" class helloWorld : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed (int key); void keyReleased(int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void windowResized(int w, int h); ofTrueTypeFont myriad; int color; }; #endif
helloWorld.h
void helloWorld::setup(){ myriad.loadFont("MyriadWebPro.ttf", 32); ofBackground(255, 255, 255); color = 0xffffff; } void helloWorld::draw(){ ofSetColor(color); myriad.drawString("Hello World!", 130, 155); } void helloWorld::keyPressed(int key){ if(key == 'r') { color = 0xff0000; } else if(key == 'g') { color = 0x00ff00; } else if(key == 'b') { color = 0x0000ff; } }
ドラッグで動かす
emptyExampleからコピーすると、.cppファイルに予めイベントハンドラが定義されているのであっさり作れる(雑ですが)
helloWorld.h
#ifndef _HELLO_WORLD #define _HELLO_WORLD #include "ofMain.h" class helloWorld : public ofBaseApp{ public: void setup(); void update(); void draw(); void keyPressed (int key); void keyReleased(int key); void mouseMoved(int x, int y ); void mouseDragged(int x, int y, int button); void mousePressed(int x, int y, int button); void mouseReleased(int x, int y, int button); void windowResized(int w, int h); ofTrueTypeFont myriad; int posX; int posY; }; #endif
helloWorld.cpp
void helloWorld::setup(){ myriad.loadFont("MyriadWebPro.ttf", 32); ofBackground(255, 255, 255); posX = 130; posY = 155; } void helloWorld::draw(){ ofSetColor(0x000000); myriad.drawString("Hello World!", posX, posY); } void helloWorld::mouseDragged(int x, int y, int button){ posX = x - 110; posY = y; }