游戏引擎开发 [英] Game engine development

查看:148
本文介绍了游戏引擎开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重新创建一个我之前创建的游戏引擎。这一次我想做正确的,所以我做了一些研究设计模式,并试图把块一起来完成我的目标。游戏引擎的想法很简单,但同时我不想牺牲可用性。



这是一个我正在想的蓝图,让我知道是否可以看到任何下降,尤其是在可扩展性:

  class Object 
{
public:
string name;
}

class Object3D:public Object
{
public:
int x;
int y;
int z;
}

class Object2D:public Object
{
public:
int x;
int y
}

class cube:public Object3D
{
cube():x(0),y(0),z ){}
}

class square
{
...
}

int main()
{
SGL Engine(paramters);

c = cube();
s = square();

Engine-> Lib3D-> AddCube(& c,cube);
Engine-> Lib2D-> AddSquare(& s,square);
Engine-> Input-> keyboard(& kbevent);
while(Engine-> running())
{
if(x)
Engine-> Draw(cube);
else
Engine-> Draw(square);
}
}

void kbevent(event-paramteres)
{
if(key.up)
engineptr-& cube]→move(x,y);
}

目标语言是C ++。

解决方案

首先,请阅读。 p>

做正确的唯一方法是将过程颠倒过来。在游戏中使用引擎后,请将引擎



C ++不是Java。 C ++没有基础 Object 类,它也不需要。不要尝试将一切都放在继承层次结构中。如果每个对象都是多态的,你会削弱自己(一切都必须通过引用或指针传递,避免切片,甚至不会让你的代码更清晰)



世界中的物体具有位置。 不是一个。所以不要继承 x,y,z 坐标。将它们放在一个坐标类中,可以在需要它的类中添加为一个成员。



字符串查找速度很慢,需要处理字符串不是唯一的。 (如果我不小心添加了两个名为cube的对象,该怎么办。)



为什么不尽可能依赖纯引用或指针?



为什么我不能简单地通过 Engine-> Draw(c)告诉引擎绘制立方体?



但真的,最重要的建议是,不要试图写一个收缩包装的游戏引擎,以供以后使用。你将到达你现在的位置:需要重写它,并且这一次做它正确如果你想要得到一些有效的东西,你需要从它的用例开始写首先游戏当你完成后,你可以开始重构代码以分离引擎。这样,你最终得到了一个工作的引擎。


I am going to be re-creating a game engine I created awhile back. This time I want to do it right, so I did some research into design patterns and tried to put pieces together to accomplish my goal. The idea of the game engine is simplicity, but at the same time I don't want to sacrifice usability.

Here is a blueprint of what I am thinking, let me know if you can see any downfalls especially in expandability:

class Object
{
    public:
        string name;
}

class Object3D : public Object
{
    public:
        int x;
        int y;
        int z;
}

class Object2D : public Object
{
    public:
        int x;
        int y;
}

class cube : public Object3D
{
    cube() : x(0), y(0), z(0), name("cube") {}
}

class square
{
    ...
}

int main()
{
    SGL Engine(paramters);

    c = cube();
    s = square();

    Engine->Lib3D->AddCube(&c, "cube");
    Engine->Lib2D->AddSquare(&s, "square");
    Engine->Input->keyboard(&kbevent);
    while(Engine->running())
    {
        if (x)
            Engine->Draw("cube");
        else
            Engine->Draw("square");
    }
}

void kbevent(event-paramteres)
{
    if (key.up)
        engineptr->objects["cube"]->move(x,y);
}

The target language is C++.

解决方案

First, read this.

The only way to do it "right" is to turn the process upside down. Make the engine after you've used it in a game.

Second. C++ isn't Java. C++ doesn't have a base Object class, and it doesn't need one either. Don't try to fit everything into an inheritance hierarchy. If every object is polymorphic you're crippling yourself (everything has to be heap-allocated and passed by reference or pointer to avoid slicing, for example, and it doesn't even make your code any clearer)

An object in the world has a position. It is not one. So don't inherit x, y, z coordinates. Put them in a coordinate class, which can be added as a member in the classes that need it.

String lookups are slow, and you need to handle the case when strings are not unique. (What if I accidentally add two objects with name "cube".

Why not rely on plain references or pointers as much as possible?

Why can't I simply do Engine->Draw(c) to tell the engine to draw the cube?

But really, the most important piece of advice is, don't try to write a shrink-wrapped game engine for later use. You're going to end up exactly where you are now: needing to "rewrite it, and this time do it right". If you want to get something out of it that works, you need to start with its use case. Write the game first. When you've done that, you can start refactoring the code to separate the engine out. That way, you end up with an engine that works.

这篇关于游戏引擎开发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆