Jump to content

Fun with Stateless API

Josh

330 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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...