diff --git a/src/bindings.c b/src/bindings.c index dcfd254..4cd0965 100644 --- a/src/bindings.c +++ b/src/bindings.c @@ -179,7 +179,8 @@ void umth_tilemap_draw(UmkaStackSlot *p, UmkaStackSlot *r) { th_tmap *t = umkaGetParam(p, 0)->ptrVal; - th_tmap_draw(t); + th_transform tr = *(th_transform *)umkaGetParam(p, 1); + th_tmap_draw(t, tr); } // checks, if tilemap collides with entity. diff --git a/src/staembed.c b/src/staembed.c index 74692c1..129fd7c 100644 --- a/src/staembed.c +++ b/src/staembed.c @@ -1498,14 +1498,14 @@ const char *th_em_modulesrc[] = { "\tt.cells[y*t.w + x] = tile\n" "}\n" "\n" -"fn umth_tilemap_draw(ct: ^Tilemap)\n" +"fn umth_tilemap_draw(ct: ^Tilemap, tr: th::Transform)\n" "//~~fn Tilemap.draw\n" "// Draws the tilemap.\n" -"fn (t: ^Tilemap) draw*() {\n" +"fn (t: ^Tilemap) draw*(tr: th::Transform) {\n" "//~~\n" "\tif len(t.cells) == 0 { return }\n" "\n" -"\tumth_tilemap_draw(t)\n" +"\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" @@ -1551,7 +1551,7 @@ const char *th_em_modulesrc[] = { "fn (t: ^Tilemap) autotile*(src, tileCfg: []th::uu, tile: th::uu) {\n" "//~~\n" "\tif len(tileCfg) < 16 {\n" -"\t\tth::__error(\"autotile: tileCfg len isn\'t 16\'\")\n" +"\t\tth::__error(\"autotile: tileCfg len isn\'t 16\")\n" "\t}\n" "\n" "\tumth_tilemap_autotile(&t.cells[0], t.w, len(t.cells)/t.w, &src[0], &tileCfg[0], tile)\n" @@ -2582,6 +2582,7 @@ const char *th_em_modulesrc[] = { "\t\"th.um\"\n" "\t\"std.um\"\n" "\t\"image.um\"\n" +"\t\"rect.um\"\n" ")\n" "\n" "//~~struct Atlas\n" @@ -2622,6 +2623,17 @@ const char *th_em_modulesrc[] = { "\treturn o, {}\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" +"//~~\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" +"}\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" @@ -2638,18 +2650,18 @@ const char *th_em_modulesrc[] = { "}\n" "\n" "//~~enum PackStrategy\n" -"const (\n" -"\tPackSquare* = 0\n" -"\tPackRow*\n" -"\tPackColumn*\n" -")\n" +"type PackStrategy = enum {\n" +"\tsquare\n" +"\trow\n" +"\tcolumn\n" +"}\n" "//~~\n" "\n" -"fn umth_atlas_pack(a: ^Atlas, images: ^[]image::Image, strategy: int): th::ErrCode\n" +"fn umth_atlas_pack(a: ^Atlas, images: ^[]image::Image, strategy: PackStrategy): th::ErrCode\n" "\n" "//~~fn pack\n" "// Packs an array of images into an atlas\n" -"fn pack*(images: []image::Image, strategy: int): (Atlas, std::Err) {\n" +"fn pack*(images: []image::Image, strategy: PackStrategy): (Atlas, std::Err) {\n" "//~~\n" "\tvar a: Atlas\n" "\tec := umth_atlas_pack(&a, &images, strategy)\n" @@ -5133,7 +5145,7 @@ const char *th_em_moduledocs[] = { "\n" "fn Tilemap.draw\n" "\n" -"fn (t: ^Tilemap) draw*() {\n" +"fn (t: ^Tilemap) draw*(tr: th::Transform) {\n" "\n" "Draws the tilemap.\n" "\n" @@ -6028,6 +6040,14 @@ const char *th_em_moduledocs[] = { "\n" "---------\n" "\n" +"fn Atlas.rect\n" +"\n" +"fn (a: ^Atlas) rect*(n: int): (rect::Rect, std::Err) {\n" +"\n" +"returns the rectangle of the nth tile\n" +"\n" +"---------\n" +"\n" "fn Atlas.cropSource\n" "\n" "fn (a: ^Atlas) cropSource*(at: th::Vf2): std::Err {\n" @@ -6038,18 +6058,18 @@ const char *th_em_moduledocs[] = { "\n" "enum PackStrategy\n" "\n" -"const (\n" -"\tPackSquare* = 0\n" -"\tPackRow*\n" -"\tPackColumn*\n" -")\n" +"type PackStrategy = enum {\n" +"\tsquare\n" +"\trow\n" +"\tcolumn\n" +"}\n" "\n" "\n" "---------\n" "\n" "fn pack\n" "\n" -"fn pack*(images: []image::Image, strategy: int): (Atlas, std::Err) {\n" +"fn pack*(images: []image::Image, strategy: PackStrategy): (Atlas, std::Err) {\n" "\n" "Packs an array of images into an atlas\n" "\n" diff --git a/src/thextdata.h b/src/thextdata.h index db95199..75e8eb9 100644 --- a/src/thextdata.h +++ b/src/thextdata.h @@ -32,27 +32,31 @@ THEXT(uu, th_line_to_tilemap, th_vf2 b, th_vf2 e, th_tmap *t, th_vf2 *ic); THEXT(th_rect, th_rect_intersect, th_rect a, th_rect b); THEXT(th_quad, th_ent_transform, th_ent *e); THEXT(void, th_ent_draw, th_ent *o); -THEXT(void, th_ent_getcoll, th_ent *e, th_ent **scene, uu count, uu *collC, uu maxColls, th_coll *colls); +THEXT(void, th_ent_getcoll, th_ent *e, th_ent **scene, uu count, uu *collC, uu maxColls, + th_coll *colls); THEXT(uint32_t, th_color_rgb, float r, float g, float b, float a); THEXT(uint32_t, th_color_hsv2rgb, float h, float s, float v, float a); THEXT(th_err, th_font_load, th_font **out, char *path, double size, uint32_t filter); -THEXT(void, th_font_draw, th_font *font, const char *s, double x, double y, uint32_t color, double scale); +THEXT(void, th_font_draw, th_font *font, const char *s, double x, double y, uint32_t color, + double scale); THEXT(th_vf2, th_font_measure, th_font *font, const char *s); THEXT(void, th_font_deinit); THEXT(th_err, th_load_image, th_image **out, char *path); THEXT(void, th_image_free, th_image *img); THEXT(th_err, th_image_from_data, th_image *img, uint32_t *data, th_vf2 dm); -THEXT(uint32_t*, th_image_get_data, th_image *img); +THEXT(uint32_t *, th_image_get_data, th_image *img); THEXT(void, th_image_draw_quad, th_image *img, th_quad q, uint32_t color); -THEXT(void, th_image_blit, th_image *img, th_rect src, th_rect dest, uint32_t color, float rot, th_vf2 origin); +THEXT(void, th_image_blit, th_image *img, th_rect src, th_rect dest, uint32_t color, float rot, + th_vf2 origin); THEXT(void, th_image_render_transformed, th_image *img, th_transform trans, uint32_t color); THEXT(void, th_image_crop, th_image *img, th_vf2 tl, th_vf2 br); THEXT(th_err, th_image_set_filter, th_image *img, sg_filter filter); THEXT(th_err, th_image_update_data, th_image *img, uint32_t *data, th_vf2 dm); -THEXT(th_image*, th_image_alloc); +THEXT(th_image *, th_image_alloc); THEXT(void, th_image_init); THEXT(void, th_image_deinit); -THEXT(th_err, th_image_create_render_target, th_render_target **out, int width, int height, int filter); +THEXT(th_err, th_image_create_render_target, th_render_target **out, int width, int height, + int filter); THEXT(th_err, th_image_set_as_render_target, th_render_target *t); THEXT(th_err, th_image_remove_render_target, th_render_target *t, th_vf2 wp); THEXT(void, th_input_key, int keycode, int bDown); @@ -67,9 +71,11 @@ THEXT(void, th_calculate_scaling, float camw, float camh); THEXT(int, th_init, const char *scriptpath, const char *script_path); THEXT(void, th_deinit); THEXT(void, th_print_umka_error_and_quit, int code); -THEXT(void, th_regularize_path, const char *path, const char *cur_folder, char *regularized_path, int size); +THEXT(void, th_regularize_path, const char *path, const char *cur_folder, char *regularized_path, + int size); THEXT(void, th_navmesh_add_quad, th_navmesh *m, th_quad *q); -THEXT(th_err, th_navmesh_nav, th_vf2s *cameFrom, void *cameFromType, th_navmesh *m, th_vf2 p1, th_vf2 p2); +THEXT(th_err, th_navmesh_nav, th_vf2s *cameFrom, void *cameFromType, th_navmesh *m, th_vf2 p1, + th_vf2 p2); THEXT(void, th_nav_init, void); THEXT(void, th_particles_draw, th_particles *p, int t); THEXT(th_vf2, th_quad_min, th_quad q); @@ -82,11 +88,11 @@ THEXT(void, th_transform_transform, th_transform *o, th_transform t); THEXT(void, th_rotate_point, th_vf2 *p, th_vf2 o, fu rot); THEXT(void, th_vector_normalize, float *x, float *y); THEXT(uint32_t, th_sg_get_gl_image, sg_image img); -THEXT(void, th_tmap_draw, th_tmap *t); +THEXT(void, th_tmap_draw, th_tmap *t, th_transform tr); THEXT(void, th_tmap_autotile, uu *tgt, uu *src, uu w, uu h, uu *tiles, uu limiter); -THEXT(th_shader*, th_get_shader, uu index); -THEXT(th_shader*, th_get_shader_err, uu index); -THEXT(th_shader*, th_alloc_shader); +THEXT(th_shader *, th_get_shader, uu index); +THEXT(th_shader *, th_get_shader_err, uu index); +THEXT(th_shader *, th_alloc_shader); THEXT(size_t, th_utf8_decode, uint32_t *out, const char *s); THEXT(size_t, th_utf8_encode, char *out, uint32_t r); THEXT(fu, th_window_dpi_scale); diff --git a/src/tilemap.c b/src/tilemap.c index 450de63..e2481f5 100644 --- a/src/tilemap.c +++ b/src/tilemap.c @@ -8,7 +8,7 @@ extern th_global *thg; void -th_tmap_draw(th_tmap *t) +th_tmap_draw(th_tmap *t, th_transform tr) { th_image *a = t->a.i; if (!a) @@ -16,30 +16,6 @@ th_tmap_draw(th_tmap *t) int tw = t->w, th = umkaGetDynArrayLen(&t->cells) / t->w; - /*fu - camx = thg->wp_offset.x, - camy = thg->wp_offset.y; - - if (camx > t->pos.x + tw*t->scale * t->a.cs.x) - return; - if (camy > t->pos.y + th*t->scale * t->a.cs.y) - return;*/ - - /*int sx = fabs((fabs(t->pos.x)-abs(camx))) / (t->scale * t->a.cs.x); - int sy = fabs((fabs(t->pos.y)-abs(camy))) / (t->scale * t->a.cs.y); - int sw = thg->viewport.x/(t->scale * t->a.cs.x) * 2; - int sh = thg->viewport.y/(t->scale * t->a.cs.y) * 2; - - if (t->pos.x>=camx) - sx = 0; - if (t->pos.y>=camy) - sy = 0; - - if (sw > tw) - sw = tw - sx; - if (sh > th) - sh = th - sy;*/ - for (int i = 0; i < tw; i++) for (int j = 0; j < th; j++) { if (t->cells.data[j * t->w + i] == 0) @@ -59,6 +35,7 @@ th_tmap_draw(th_tmap *t) th_quad q = {0}; th_transform_rect(&q, tt, (th_rect){.w = t->a.cs.x, .h = t->a.cs.y}); + th_transform_quad(&q, tr); th_image_draw_quad(a, q, 0xffffffff); } } diff --git a/src/tophat.h b/src/tophat.h index 3a917f7..e4a1466 100644 --- a/src/tophat.h +++ b/src/tophat.h @@ -591,7 +591,7 @@ th_sg_get_gl_image(sg_image img); // tilemap void -th_tmap_draw(th_tmap *t); +th_tmap_draw(th_tmap *t, th_transform tr); void th_tmap_autotile(uu *tgt, uu *src, uu w, uu h, uu *tiles, uu limiter); diff --git a/umka/atlas.um b/umka/atlas.um index ba376cc..2126c78 100644 --- a/umka/atlas.um +++ b/umka/atlas.um @@ -3,6 +3,7 @@ import ( "th.um" "std.um" "image.um" + "rect.um" ) //~~struct Atlas @@ -43,6 +44,17 @@ fn (a: ^Atlas) coords*(n: int): (th::Vf2, std::Err) { return o, {} } +//~~fn Atlas.rect +// returns the rectangle of the nth tile +fn (a: ^Atlas) rect*(n: int): (rect::Rect, std::Err) { +//~~ + o := th::Vf2{ n % trunc(a.dm.x), (n - n % trunc(a.dm.x)) / a.dm.x } + if o.x > a.dm.x || o.y > a.dm.y { + return {}, th::__errFromCode(.out_of_bounds) + } + return {o.x*a.cs.x, o.y*a.cs.y, a.cs.x, a.cs.y}, {} +} + //~~fn Atlas.cropSource // Crops the sourse image to only show a wanted tile fn (a: ^Atlas) cropSource*(at: th::Vf2): std::Err { @@ -59,18 +71,18 @@ fn (a: ^Atlas) cropSource*(at: th::Vf2): std::Err { } //~~enum PackStrategy -const ( - PackSquare* = 0 - PackRow* - PackColumn* -) +type PackStrategy = enum { + square + row + column +} //~~ -fn umth_atlas_pack(a: ^Atlas, images: ^[]image::Image, strategy: int): th::ErrCode +fn umth_atlas_pack(a: ^Atlas, images: ^[]image::Image, strategy: PackStrategy): th::ErrCode //~~fn pack // Packs an array of images into an atlas -fn pack*(images: []image::Image, strategy: int): (Atlas, std::Err) { +fn pack*(images: []image::Image, strategy: PackStrategy): (Atlas, std::Err) { //~~ var a: Atlas ec := umth_atlas_pack(&a, &images, strategy) diff --git a/umka/tilemap.um b/umka/tilemap.um index 028b13a..fafdb95 100644 --- a/umka/tilemap.um +++ b/umka/tilemap.um @@ -54,14 +54,14 @@ fn (t: ^Tilemap) edit*(x, y, tile: int) { t.cells[y*t.w + x] = tile } -fn umth_tilemap_draw(ct: ^Tilemap) +fn umth_tilemap_draw(ct: ^Tilemap, tr: th::Transform) //~~fn Tilemap.draw // Draws the tilemap. -fn (t: ^Tilemap) draw*() { +fn (t: ^Tilemap) draw*(tr: th::Transform) { //~~ if len(t.cells) == 0 { return } - umth_tilemap_draw(t) + umth_tilemap_draw(t, tr) } fn umth_tilemap_getcoll(pos: ^th::Vf2, vert: ^th::Vf2, t: ^Tilemap, e: ^ent::Ent): int32 @@ -107,7 +107,7 @@ fn umth_tilemap_autotile(tgt: ^th::uu, w, h: th::uu, src, cfg: ^th::uu, tile: th fn (t: ^Tilemap) autotile*(src, tileCfg: []th::uu, tile: th::uu) { //~~ if len(tileCfg) < 16 { - th::__error("autotile: tileCfg len isn't 16'") + th::__error("autotile: tileCfg len isn't 16") } umth_tilemap_autotile(&t.cells[0], t.w, len(t.cells)/t.w, &src[0], &tileCfg[0], tile)