うーん、そーなのかー...
自機/敵機/弾などなどの動きをタスクに分割し、
それぞれ個別にオブジェクト定義してうんぬんしてたわけですが...遅い。
大したことやらせてないのに80fpsがやっと。
こんなんじゃ先が思いやられます。
どうもSystem.Collections.Generic族(List<T>とか)使ったり、仮想メソッド
経由でごしょごしょやるとパフォーマンスがかくんと落ちるポいです。
美しく書こうとしちゃイカンみたい、一旦全部捨てて考え直しだなこりゃ。
C++Dayでmeltさんが紹介してくれた shared_ptr を使ったおあそび。
C#でいう参照型のようにふるまう猫。
#include <memory>
#include <string>
#include <iostream>
// VC++9 SP1 でサポートされた std::tr1::shared_ptr を使います
namespace std { using namespace std::tr1; }
using namespace std;
class Cat {
// 猫Body
class CatImpl {
friend class Cat;
string name;
string tail;
CatImpl(const string& n, const string& t) : name(n), tail(t) {}
void greet() const { cout << name << "だ" << tail << endl; }
public:
~CatImpl() { cout << "ばいばい" << tail << endl; }
};
std::shared_ptr<CatImpl> impl; // 猫Handle
public:
void greet() const { impl->greet(); }
static Cat make(const string& n, const string& t) {
Cat result;
result.impl = shared_ptr<CatImpl>(new CatImpl(n,t));
return result;
}
};
int main() {
Cat s = Cat::make("シュウたん","にゃー");
Cat m = Cat::make("マグさん","ほげ");
Cat c; // シュウたん/マグさん にすり替わります!!
// コピーしているように見せかけて、ぢつはしてまてーん
c = s; c.greet();
c = m; c.greet();
}
# C++ネタもたまには書かんとのぅ...