Skip to content

Commit

Permalink
add errors in tilemap parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Maskarinec committed May 29, 2021
1 parent 3ed5c77 commit ca4c244
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 3 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ WIN_FULL=$(SOURCES) lib/rawdraw/chew.c lib/windows/libumka_static.a $(WARNINGS)
# $(CC) $(OBJ) $(LINUX_FULL) -o tophat

build: libs
$(CC) -o tophat $(SRC) $(LINUX_FULL) $(RELEASE_FLAGS) -Os
$(CC) -o tophat $(SRC) $(LINUX_FULL) $(RELEASE_FLAGS) -s -Os

install: build
install: libs
tcc -o tophat $(SRC) $(LINUX_FULL) $(RELEASE_FLAGS) -g && sudo cp tophat /usr/share/tophat/bin/tophat-linux

wbuild: libs
$(MINGW) -o tophat.exe $(SRC) $(WIN_FULL) $(RELEASE_FLAGS)
$(MINGW) -o tophat.exe $(SRC) $(WIN_FULL) $(RELEASE_FLAGS) -s -Os

run:
$(CC) -o tophat $(SRC) $(LINUX_FULL) -g && ./tophat
Expand Down
1 change: 1 addition & 0 deletions umka/misc.um
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ fn ysort*(ents: []entity.ent): []entity.ent {

return ents
}

36 changes: 34 additions & 2 deletions umka/tilemap.um
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import (
"csv.um"
"image.um"
"std/std.um"
"std.um"
"rectangle.um"
"entity.um"
"polygon.um"
Expand Down Expand Up @@ -36,13 +36,32 @@ const (
TOPLEFT* = 1
)

fn parse_num(inp: char): (int, bool) {
if inp >= '0' && inp <= '9' {
return int(inp) - int('0'), true
}

if inp >= 'A' && inp <= 'Z' {
return int(inp) - int('A'), true
}

if inp >= 'a' && inp <= 'z' {
return int(inp) - int('a'), true
}

return 0, false
}

fn mk*(inp: str, images: []image.img): tmap {
res := csv.parse(inp)

if len(res) < 2 { return tmap{} }
if len(res[0]) < 2 { return tmap{} }
if res[0][0] != "tophat tilemap" { return tmap{} }

if len(res[1]) != len(images) {
error("incorrect collision mask in tilemap")
}
collmask := make([]bool, len(res[1]))
for i:=0; i < len(res[1]); i++ {
if res[1][i] == "true" { collmask[i] = true; continue }
Expand All @@ -52,7 +71,20 @@ fn mk*(inp: str, images: []image.img): tmap {
cells := []int32{}
for i:=2; i < len(res); i++ {
for j:=0; j < len(res[i]); j++ {
cells = append(cells, std.atoi(res[i][j]))
if res[i][j] == "" {
continue
}

val, check := parse_num(res[i][j][0])
if !check {
error("number in tilemap isn't valid")
}

if val > len(images) {
error("number on " + repr(i) + ", " + repr(j) + "exceeds number of images")
}

cells = append(cells, val)
}
}

Expand Down

0 comments on commit ca4c244

Please sign in to comment.