Skip to content

RobLoach/raylib-tmx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

118 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

raylib-tmx

Load Tiled .tmx files for tile maps in raylib, with TMX C Loader.

example/raylib-tmx-example.png

Usage

This is a header-only library. To use it, define RAYLIB_TMX_IMPLEMENTATION in one .c source file before including raylib-tmx.h. You will also have to link its dependencies:

If you're using CMake, libxml2 and zlib come packed in.

Example

See the example directory for a demonstration of how to use raylib-tmx.

#include "raylib.h"

#define RAYLIB_TMX_IMPLEMENTATION
#include "raylib-tmx.h"

int main() {
    InitWindow(800, 450, "[raylib-tmx] example");

    tmx_map* map = LoadTMX("desert.tmx");

    while(!WindowShouldClose()) {

        BeginDrawing();
        {
            ClearBackground(RAYWHITE);
            DrawTMX(map, 0, 0, WHITE);
        }
        EndDrawing();
    }

    UnloadTMX(map);

    CloseWindow();
    return 0;
}

API

tmx_map* LoadTMX(const char* fileName);
void UnloadTMX(tmx_map* map);
Color ColorFromTMX(uint32_t color);
void DrawTMX(tmx_map *map, int posX, int posY, Color tint);
void DrawTMXLayer(tmx_map *map, tmx_layer *layers, int posX, int posY, Color tint);
void DrawTMXTile(tmx_tile* tile, int posX, int posY, Color tint);
void DrawTMXObjectTile(tmx_tile* tile, int baseGid, Rectangle destRect, float rotation, Color tint);

typedef struct {
    enum {
        COLLISION_RECT,
        COLLISION_POINT,
        COLLISION_POLYGON,
        COLLISION_POLYLINE,
        COLLISION_ELLIPSE
    } type;
    union {
        Rectangle rect;
        Vector2   point;
        struct {
            double** points;
            int count;
        } polygon;
    };
} RaylibTMXCollision;

typedef void (*tmx_collision_functor)(tmx_object *object, RaylibTMXCollision collision, void* userdata);
void CollisionsTMXForeach(tmx_map *map, tmx_collision_functor callback, void* userdata);
RaylibTMXCollision HandleTMXCollision(tmx_object* object);

Refer to the libTMX documentation to see how to use the tmx_map* map object beyond rendering.

Development

To build the example locally, and run tests, use cmake.

git submodule update --init
mkdir build
cd build
cmake ..
make
cd examples
./raylib-tmx-example

Alternatives

This is not the only attempt to get Tiled working in raylib...

Contributors

License

raylib-tmx is licensed under an unmodified zlib/libpng license, which is an OSI-certified, BSD-like license that allows static linking with closed source software. Check LICENSE for further details.

About

Load Tiled .tmx files for tile maps in raylib, using the TMX C Loader.

Topics

Resources

License

Stars

Watchers

Forks

Contributors