Skip to content

Instantly share code, notes, and snippets.

@takashi
Last active August 29, 2015 14:05
Show Gist options
  • Save takashi/c07eea51a5587b14a8eb to your computer and use it in GitHub Desktop.
Save takashi/c07eea51a5587b14a8eb to your computer and use it in GitHub Desktop.
for my c++ programming study with oF

c++ tips

debugはcoutで

int hoge = 100
cout << hoge << endl;  // 100

#pragma once

#pragma onceは重複してincludeした場合に2回目のincludeを無視してくれる

virtualは仮想関数

仮想関数に宣言した関数のみ、継承先のクラスでoverrideができる

#pragma once

#include "ofMain.h"
#include "Particle.h"

class Hanabi {
    ofVec2f originPos;
    int size;

public:
    Hanabi();
    virtual ~Hanabi(){};
    void setPosition(float px, float py);
    void setColor(int r, int g, int b, int a);
    void setSize(int size);
    void prepare();
    void fire();
    
    vector <Particle> sparks;
};

この場合、継承先のクラスは、Particleのdescractorを上書きできる

C++では、virtualデストラクタのないクラスは継承するつもりのないクラス(Javaでいうfinalクラス)であると考えた方がよい。

http://d.hatena.ne.jp/ajiyoshi/20080925/p1

引数の名前と代入したい変数の名前が同じ場合はthis-> を使う

void hoge(int size){
  this->size = size;
}

複数の要素を扱う

arrayvectorを使う

arrayは要素数が決まってる、vectorは決まっていない

// array
Particle p[100];


// vector
vector<Particle> particles;

int i = 0;
for(; i < 100; i++){
	Particle p;
	particles.push_back(p);
}

vectorから指定した要素を削除する

vector<Particle> particles;

int i = 0;
for(; i < 100; i++){
	Particle p;
	particles.push_back(p);
}

particles.erase(sparks.begin() + 10); // 10番目の要素を削除

method名が一緒でも引数が違えば別の関数(objcと一緒?)

ofGraphics.hの中

void ofSetColor(int r, int g, int b); // 0-255
void ofSetColor(int r, int g, int b, int a); // 0-255
void ofSetColor(const ofColor & color);
void ofSetColor(const ofColor & color, int _a);
void ofSetColor(int gray); // new set a color as grayscale with one argument

動きの軌跡を表示

これをsetup()に

ofSetBackgroundAuto(false); //フレーム更新時に塗り潰しを無効化
ofBackground(0, 0, 0); //背景色を黒に設定

動きの軌跡をフェードさせる

これをdrawに

ofSetColor(0, 0, 0, 30); //半透明の黒(背景色)
ofRect(0, 0, ofGetWidth(), ofGetHeight()); //画面と同じ大きさの四角形を描画

oFでdebugしたい

ofSetLogLevel(OF_LOG_VERBOSE);

とsetUpに書くとdebug consoleにlogが表示される

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment