implement initial tilemap rendering
This commit is contained in:
209
tmx.sex
Normal file
209
tmx.sex
Normal file
@@ -0,0 +1,209 @@
|
||||
(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))
|
||||
|
||||
(fn find-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)
|
||||
(find-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)
|
||||
(find-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 find-object-by-name ((map (* TmxMap))
|
||||
(layer-name (* const char))
|
||||
(obj-name (* const char)))
|
||||
(TmxObject *)
|
||||
)
|
||||
Reference in New Issue
Block a user