Skip to content

Commit

Permalink
Add tint parameterto Atlas and Tilemap
Browse files Browse the repository at this point in the history
  • Loading branch information
skejeton committed Aug 24, 2024
1 parent d07ca1c commit 1676de5
Show file tree
Hide file tree
Showing 7 changed files with 329 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ umth_tilemap_draw(UmkaStackSlot *p, UmkaStackSlot *r)
{
th_tmap *t = umkaGetParam(p, 0)->ptrVal;
th_transform tr = *(th_transform *)umkaGetParam(p, 1);
th_tmap_draw(t, tr);
uint32_t tint = umkaGetParam(p, 2)->uintVal;

th_tmap_draw(t, tr, tint);
}

// checks, if tilemap collides with entity.
Expand Down
325 changes: 317 additions & 8 deletions src/staembed.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "tophat.h"
#include "tophat.h"
const char *th_em_modulesrc[] = {
"\n"
"import (\n"
Expand Down Expand Up @@ -1559,14 +1559,14 @@ const char *th_em_modulesrc[] = {
"}\n"
"\n"
"\n"
"fn umth_tilemap_draw(ct: ^Tilemap, tr: th::Transform)\n"
"fn umth_tilemap_draw(ct: ^Tilemap, tr: th::Transform, tint: uint32)\n"
"//~~fn Tilemap.draw\n"
"// Draws the tilemap.\n"
"fn (t: ^Tilemap) draw*(tr: th::Transform) {\n"
"fn (t: ^Tilemap) draw*(tr: th::Transform, tint: uint32 = 0xFFFFFFFF) {\n"
"//~~\n"
"\tif len(t.cells) == 0 { return }\n"
"\n"
"\tumth_tilemap_draw(t, tr)\n"
"\tumth_tilemap_draw(t, tr, tint)\n"
"}\n"
"\n"
"fn umth_tilemap_getcoll(pos: ^th::Vf2, vert: ^th::Vf2, t: ^Tilemap, e: ^ent::Ent): bool\n"
Expand Down Expand Up @@ -2773,10 +2773,10 @@ 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) {\n"
"fn (a: ^Atlas) draw*(at: th::Vf2, t: th::Transform, tint: uint32 = 0xFFFFFFFF) {\n"
"//~~\n"
"\ta.cropSource(at)\n"
"\ta.i.draw(t, th::white)\n"
"\ta.i.draw(t, tint)\n"
"\ta.i.crop({}, {1, 1})\n"
"}\n"
"",
Expand Down Expand Up @@ -4389,6 +4389,227 @@ const char *th_em_moduledocs[] = {
"---------\n"
"\n"
"",
"opaque Image\n"
"\n"
"type Image* = struct{ _: ^struct{} }\n"
"\n"
"Represents a drawable image. It is an opaque structure.\n"
"Images support a color filter. It is applied by multiplying the color\n"
"of each pixel with the filter.\n"
"\n"
"---------\n"
"\n"
"opaque RenderTarget\n"
"\n"
"type RenderTarget* = struct { _: ^struct{} }\n"
"\n"
"An image you can render to.\n"
"\n"
"---------\n"
"\n"
"fn createRenderTarget\n"
"\n"
"fn createRenderTarget*(size: th::Vf2, filter: int): (RenderTarget, std::Err) {\n"
"\n"
"Creates a render target you can draw to, like to a window.\n"
"Filter specifies specfifies filtering for resulting image.\n"
"Image can be retrieved via `toImage`.\n"
"\n"
"---------\n"
"\n"
"fn RenderTarget.begin\n"
"\n"
"fn (rt: ^RenderTarget) begin*(): std::Err {\n"
"\n"
"Begins the render target rendering pass.\n"
"\n"
"---------\n"
"\n"
"fn RenderTarget.end\n"
"\n"
"fn (rt: ^RenderTarget) end*(wp: th::Vf2): std::Err {\n"
"\n"
"Ends the render target rendering pass.\n"
"\n"
"---------\n"
"\n"
"fn RenderTarget.toImage\n"
"\n"
"fn (rt: ^RenderTarget) toImage*(): Image {\n"
"\n"
"Returns the image of the render target. The resulting image has the same\n"
"lifetime as the base RenderTarget. If you need to use it past the lifetime\n"
"of the RenderTarget, use the copy method.\n"
"Do not call `setfilter` on the resulting image.\n"
"\n"
"---------\n"
"\n"
"fn load\n"
"\n"
"fn load*(path: str): (Image, std::Err) {\n"
"\n"
"Loads an image at path.\n"
"\n"
"---------\n"
"\n"
"fn Image.validate\n"
"\n"
"fn (i: ^Image) validate*(): bool {\n"
"\n"
"Returns true, if i\'s handle points to an image.\n"
"\n"
"---------\n"
"\n"
"fn Image.flipv\n"
"\n"
"fn (i: ^Image) flipv*(flip: bool) {\n"
"\n"
"Flips image on it\'s vertical axis.\n"
"\n"
"---------\n"
"\n"
"fn Image.fliph\n"
"\n"
"fn (i: ^Image) fliph*(flip: bool) {\n"
"\n"
"Flips image on it\'s horizontal axis.\n"
"\n"
"---------\n"
"\n"
"fn Image.draw\n"
"\n"
"fn (i: ^Image) draw*(t: th::Transform, color: uint32 = th::white) {\n"
"\n"
"Draws the image in screen coordinates. It transforms it with t and\n"
"applies color as a color filter.\n"
"\n"
"---------\n"
"\n"
"fn Image.blit\n"
"\n"
"fn (i: ^Image) blit*(src, dest: rect::Rect, color: uint32 = 0xFFFFFFFF, rot: th::fu = 0, origin: th::Vf2 = {0, 0}) {\n"
"\n"
"Copies source rectangle (texture coordinates) to destination rectangle (screen coordinates).\n"
"\n"
"---------\n"
"\n"
"fn Image.drawNinepatch\n"
"\n"
"fn (i: ^Image) drawNinepatch*(outer, inner, dest: rect::Rect, color: uint32 = th::white, scale: real = 1.0) {\n"
"\n"
"Draws \"nine-patch\" image.\n"
"`outer` specifies the texture coordinates of the outer rect of source image,\n"
"`inner` specifies coordinates of inner rect of source image, positioned relative to `outer`.\n"
"You can tint with `color`.\n"
"\n"
"![](img/ninepatch.png)\n"
"\n"
"---------\n"
"\n"
"fn Image.drawOnQuad\n"
"\n"
"fn (i: ^Image) drawOnQuad*(q: th::Quad, color: uint32 = th::white) {\n"
"\n"
"Draws the image on top of a quad with corners of the image positioned\n"
"on the verticies of the quad.\n"
"\n"
"---------\n"
"\n"
"fn Image.getDims\n"
"\n"
"fn (i: ^Image) getDims*(): th::Vf2 {\n"
"\n"
"Returns width and heigth.\n"
"\n"
"---------\n"
"\n"
"fn Image.crop\n"
"\n"
"fn (i: ^Image) crop*(tl, br: th::Vf2) {\n"
"\n"
"Crops an image. Coordinates are between 0, 0 (top left) and\n"
"1, 1 (bottom right)\n"
"\n"
"---------\n"
"\n"
"fn Image.cropPx\n"
"\n"
"fn (i: ^Image) cropPx*(tr, br: th::Vf2) {\n"
"\n"
"Same as `Image.crop`, but the positions are in pixels.\n"
"\n"
"---------\n"
"\n"
"fn Image.cropRect\n"
"\n"
"fn (i: ^Image) cropRect*(r: rect::Rect) {\n"
"\n"
"Same as `Image.crop`, but uses a rect instead of two positions.\n"
"\n"
"---------\n"
"\n"
"fn Image.cropQuad\n"
"\n"
"fn (i: ^Image) cropQuad*(q: th::Quad) {\n"
"\n"
"Crop an image using a quad.\n"
"\n"
"---------\n"
"\n"
"fn Image.getCropQuad\n"
"\n"
"fn (i: ^Image) getCropQuad*(): th::Quad {\n"
"\n"
"Crop an image using a quad.\n"
"\n"
"---------\n"
"\n"
"fn mk\n"
"\n"
"fn mk*(data: []uint32, dm: th::Vf2): (Image, std::Err) {\n"
"\n"
"Creates an image from raw data.\n"
"\n"
"---------\n"
"\n"
"fn Image.copy\n"
"\n"
"fn (i: ^Image) copy*(): (Image, std::Err) {\n"
"\n"
"Copies image into a new one.\n"
"\n"
"---------\n"
"\n"
"fn Image.setfilter\n"
"\n"
"fn (i: ^Image) setfilter*(filter: int): std::Err {\n"
"\n"
"Sets a mag/min filter. 0 is nearest, others are linear.\n"
"This function will regenerate the texture. This means it shouldn\'t be\n"
"used in a loop.\n"
"https://learnopengl.com/img/getting-started/texture_filtering.png\n"
"left is nearest, right is linear.\n"
"\n"
"---------\n"
"\n"
"fn Image.setData\n"
"\n"
"fn (i: ^Image) setData*(data: []uint32, dm: th::Vf2): std::Err {\n"
"\n"
"Updates the image data. dm are the dimensions of the new image.\n"
"The new image doesn\'t have to be the same size as the old one.\n"
"\n"
"---------\n"
"\n"
"fn Image.getData\n"
"\n"
"fn (i: ^Image) getData*(): []uint32 {\n"
"\n"
"Gets the image data. This downloads the data from the GPU on **each call**.\n"
"Don\'t use in performance critical sections.\n"
"\n"
"---------\n"
"\n"
"",
"\n"
"\n"
Expand Down Expand Up @@ -4796,6 +5017,94 @@ const char *th_em_moduledocs[] = {
"---------\n"
"\n"
"",
"\n"
"\n"
"\n"
"Canvas library allowing for drawing basic shapes. Coordinates are based on\n"
"the screen.\n"
"\n"
"---------\n"
"\n"
"fn drawText\n"
"\n"
"fn drawText*(text: str, pos: th::Vf2, color: uint32, size: th::fu) {\n"
"\n"
"Draws a basic pixel text. Only ascii is supported.\n"
"\n"
"---------\n"
"\n"
"fn textSize\n"
"\n"
"fn textSize*(text: str, scale: th::fu): th::Vf2 {\n"
"\n"
"Returns the size of text taken by an equivalent drawText call.\n"
"\n"
"---------\n"
"\n"
"Pixel Font\n"
"\n"
"var pixelFont*: PixelFont\n"
"\n"
"The `pixelFont` variable exposes the canvas pixel font as a generic font.\n"
"\n"
"---------\n"
"\n"
"fn drawRect\n"
"\n"
"fn drawRect*(color: uint32, r: rect::Rect) {\n"
"\n"
"Draws a Rectangle.\n"
"\n"
"---------\n"
"\n"
"fn drawLine\n"
"\n"
"fn drawLine*(color: uint32, b, e: th::Vf2, thickness: th::fu) {\n"
"\n"
"Draws a line.\n"
"\n"
"---------\n"
"\n"
"fn drawRectLines\n"
"\n"
"fn drawRectLines*(color: uint32, r: rect::Rect, thickness: real32 = 1.0) {\n"
"\n"
"Draws rect border.\n"
"\n"
"---------\n"
"\n"
"fn drawQuad\n"
"\n"
"fn drawQuad*(color: uint32, q: th::Quad) {\n"
"\n"
"Draws a convex quad.\n"
"\n"
"---------\n"
"\n"
"fn drawTrig\n"
"\n"
"fn drawTrig*(color: uint32, a, b, c: th::Vf2) {\n"
"\n"
"Draws a triangle.\n"
"\n"
"---------\n"
"\n"
"fn beginScissorRect\n"
"\n"
"fn beginScissorRect*(r: rect::Rect, debug: bool = false) {\n"
"\n"
"Disable rendering outside of rect `r`\n"
"\n"
"---------\n"
"\n"
"fn endScissor\n"
"\n"
"fn endScissor*() {\n"
"\n"
"Stops cropping\n"
"\n"
"---------\n"
"\n"
"",
"struct Rect\n"
"\n"
Expand Down Expand Up @@ -4974,7 +5283,7 @@ const char *th_em_moduledocs[] = {
"\n"
"fn Tilemap.draw\n"
"\n"
"[3mfn (t: ^Tilemap) draw*(tr: th::Transform) {\n"
"[3mfn (t: ^Tilemap) draw*(tr: th::Transform, tint: uint32 = 0xFFFFFFFF) {\n"
"\n"
"Draws the tilemap.\n"
"\n"
Expand Down Expand Up @@ -5936,7 +6245,7 @@ const char *th_em_moduledocs[] = {
"\n"
"fn Atlas.draw\n"
"\n"
"[3mfn (a: ^Atlas) draw*(at: th::Vf2, t: th::Transform) {\n"
"[3mfn (a: ^Atlas) draw*(at: th::Vf2, t: th::Transform, tint: uint32 = 0xFFFFFFFF) {\n"
"\n"
"Draws the tile at `at`\n"
"\n"
Expand Down
2 changes: 1 addition & 1 deletion src/thextdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ 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, th_transform tr);
THEXT(void, th_tmap_draw, th_tmap *t, th_transform tr, uint32_t tint);
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);
Expand Down
Loading

0 comments on commit 1676de5

Please sign in to comment.