OpenFrameworks for Visual studio
(→クラスの作成) |
(→クラスの作成) |
||
| 54行: | 54行: | ||
# 新しくクラスを作るには、'''「プロジェクト > 新しい項目の追加...」''' を開き,'''「C++ファイル」'''と'''「ヘッダーファイル」'''を作る。 | # 新しくクラスを作るには、'''「プロジェクト > 新しい項目の追加...」''' を開き,'''「C++ファイル」'''と'''「ヘッダーファイル」'''を作る。 | ||
| − | #* 名前を それぞれ''' | + | #* 名前を それぞれ'''Xxx.cpp''','''Xxx.h'''とし、場所はともに '''..\src''' とする |
#* (注)「クラスの追加」や「クラスウィザード」は使えない。 | #* (注)「クラスの追加」や「クラスウィザード」は使えない。 | ||
| − | # ソリューションエクスプローラー上の'''src'''の中に''' | + | # ソリューションエクスプローラー上の'''src'''の中に'''Xxx.cpp''' と '''Xxx.h''' が新規作成される。 |
| − | ヘッダファイル ''' | + | ヘッダファイル '''Xxx.h''' は, |
<pre> | <pre> | ||
#pragma once | #pragma once | ||
#include "ofMain.h" | #include "ofMain.h" | ||
| − | class | + | class Xxx { |
private: | private: | ||
ofPoint pos; | ofPoint pos; | ||
| 69行: | 69行: | ||
public: | public: | ||
| − | + | Xxx(); | |
void hogehoge(); | void hogehoge(); | ||
}; | }; | ||
| 80行: | 80行: | ||
* '''private:''' に続く部分には,クラス内部のみで利用する変数、メソッドを宣言する。 | * '''private:''' に続く部分には,クラス内部のみで利用する変数、メソッドを宣言する。 | ||
* '''public:''' に続く部分には,クラス外部からアクセスできる変数、メソッドを宣言する。 | * '''public:''' に続く部分には,クラス外部からアクセスできる変数、メソッドを宣言する。 | ||
| − | * ''' | + | * '''Xxx():''' はコンストラクターといい、クラスと同じ名前のメソッドである。クラスのインスタンスを生成するときの初期化処理などを記述する。 |
| − | C++ファイル | + | C++ファイル Xxx.cpp は, |
<pre> | <pre> | ||
| − | #include " | + | #include "Xxx.h" // クラスのヘッダーを読み込む |
| − | + | Xxx::Xxx(){ | |
pos = ofPoint(ofGetWidth()/2, ofGetHeight()/2); | pos = ofPoint(ofGetWidth()/2, ofGetHeight()/2); | ||
radius = 100.0; | radius = 100.0; | ||
} | } | ||
| − | void | + | void Xxx::hogehoge(){ |
ofSetColor(31, 63, 255, 100); | ofSetColor(31, 63, 255, 100); | ||
ofCircle(pos.x, pos.y, radius); | ofCircle(pos.x, pos.y, radius); | ||
| 101行: | 101行: | ||
などと、メソッドの本体を記述する。 | などと、メソッドの本体を記述する。 | ||
* メソッドの定義の最後にセミコロンが'''つかない'''ことに注意する。 | * メソッドの定義の最後にセミコロンが'''つかない'''ことに注意する。 | ||
| + | |||
| + | |||
| + | もとのcppファイルodApp.cppのヘッダーファイル'''ofApp.h'''に、Xxx.hをincludeする。 | ||
| + | 例えば、'''ofApp.h'''は、以下のとおり。 | ||
| + | <pre> | ||
| + | #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); | ||
| + | |||
| + | Xxx myCbj; | ||
| + | }; | ||
| + | </pre> | ||
| + | '''public'''の領域に'''Xxxクラス'''のインスタンス'''myObj'''を宣言している。 | ||
[[Category:授業]] | [[Category:授業]] | ||
2014年10月29日 (水) 05:21時点における版
目次 |
セットアップ
以下のサイトからダウンロードしてください。
http://openframeworks.jp/download/
「windows download openFrameworks for visual studio」をダブルクリックして、of_v0.8.4_vs_release.zipをダウンロードします。(約90MB)
また、PC演習室のdeliveryドライブからも配布します。
of_v0.8.4_vs_release.zipを解凍すると、of_v0.8.4_vs_releaseフォルダー(約1GB)ができます。 このフォルダーをして、自分のポータブルハードディスク、または、PC演習室の、seminarドライブのgenerativeフォルダー内に移動してください。
プログラム開発は、このof_v0.8.4_vs_releaseフォルダー(以下、ofフォルダーと略す)内で行います。 また、このofフォルダーを持ち歩けば、Visual Studioの入ったパソコンならどこでも、開発ができます。
- ofフォルダーの構成
- exampleフォルダー サンプルプログラムが入っている。
- appsフォルダー MyAppsフォルダーに自作のプログラムを入れる。
- addonsフォルダー 機能を追加するときには、ここにアドオンを入れる。すでにいろいろ入っている。
サンプルの実行
ofフォルダー内のexampleフォルダーにはたくさんのサンプルプログラムが入っているので、いろいろ試してみるとよい。
例 graphicExample graphicExampleフォルダー内のgraphicExample.sln(ソルーション)または、graphicExample.vcxproj(VC++プロジェクト)をダブルクリックして、プロジェクトを立ち上げてみる。
- Visual studioではプログラムをプロジェクトという単位で管理する。複数のプロジェクトをまとめたものをソリューションという。この授業では、複数のプロジェクトからなるソリューションは扱わない。
Microsoft Visual Studioが立ち上がり、サンプルプログラムのプロジェクトが開かれる。
- ソリューションエクスプローラーに、プロジェクトの構成要素がリストされている。
- プログラム本体のソースコードはsrcに含まれている。
- main.cpp ofApp.cpp ofApp.hの3つのプログラムが基本。このうち、ofApp.cppが一番、重要。
- setup() update() draw() の流れでプログラムが実行される。
プログラムを実行してみる。
- ウィンドウの上部中央にある緑色の三角ボタンをクリックする。
binフォルダーには、実行形式exeファイルができている。 これをダブルクリックすると、プログラムが実行される。
マイプログラムの作成
- oFフォルイダー内のprojectGeneratorフォルダー内のprojectGenerator.exeを実行する。
- Name:に、プロジェクトの名前を入れる。
- Addons:に、使用する追加機能(アドオン)を入れる。(はじめは、「なし」でよい。
- GENERATE PROJECTをクリックする。
- Apps内のMyAppsにマイプロジェクトが作成されている。
- 作成したプロジェクトを起動し、srcを見てみる。
- ofApp.xppのメソッドの中身が空っぽ。
- ここにプログラムを書いていく。
クラスの作成
- 新しくクラスを作るには、「プロジェクト > 新しい項目の追加...」 を開き,「C++ファイル」と「ヘッダーファイル」を作る。
- 名前を それぞれXxx.cpp,Xxx.hとし、場所はともに ..\src とする
- (注)「クラスの追加」や「クラスウィザード」は使えない。
- ソリューションエクスプローラー上のsrcの中にXxx.cpp と Xxx.h が新規作成される。
ヘッダファイル Xxx.h は,
#pragma once
#include "ofMain.h"
class Xxx {
private:
ofPoint pos;
float radius;
public:
Xxx();
void hogehoge();
};
などと、記述する。
- ofMain.h をインクルードし、クラスの定義の最後にセミコロンがつくことに注意する。
- #pragma once は,このヘッダファイルを複数回読み込まないようにするためのもの。
- 括弧で囲まれた部分 {...} には,変数の宣言やメソッド(関数)の宣言を書く
- private: に続く部分には,クラス内部のみで利用する変数、メソッドを宣言する。
- public: に続く部分には,クラス外部からアクセスできる変数、メソッドを宣言する。
- Xxx(): はコンストラクターといい、クラスと同じ名前のメソッドである。クラスのインスタンスを生成するときの初期化処理などを記述する。
C++ファイル Xxx.cpp は,
#include "Xxx.h" // クラスのヘッダーを読み込む
Xxx::Xxx(){
pos = ofPoint(ofGetWidth()/2, ofGetHeight()/2);
radius = 100.0;
}
void Xxx::hogehoge(){
ofSetColor(31, 63, 255, 100);
ofCircle(pos.x, pos.y, radius);
ofSetColor(255, 0, 0, 200);
ofCircle(pos.x, pos.y, radius);
}
などと、メソッドの本体を記述する。
- メソッドの定義の最後にセミコロンがつかないことに注意する。
もとのcppファイルodApp.cppのヘッダーファイルofApp.hに、Xxx.hをincludeする。
例えば、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);
Xxx myCbj;
};
publicの領域にXxxクラスのインスタンスmyObjを宣言している。