An open API service indexing awesome lists of open source software.

https://github.com/robloach/raylib-tmx

Load Tiled .tmx files for tile maps in raylib, using the TMX C Loader.
https://github.com/robloach/raylib-tmx

raylib tiled tiled-map-editor tmx

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# raylib-tmx

Load [Tiled](https://www.mapeditor.org) `.tmx` files for tile maps in [raylib](https://www.raylib.com), with [TMX C Loader](https://github.com/baylej/tmx).

![example/raylib-tmx-example.png](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](include/raylib-tmx.h)*. You will also have to link its dependencies:

- [raylib](https://www.raylib.com/)
- [tmx](https://github.com/baylej/tmx)
- [libxml2](http://xmlsoft.org)
- [zlib](http://zlib.net/) (optional)

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

### Example

See the [example directory](example) for a demonstration of how to use *raylib-tmx*.

``` c
#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

``` c
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](http://libtmx.rtfd.io/) to see how to use the `tmx_map*` map object beyond rendering.

## Development

To build the example locally, and run tests, use [cmake](https://cmake.org/).

``` bash
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...

- [raylib-tileson](https://github.com/robloach/raylib-tileson): Uses the [tileson C++ library](https://github.com/SSBMTonberry/tileson)
- [raylib-tiled](https://github.com/RobLoach/raylib-tiled): Not working, but leverages the [cute_tiled.h library](https://github.com/RandyGaul/cute_headers/blob/master/cute_tiled.h) instead

## Contributors

- [Bayle Jonathan](https://github.com/baylej) for TMX C Loader, and the [tmx example](https://github.com/baylej/tmx/blob/master/examples/raylib/raylib.c) this was inspired from
- [burakssen](https://github.com/burakssen)
- [mattj1](https://github.com/mattj1)
- [brccabral](https://github.com/brccabral)
- [cortexmancer](https://github.com/cortexmancer)

## 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](LICENSE) for further details.