Skip to content

A C++11 loader for levels and tile maps created with LDtk (Level Designer ToolKit)

License

Notifications You must be signed in to change notification settings

Madour/LDtkLoader

Repository files navigation

LDtk Loader

LDtk Version Build CodeQL

LDtkLoader is a loader for LDtk levels and tile maps.

Use it to load and get data from LDtk projects in your game.

Supports the entire .ldtk format (including external levels).

For loading older versions, see the Releases page.

Sample code

A taste of simplicity :

// load the file
ldtk::Project ldtk_project;
ldtk_project.loadFromFile("my_project.ldtk");

// get a world
const auto& world = ldtk_project.getWorld("MyWorld");

// get a level
const auto& level1 = world.getLevel("Level1");

// get a layer
const auto& bg_layer = level1.getLayer("BgLayer");

// iterate on the tiles of the layer
for (const auto& tile : bg_layer.allTiles()) {
    // do something with the tile (storing, rendering ...)
}

// iterate on Enemy entities
for (const ldtk::Entity& enemy : level1.getLayer("Entities").getEntitiesByName("Enemy")) {

    // iterate over an array field of Enum values
    for (const auto& item : enemy.getArrayField<ldtk::EnumValue>("items")) {
        // test if field is null
        if (!item.is_null()) {
            // do something with item
            if (item == world.getEnum("Items")["Sword"]) {
                // the enemy has a Sword !
            }
        }
    }

    // get an Entity field
    int enemy_hp = enemy.getField<int>("HP").value();
}

Build

Should work with any C++11 compiler. You can select Debug or Release mode when building.

mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE={Debug|Release} ..
cmake --build . --config {Debug|Release}

This will compile the static library named LDtkLoader in build/lib.

When built in Debug configuration, the library name has the suffix '-d'.

Additional CMake options :

  • -DLDTK_NO_THROW: to print message and exit when there is an error instead of throwing exceptions
  • -DLDTK_BUILD_API_TEST: to build the API test example, to make sure the library works as expected
  • -DLDTK_BUILD_SFML_EXAMPLE=ON: to build the SFML example (requires SFML installed)
  • -DLDTK_BUILD_SDL_EXAMPLE=ON: to build the SDL example (requires SDL2 and SDL2_image installed)
  • -DLDTK_BUILD_RAYLIB_EXAMPLE=ON: to build the raylib example (requires raylib installed)

Advanced CMake options (use if you know what you are doing):

  • LDTK_FIELD_PUBLIC_OPTIONAL=ON: to enable the full optional interface for the ldtk::Field structure.

Install

In the build directory, run :

cmake [-DCMAKE_INSTALL_PREFIX=/install/path/LDtkLoader] ..
cmake --install . --config {Debug|Release}

This will copy the libraries, the headers and the cmake config files to the install path.

How to use LDtkLoader in your project

After installation, to use LDtkLoader in a CMake project, use the find_package command:

find_package(LDtkLoader 1.5)

The find_package command will automatically get the correct library (Release or Debug) depending on your current CMake configuration.

Note

If LDtkLoader was installed at a custom path and CMake is unable to find the package, pass the LDtkLoader_ROOT option to CMake, like this: cmake -DLDtkLoader_ROOT=/install/path/LDtkLoader ...

Then, the library should be linked to the target:

target_link_libraries(YourTarget PRIVATE LDtkLoader::LDtkLoader)

Documentation

API documentation can be found on the wiki.

Demo

Warning

The demos below might be using an older version of the library.

A demo using LDtkLoader with SFML can be found here.

Here is a preview :

LDtkLoader-SFML.mp4

A demo using LDtkLoader with Direct X11 can be found here (by @dontpanic5)

Here is a preview :

Level.Builder.2021-08-11.18-26-08.mp4

To Do

  • Helper classes/methods for SFML or SDL rendering

Contributing

Contributions are welcome.

If you have LLVM tools installed, run clang-format to make sure your code respects the project's code style:

python tools/run_clang_format.py -r src include

To apply the format changes, run:

python tools/run_clang_format.py -i -r src include

Optionaly, you can also run clang-tidy to make sure there are no coding mistakes or code smells (although, some warnings can be ignored):

clang-tidy --config-file=.clang-tidy src/*.cpp -- -I include -I build/include

License

LDtkLoader is licensed under the zlib license.

Bundled dependencies :