我目前被一些问题卡住了,不知道如何解决它。
我开始开发一个简单的2D引擎,使用SFML渲染东西,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圆时的结果:

希望你能帮上忙,我描述得很好:
谢谢:)
--更新了main.cpp中的代码
发布于 2016-07-12 20:25:43
我不确定发生了什么,但我在代码中看到了一些问题。
首先:不要从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的长度。你真的想这么做吗?
更重要的是,你在每一帧中都画出了飞溅。如果您希望仅在开头显示splash,那么行window.draw(splash_screen); // splash screen before Lua script is loaded可能应该在某种条件指令中。
我不知道画splash_screen和画圆之间有什么区别。它可能会对我们在屏幕上看到的东西产生一些影响。
还有一件事:强烈建议避免使用全局变量。CircleDrawList是一个全局变量,我的建议是至少将其放在某个名称空间中,或者更好地将其包含在某个类中。
https://stackoverflow.com/questions/38328254
复制相似问题