Log In  

You can use your spritesheets as-is in Picotron! First, add your spritesheet(s) into sprites in Picotron, like this:

Next, paste this function into your lua code (you can delete the comments for a smaller footprint):

-- 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

Note that the idx paramter counts from left-to-right, then top-to-bottom. With the example above, the first row of indices would count "0, 1, 2, 3".

Finally, just call the function when needed, and enjoy seeing your atlas come to life! Cheers!

P#144162 2024-03-22 20:51

1

Thanks for figuring this out! Very useful!

P#144928 2024-03-29 02:13

The guide was great. Simple access! scratch games

P#147171 2024-04-23 03:39

[Please log in to post a comment]