3. Display a window

You need to initialize a rectangular area on the screen that will be able to display 3D content. This is called an OpenGL context. There are two ways of doing this:

  1. Integration inside Lazarus form: Use our TCastleControl inside normal Lazarus form. You should choose this method if you want to integrate game context with normal GUI (Lazarus forms, controls) or if you're just more comfortable with dropping components on a Lazarus form.

    • Create new project (using Lazarus "New Project" menu item). Choose "Application".
    • Pick TCastleControl from the component palette (tab "Castle") and drop it on a regular Lazarus form.
    • Press "Run" :)

    There is no example code to show here, as you don't need to write any code to make this work :) In the following tutorial chapters we will show some code to run your game. You will want to place it inside the OnOpen event.

  2. Without using Lazarus forms: Use our own CastleWindow unit, that defines window class TCastleWindow. This avoids some problems with Lazarus application loop (for example, mouse look is smooth) and is suitable for desktops as well as Android/iOS.

    You can develop such programs using Lazarus, or any other text editor. The only real requirement is having a Free Pascal Compiler installed.

    Place this source code in your program file (lpr).

    {$mode objfpc}{$H+} // you can also set ObjFpc and long strings in project compiler options
     
    uses CastleWindow;
    var
      Window: TCastleWindow;
    begin
      Window := TCastleWindow.Create(Application);
      Window.OpenAndRun;
    end.

    See the previous chapter for description how to compile your project, so that FPC can find our units like CastleWindow. Press "Run" and behold, a black window :)