All methods in our engine take URL as the parameter, not just a FileName. Although in almost all cases you can also pass a filename (absolute or relative to the current directory), if that's all you're interested in.
All loading and saving routines (for 3D models, images, sounds, and all
other resources) automatically deal with URLs. To actually load network
URLs (like http) you only need to set
To directly load or save files (as ObjectPascal
in your own code:
To load, use our simple
It automatically handles all the details for you,
including downloading from network (if
and returns you a
TStream that contains the resource indicated
by the URL. There is an example in the engine code
examples/tools/castle_download that uses this to implement a simple
command-line downloading tool (like
Details about supported URL protocols are below.
To save, use trivial
URLSaveStream function. Right now, it can only save to a local file,
so it merely translates a URL to local filename and creates a
for you. Still, it's a good idea to use it, to uniformly deal with
URLs throughout your application.
Support for network protocols:
Our engine automatically handles downloading data from the network.
http is supported (through
although we plan to add other network protocols (like
https) in the future.
All you have to do to enable network usage is set global
CastleDownload.EnableNetwork variable to
true. By default it is
because for now the downloads are not user-friendly —
they are blocking (we wait for them to finish, there's no way
to cancel a download). This will be improved in the future, and eventually
downloading from network may be enabled by default.
All of the attributes in various files are URLs, so you can refer from a local files to resources on the network.
For example, your game level data may be actually downloaded from the
network when loading level. To do this,
level.xml may use an http protocol when referring to
scene. Like this:
<?xml version="1.0"?> <level name="pits" type="Level" scene="http://svn.code.sf.net/p/castle-engine/code/trunk/castle/data/levels/fountain/fountain_final.x3dv" title="The Pits of Azeroth" placeholders="blender" />and the scene with all associated resources will be downloaded.
Inside 3D models (like X3D, VRML and others), you can use network resources,
for example you can
Inline them (add a 3D model from another file),
you can make
Anchor to them,
you can refer to textures and sounds and scripts and everything else
from the network. Relative URLs are always resolved
with respect to the containing document.
Always (regardless of
we support getting resources from
These are just local filenames encoded as URL.
CastleURIUtils contains routines to operate on URLs (and more general URIs),
including converting between regular filenames and URLs with
Use this to convert a FileName (relative or absolute) to an absolute URL.
URL := FilenameToURISafe(FileName);
FilenameToURISafe(...), in that it will never touch something that already is an URL. On the other hand, there are some obscure cases (when a relative filename starts with a component with colon inside) when it may think that it has URL, while in fact it has a filename that should be converted to
URL := AbsoluteURI(FileNameOrURL);
file:protocol, it will decode back the simple filename. Right now, URL without protcol is also returned back as a simple filename. When the URL uses a different protocol (like
http), returns empty string.
FileName := URIToFilenameSafe(URL);
If you read/write filenames from/to Lazarus classes,
for example if you use Lazarus
TSaveDialog.FileName, use the UTF-8 variants instead:
That is because Lazarus uses UTF-8 for all strings (as opposed to FPC RTL
that uses system encoding).
We can always load resources from
data URIs allow you to embed various resources
(like textures, sounds, other 3D models) inside a parent file,
for example instead of referring to the texture URL from 3D model —
you can embed the actual texture contents inside 3D model file.
This allows you to pack your data into a single file,
which is sometimes desired (it may be easier to distribute),
data URIs are very handy for this. See
data: URI specification.
Our engine includes a tool
to_data_uri (see inside
that can turn any file
into a data URI, and you can use such data URI everywhere where we expect URL.
assets protocol (Android only).
Used to access Android asset files from the Android application.
Asset files live inside your application's .apk file, together with
your compiled game.
Typically you just copy/place the asset files to the
assets/ subdirectory of your Android project,
and then build the .apk using normal Android tools.
For example, file placed in
can be accessed (from the Android app) using the URL
assets:/ protocol to access Anroid assets by URLs is
consistent at least with Qt.
See also Android
docs for more information about assets and project layout.
If you use Lazarus with TCastleControl, we advise to use our dialog componets: TCastleOpenDialog, TCastleSaveDialog, TCastleOpen3DDialog, TCastleOpenPictureDialog, TCastleSavePictureDialog. You can also continue using standard Lazarus dialog components. Our routines (almost) always handle a filename instead of an URL, or you can explicitly convert between filenames and URLs using functions mentioned earlier.
is a more general term. URI uniquely identifies a resource but does not
necessarily tell us how to load (download) or save (upload) it.
We have many routines in CastleURIUtils unit that process URIs (strings), they use the more general term
They complement standard FPC
is a specific type of URI that also tells you how to load or save the resource.
file protocols define URLs.
Most of our routines that load or save use the term
Things get a little more cloudy when you realize there's also
data URI scheme.
It's not precisely an URL (it's not an address of a resource),
but you can load it (since the URI itself contains the resource).
And we support it fully (our
loads it automatically). Admittedly, this means that our loading routines
should rather use the term URL or data URI, but that's just long
and (for those who don't use data URI) confusing, so for simplicity we
just keep (over-)using the term URL. Also, other standards (like CSS
and X3D and VRML) allow placing data URIs inside fields called