繁体   English   中英

抽奖和Lua(Luabridge)

[英]Draw loop and Lua (Luabridge)

我目前在某个问题上遇到问题,不知道如何解决。

我开始使用SFML制作简单的2D引擎来渲染内容,并使用Lua作为我的脚本语言。 在加载Lua代码之前,引擎首先启动屏幕。

我的问题是我不知道如何为我的Lua对象编写一个“好的”绘制循环。 当我们看一下我的代码时,也许您会理解的:

Main.cpp:

...

int draw_stuff()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), TITLE);

    while (window.isOpen()) {

        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

    window.clear();

    if (!success) {
        window.draw(sp_splash_screen);
    }

    if (clock.getElapsedTime().asSeconds() > 2) {

        for (static bool first = true; first; first = false)
        {
            std::thread thr(&lua_module);
            thr.detach();

            success = true;
        }
    }


    for (sf::CircleShape obj : CircleDrawList) {
        window.draw(obj);
        //window.draw(CircleDrawList.front());
    }

    }

    return 0;
}

我的Lua的CircleShape包装器类:

/////shapes.h

extern std::list<sf::CircleShape> CircleDrawList;

class Circle
{
public:

    Circle();

         ...

    void draw();

private:
    sf::CircleShape circleShape;

protected:
    //~Circle();();
};


/////shapes.cpp

std::list<sf::CircleShape> CircleDrawList;

 ...

void Circle::draw()
{
    //std::cout << "DRAW IN LIST" << std::endl;

    CircleDrawList.push_front(circleShape);
    //if (drawableList.size() == 4)
        //drawableList.pop_front();
}

 ...

int luaopen_shapes(lua_State *L)
{
    luabridge::getGlobalNamespace(L)
        .beginClass<Circle>("Circle")
        .addConstructor <void(*) (void)>()

                 ... "other stuff to register" ...

        .addFunction("draw", &Circle::draw)
        .endClass();

    return 1;
}  

最后(如果需要)我的lua脚本:

local ball = Circle()
ball:setColor(255,0,0,255)

while true do

    ball:draw()

end

通过增加Vector2值之一来移动Lua Circle时的结果:

在此处输入图片说明

希望你能提供帮助,我对它的描述很好:x

谢谢 :)

-更新了main.cpp中的代码

我不确定发生了什么,但是我在代码中看到了一些问题。

第一:不要从CircleDrawList删除“旧的” CircleDrawList

在功能Circle::draw()中,使用CircleDrawList.push_front(circleShape);将对象推到列表的前面CircleDrawList.push_front(circleShape); 但您绝不会从中删除任何东西。 使用CircleDrawList.front()可以访问第一个元素,但是必须使用方法pop_front()才能将其删除。

第二件事。 看一下代码:

//iterating through whole list with range-for:
for (sf::CircleShape obj : CircleDrawList)
{
    //why do you use CircleDrawList.front()?
    //You should probably use obj here!
    window.draw(CircleDrawList.front());
}

现在,此代码将绘制列表中的第一个元素n次,其中n是CircleDrawList的长度。 您真的要这样做吗?

此外,您正在绘制飞溅的每一帧。 如果只希望在开始时显示启动window.draw(splash_screen); // splash screen before Lua script is loaded ,则请在window.draw(splash_screen); // splash screen before Lua script is loaded行中window.draw(splash_screen); // splash screen before Lua script is loaded window.draw(splash_screen); // splash screen before Lua script is loaded可能应该是某种条件指令。

我不知道绘制splash_screen和绘制圆圈之间是什么。 这可能会影响我们在屏幕上看到的内容。

还有一件事:强烈建议避免使用全局变量。 CircleDrawList是一个全局变量,我的建议是至少将其放在某个名称空间中,甚至最好将其包含在某个类中。

暂无
暂无

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

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