(include raylib.h) (include string.h) (define RAYTMX_IMPLEMENTATION 1) (pub include raytmx.h) (import utils) (fn find-layer ((layers (* TmxLayer)) (layers-length u32) (name (* const char))) (* (struct TmxLayer)) (if (== layers NULL) (return NULL)) (var i u32 0) (while (< i layers-length) (var layer (* (struct TmxLayer)) (& [layers i])) (if (== (strcmp (. layer name) name) 0) (return layer)) (if (&& (== (. layer type) LAYER-TYPE-GROUP) (> (. layer layersLength) 0)) (do (var found (* (struct TmxLayer)) (find-layer (. layer layers) (. layer layersLength) name)) (if (!= found NULL) (return found)))) (++ i)) (return NULL)) (pub fn tmx-object-group ((map (* TmxMap)) (name (* const char))) (* TmxObjectGroup) (if (== map NULL) (return NULL)) (var layer (* TmxLayer) (find-layer (. map layers) (. map layersLength) name)) (if (== layer NULL) (return NULL)) (if (!= (. layer type) LAYER-TYPE-OBJECT-GROUP) (return NULL)) (return (& (. layer exact objectGroup)))) (fn find-object ((map (* TmxMap)) (layer-name (* const char)) (index int)) (* TmxObject) (var group (* TmxObjectGroup) (tmx-object-group map layer-name)) (if (== group NULL) (return NULL)) (if (< index 0) (return NULL)) (if (>= index (cast (. group objectsLength) int)) (return NULL)) (var objects (* TmxObject) (. group objects)) (return (& [objects index]))) (pub record tmx-map ((handle (* TmxMap)) (width int) (height int) (tile-width int) (tile-height int))) (pub fn load-tmx ((file (* const char))) (* tmx-map) (var handle (* TmxMap) (LoadTMX file)) (if (== handle NULL) (do (TraceLog LOG-ERROR "failed to load TMX \"%s\"" file) (return NULL))) (var map (* tmx-map) (mem-alloc tmx-map)) (= (. map handle) handle) (= (. map width) (cast (. handle width) int)) (= (. map height) (cast (. handle height) int)) (= (. map tile-width) (cast (. handle tileWidth) int)) (= (. map tile-height) (cast (. handle tileHeight) int)) (TraceLog LOG-INFO "loaded TMX \"%s\": %d x %d tiles, %d x %d pixels" file (. map width) (. map height) (* (. map width) (. map tile-width)) (* (. map height) (. map tile-height))) (return map)) (pub fn unload-tmx ((map (* tmx-map))) void (if (!= map NULL) (do (UnloadTMX (. map handle)) (free map)))) (pub fn animate-tmx ((map (* tmx-map))) void (if (!= map NULL) (AnimateTMX (. map handle)))) (pub fn draw-tmx ((map (* tmx-map)) (cam (* const Camera2D))) void (if (!= map NULL) (DrawTMX (. map handle) cam NULL 0 0 WHITE))) (pub fn draw-tmx-layer ((map (* tmx-map)) (layer-name (* const char)) (cam (* const Camera2D))) void (if (!= map NULL) (do (var handle (* TmxMap) (. map handle)) (var layer (* TmxLayer) (find-layer (. handle layers) (. handle layersLength) layer-name)) (if (== layer NULL) (do (TraceLog LOG-WARNING "TMX: layer \"%s\" not found" layer-name) (return))) (DrawTMXLayers handle cam NULL layer 1 0 0 WHITE)))) (pub fn tmx-pixel-width ((map (* tmx-map))) int (if (== map NULL) (return 0)) (return (* (. map width) (. map tile-width)))) (pub fn tmx-pixel-height ((map (* tmx-map))) int (if (== map NULL) (return 0)) (return (* (. map height) (. map tile-height)))) (pub fn tmx-object-count ((map (* tmx-map)) (layer-name (* const char))) int (if (== map NULL) (return 0)) (var group (* TmxObjectGroup) (tmx-object-group (. map handle) layer-name)) (if (== group NULL) (return 0)) (return (cast (. group objectsLength) int))) (pub fn tmx-object-name ((map (* tmx-map)) (layer-name (* const char)) (index int)) (* const char) (if (== map NULL) (return "")) (var object (* TmxObject) (find-object (. map handle) layer-name index)) (if (== object NULL) (return "")) (if (== (. object name) NULL) (return "")) (return (. object name))) (pub fn tmx-object-type ((map (* tmx-map)) (layer-name (* const char)) (index int)) (* const char) (if (== map NULL) (return "")) (var object (* TmxObject) (find-object (. map handle) layer-name index)) (if (== object NULL) (return "")) (if (== (. object typeString) NULL) (return "")) (return (. object typeString))) (pub fn tmx-object-x ((map (* tmx-map)) (layer-name (* const char)) (index int)) float (if (== map NULL) (return 0.0)) (var object (* TmxObject) (find-object (. map handle) layer-name index)) (if (== object NULL) (return 0.0)) (return (cast (. object x) float))) (pub fn tmx-object-y ((map (* tmx-map)) (layer-name (* const char)) (index int)) float (if (== map NULL) (return 0.0)) (var object (* TmxObject) (find-object (. map handle) layer-name index)) (if (== object NULL) (return 0.0)) (return (cast (. object y) float))) (pub fn tmx-object-width ((map (* tmx-map)) (layer-name (* const char)) (index int)) float (if (== map NULL) (return 0.0)) (var object (* TmxObject) (find-object (. map handle) layer-name index)) (if (== object NULL) (return 0.0)) (return (cast (. object width) float))) (pub fn tmx-object-height ((map (* tmx-map)) (layer-name (* const char)) (index int)) float (if (== map NULL) (return 0.0)) (var object (* TmxObject) (find-object (. map handle) layer-name index)) (if (== object NULL) (return 0.0)) (return (cast (. object height) float))) (pub fn tmx-find-object-by-name ((map (* TmxMap)) (layer-name (* const char)) (obj-name (* const char))) (TmxObject *) (if (== obj-name NULL) (return NULL)) (var group (* (struct TmxObjectGroup)) (tmx-object-group map layer-name)) (if (== group NULL) (return NULL)) (var objects (* (struct TmxObject)) (. group objects)) (var i u32 0) (while (< i (. group objectsLength)) (var object (* (struct TmxObject)) (& [objects i])) (if (&& (!= (. object name) NULL) (== (strcmp (. object name) obj-name) 0)) (return object)) (++ i)) (return NULL)) (pub fn tmx-is-tile-walkable ((map (* tmx-map)) (layer-name (* const char)) (x float) (y float)) bool ;; x and y must be already in map coordinates (compensated for map origin) (if (|| (== map NULL) (== layer-name NULL)) (return false)) (var layer (* TmxLayer) (find-layer (. map handle layers) (. map handle layersLength) layer-name)) (if (== NULL layer) (do (TraceLog LOG-WARNING "TMX: layer \"%s\" not found" layer-name) (return false))) (var tx _ (cast (/ x (. map handle tileWidth)) int)) (var ty _ (cast (/ y (. map handle tileHeight)) int)) (if (|| (< tx 0) (< ty 0) (>= tx (. map width)) (>= ty (. map height))) (do ;; Miss! (TraceLog LOG-INFO "TMX: no tile at %d %d on %s" tx ty layer-name) (return false))) (var raw-gid u32 [(. layer exact tileLayer tiles) (+ tx (* ty (. map width)))]) (if (== raw-gid 0) ;; Empty tile (do (TraceLog LOG-INFO "TMX: tile at %d %d on %s is empty" tx ty layer-name) (return false))) ;; strip flip/rotation bits (var gid _ (& raw-gid 0x0FFFFFFF)) (if (>= gid (. map handle gidsToTilesLength)) (return false)) ;; TODO: add "blocked" property ;;;(for (var i u32 0) (< i (. map handle tilesetsLength)) (++ i) ;;; (var tileset (* TmxTileset) (& [(. map handle tilesets) i])) ;;; (if (&& (>= gid (. tileset firstGid)) ;;; (<= gid (. tileset lastGid))) ;;; (do ;;; (var local-gid _ (- gid (. tileset firstGid))) ;;; (return (& [(. tileset tiles) local-gid]))))) ;; ??? (return true)) (pub fn tmx-object-rect ((o (* TmxObject))) Rectangle (return #(Rectangle : (. o x) (. o y) (. o width) (. o height)))) (pub fn tmx-object-at-point ((group (* TmxObjectGroup)) (point Vector2)) (* TmxObject) (if (== group NULL) (return NULL)) (var objects (* TmxObject) (. group objects)) (var i u32 0) (while (< i (. group objectsLength)) (var obj (* TmxObject) (& [objects i])) (if (&& (== (. obj type) OBJECT-TYPE-RECTANGLE) (CheckCollisionPointRec point (tmx-object-rect obj))) (return obj)) (++ i)) (return NULL))