は実際にあなたの質問は、SDLと同じ問題に直接refferingずに答えることができる(SFMLのような)任意のライブラリで発生する可能性があり、その溶液は、すべてほぼ同じである:なぜ 答えはあなたのテクスチャ以来、シングルトンデザインパターン ですシングルトンって? 壁のタイルについて話しましょう。あなたは何千もの壁タイルを持っているかもしれません すべては同じのテクスチャを持っています。いいえ。それは同じテクスチャです1つのインスタンス各テクスチャ、さらに多くの:あなたは、すべてが含まれているスプライトシートを使用する場合はリソースの作業を保存することができますまたは3つのシートを言う:ここにsfmlの例ですSDLでも同じです。ここ https://en.wikipedia.org/wiki/Singleton_pattern
はSFML実装だが、その背後にある考え方は明確でimmitateが容易であるべき:
class Resources {
sf::Clock m_clock; //holds clock
sf::Texture m_soma,m_lvl,m_enemies; //holds hero,lvl &enemies textures respectively
sf::Font m_font; //holds game font
Resources();
public:
~Resources() {}
Resources(const Resources &) = delete;
Resources& operator=(const Resources &) = delete;
static Resources& instance();
sf::Texture& texture();
sf::Texture& lvlTexture();
sf::Texture& Enemies();
sf::Clock& clock();
sf::Font & Font();
};
のcppファイル:私は、ベクトルの代わりに、3つのテクスチャ
Resources::Resources()
{
//loading textures(hero,lvls,enemies)
if (!m_soma.loadFromFile("..\\resources\\sprites\\soma.png"))
{
std::cerr << "problem loading texture\n";
throw ResourceExceptions("couldn't load player sprites!: must have ..\\resources\\sprites\\soma.png");
}
if (!m_lvl.loadFromFile("..\\resources\\sprites\\lv.png"))
{
std::cerr << "problem loading texture\n";
throw ResourceExceptions("couldn't load level sprites!: must have ..\\resources\\sprites\\lv.png");
}
if (!m_enemies.loadFromFile("..\\resources\\sprites\\enemies.png"))
{
std::cerr << "problem loading texture\n";
throw ResourceExceptions("couldn't load enemies sprites!: must have ..\\resources\\sprites\\enemies.png");
}
//loading font once
if (!m_font.loadFromFile("..\\resources\\font\\gothic.otf"))
{
std::cerr << "problem loading font\n";
throw ResourceExceptions("couldn't load game Font: must have ..\\resources\\font\\gothic.otf");
}
}
Resources & Resources::instance()
{
static Resources resource;
return resource;
}
sf::Texture & Resources::texture()
{
return m_soma;
}
sf::Texture & Resources::lvlTexture()
{
return m_lvl;
}
sf::Texture & Resources::Enemies()
{
return m_enemies;
}
sf::Clock & Resources::clock()
{
return m_clock;
}
sf::Font & Resources::Font()
{
return m_font;
}
を使用することができ気づきます例えば。使用法は次のようになります。 m_sprite.setTexture(Resources :: instance()。texture());
// ---------------------------------------
同じあなたが3Dオブジェクトを扱うチャンスがあれば、アイデアをどこにでも適用することができます。シングルトンが十分ではないことが分かり、 "flyweight"デザインパターンを参照することができます。 overall iあなたは二つのことを読むことをお勧め: gameprogrammingpatterns.com と伝説的なブック:デザインパターン: 例えば:あなたのコード内に存在するいくつかのコードの問題がそこにいる
オブジェクト指向における再利用のため。スイッチケースは動いています。突然私が余分なアクションを追加したい場合はどうなりますか? 「もしかしたら、以前の動きに応じて違う振る舞いをしたい?あなたのアプローチは、大規模かつ長期にわたるスイッチのケースにつながります。どちらもコードをより簡単に変更できる方法を示します
この本を参照してくださいhttps://www.amazon.com/SDL-Game-Development-Black-White/dp/1849696829あなたはゲームのクラスを作り、管理する方法を見ていきます。 – CroCo
ありがとう!私は、SDLのプログラミングの本がたくさんあることがわかりましたが、これはうまく見えます! – Paingouin