openFrameworksを使うとC++で手軽にグラフィカルなものが作れると聞いてちょっと触ってみました。
まずはここからopenFrameworksのパッケージをダウンロード。自分はVisualStudio2008用をダウンロードしました。
openFrameworks用のプロジェクトの生成方法はこちらで紹介されていました。
簡単に設定やらをやってくれるプログラムを公開されていてとても助かりました。
Flashでもよくつくってるパーティクルのデモを作ってみました。
自分で書いたコードは70行くらいです。
以下ソース
main.cpp
#include "ofMain.h"
#include "testApp.h"
#include "ofAppGlutWindow.h"
//========================================================================
int main( ){
ofAppGlutWindow window;
ofSetupOpenGL(&window, 500,500, 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 testApp());
}
App.cpp
#include "testApp.h"
struct Particle{
float x, y, radius, vx, vy;
int r, g, b;
void draw(){
ofSetColor(r, g, b);
ofCircle(x, y, radius);
}
void update(){
this->x += vx;
this->y += vy;
}
};
static const int N = 100;
int W, H;
Particle particles[N];
//初期化関数--------------------------------------------------------------
void testApp::setup(){
//背景色の設定(r,g,b)
ofBackground(0,0,0);
//円の解像度の設定
ofSetCircleResolution(64);
W = ofGetWidth();
H = ofGetHeight();
for(int i = 0; i < N; i++){
Particle p;
p.x = ofRandom(100, W-100);
p.y = ofRandom(100, H-100);
p.radius = ofRandom(5.0, 20.0);
p.vx = ofRandom(-5, 5);
p.vy = ofRandom(-5, 5);
p.r = ofRandom(0,255);
p.g = ofRandom(0,255);
p.b = ofRandom(0,255);
p.draw();
particles[i] = p;
}
}
//メインループ関数--------------------------------------------------------------
void testApp::update(){
for(int i = 0; i < N; i++){
Particle& p = particles[i];
p.update();
if(p.x < p.radius){
p.x = p.radius;
p.vx *= -1;
}
else if(p.x > W-p.radius){
p.x = W-p.radius;
p.vx *= -1;
}
if(p.y < p.radius){
p.y = p.radius;
p.vy *= -1;
}
else if(p.y > H-p.radius){
p.y = H-p.radius;
p.vy *= -1;
}
}
}
//描画関数--------------------------------------------------------------
void testApp::draw(){
for(int i = 0; i < N; i++){
particles[i].draw();
}
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
App.h
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
class testApp : 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);
};
#endif
1 個のping
openFrameworksの環境構築 (Windows・VS2010) « Sakef Blog says:
2011年6月5日 at 10:09 (UTC 9)
[...] openFrameworksで遊ぶ 01 » Every day is Carnival [...]