TCastleWindow missing?

I’m not sure if it’s supposed to be a component like TCastleControl but I can’t see it anywhere in my lazarus, if I try to autocomplete window:= then TCastleWindow doesn’t even show up

Hi, @gregest and welcome to the forum! :slight_smile:

There are two ways you can create a game in Castle Game Engine:

  1. You can use Lazarus LCL, and add TCastleControl, among any other Lazarus controls, to the form. This is a bit easier, as Lazarus will automatically add all dependencies to Uses section and will configure the project options for you.

  2. (I really advice going this way) Use an universal TCastleWindowBase to create game environment. With TCastleWindowBase you can ensure portability of your game much easier (like compilation for Android).

In order to have TCastleWindow-related classes available for hints in Lazarus (and use the second approach) you have to “File > New > Simple Program”. Then “Package > Open Package File (*.lpk)”. Navigate to Castle Game Engine folder and add castle_base.lpk and castle_window.lpk to your project (“Use > Add to project” for both).

Now in your Uses section include CastleWindow and you are ready to go.

This is the very minimal example how to create a TCastleWindowBase this way:

program Project1;

uses CastleWindow;

var
  Window: TCastleWindowBase;

begin
  Window := TCastleWindowBase.Create(Application);
  Window.OpenAndRun;
end.

However, this is a “minimal” example. In order to use the newly created window efficiently, check the documentation https://castle-engine.io/manual_window.php and/or examples with your Castle Game Engine.

oh, so it needs to be in a seperate code, not inside the main bigger code or maybe I’m misunderstanding

In order to work with Castle Window you have to initialize it properly (at the beginning of your code) and add something to it (like images, 3D objects, effects, etc.). That would require reorganization of your code a bit. You’ll need to make a proper initialization like you can see e.g. in examples/portable_game_skeleton/my_fantastic_game_standalone.lpi - Everything else should work out-of-the box, unless your code did something very complex that goes beyond what is “easy to port” to CGE.

If you already have a “main bigger code” done in Lazarus LCL, then indeed you’re much easier to work with TCastleControl which will simply add itself to the other objects on your Form and you can use it right away from your ready app. In fact that was the way I my first Castle Game Engine-made game worked (Project Helena) - it uses a “ready” Form developed for many years, 10k lines code base; and using Castle Game Engine TCastleControl for drawing and SoundEngine for music/sound.

okay I think I understand, I mainly asked because the manual mostly uses Tcastlewindow rather than castlecontrol so I thought I was missing something, thank you for the help

Yeah. Note that TCastleControl is working almost exactly like TCastleWindow. I.e. when in examples a Window is used, you can simple write CastleControl1 instead and it’ll work most of the time (unless you’re doing something too complex).

oh that’s very helpful to know, thank you for the help, I will still need to create the simple program portion for the other things, correct?

No. If you’re adding a TCastleControl to your already-working Lazarus project (not a “simple program” but an “Application”), nothing more is needed - Lazarus will make everything for you. Of course, you’ll still need to add and use events, like OnRender (to draw something on TCastleControl) or OnPress (to receive keyboard/mouse events) or OnUpdate (to do something every frame) - but that’ll be “Extending your code” to use Castle Game Engine.

You’ll need to start with a “simple program” only if you want to use TCastleWindow, e.g. if you’re planning to port your game/project to Android or want additional features, like a full-screen mode. UPD: note that even if you start with TCastleControl, you can “translate” your code from TCastleControl into using TCastleWindowBase - however, that’ll require some reworking of how the application is initialized and manages GUI elements.

perfect, that’s all of the questions I had and I guess I will be staying with TCastleControl at the beginning since it just seems simpler to use, thank you for the help

1 Like

turns out I don’t actually understand because I just can’t figure out what to replace the variables with that the examples use because even when I put them as global variables like MyLabel: TCastleLabel it just says identifier not found so I have no idea how to recreate it using TCastleControl, so I just don’t see what is the “conversion” between TCastleControl and CastleWindow

To use TCastleLabel you have either to add it from Lazarus “Castle” tab (in CGE 6.4; I think it was removed in CGE 6.5) or manually add CastleControls to your Uses section. There are a lot of different “controls” you can insert into your TCastleControl, most of which are found in CastleControls unit. Note that you can see which unit the class belongs to in API reference: https://castle-engine.io/apidoc/html/ClassHierarchy.html — it’s very efficient, once you learn to use it. E.g. if you find TCastleLabel there you’ll get to a thorough description page https://castle-engine.io/apidoc/html/CastleControls.TCastleLabel.html which also mentions: “Unit CastleControls” - i.e. it’s the unit you need to have in your Uses section to use this element.

If you’re working with CGE examples or other projects created with Castle Game Engine - you can simply ctrl+click the definition of some control or other class (TCastleLabel in example above) and Lazarus will automatically take you to the definition of the class in the unit it is defined.

E.g. if you want to draw a TDrawableImage (TGlImage in CGE 6.4) then you’ll need to add Uses ... CastleGlImages;. And if you want a TCastleSceneManager then you’ll need Uses ... CastleSceneManager;. Etc.

It doesn’t have anything to do with the problem above. E.g. you need to Window.Controls.Add(SomeCastleLabel); or CastleControl.Controls.Add(SomeCastleLabel); - they’ll work the same way. In both ways you need to have TCastleLabel available in “namespace” - and that needs Uses ... CastleControls;.

right, I forgot about the components, my bad

thank you for the help again and I’m sorry for asking stupid questions

1 Like
  1. The difference between TCastleControl and TCastleWindow is that TCastleControl is a component on a normal, Lazarus form. In contrast: TCastleWindow is a complete window, unrelated to normal Lazarus GUI.

    Indeed if you have an existing project using Lazarus forms, Lazarus GUI, then adding TCastleControl to the form may be easiest way to start with CGE.

    On the other hand, for typical games, you only need a TCastleWindow, and you want to do 100% of user interface using CGE. So, really, both approaches make sense. And both approaches offer the same functionality from CGE, i.e. you can place the same stuff inside TCastleControl and in TCastleWindow.

  2. An example how to add CGE UI controls (like TCastleLabel) to TCastleControl is inside the engine examples/lazarus/model_3d_with_2d_controls/, https://github.com/castle-engine/castle-engine/tree/master/examples/lazarus/model_3d_with_2d_controls/ . See mainf.pas unit implementation, https://github.com/castle-engine/castle-engine/blob/master/examples/lazarus/model_3d_with_2d_controls/mainf.pas#L62 .

    Note that this is the “manual” way of adding CGE components, by creating them and adding from code. A better way is to use CGE editor ( https://castle-engine.io/manual_editor.php ), which allows to design a user interface and then you can easily load it using UserInterfaceLoad function from CastleComponentSerialize unit. At some point we’ll update this example to use the CGE editor and UserInterfaceLoad.

  3. To complicate matters a bit, note that there are also TCastleControlBase and TCastleWindowBase. They are similar to their counterparts (without the Base suffix), but they do not have a default SceneManager instance inside. For new games, I would advise using TCastleControlBase / TCastleWindowBase (instead of TCastleControl / TCastleWindow), although our examples are not yet updated to show the new way.

1 Like

I tried using the CGE editor but for some reason it keeps telling me “cannot find “zip” program on $PATH” even though I’m pretty sure I already added WinRAR to my path variable so I’m not sure why it’s still not recognizing it

I think you miss zlib1.dll (and maybe other required DLLs) in the $PATH or in the game “root” folder. You can find it in tools/build-tool/data/external_libraries/ and select the corresponding 32 or 64 bit libraries directory (corresponding to whether you use 32 or 64 bit Lazarus+FPC). It doesn’t have any relation to WinRAR or any other standalone archiver.

See https://castle-engine.io/documentation.php#section_libraries for more details.

oh I thought I didn’t need those DLLs since it even mentions that “If you use our or build tool, the DLL files will be automatically copied alongside your EXE file, so you don’t have to do anything.” even after putting all the DLLs in the folder where the exe is, it still says the same thing about zip

Indeed you should not need to deal with DLLs if you just run a ready precompiled CGE from https://github.com/castle-engine/castle-engine/releases/tag/snapshot . The editor comes packaged with necessary DLLs, and compiling applications from the editor will automatically copy the DLLs alongside your EXE.

As for the “zip” error - when exactly do you see it?

Compiling and running CGE applications should not make any error about “zip”.

It’s possible to see this error, if you try “package” option (from the editor or the build tool). You need to install a “zip” program to make it work, since the default package format on Windows is zip. You can get “zip” e.g. by installing Cygwin. Zip is not related to WinRAR at all.

In any case, you should not need “zip” if you don’t need to execute “package”.

1 Like

I was trying to run the “package” which is when it told me that “zip” is missing so I assumed it was something with .zip file extension, but if I don’t need the package function which I’m not sure what it’s for, I assume releasing the app, then I won’t bother with it for now

The “package” function packages the application exe + data into a zip file (on Windows). See https://github.com/castle-engine/castle-engine/wiki/Build-Tool , section “package” https://github.com/castle-engine/castle-engine/wiki/Build-Tool#package for details. It uses command-line “zip” for that purpose, so indeed it will make an error if you don’t have it.

Indeed you don’t need to use “package” for now :slight_smile: The important thing is that using “compile” and “run” works.

You can later release CGE applications in any way you like, you can also package them yourself into any format (rar, zip). The “package” function is helpful… but it’s definitely not necessary to use it :slight_smile:

1 Like

Ah I see, I thought I needed so I wanted to get it working because I like it when all the things work