How to render 2D games with images and sprites

Platformer

1. How to render 2D things

Warning
This page compares 2 possible approaches to 2D rendering in CGE. This page is really not a good introduction into "making 2D games in CGE". For such introduction, instead follow the manual from the beginning, in particular learn about viewports, scenes and how to use them for 2D.

Use TCastleViewport. Add TCastleViewport control to the window, and inside it draw things by creating TCastleTransform descendants (like TCastleScene, TCastleImageTransform, TCastlePlane).

You should call TCastleViewport.Setup2D on the viewport to make it most natural for 2D games (makes camera orthographic and looking down Z axis, so your 2D world spans in XY). When using the engine editor, use the menu item "Add User Interface → Viewport (2D)"  — it adds a TCastleViewport and calls TCastleViewport.Setup2D to configure it.

Advice when to use:

  • This approach is most powerful and flexible, so this is the approach we advice now, always.

  • This approach allows engine to take care of animations, physics, and other cool stuff for you.

  • All Cat-astrophe Games 2D games ("Dragon Squash", "Escape from the Universe", "The Unholy Society") were implemented using this approach.

1.2. Simple (imperative) option: Render using TDrawableImage

Use TDrawableImage as your main way to draw. In this approach, you create TDrawableImage instance for each image, and then draw it in overridden TCastleUserInterface.Render method. This is the same approach as we use for our 2D user-interface rendering (various TCastleUserInterface instances use it). The main advantage of this approach is simplicity: you just draw 2D images.

A similar approach is to draw your game using multiple TCastleImageControl instances. TCastleImageControl is a simple user-interface control that draws images, using TDrawableImage under the hood, exposing mostly the same features.

Advice when to use: This approach is very easy to start. You have relatively small API to learn. You just learn how to use TDrawableImage, and you draw inside your own TMyControl.Render however you like. If all you really want is a flexible API to draw images — this is it.

2. Why there are 2 approaches to render 2D images/sprites

Because they are both useful :)

  • Drawing using TDrawableImage is imperative.

  • Settings things up using TCastleScene is declarative.

The declarative approach is more powerful (the engine can do automatically a lot of more stuff for you, this way).

The imperative stuff is simpler to use, and enough for simple use-cases. I wondered about removing this approach, but it seems many people like it, and it is enough for many use-cases.

3. Can these methods be combined, to render using TDrawableImage within TCastleScene?

I plan to enable rendering using TDrawableImage into a TCastleScene one day. Then you could render user interface into TCastleScene, rotate this TCastleScene, and have easy user-interface in 3D. This is part of roadmap .


To improve this documentation just edit this page and create a pull request to cge-www repository.