2.2. Manage your own scene manager

For more advanced uses, you may use TCastleCustomWindow, which doesn't create scene manager automatically for you. Instead, you have to create and manage scene manager instance yourself. You create yourself an instance of TCastleSceneManager (or any descendant of this class), and you add it to TCastleCustomWindow.Controls. This is slightly more complex, but also allows more flexibility:

  • You can implement and use your own descendant of TCastleSceneManager, overriding some methods, and thus making some special rendering tricks.

  • Sometimes, you don't want your scene manager to be present on controls all the time. For example, if you create new scene manager for every level of your game, you probably want to manually remove/add chosen scene manager instance from/to TCastleCustomWindow.Controls.

Example using this approach:

var
  Window: TCastleWindowCustom;
  SceneManager: TCastleSceneManager;
  Scene: TCastleScene;
begin
  Scene := TCastleScene.Create(Application
    { Owner that will free the Scene });
  Scene.Load('my_scene.x3d');
  Scene.Spatial := [ssRendering, ssDynamicCollisions];
  Scene.ProcessEvents := true;

  SceneManager := TCastleSceneManager.Create(Application);
  SceneManager.Items.Add(Scene);
  SceneManager.MainScene := Scene;

  Window := TCastleWindowCustom.Create(Application);
  Window.Controls.Add(SceneManager);
  Window.InitAndRun;
end.

This still looks relatively straightforward, right? You create 3D object (Scene), you create 3D world (SceneManager), and a window to display the 3D world (Window). The Lazarus component equivalent to TCastleWindowCustom is called TCastleControlCustom.