Jump to content
  • Advertisement
Sign in to follow this  
Memorix101

Help with draw loop and Lua (Luabridge)

This topic is 739 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey :)

I'm currently stuck on some issue and don't know how to fix it.

I started working on a simple 2D engine using SFML for rendering stuff and Lua as my scripting language.
The engine starts with a splash screen first before Lua code is loaded ...

My problem is I don't know how to write a "good" draw loop for my Lua objects.
Maybe you'll understand when we take a look on my 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();
	        window.draw(splash_screen); // splash screen before Lua script is loaded

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


		window.display();

	}

	return 0;
}

My CircleShape wrapper class for 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 to register" ...

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

	return 1;
}  

and finally (if needed) my lua script:

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

while true do

	ball:draw()

end

Result when the Lua Circle is moving by increasing one of the Vector2 values:

Screenshot%20%28186%29.png
 

Hope you can help and I described it good :x

Thanks :)

 

Share this post


Link to post
Share on other sites
Advertisement

for (sf::CircleShape obj : CircleDrawList) { window.draw(CircleDrawList.front()); }
Can't say I understand what you're trying to do, but why do you iterate over the circle list, and constantly draw the first circle?

Share this post


Link to post
Share on other sites

As I tried to explain. I can't find a better solution for my drawing loop. That's the reason why I made this topic ;) 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!