c++ - Draw loop and Lua (Luabridge) -
i'm stuck on issue , don't know how fix it.
i started working on simple 2d engine using sfml rendering stuff , lua scripting language. engine starts splash screen first before lua code loaded ...
my problem don't know how write "good" draw loop lua objects. maybe you'll understand when take on code:
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) { (static bool first = true; first; first = false) { std::thread thr(&lua_module); thr.detach(); success = true; } } (sf::circleshape obj : circledrawlist) { window.draw(obj); //window.draw(circledrawlist.front()); } } return 0; }
my circleshape wrapper class lua:
/////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 register" ... .addfunction("draw", &circle::draw) .endclass(); return 1; }
and (if needed) lua script:
local ball = circle() ball:setcolor(255,0,0,255) while true ball:draw() end
result when lua circle moving increasing 1 of vector2 values:
hope can , described :x
thanks :)
-- updated code in main.cpp
i'm not sure happening, see problems in code.
first: don't remove "old" circleshapes circledrawlist
.
in function circle::draw()
pushing object front of list circledrawlist.push_front(circleshape);
never remove it. using circledrawlist.front()
gives access first element have use method pop_front()
remove it.
second thing. @ code:
//iterating through whole list range-for: (sf::circleshape obj : circledrawlist) { //why use circledrawlist.front()? //you should use obj here! window.draw(circledrawlist.front()); }
as code draws first element of list n
times n
length of circledrawlist. want this?
moreover drawing splash every frame. if wish display splash @ beginning, line window.draw(splash_screen); // splash screen before lua script loaded
should in sort of conditional instruction.
i don't know between drawing splash_screen , drawing circles. have impact on see on screen.
and 1 more thing: recommended avoid using global variables. circledrawlist
global variable , advice put @ least in namespace, or better enclose in class.
Comments
Post a Comment