Drawing polygon between sprites

Hello! I'm trying to draw a polygon between a background (sprite) and another sprite.
playdate-20220422-151456
If I draw the polygon after sprite.update() then the sprites appear under the polygon, and if I call draw polygon before update() then the whole polygon disappears.

I also tried putting the drawing inside setBackgroundDrawingCallback() but it won't clean the background
playdate-20220422-154929

(also ideally I do wanna put the boat sprite behind the wave and fish in front of the wave)

I'm on Windows using SDK 1.10.0. Thanks in advance.

While there are several ways of solving this, my favorite is to simply make everything a sprite. Do it with the polygon too!

2 Likes

Thank you! That works great for the z index! But now I'm not sure how to optimize the drawing rect now. It seems like it's updating the entire screen now. This is currently how I'm doing the image drawing

-- draw wave to a image
waterImage = gfx.image.new(400, 240)
waterSprite = gfx.sprite.new(waterImage)
waterSprite:setZIndex(4)
waterSprite:setCenter(0, 0)
waterSprite:add()

function playdate.update()
    -- draw dynamic waves
    gfx.lockFocus(waterImage)
    gfx.setScreenClipRect(0, waterHeight - waterStrength, 400, 240 - waterHeight + waterStrength)
    waterImage:clear(gfx.kColorClear)
    drawWave(calcWave(100, waterHeight), gfx.kColorXOR)
    gfx.clearClipRect()
    gfx.unlockFocus()
    
    -- update sprites
    gfx.sprite.update()
end

It's my first project in Playdate as I'm also trying to learn about the drawing so I'm not sure what I'm missing😅

You could manually set the sprite dirty rect:

  • playdate.graphics.sprite.addDirtyRect(x, y, width, height)

Using the bounds of your polygon:

  • playdate.geometry.polygon:getBounds()
1 Like