Jump to content

Fun with Stateless API

Josh

220 views

Leadwerks 5 / Turbo makes extensive use of multithreading. Consequently, the API is stateless and more explicit. There is no such thing as a "current" world or context. Instead, you explicitly pass these variables to the appropriate commands.

One interesting aspect of this design is code like that below works perfectly fine. See if you can work through it and understand what's going on:

int main(int argc, const char *argv[])
{
	//Create a model ;)
	auto box = CreateBox(nullptr);

	//Create the world
	auto world = CreateWorld();
	
	//Create a camera
	auto camera = CreateCamera(world);
	camera->Move(0,0,-5);

	//Create an instance of the model in the new world
	auto model = box->Instance(world);

	//Create a window
	auto window = CreateWindow();

	//Create a rendering context
	auto context = CreateContext(window);

	while (not window->Closed())
	{
		if (window->KeyDown(KEY_ESCAPE)) window->Close();
		world->Update();
		world->Render(context);
	}
	return 0;
}

 



5 Comments


Recommended Comments

Just to play devils advocate, instead of having global functions like CreateBox() CreateCamera() and passing in the world, what about functions tied to the world itself like world->CreateCamera(), world->CreateBox()?

Share this comment


Link to comment
1 minute ago, Rick said:

Just to play devils advocate, instead of having global functions like CreateBox() CreateCamera() and passing in the world, what about functions tied to the world itself like world->CreateCamera(), world->CreateBox()?

Good question.

  1. Entities can be created without a world, as shown in the example above.
  2. It's ugly.

Share this comment


Link to comment

For 2, in the C++ realm isn't it kind of "ugly" to have straggler C functions?

Share this comment


Link to comment

If we were following C++ ideology we would not have a creation function at all. You would do this for each object created:

auto model = make_shared<Model>(world);
model->MakeBox();

And I would go out of business because no one would want to use it. 😭

Share this comment


Link to comment

I suppose the closest you could get is having static methods on the class that do the same thing as your straggler C methods. Might help with intellisense to have that instead of having to remember?

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Create Your Account

Sign in

Already have an account? Sign in here.

Sign In Now
×