diff --git a/src/staembed.c b/src/staembed.c index c45bf74..7376e5a 100644 --- a/src/staembed.c +++ b/src/staembed.c @@ -2133,6 +2133,7 @@ const char *th_em_modulesrc[] = { "\talready\n" "\tout_of_bounds\n" "}\n" +"//~~\n" "\n" "fn (e: ^ErrCode) toStr*(): str {\n" "\tswitch e^ {\n" @@ -5730,410 +5731,363 @@ const char *th_em_moduledocs[] = { "\talready\n" "\tout_of_bounds\n" "}\n" +"```\n" "\n" -"fn (e: ^ErrCode) toStr*(): str {\n" -"\tswitch e^ {\n" -"\tcase .ok:\n" -"\t\treturn \"ok\"\n" -"\tcase .io:\n" -"\t\treturn \"input/output error\"\n" -"\tcase .bad_enum:\n" -"\t\treturn \"invalid enum value\"\n" -"\tcase .bad_action:\n" -"\t\treturn \"invalid action\"\n" -"\tcase .bad_input:\n" -"\t\treturn \"invalid input value\"\n" -"\tcase .alloc:\n" -"\t\treturn \"failed allocation\"\n" -"\tcase .already:\n" -"\t\treturn \"action already performet\"\n" -"\tcase .out_of_bounds:\n" -"\t\treturn \"index out of bounds\"\n" -"\t}\n" "\n" -"\treturn \"unknown error\"\n" -"}\n" "\n" -"fn __errFromCode*(ec: ErrCode, msg: str = \"\"): std::Err {\n" -"\tif msg != \"\" {\n" -"\t\treturn std::error(int(ec), ec.toStr() + \": \" + msg, \"tophat\")\n" -"\t}\n" +"## struct Vf2\n" "\n" -"\treturn std::error(int(ec), ec.toStr(), \"tophat\")\n" +"```\n" +"type Vf2* = struct {\n" +"\tx, y: fu\n" "}\n" "```\n" "\n" +"vector 2\n" "\n" "\n" -"## \n" +"## fn mkVf2\n" "\n" "```\n" +"fn mkVf2*(x: fu = 0, y: fu = 0): Vf2 {\n" +"\treturn Vf2{x, y}\n" +"}\n" "```\n" "\n" +"Vf2 constructor\n" "\n" "\n" -"## \n" +"## fn Vf2.rotated\n" "\n" "```\n" +"fn (p: ^Vf2) rotated*(origin: Vf2, rot: fu): Vf2 {\n" "```\n" "\n" +"rotates `p` around `origin` with `rot` in degrees\n" "\n" "\n" -"## \n" +"## fn Vf2.distanceTo\n" "\n" "```\n" -"\tangle := (rot * std::pi) / 180\n" -"\n" -"\tpos := p^\n" -"\tpos.x -= origin.x\n" -"\tpos.y -= origin.y\n" -"\n" -"\tx := pos.x * cos(angle) - pos.y * sin(angle)\n" -"\ty := pos.x * sin(angle) + pos.y * cos(angle)\n" -"\n" -"\tpos.x = x + origin.x\n" -"\tpos.y = y + origin.y\n" -"\n" -"\treturn pos\n" -"}\n" +"fn (p1: ^Vf2) distanceTo*(p2: Vf2): fu {\n" "```\n" "\n" +"distance between p1 and p2\n" "\n" "\n" -"## \n" +"## fn Vf2.angleTo\n" "\n" "```\n" -"\treturn sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y))\n" -"}\n" +"fn (p1: ^Vf2) angleTo*(p2: Vf2): real {\n" "```\n" "\n" +"Angle between p1 and p2\n" "\n" "\n" -"## \n" +"## fn Vf2.abs\n" "\n" "```\n" -"\tif p1.x == p2.x && p1.y == p2.y { return 0 }\n" -"\treturn atan2(p2.y - p1.y, p2.x - p1.x) * 180 / std::pi + 180\n" -"}\n" +"fn (p: ^Vf2) abs*(): Vf2 {\n" "```\n" "\n" +"Absolute value of a vector.\n" "\n" "\n" -"## \n" +"## fn Vf2.round\n" "\n" "```\n" -"\treturn Vf2{fabs(p.x), fabs(p.y)}\n" -"}\n" +"fn (p: ^Vf2) round*(): Vf2 {\n" "```\n" "\n" +"Rounds a vector.\n" "\n" "\n" -"## \n" +"## fn Vf2.trunc\n" "\n" "```\n" -"\treturn Vf2{round(p.x), round(p.y)}\n" -"}\n" +"fn (p: ^Vf2) trunc*(): Vf2 {\n" "```\n" "\n" +"Truncates a vector.\n" "\n" "\n" -"## \n" +"## fn Vf2.floor\n" "\n" "```\n" -"\treturn Vf2{trunc(p.x), trunc(p.y)}\n" -"}\n" +"fn (p: ^Vf2) floor*(): Vf2 {\n" "```\n" "\n" +"Floors a vector.\n" "\n" "\n" -"## \n" +"## fn Vf2.ceil\n" "\n" "```\n" -"\treturn Vf2{floor(p.x), floor(p.y)}\n" -"}\n" +"fn (p: ^Vf2) ceil*(): Vf2 {\n" "```\n" "\n" +"Ceils a vector.\n" "\n" "\n" -"## \n" +"## fn vf2f\n" "\n" "```\n" -"\treturn Vf2{ceil(p.x), ceil(p.y)}\n" -"}\n" +"fn vf2f*(f: fu): Vf2 {\n" "```\n" "\n" +"Creates a vector with both x and y set to f\n" "\n" "\n" -"## \n" +"## fn Vf2.sub\n" "\n" "```\n" -"\treturn Vf2{f, f}\n" -"}\n" +"fn (p: ^Vf2) sub*(p2: Vf2): Vf2 {\n" "```\n" "\n" +"Subtracts a vector from another one.\n" "\n" "\n" -"## \n" +"## fn Vf2.subf\n" "\n" "```\n" -"\treturn Vf2{p.x - p2.x, p.y - p2.y}\n" -"}\n" +"fn (p: ^Vf2) subf*(f: fu): Vf2 {\n" "```\n" "\n" +"Subtracts a fu from a vector.\n" "\n" "\n" -"## \n" +"## fn Vf2.add\n" "\n" "```\n" -"\treturn Vf2{p.x - f, p.y - f}\n" -"}\n" +"fn (p: ^Vf2) add*(p2: Vf2): Vf2 {\n" "```\n" "\n" +"Adds a vector to another one.\n" "\n" "\n" -"## \n" +"## fn Vf2.addf\n" "\n" "```\n" -"\treturn Vf2{p.x + p2.x, p.y + p2.y}\n" -"}\n" +"fn (p: ^Vf2) addf*(f: fu): Vf2 {\n" "```\n" "\n" +"Adds a fu to a vector.\n" "\n" "\n" -"## \n" +"## fn Vf2.div\n" "\n" "```\n" -"\treturn Vf2{p.x + f, p.y + f}\n" -"}\n" +"fn (p: ^Vf2) div*(p2: Vf2): Vf2 {\n" "```\n" "\n" +"Divides a vector by another one.\n" "\n" "\n" -"## \n" +"## fn Vf2.divf\n" "\n" "```\n" -"\treturn Vf2{p.x / p2.x, p.y / p2.y}\n" -"}\n" +"fn (p: ^Vf2) divf*(f: fu): Vf2 {\n" "```\n" "\n" +"Divides a vector by a fu.\n" "\n" "\n" -"## \n" +"## fn Vf2.mul\n" "\n" "```\n" -"\treturn Vf2{p.x / f, p.y / f}\n" -"}\n" +"fn (p: ^Vf2) mul*(p2: Vf2): Vf2 {\n" "```\n" "\n" +"Multiplies a vector by another one.\n" "\n" "\n" -"## \n" +"## fn Vf2.mulf\n" "\n" "```\n" -"\treturn Vf2{p.x * p2.x, p.y * p2.y}\n" -"}\n" +"fn (p: ^Vf2) mulf*(f: fu): Vf2 {\n" "```\n" "\n" +"Multiplies a vector by a fu.\n" "\n" "\n" -"## \n" +"## fn Vf2.mag\n" "\n" "```\n" -"\treturn Vf2{p.x * f, p.y * f}\n" -"}\n" +"fn (p: ^Vf2) mag*(): fu {\n" "```\n" "\n" +"Returns the magnitude of a vector p.\n" "\n" "\n" -"## \n" +"## fn Vf2.norm\n" "\n" "```\n" -"\treturn sqrt(p.x * p.x + p.y * p.y)\n" -"}\n" +"fn (p: ^Vf2) norm*(): Vf2 {\n" "```\n" "\n" +"Normalizes a vector.\n" "\n" "\n" -"## \n" +"## fn Vf2.dot\n" "\n" "```\n" -"\tif p.x == 0 && p.y == 0 {\n" -"\t\treturn p^\n" -"\t}\n" -"\n" -"\treturn p.divf(p.mag())\n" -"}\n" +"fn (p: ^Vf2) dot*(q: Vf2): fu {\n" "```\n" "\n" +"Calculates dot product between 2 vectors.\n" "\n" "\n" -"## \n" +"## struct Transform\n" "\n" "```\n" -"\treturn p.x*q.x + p.y*q.y\n" +"type Transform* = struct {\n" +"\tp: Vf2 // position\n" +"\ts: Vf2 // scale\n" +"\to: Vf2 // origin\n" +"\tr: fu // rotation\n" "}\n" "```\n" "\n" +"Struct defining transformation. Used for example by entities.\n" "\n" "\n" -"## \n" +"## fn mkTransform\n" "\n" "```\n" +"fn mkTransform*(p: Vf2, s: Vf2 = Vf2{1, 1}, o: Vf2 = Vf2{0, 0}, r: fu = 0.0): Transform {\n" "```\n" "\n" +"Transform constructor\n" "\n" "\n" -"## \n" +"## fn Transform.transformed\n" "\n" "```\n" -"\treturn Transform{p: p, s: s, o: o, r: r}\n" -"}\n" -"\n" -"fn umth_transform_transform(o, t: ^Transform)\n" +"fn (o: ^Transform) transformed*(t: Transform): Transform {\n" "```\n" "\n" +"Transforms a transform with another transform.\n" "\n" "\n" -"## \n" +"## fn Vf2.transformed\n" "\n" "```\n" -"\tret := o^\n" -"\tumth_transform_transform(&ret, &t)\n" -"\n" -"\treturn ret\n" -"}\n" -"\n" -"fn umth_transform_vf2(v: ^Vf2, t: ^Transform)\n" +"fn (v: ^Vf2) transformed*(t: Transform): Vf2 {\n" "```\n" "\n" +"Transforms a vf2 to another vf2.\n" +"Order:\n" +"1. scale\n" +"2. rotation\n" +"3. position\n" "\n" -"\n" -"## \n" -"\n" -"```\n" -"\tret := v^\n" -"\tumth_transform_vf2(&ret, &t)\n" -"\treturn ret\n" -"}\n" -"```\n" -"\n" +"This allows conversion from a relative to an absolute vf2.\n" "\n" "\n" -"## \n" +"## type Quad\n" "\n" "```\n" -"\n" -"fn umth_quad_max(o: ^Vf2, q: ^Quad)\n" +"type Quad* = [4]Vf2\n" "```\n" "\n" "\n" "\n" -"## \n" +"## fn Quad.getMax\n" "\n" "```\n" -"\to := Vf2{}\n" -"\tumth_quad_max(&o, q)\n" -"\treturn o\n" -"}\n" -"\n" -"fn umth_quad_min(o: ^Vf2, q: ^Quad)\n" +"fn (q: ^Quad) getMax*(): Vf2 {\n" "```\n" "\n" +"Gets the maximum coordinate.\n" "\n" "\n" -"## \n" +"## fn Quad.getMin\n" "\n" "```\n" -"\to := Vf2{}\n" -"\tumth_quad_min(&o, q)\n" -"\treturn o\n" -"}\n" +"fn (q: ^Quad) getMin*(): Vf2 {\n" "```\n" "\n" +"Gets the minimum coordinate.\n" "\n" "\n" -"## \n" +"## fn Quad.getDims\n" "\n" "```\n" -"\tmax := q[0]\n" -"\tmin := q[0]\n" -"\n" -"\tmaxmag := max.mag()\n" -"\tminmag := min.mag()\n" -"\n" -"\tfor i:=1; i < len(q^); i++ {\n" -"\t\tqmag := q[i].mag()\n" -"\n" -"\t\tif maxmag < qmag {\n" -"\t\t\tmax = q[i]\n" -"\t\t\tmaxmag = qmag\n" -"\t\t}\n" -"\n" -"\t\tif minmag > qmag {\n" -"\t\t\tmin = q[i]\n" -"\t\t\tminmag = qmag\n" -"\t\t}\n" -"\t}\n" -"\n" -"\treturn max.sub(min).abs()\n" -"}\n" -"\n" -"fn umth_th_getglobal(): ^struct{}\n" +"fn (q: ^Quad) getDims*(): Vf2 {\n" "```\n" "\n" +"Returns the dimensions of the quad\'s bounding box\n" "\n" "\n" -"## \n" +"## fn getGlobal\n" "\n" "```\n" -"\treturn umth_th_getglobal()\n" -"}\n" -"\n" -"fn umth_th_getfuncs(): ^struct{}\n" +"fn getGlobal*(): ^struct{} {\n" "```\n" "\n" +"returns a pointer to the th_global. Set this as your extensions thg.\n" "\n" "\n" -"## \n" +"## fn getFuncs\n" "\n" "```\n" -"\treturn umth_th_getfuncs()\n" -"}\n" +"fn getFuncs*(): ^struct{} {\n" "```\n" "\n" +"returns pointer to tophat functions. Pass this to th_ext_set.\n" "\n" "\n" -"## \n" +"## var enableErrrors\n" "\n" "```\n" -"fn __error*(msg: str) {\n" -"\tif enableErrors {\n" -"\t\texit(255, msg)\n" -"\t} else {\n" -"\t\tprintf(\"error: %s\\n\", msg)\n" -"\t}\n" -"}\n" +"var enableErrors*: bool = true\n" "```\n" "\n" +"If true, errors will result in a call to exit(255), otherwise printf is used.\n" "\n" "\n" -"## \n" +"## Color constants\n" "\n" "```\n" +"const (\n" +"\tblack* = 0xff\n" +"\twhite* = 0xffffffff\n" +"\tred* = 0xff0000ff\n" +"\tgreen* = 0x00ff00ff\n" +"\tblue* = 0x0000ffff\n" +"\tyellow* = 0xffff00ff\n" +"\tmagenta* = 0xff00ffff\n" +"\tcyan* = 0x00ffffff\n" +")\n" "```\n" "\n" "\n" "\n" -"## \n" +"## enum Platform\n" "\n" "```\n" +"type Platform* = enum {\n" +"\tunknown\n" +"\tlinux\n" +"\twindows\n" +"\tmacOs\n" +"\tweb\n" +"}\n" "```\n" "\n" "\n" "\n" -"## \n" +"## Misc variables\n" "\n" "```\n" +"var (\n" +"\t// time in ms from start of the game\n" +"\ttime*: uint\n" +"\t// length of the last frame in ms\n" +"\tdelta*: int\n" +"\t// platform tophat is running on\n" +"\tplatform*: Platform\n" +")\n" "```\n" "\n" "\n" diff --git a/umka/th.um b/umka/th.um index 16529ec..0a760a4 100644 --- a/umka/th.um +++ b/umka/th.um @@ -29,6 +29,7 @@ type ErrCode* = enum (int32) { already out_of_bounds } +//~~ fn (e: ^ErrCode) toStr*(): str { switch e^ {