implement initial tilemap rendering
This commit is contained in:
10
Makefile
10
Makefile
@@ -25,7 +25,7 @@ else
|
||||
RAYLIB_LIBS := -lGL -lm -lpthread -ldl -lrt -lX11
|
||||
endif
|
||||
|
||||
OBJECTS := main.o menu.o game.o
|
||||
OBJECTS := main.o menu.o game.o tmx.o
|
||||
|
||||
all: catch-phase
|
||||
|
||||
@@ -38,6 +38,7 @@ catch-phase: $(OBJECTS) $(RAYLIB_LIB)
|
||||
# raylib ------------------------------------------------------------
|
||||
$(RAYLIB_LIB):
|
||||
mkdir -p build
|
||||
$(MAKE) -C $(RAYLIB_SRC) clean
|
||||
$(MAKE) -C $(RAYLIB_SRC) \
|
||||
PLATFORM=$(RAYLIB_PLATFORM) \
|
||||
RAYLIB_LIBTYPE=STATIC \
|
||||
@@ -54,9 +55,9 @@ EMFLAGS := -O2 -DPLATFORM_WEB \
|
||||
-sTOTAL_MEMORY=134217728 \
|
||||
-sFORCE_FILESYSTEM=1 \
|
||||
--shell-file shell.html \
|
||||
--preload-file ./res@/res
|
||||
--preload-file ./res@res
|
||||
|
||||
WEB_OBJECTS := web/menu.o web/game.o
|
||||
WEB_OBJECTS := web/menu.o web/game.o web/tmx.o
|
||||
|
||||
web/%.o: %.sex
|
||||
mkdir -p web
|
||||
@@ -65,6 +66,7 @@ web/%.o: %.sex
|
||||
|
||||
web/libraylib.web.a:
|
||||
mkdir -p web
|
||||
$(MAKE) -C $(RAYLIB_SRC) clean
|
||||
$(MAKE) -C $(RAYLIB_SRC) \
|
||||
PLATFORM=PLATFORM_WEB \
|
||||
RAYLIB_LIBTYPE=STATIC \
|
||||
@@ -89,6 +91,6 @@ serve: web
|
||||
clean:
|
||||
$(MAKE) -C $(RAYLIB_SRC) clean
|
||||
rm -rf ./build ./web
|
||||
rm -f catch-phase
|
||||
rm -f $(OBJECTS) catch-phase
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
133
game.sex
133
game.sex
@@ -1,37 +1,130 @@
|
||||
(include raylib.h)
|
||||
(include raygui.h)
|
||||
|
||||
(pub import pages)
|
||||
(import main)
|
||||
(import utils)
|
||||
(pub import tmx)
|
||||
|
||||
(pub record game-state
|
||||
((app-state (* app-state))))
|
||||
|
||||
(pub fn mk-game ((app-state (* app-state))) page
|
||||
(var state _ (mem-alloc game-state))
|
||||
(= (. state app-state) app-state)
|
||||
(return (page/game state)))
|
||||
|
||||
(pub fn rm-game ((state (* game-state))) void
|
||||
(free state)
|
||||
(return))
|
||||
(define MAP-FILE "res/tilemaps/SaveAss.tmx")
|
||||
|
||||
#-emscripten (define WIN-WIDTH 1440)
|
||||
#-emscripten (define WIN-HEIGHT 900)
|
||||
#+emscripten (define WIN-WIDTH 960)
|
||||
#+emscripten (define WIN-HEIGHT 600)
|
||||
|
||||
(pub record game-state
|
||||
((app-state (* app-state))
|
||||
(map (* tmx-map))
|
||||
(cam Camera2D)
|
||||
(fih (* TmxObject))))
|
||||
|
||||
(pub fn mk-game ((app-state (* app-state))) page
|
||||
(var state _ (mem-alloc game-state))
|
||||
(= (. state app-state) app-state)
|
||||
|
||||
(= (. state cam offset x) (/ WIN-WIDTH 2.0))
|
||||
(= (. state cam offset y) (/ WIN-HEIGHT 2.0))
|
||||
(= (. state cam rotation) 0.0f)
|
||||
(= (. state cam zoom) 3.0)
|
||||
|
||||
(var map (* tmx-map) (load-tmx MAP-FILE))
|
||||
(= (. state map) map)
|
||||
|
||||
(if (== map NULL)
|
||||
(do
|
||||
(= (. state cam target x) 0.0)
|
||||
(= (. state cam target y) 0.0)
|
||||
(return (page/game state))))
|
||||
|
||||
(var object-count int (tmx-object-count map "FIH"))
|
||||
(TraceLog LOG-INFO "FIH has %d object(s)" object-count)
|
||||
|
||||
(if (== object-count 0)
|
||||
(do
|
||||
(= (. state cam target x)
|
||||
(/ (cast (tmx-pixel-width map) float) 2.0))
|
||||
(= (. state cam target y)
|
||||
(/ (cast (tmx-pixel-height map) float) 2.0))
|
||||
(return (page/game state))))
|
||||
|
||||
(var player-x float (tmx-object-x map "FIH" 0))
|
||||
(var player-y float (tmx-object-y map "FIH" 0))
|
||||
(var player-width float (tmx-object-width map "FIH" 0))
|
||||
(var player-height float (tmx-object-height map "FIH" 0))
|
||||
|
||||
(= (. state fih) NULL)
|
||||
|
||||
(TraceLog LOG-INFO
|
||||
"FIH[0]: \"%s\" type \"%s\" at %.1f, %.1f (%.1f x %.1f)"
|
||||
(tmx-object-name map "FIH" 0)
|
||||
(tmx-object-type map "FIH" 0)
|
||||
player-x player-y player-width player-height)
|
||||
|
||||
(= (. state cam target x)
|
||||
(+ player-x (/ player-width 2.0)))
|
||||
(= (. state cam target y)
|
||||
(+ player-y (/ player-height 2.0)))
|
||||
(return (page/game state)))
|
||||
|
||||
(pub fn rm-game ((state (* game-state))) void
|
||||
(unload-tmx (. state map))
|
||||
(free state)
|
||||
(return))
|
||||
|
||||
(pub fn render-game ((state (* game-state))) void
|
||||
(BeginDrawing)
|
||||
(ClearBackground BLACK)
|
||||
(DrawTriangleGradient
|
||||
#(Vector2 : (+ (/ WIN-WIDTH 2) 100) (- (/ WIN-HEIGHT 2) 100.0f))
|
||||
#(Vector2 : (- (/ WIN-WIDTH 2) 100) (- (/ WIN-HEIGHT 2) 100.0f))
|
||||
#(Vector2 : (/ WIN-WIDTH 2) (+ (/ WIN-HEIGHT 2) 100))
|
||||
#(Color : 255 255 0 255)
|
||||
#(Color : 255 0 255 255)
|
||||
#(Color : 0 255 255 255))
|
||||
|
||||
(if (== (. state map) NULL)
|
||||
(do
|
||||
(DrawText "Failed to load res/tilemaps/SaveAss.tmx" 20 20 20 RED)
|
||||
(return)))
|
||||
|
||||
(BeginMode2D (. state cam))
|
||||
(animate-tmx (. state map))
|
||||
(draw-tmx-layer (. state map) "Background" (& (. state cam)))
|
||||
(draw-tmx-layer (. state map) "FIH" (& (. state cam)))
|
||||
(EndMode2D)
|
||||
|
||||
(DrawText "WASD/arrows: move wheel: zoom click: back to menu"
|
||||
10 10 20 RAYWHITE)
|
||||
(DrawText (TextFormat "FIH objects: %d"
|
||||
(tmx-object-count (. state map) "FIH"))
|
||||
10 30 20 RAYWHITE)
|
||||
|
||||
(EndDrawing))
|
||||
|
||||
(pub fn process-input-game ((state (* game-state))) void
|
||||
(if (IsMouseButtonReleased MOUSE-LEFT-BUTTON)
|
||||
(.change-page (. state app-state) (sum-type page menu))))
|
||||
(if (!= (. state map) NULL)
|
||||
(do
|
||||
(var speed float (* 300.0 (GetFrameTime)))
|
||||
|
||||
(if (|| (IsKeyDown KEY-LEFT) (IsKeyDown KEY-A))
|
||||
(-= (. state cam target x) speed))
|
||||
(if (|| (IsKeyDown KEY-RIGHT) (IsKeyDown KEY-D))
|
||||
(+= (. state cam target x) speed))
|
||||
(if (|| (IsKeyDown KEY-UP) (IsKeyDown KEY-W))
|
||||
(-= (. state cam target y) speed))
|
||||
(if (|| (IsKeyDown KEY-DOWN) (IsKeyDown KEY-S))
|
||||
(+= (. state cam target y) speed))
|
||||
|
||||
(var map-width float (cast (tmx-pixel-width (. state map)) float))
|
||||
(var map-height float (cast (tmx-pixel-height (. state map)) float))
|
||||
(if (< (. state cam target x) 0.0)
|
||||
(= (. state cam target x) 0.0))
|
||||
(if (> (. state cam target x) map-width)
|
||||
(= (. state cam target x) map-width))
|
||||
(if (< (. state cam target y) 0.0)
|
||||
(= (. state cam target y) 0.0))
|
||||
(if (> (. state cam target y) map-height)
|
||||
(= (. state cam target y) map-height))
|
||||
|
||||
(var wheel float (GetMouseWheelMove))
|
||||
(if (!= wheel 0.0)
|
||||
(do
|
||||
(+= (. state cam zoom) (* wheel 0.5))
|
||||
(if (< (. state cam zoom) 0.5)
|
||||
(= (. state cam zoom) 0.5))
|
||||
(if (> (. state cam zoom) 8.0)
|
||||
(= (. state cam zoom) 8.0)))))))
|
||||
|
||||
3
main.sex
3
main.sex
@@ -1,6 +1,9 @@
|
||||
(pub import pages)
|
||||
(pub import utils)
|
||||
|
||||
(define RAYGUI_IMPLEMENTATION 1)
|
||||
(include raygui.h)
|
||||
|
||||
(include raylib.h)
|
||||
|
||||
#-emscripten (define WIN-WIDTH 1440)
|
||||
|
||||
4
res/tilemaps/FIH 3.tsx
Normal file
4
res/tilemaps/FIH 3.tsx
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<tileset version="1.10" tiledversion="1.12.2" name="FIH 3" tilewidth="16" tileheight="16" tilecount="1" columns="1">
|
||||
<image source="Sprite-0001.png" trans="ff00ff" width="16" height="16"/>
|
||||
</tileset>
|
||||
32
res/tilemaps/SaveAss.tmx
Normal file
32
res/tilemaps/SaveAss.tmx
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<map version="1.10" tiledversion="1.12.2" orientation="orthogonal" renderorder="right-down" width="30" height="20" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="4">
|
||||
<tileset firstgid="1" source="WATAH 2.tsx"/>
|
||||
<tileset firstgid="257" source="FIH 3.tsx"/>
|
||||
<layer id="1" name="Background" width="30" height="20">
|
||||
<data encoding="csv">
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,3,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,1,2,139,18,18,18,18,18,18,18,18,18,18,18,18,140,2,2,2,2,2,2,2,2,3,0,0,
|
||||
0,0,1,139,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,156,34,34,34,34,35,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,0,0,0,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,0,0,0,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,0,0,0,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,140,2,2,2,3,0,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,0,
|
||||
0,0,17,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,0,
|
||||
0,0,33,155,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,19,0,0,0,
|
||||
0,0,0,33,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,35,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
||||
</data>
|
||||
</layer>
|
||||
<objectgroup color="#ffffff" id="3" name="FIH">
|
||||
<object id="2" name="Player" gid="257" x="194.52" y="95.0905" width="16" height="16"/>
|
||||
</objectgroup>
|
||||
</map>
|
||||
BIN
res/tilemaps/Sprite-0001.png
Normal file
BIN
res/tilemaps/Sprite-0001.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 390 B |
4
res/tilemaps/WATAH 2.tsx
Normal file
4
res/tilemaps/WATAH 2.tsx
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<tileset version="1.10" tiledversion="1.12.2" name="path_tiles_rounded" tilewidth="16" tileheight="16" tilecount="256" columns="16">
|
||||
<image source="path_tiles_rounded.png" width="256" height="256"/>
|
||||
</tileset>
|
||||
BIN
res/tilemaps/path_tiles_rounded.png
Normal file
BIN
res/tilemaps/path_tiles_rounded.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
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