Tile image function

Since I’ve seen no one do this yet with the new sprite zsize function, it thought I might as well. It was extremely easy, but here is the code:

function setup()
    mx = 0
    my = 0
end

function tileImage(image,movex,movey,iscale)
    local oldScale = scale
    local currentScale = 1
    scale = function( d )
        if d == nil then
            return currentScale
        else
            currentScale = currentScale * d 
            oldScale(d)
        end
    end
    bsmode = spriteMode()
    currentScale = scale()
    bscale = currentScale
    spriteMode(CORNER)
    scale(iscale)
    ssx, ssy = spriteSize(image)
    mx = mx + movex
    my = my + movey
    for y = -1, HEIGHT/ssy/iscale do
        for x = -1, WIDTH/ssx/iscale do
            if mx >= ssx or mx <= -ssx then
                mx = 0
            elseif my >= ssy or my <= -ssy then
                my = 0
            end
            sprite(image, x * ssx+mx, y * ssy+my)
        end
    end
    spriteMode(bsmode)
    scale(bscale)
end

That was easy. But it’s still full of bugs and needs improvement. Here’s what’s on my list:

  1. Fix the jolt when repeating the images when moving
  2. Fix the problem when you tell it to move mor pixels than the image is
  3. Fix the returning to the original scale and sprite mode
  4. Madage to contain all the variables within the function
  5. Speed up the process. It really slows down the program and even crashes Codea when you use some pixel art.
  6. Add option to tell it if it’s a block or not and it will automatically offset it correctly and draw the blocks right.

Oh. Is pushStyle() a way to save the style? How do you use it? It might be helpful.

Yes. Push/pull is like a stack of plastic cups. You push the current style down on the stack, then you can change whatever and when done, pop the style back up. Since it’s a stack, you can push multiple times and they pop LIFO - last (most recent) in first out

Do you have a very simple example? Codea didn’t really supply a really good, simple sample.

-- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    line(100,100,100,200) --5
    pushStyle() --push 5
    line(110,100,110,200) --still 5
    strokeWidth(10) --changed to 10
    line(120,100,120,200) --10
    popStyle() --pop 5
    line(130,100,130,200) --5

Alright. Thanks. I’ll include it as soon as possible.

Also the “Graphical Styles” example that ships with Codea is designed to show use of pushStyle() and popStyle().

Oh. Thanks. I’ll check it out.

This really slows down the app. Any idea how to make it faster? Thanks.

I don’t understand why you loop x (and y) from -1 to (for example) +4?

Are you talking about movex and movey? Those automatically move the background. In the loop, I start at -1 so when it is moving 1,1 pixels, I loop the position back to 0 (which there is a better method I’m about to add) to simulate continuous motion.

No, I meant:

    for x = -1, WIDTH/ssx/iscale do

and then

            sprite(image, x * ssx+mx, y * ssy+my)

@Martin - mx means move x. Mabey I shouldve called it adjust x. I change it whatever amount the image should be offset to make the images move. Then when it’s greater than the image size, I set it back to 0 to simulate an ongoing background.