gpt4 book ai didi

c++ - 绘制循环和 Lua (Luabridge)

转载 作者:行者123 更新时间:2023-11-28 05:35:46 24 4
gpt4 key购买 nike

我目前遇到了一些问题,不知道如何解决。

我开始研究一个简单的 2D 引擎,使用 SFML 渲染内容,并使用 Lua 作为我的脚本语言。在加载 Lua 代码之前,引擎首先以启动画面启动 ...

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

主要.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

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

enter image description here

希望你能帮忙,我描述的很好:x

谢谢:)

-- 更新了 main.cpp 中的代码

最佳答案

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

首先:您不会从 CircleDrawList 中删除“旧的”CircleShapes。

在函数 Circle::draw() 中,您使用 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);//加载 Lua 脚本之前的闪屏 应该是某种条件指令。

我不知道绘制 splash_screen 和绘制圆圈之间有什么区别。它可能会对我们在屏幕上看到的内容产生一些影响。

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

关于c++ - 绘制循环和 Lua (Luabridge),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38328254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com