クラスの使用
提供:kuhalaboWiki
- Ballクラスを作成
- Ball.h Ballクラスの宣言。変数とメソッド(関数)
- Ball.cpp コンストラクターBall()とメソッドの定義
- 新しくクラスを作るには、「プロジェクト > 新しい項目の追加...」 を開き,「C++ファイル」と「ヘッダーファイル」を作る。
- 名前を それぞれBall.cpp,Ball.hとし、場所はともに ..\src とする
- (注)「クラスの追加」や「クラスウィザード」は使えない。
- ソリューションエクスプローラー上のsrcの中にBall.cpp と Ball.h が新規作成される。
- Ball.h
#pragma once
#include "ofMain.h"
class Ball {
public:
ofPoint pos;
ofPoint vel;
ofPoint acc;
float radius;
ofColor bcolor;
public:
Ball();
void draw();
void update();
};
- Ball.cpp
#include "Ball.h"
Ball::Ball(){
pos = ofPoint(ofGetWidth()/2, ofGetHeight()/2);
vel = ofPoint(ofRandom(-5,5),ofRandom(-3,3));
acc = ofPoint(ofRandom(-0.5,0.5),ofRandom(-0.3,0.3));
radius = ofRandom(35,50);
bcolor = ofColor(ofRandom(200,255), ofRandom(0,255), ofRandom(0,255), 100);
}
void Ball::update(){
acc = ofPoint(ofRandom(-0.5,0.5),ofRandom(-0.3,0.3));
vel.x += acc.x;
vel.y += acc.y;
pos.x += vel.x;
pos.y += vel.y;
if( pos.x > ofGetWidth() || pos.x < 0 ){
vel.x *= -1;
}
if( pos.y > ofGetHeight() || pos.y < 0 ){
vel.y *= -1;
}
}
void Ball::draw(){
ofSetColor(bcolor);
ofCircle(pos.x, pos.y, radius);
}
- ofApp.h
#pragma once
#include "ofMain.h"
#include "Ball.h"
class ofApp : public ofBaseApp{
public:
bool mouse_pressed;
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);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
Ball myball;
};
- ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(0, 0, 0); //背景色の設定
ofSetColor(255, 255, 0); //描画色の設定
ofSetBackgroundAuto(false);
ofSetFrameRate(30);
ofEnableAlphaBlending(); //アルファチャンネルを有効にする
ofSetCircleResolution(64);
mouse_pressed = false;
}
//--------------------------------------------------------------
void ofApp::update(){
if(mouse_pressed ){
myball.vel.x = ( mouseX - myball.pos.x ) * 0.01;
myball.vel.y = ( mouseY - myball.pos.y ) * 0.01;
}
myball.update();
}
//--------------------------------------------------------------
void ofApp::draw(){
//全画面を半透明の黒でフェード
ofSetColor(0, 0, 0, 10);
ofRect(0, 0, ofGetWidth(), ofGetHeight());
myball.draw();
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
mouse_pressed = true;
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
mouse_pressed = false;
}
クラスに配列を使用
- クラスに配列を使用して、複数のボールを作成
- ofApp.h
#pragma once
#include "ofMain.h"
#include "Ball.h"
class ofApp : public ofBaseApp{
public:
static const int NUM = 10; //配列の大きさを指定
bool mouse_pressed;
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);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
Ball balls[NUM]; //Ballクラスのインスタンスを配列で指定
};
- ofApp.cppのupdate()とdraw()
//--------------------------------------------------------------
void ofApp::update(){
for(int i = 0; i < NUM; i++){
if(mouse_pressed ){
balls[i].vel.x = ( mouseX - balls[i].pos.x ) * 0.01;
balls[i].vel.y = ( mouseY - balls[i].pos.y ) * 0.01;
}
balls[i].update();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
//全画面を半透明の黒でフェード
ofSetColor(0, 0, 0, 10);
ofRect(0, 0, ofGetWidth(), ofGetHeight());
for(int i = 0; i < NUM; i++){
balls[i].draw();
}
}