Skip to content

Commit

Permalink
atlas.um and tilemap.um updates (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
skejeton authored Jul 16, 2024
1 parent 53ab81b commit 075bc56
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ umth_tilemap_draw(UmkaStackSlot *p, UmkaStackSlot *r)
// ent - entity to collide with, t - tilemap, x and y - pointers to ints used to return, where the
// collision occured

// fn umth_tilemap_getcoll(tc: ^th::Vf2, ic: ^th::Vf2, t: ^Tilemap, ent: ^Entity): int
// fn umth_tilemap_getcoll(tc: ^th::Vf2, ic: ^th::Vf2, t: ^Tilemap, ent: ^Entity): bool
void
umth_tilemap_getcoll(UmkaStackSlot *p, UmkaStackSlot *r)
{
Expand All @@ -199,7 +199,7 @@ umth_tilemap_getcoll(UmkaStackSlot *p, UmkaStackSlot *r)
umkaGetResult(p, r)->intVal = th_coll_on_tilemap(ent, t, ic, tc);
}

// fn umth_tilemap_getcoll_line(b: th::Vf2, e: th::Vf2, t: ^Tilemap, ic: ^th::Vf2): int
// fn umth_tilemap_getcoll_line(b: th::Vf2, e: th::Vf2, t: ^Tilemap, ic: ^th::Vf2): bool
void
umth_tilemap_getcoll_line(UmkaStackSlot *p, UmkaStackSlot *r)
{
Expand Down
179 changes: 138 additions & 41 deletions src/staembed.c
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,26 @@ const char *th_em_modulesrc[] = {
"//~~\n"
"\n"
"//~~fn mk\n"
"fn mk*(cells: []th::uu, w: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {\n"
"// Make a tilemap with all cells set to 0.\n"
"fn mk*(w, h: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {\n"
"//~~\n"
"\tif !at.i.validate() {\n"
"\t\tth::__error(\"invalid atlas\")\n"
"\t}\n"
"\n"
"\tt := Tilemap{}\n"
"\tt.cells = make([]th::uu, w*h)\n"
"\tt.w = w\n"
"\tt.atlas = at\n"
"\tt.collMask = make([]bool, 10) // TODO\n"
"\tt.scale = scale\n"
"\n"
"\treturn t\n"
"}\n"
"\n"
"//~~fn mk2\n"
"// Make a tilemap from a list of cells.\n"
"fn mk2*(cells: []th::uu, w: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {\n"
"//~~\n"
"\tif !at.i.validate() {\n"
"\t\tth::__error(\"invalid atlas\")\n"
Expand All @@ -1512,12 +1531,20 @@ const char *th_em_modulesrc[] = {
"}\n"
"\n"
"//~~fn Tilemap.edit\n"
"// Sets tile at [x, y] to tile.\n"
"fn (t: ^Tilemap) edit*(x, y, tile: int) {\n"
"// Edit a tile in the tilemap.\n"
"fn (t: ^Tilemap) edit*(x, y, tile: int){\n"
"//~~\n"
"\tt.cells[y*t.w + x] = tile\n"
"}\n"
"\n"
"//~~fn Tilemap.get\n"
"// Get a tile from the tilemap.\n"
"fn (t: ^Tilemap) get*(x, y: int): int {\n"
"//~~\n"
"\treturn t.cells[y*t.w + x]\n"
"}\n"
"\n"
"\n"
"fn umth_tilemap_draw(ct: ^Tilemap, tr: th::Transform)\n"
"//~~fn Tilemap.draw\n"
"// Draws the tilemap.\n"
Expand All @@ -1528,7 +1555,7 @@ const char *th_em_modulesrc[] = {
"\tumth_tilemap_draw(t, tr)\n"
"}\n"
"\n"
"fn umth_tilemap_getcoll(pos: ^th::Vf2, vert: ^th::Vf2, t: ^Tilemap, e: ^ent::Ent): int32\n"
"fn umth_tilemap_getcoll(pos: ^th::Vf2, vert: ^th::Vf2, t: ^Tilemap, e: ^ent::Ent): bool\n"
"//~~fn Tilemap.getColl\n"
"// Checks whether `e` collides with any of the tiles in `t`, which are in the\n"
"// collmask.\n"
Expand All @@ -1546,9 +1573,7 @@ const char *th_em_modulesrc[] = {
"\tif ic == null { ic = &_ }\n"
"\tif pos == null { pos = &_ }\n"
"\n"
"\tc := umth_tilemap_getcoll(pos, ic, t, &e)\n"
"\n"
"\treturn bool(c)\n"
"\treturn umth_tilemap_getcoll(pos, ic, t, &e)\n"
"}\n"
"\n"
"fn umth_tilemap_getcoll_line(b, e: th::Vf2, t: ^Tilemap, ic: ^th::Vf2): bool\n"
Expand Down Expand Up @@ -2640,41 +2665,69 @@ const char *th_em_modulesrc[] = {
"\treturn a\n"
"}\n"
"\n"
"//~~fn mk2\n"
"// i: source image\n"
"// cs: size of a cell in pixels\n"
"fn mk2*(i: image::Image, cs: th::Vf2): Atlas {\n"
"//~~\n"
"\tif !i.validate() {\n"
"\t\tth::__error(\"Image is not valid\")\n"
"\t}\n"
"\n"
"\ta := Atlas{}\n"
"\n"
"\ta.i = i\n"
"\ta.cs = cs\n"
"\ta.dm = i.getDims().div(cs)\n"
"\n"
"\treturn a\n"
"}\n"
"\n"
"//~~fn Atlas.coords\n"
"// returns the coordinates of the nth tile\n"
"fn (a: ^Atlas) coords*(n: int): (th::Vf2, std::Err) {\n"
"fn (a: ^Atlas) coords*(n: int): th::Vf2 {\n"
"//~~\n"
"\to := th::Vf2{ n % trunc(a.dm.x), (n - n % trunc(a.dm.x)) / a.dm.x }\n"
"\tif o.x > a.dm.x || o.y > a.dm.y {\n"
"\t\treturn {}, th::__errFromCode(.out_of_bounds)\n"
"\t}\n"
"\treturn o, {}\n"
"\tn--\n"
"\treturn th::Vf2{ n % trunc(a.dm.x), (n - n % trunc(a.dm.x)) / a.dm.x }\n"
"}\n"
"\n"
"//~~fn Atlas.rect\n"
"// returns the rectangle of the nth tile\n"
"fn (a: ^Atlas) rect*(n: int): (rect::Rect, std::Err) {\n"
"fn (a: ^Atlas) rect*(n: int): rect::Rect {\n"
"//~~\n"
"\to := th::Vf2{ n % trunc(a.dm.x), (n - n % trunc(a.dm.x)) / a.dm.x }\n"
"\tif o.x > a.dm.x || o.y > a.dm.y {\n"
"\t\treturn {}, th::__errFromCode(.out_of_bounds)\n"
"\t}\n"
"\treturn {o.x*a.cs.x, o.y*a.cs.y, a.cs.x, a.cs.y}, {}\n"
"\tn--\n"
"\treturn {(n % trunc(a.dm.x))*a.cs.x, ((n - n % trunc(a.dm.x)) / a.dm.x)*a.cs.y, a.cs.x, a.cs.y}\n"
"}\n"
"\n"
"//~~fn Atlas.index\n"
"// returns the index of the tile at the given coordinates\n"
"fn (a: ^Atlas) index*(at: th::Vf2): int {\n"
"//~~\n"
"\treturn int(trunc(at.x) + trunc(at.y) * trunc(a.dm.x)) - 1\n"
"}\n"
"\n"
"//~~fn Atlas.has\n"
"// returns true if the tile at the given coordinates exists\n"
"fn (a: ^Atlas) has*(at: th::Vf2): bool {\n"
"//~~\n"
"\treturn at.x >= 0 && at.y >= 0 && at.x < a.dm.x && at.y < a.dm.y\n"
"}\n"
"\n"
"//~~fn Atlas.hasIndex\n"
"// returns true if the tile at the given index exists\n"
"fn (a: ^Atlas) hasIndex*(n: int): bool {\n"
"//~~\n"
"\treturn n > 0 && n <= trunc(a.dm.x * a.dm.y)\n"
"}\n"
"\n"
"//~~fn Atlas.cropSource\n"
"// Crops the sourse image to only show a wanted tile\n"
"fn (a: ^Atlas) cropSource*(at: th::Vf2): std::Err {\n"
"fn (a: ^Atlas) cropSource*(at: th::Vf2) {\n"
"//~~\n"
"\tif (at.x > a.dm.x || at.y > a.dm.y || at.x * at.y < 0) {\n"
"\t\treturn th::__errFromCode(.out_of_bounds)\n"
"\t}\n"
"\n"
"\ta.i.crop(\n"
"\t\t{at.x / a.dm.x, at.y / a.dm.y},\n"
"\t\t{(at.x+1) / a.dm.x, (at.y+1) / a.dm.y})\n"
"\n"
"\treturn {}\n"
"\t\t{(at.x+1) / a.dm.x, (at.y+1) / a.dm.y}\n"
"\t)\n"
"}\n"
"\n"
"//~~enum PackStrategy\n"
Expand All @@ -2698,17 +2751,11 @@ const char *th_em_modulesrc[] = {
"\n"
"//~~fn Atlas.draw\n"
"// Draws the tile at `at`\n"
"fn (a: ^Atlas) draw*(at: th::Vf2, t: th::Transform): std::Err {\n"
"fn (a: ^Atlas) draw*(at: th::Vf2, t: th::Transform) {\n"
"//~~\n"
"\tif (at.x > a.dm.x || at.y > a.dm.y || at.x * at.y < 0) {\n"
"\t\treturn th::__errFromCode(.out_of_bounds)\n"
"\t}\n"
"\n"
"\ta.cropSource(at)\n"
"\ta.i.draw(t, th::white)\n"
"\ta.i.crop({}, {1, 1})\n"
"\n"
"\treturn {}\n"
"}\n"
"",
"\n"
Expand Down Expand Up @@ -5166,16 +5213,33 @@ const char *th_em_moduledocs[] = {
"\n"
"fn mk\n"
"\n"
"[3mfn mk*(cells: []th::uu, w: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {\n"
"[3mfn mk*(w, h: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {\n"
"\n"
"Make a tilemap with all cells set to 0.\n"
"\n"
"---------\n"
"\n"
"fn mk2\n"
"\n"
"fn mk2*(cells: []th::uu, w: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {\n"
"\n"
"Make a tilemap from a list of cells.\n"
"\n"
"---------\n"
"\n"
"fn Tilemap.edit\n"
"\n"
"fn (t: ^Tilemap) edit*(x, y, tile: int) {\n"
"fn (t: ^Tilemap) edit*(x, y, tile: int){\n"
"\n"
"Edit a tile in the tilemap.\n"
"\n"
"---------\n"
"\n"
"fn Tilemap.get\n"
"\n"
"fn (t: ^Tilemap) get*(x, y: int): int {\n"
"\n"
"Sets tile at [x, y] to tile.\n"
"Get a tile from the tilemap.\n"
"\n"
"---------\n"
"\n"
Expand Down Expand Up @@ -6057,25 +6121,58 @@ const char *th_em_moduledocs[] = {
"\n"
"---------\n"
"\n"
"fn mk2\n"
"\n"
"fn mk2*(i: image::Image, cs: th::Vf2): Atlas {\n"
"\n"
"i: source image\n"
"cs: size of a cell in pixels\n"
"\n"
"---------\n"
"\n"
"fn Atlas.coords\n"
"\n"
"[3mfn (a: ^Atlas) coords*(n: int): (th::Vf2, std::Err) {\n"
"[3mfn (a: ^Atlas) coords*(n: int): th::Vf2 {\n"
"\n"
"returns the coordinates of the nth tile\n"
"\n"
"---------\n"
"\n"
"fn Atlas.rect\n"
"\n"
"[3mfn (a: ^Atlas) rect*(n: int): (rect::Rect, std::Err) {\n"
"[3mfn (a: ^Atlas) rect*(n: int): rect::Rect {\n"
"\n"
"returns the rectangle of the nth tile\n"
"\n"
"---------\n"
"\n"
"fn Atlas.index\n"
"\n"
"fn (a: ^Atlas) index*(at: th::Vf2): int {\n"
"\n"
"returns the index of the tile at the given coordinates\n"
"\n"
"---------\n"
"\n"
"fn Atlas.has\n"
"\n"
"fn (a: ^Atlas) has*(at: th::Vf2): bool {\n"
"\n"
"returns true if the tile at the given coordinates exists\n"
"\n"
"---------\n"
"\n"
"fn Atlas.hasIndex\n"
"\n"
"fn (a: ^Atlas) hasIndex*(n: int): bool {\n"
"\n"
"returns true if the tile at the given index exists\n"
"\n"
"---------\n"
"\n"
"fn Atlas.cropSource\n"
"\n"
"[3mfn (a: ^Atlas) cropSource*(at: th::Vf2): std::Err {\n"
"[3mfn (a: ^Atlas) cropSource*(at: th::Vf2) {\n"
"\n"
"Crops the sourse image to only show a wanted tile\n"
"\n"
Expand All @@ -6102,7 +6199,7 @@ const char *th_em_moduledocs[] = {
"\n"
"fn Atlas.draw\n"
"\n"
"[3mfn (a: ^Atlas) draw*(at: th::Vf2, t: th::Transform): std::Err {\n"
"[3mfn (a: ^Atlas) draw*(at: th::Vf2, t: th::Transform) {\n"
"\n"
"Draws the tile at `at`\n"
"\n"
Expand Down
Loading

0 comments on commit 075bc56

Please sign in to comment.