简体   繁体   English

我的c ++游戏架构

[英]my c++ game architecture

I'm a fairly experienced programmer, but I'm still relatively new to OOP architecture and design in c++. 我是一个相当有经验的程序员,但我仍然相对较新的OOP架构和c ++设计。 Most of my experience is with C# and Java. 我的大部分经验都是使用C#和Java。 I recently endeavored to code up a simple game engine in c++. 我最近努力用c ++编写一个简单的游戏引擎。 I used SDL for the graphics. 我使用SDL作为图形。 In this post, I would like to discuss my architecture and to get some feedback on it. 在这篇文章中,我想讨论我的架构并获得一些反馈。 Particularly, I've run into a design issue that I would like some help with. 特别是,我遇到了一个我想要帮助的设计问题。 So, here goes: 所以,这里是:

  • In my main function, I initialize all of the SDL stuff for drawing to the screen, etc. 在我的主要功能中,我初始化所有SDL内容以绘制到屏幕等。
  • Then I instantiate all of the objects that I intend to use: floor, wall, player, etc. 然后我实例化我打算使用的所有对象:floor,wall,player等。
  • Next I start the main loop. 接下来我开始主循环。 This loop executes each object's movement, collision detection and collision handling functions, and redraws them. 该循环执行每个对象的移动,碰撞检测和碰撞处理功能,并重绘它们。
  • The main loop runs until the application is quit, drawing one frame each iteration. 主循环运行直到应用程序退出,每次迭代绘制一帧。

My problem is this: I tried to do a kind of interface-style design. 我的问题是:我尝试做一种界面风格的设计。 It involves a series of abstract base classes that allow each object to implement a behavior. 它涉及一系列允许每个对象实现行为的抽象基类。 For example, if I want an object to be movable, it would have to inherit from the movable base class which contains a virtual function called move() and some position coordinates. 例如,如果我想要一个可移动的对象,它必须从包含一个名为move()的虚函数和一些位置坐标的可移动基类继承。 If I wanted it to be collidable, the object would inherit from the collidable abstract class, which contains the virtual functions checkCollision() and handleCollision() as well as a hitbox member variable. 如果我希望它是可碰撞的,那么该对象将继承可碰撞的抽象类,它包含虚函数checkCollision()handleCollision()以及一个hitbox成员变量。 An object like the player inherits from both of these base classes as well as several others. 像播放器这样的对象继承了这两个基类以及其他几个基类。

This works well enough as long as I'm doing everything by hand in the main loop. 只要我在主循环中手动完成所有操作,这就足够了。 I can just say: 我可以说:

player.move();
player.checkCollision();
player.handleCollision();
player.draw(). 

and it's fine. 这没关系。 But I would like to be able to have a vector or array of generic objects in the main loop and do something like this: 但我希望能够在主循环中有一个矢量或通用对象数组,并执行以下操作:

for each object in vector
    if object is of type movable
        object.move();
    if object is of type collidable
        object.checkCollision();

I thought that I could accomplish this with dynamic casting, but I really haven't been able to come up with anything. 我认为我可以通过动态铸造实现这一目标,但我真的无法想出任何东西。 I've tried storing them as void pointers, but that doesn't work the way I want it to. 我已经尝试将它们存储为void指针,但这并不像我想要的那样工作。 I've been reading about this gameobject-component architecture for video games that I might try, but I'd really like to salvage what I've already written. 我一直在阅读有关视频游戏的游戏对象组件架构,我可能会尝试,但我真的想要挽救我已经写过的内容。 I figure this is a good learning opportunity. 我认为这是一个很好的学习机会。 If anybody has any ideas I'd really appreciate it. 如果有人有任何想法我会非常感激。 How does my architecture compare to other simple game engine designs? 我的架构与其他简单的游戏引擎设计相比如何? does my interface architecture make sense or is it totally wonky? 我的界面架构是否有意义还是完全不稳定?

If you work in C++, try SFML , it's faster than SDL , and if you know OpenGL , you can use it too. 如果您使用的是C ++,请尝试SFML ,它比SDL更快,如果您了解OpenGL ,也可以使用它。
For your problem: 对于你的问题:

class Entity {
     //constructor and other stuff
     void virtual exec() =0; ///<= pure virtual method
};

class Movable : Entity {
    void move(); //do somthing
    void exec() {move();};
};

class Collidable : Entity {
   void col(); //do your job
   void exec(){col();};
};

std::vector<Entity*> e_v;
///push some instance
for (Entity* e : e_v)
   e->exec();

What about using the template method pattern and non-abstract empty virtual functions in the base class? 如何在基类中使用模板方法模式和非抽象空虚函数?

class Entity
{
    void update()
    {
        move();
        checkCollision();
    }

    virtual void move() {}
    virtual void checkCollision() {}
};

class Movable : public virtual Entity
{
    virtual void move() {}   // Stuff
};

class Collidable : public virtual Entity
{
    virtual void checkCollision() {}   // Stuff
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM