Codea Utilities

@dave1707 - here’s an idea for you. I was thinking about building a library of Codea routines that users could load up as and when they need them so that users could build prototypes fairly quickly. They’d have to be well programmed (leaves me out) and annotated so users could understand the routines and how to apply them.

I started putting together a routine to tile a background with images which would fit and not look distorted ie it could load various sized images of different aspect ratios. Just repeating with the same image.

I realised, after my first couple of attempts, that it would mean you would have to consider the image size, the image aspect ratio and the screen width/height.

My initial attempts, have no annotations, and fell well short of my aims. I post one here below:


-- CodeaUtils01

viewer.mode = FULLSCREEN
function setup()
    --
    tName = "CodeaUtils01"
    init()
end

function update(dt)
    --
    checkAspect()
end

function draw()
    -- 
    update(DeltaTime)
  --  background(45, 26, 45)
    sprite(backing, cW, cH)
    nameit()
end

function touched(touch)
    --
    if touch.state == ENDED then
        print("touched")
    end
    
end

function init()
    --
    
    sW, sH, cW, cH = WIDTH ,HEIGHT, WIDTH//2, HEIGHT//2
    aspect = WIDTH/HEIGHT
    backing(backGnd[3])
end

function nameit()
    --
    pushStyle()
        fill(117, 233, 80)
        font("AmericanTypewriter-Bold")
        fontSize(64)
        textMode(CENTER)
        text(tName, cW, cH)
    popStyle()
end

function init()
    --
    
    sW, sH, cW, cH = WIDTH ,HEIGHT, WIDTH//2, HEIGHT//2
    aspect = WIDTH/HEIGHT
    backing(backGnd[3])
end

function checkAspect()
    --
    local check = WIDTH/HEIGHT
    if check ~= aspect then 
        aspect = WIDTH//HEIGHT 
        sW, sH, cW, cH = WIDTH ,HEIGHT, WIDTH//2, HEIGHT//2
    end
end

function backing(back)
    --
    backing = image(sW, sH)
    bW, bH = spriteSize(back)
    hbW, hbH = bW//2, bH//2
    print(bW, bH)
    twW, twH = sW//bW, sH//bH
    setContext(backing)
    for lph = 1, twH do
        for lpw = 1, twW do
            sprite(back,lpw*bW-hbW, lph*bH-hbH)
        end
    end
    setContext()
end

backGnd = {
        asset.documents.Tiles.panelB,
        asset.documents.Tiles.map1,
        asset.documents.Tiles.map2
        }

Can you think of the best way to scale the images so that they fit corner to corner. I’ll post any progress I make and annotate when it works properly.

Edit: you’ll have to provide your own images to do this.

@Bri_G Thats a lot of figuring out. Maybe instead of trying to get everything to fit, you just place the pictures at positions to fill the background. If pictures overlap that’s ok. Then 30 seconds or so later you redo the background again so it’s a different arrangement of pictures so overlapping doesn’t really matter.