Using the Castle Game Engine

Accomplished Pascal students considering a career in game development or computer graphics should take a look at the Castle Game Engine developed by Michalis Kamburelis (Kambi). A good place to start to learn the capabilities of the engine is the PDF of the slide show presented by Michalis in June 2015. The document provides instructions on getting started. We managed to get the FPS 3D game working after making some changes to the information given on the slides. We trust that our instructions on the next page will help you to get started more easily.

Note added in March 2017:

The Castle Game Engine now has more thorough documentation, enhanced functionality and further examples.

In order to learn how the engine works (and a lot more besides) see the original documentation. This shows how the x3d format for 3D models evolved from VRML and explains how the data in the model files is converted to OpenGL code for rendering. There are detailed descriptions of event handling and animations and the use of octrees (recursive divisions of space into octants). Michalis explains crucial choices that he made when designing the engine and covers among many concepts:
  • alpha blending;
  • bounding boxes;
  • bump mapping;
  • caching;
  • collision detection;
  • crease angles;
  • culling;
  • directed graphs;
  • fog;
  • Gouraud shading;
  • inline nodes;
  • interpolations;
  • lighting;
  • polygon stipples;
  • ray tracing;
  • shaders;
  • shadow volumes;
  • state machines;
  • transforms (including transforms of texture coordinates);
  • transparency;
  • viewports.

Supplied Examples of Engine Use

You will find many interesting examples to learn from in the main download. We describe a few of them in this section.

See a huge range of features demonstrated in fps_game. Use the arrow or wasd keys to move towards a shooting eye or MedKit. When you touch your target, it will appear at the top left of the screen in the inventory. README.txt supplies the instructions for the inventory:

use current item by Enter,
change current item by [ ] or mouse wheel,
drop item by R,
attack (with currently equipped weapon) by Ctrl key

You could deduce these from (1) the code in the initialization section of the CastleScreenManager unit:

{ Basic shortcuts. }
Input_Attack := TInputShortcut.Create(nil, 'Attack', 'attack', igBasic);
Input_Attack.Assign(K_Ctrl, K_None, #0, false, mbLeft);
Input_Attack.GroupOrder := -100; { before other (player) shortcuts }

{ Items shortcuts. }
Input_InventoryShow := TInputShortcut.Create(nil, 'Inventory show / hide', 'inventory_toggle', igItems);
Input_InventoryShow.Assign(K_None, K_None, #0, false, mbLeft);
Input_InventoryPrevious := TInputShortcut.Create(nil, 'Select previous item', 'inventory_previous', igItems);
Input_InventoryPrevious.Assign(K_LeftBracket, K_None, #0, false, mbLeft, mwUp);
Input_InventoryNext := TInputShortcut.Create(nil, 'Select next item', 'inventory_next', igItems);
Input_InventoryNext.Assign(K_RightBracket, K_None, #0, false, mbLeft, mwDown);
Input_UseItem := TInputShortcut.Create(nil, 'Use (or equip) selected item', 'item_use', igItems);
Input_UseItem.Assign(K_Enter, K_None, #0, false, mbLeft);      
and (2) the main program:
  { Allow player to drop items by "R" key. This shortcut is by default inactive
    (no key/mouse button correspond to it), because not all games may want
    to allow player to do this. }

See a video of the demo with interspersed notes about the facilities of the engine.

Press buttons on the right of example resource_animations to see a knight idle, walk, attack, die and hurt.

You can use examples\lazarus\model_3d_viewer to view .x3d models. You can make your own models using Blender then export them as .x3d files.

The folder examples\3d_rendering_processing contains several useful demonstrations including the following.
  • The example multiple_viewports not only demonstrates how to display more than one viewport in a window but also enables you to load a model file and view it after processing in different ways e.g. to obtain a wire-frame view.
  • You do not need to import a model file. Instead, you can build up your scene in code. You can either output it as a .x3d file or use it within the same program. See build_3d_object_by_code.
  • The interesting example call_pascal_code_from_3d_model_script is aptly named!

The example terrain is a very impressive demonstration of randomly generated 3D terrains.

See rift in the fixed_camera_game folder for a way of making a humanoid walk up walls.

You can use doppler_demo in the audio folder to change the pitch of a sound by dragging an object around the screen.

Documentation of the Code

The code is thoroughly commented, and Michalis generates HTML documentation with PasDoc (to the development of which he has contributed). You will see in the comments in the source code some symbols that enhance the automatic documentation. See doc\writing_doc_comments.txt for advice on writing comments for PasDoc.

The Units page tabulates the unit names and their descriptions. Drill down from there to the page for that unit and then to the classes in that unit and then to fields and methods. Many links are supplied to facilitate browsing of the documentation.

Game Assets

The zip file from the download page contains many models used to test the Castle Game Engine, together with text files containing details of their sources and licences. In addition to the model files you will find textures, sounds and shaders.
Programming - a skill for life!

Using SDL, Box2D or the GLScene or Castle Game Engine to write games in Pascal