我不知道如何使用 SDL2 管理我的课程结构

I don't know how to manage my classes sturctures with SDL2

本文关键字:程结构 结构 我的 何使用 SDL2 管理 我不知道      更新时间:2023-10-16

我尝试使用 SDL-2 创建一些C++"流氓"游戏。为此,我按照 Lazy foo 的教程来了解如何使用 SDL。

我已经学习了 3 年的 C++/C#,但现在我学习项目管理,不再有 IT 课程......

这是代码的github:https://github.com/Paingouin/Roguelike-SDL2-train/tree/master/

我创建了 2 个类:LTexture来帮助管理图片的加载和渲染,Glyph管理动画/缩放和图片的位置......

现在,我想创建一个实体类,由一个Glyph对象组成,我将用它来表示一堵墙、一个怪物、一个物品等......但是,我认为如果我这样做,我会使用太多的内存......

也许我应该通过初始化字形指针数组并将其关联到我的实体对象来使用聚合......我不知道,我迷路了...

你可以帮我吗?而且,您有什么提示或建议可以帮助我正确构建结构吗?

实际上您的问题可以在不直接引用 SDL 的情况下得到回答,并且任何库(如 sfml)都可能出现相同的问题,解决方案大致相同:答案是单例设计模式既然你的质地为什么我说单身?让我们谈谈墙砖。你可能有数千甚至更多的墙砖都有相同的纹理,你真的想在每面墙上一次又一次地加载它吗?不。它是相同的纹理,您希望每个给定纹理都有一个实例,甚至更多:如果您使用包含所有内容的精灵表或假设有 3 张工作表,您可以节省资源工作:这是 SFML 中的一个例子,尽管在 SDL 中的想法应该是相同的。https://en.wikipedia.org/wiki/Singleton_pattern

这是一个 SFML 实现,但它背后的想法应该清晰且易于模仿:

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 texturen";
       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 texturen";
    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 texturen";
       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 fontn";
       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对象并渲染那些你会发现单例在那里甚至不够,你可以参考"蝇量级"设计模式。总的来说,我建议你阅读两件事:gameprogrammingpatterns.com以及传奇书籍:设计模式:可重用面向对象软件的元素

代码中存在多个代码问题:例如。机芯上的开关盒。问问自己"如果突然我想添加一个额外的操作,会发生什么?"如果为EG。我希望它的行为与之前的运动不同?" 您的方法将带您进入广泛而长的开关案例。两者都将向您展示允许您更轻松地更改代码的方法