diff --git a/src/sprite_atlas.lua b/src/sprite_atlas.lua new file mode 100644 index 0000000..6a24657 --- /dev/null +++ b/src/sprite_atlas.lua @@ -0,0 +1,31 @@ +--[[pod_format="raw",created="2024-04-23 18:57:57",modified="2024-04-23 18:57:58",revision=1]] +-- spr_from_atlas() - draw to the screen a slice of a sprite atlas +-- param: s - sprite atlas to use (the number in the Picotron spritesheet) +-- param: idx - the 0-based index to render from the atlas +-- param: cols - number of columns in the sprite atlas +-- param: rows - number of rows in the sprite atlas +-- param: x - screen x-coordinate to draw top-left corner of the sprite +-- param: y - screen y-coordinate to draw top-left corner of the sprite +-- param: flip_x (optional) - flip the sprite horiztonally +-- param: flip_y (optional) - flip the sprite vertically +function spr_from_atlas(s, idx, cols, rows, x, y, flip_x, flip_y) + + -- assign default values to optional parameters if nil + local fx = flip_x ~= nil and flip_x or false + local fy = flip_y ~= nil and flip_y or false + + -- retrieve userdata about the sprite + local spr_data = get_spr(s) + local spr_w = spr_data:width() + local spr_h = spr_data:height() + + -- calculate width and height of each atlas entry + local w, h = (spr_w // cols), (spr_h // rows) + + -- determine where in the sprite's coordinate system to start referencing + local spr_x = (idx % cols) * w + local spr_y = (idx // rows) * h + + -- sspr( sprite, sx, sy, sw, sh, dx, dy, dw, dh, flip_x, flip_y ) + sspr(s, spr_x, spr_y, w, h, x, y, w, h, fx, fy) +end \ No newline at end of file