Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

spriteBatch class

edited February 2013 in General Posts: 26

I don't know if anyone will find this useful.

One thing to keep in mind, which is demonstrated in the Test project, is the order of the sprites being drawn. It's not drawn in the same order as just using Sprite.

The spriteBatch will draw all the sprites with the same texture in one layer. You don't have much control over this. They are all part of a single mesh object.

To have more control over the "layers" use multiple sets of spritebatch Start->Draw->End sections.

Here is a project that demonstrates how to use the spriteBatch class.

-- RenderTest

-- Use this function to perform your initial setup
function setup()
    --displayMode(FULLSCREEN)
    parameter.boolean("SpriteBatch", false)

    mySpriteBatch = spriteBatch()
    touching = false

end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(29, 61, 64, 255)
    if SpriteBatch then
        mySpriteBatch:Start()
    end   
    for x = 0, WIDTH, 25 do
        for y = 0, HEIGHT, 50 do
            if  x % 100 == 0 or touching then
                if not SpriteBatch then
                    sprite("Planet Cute:Character Horn Girl",x,y,100,100)
                else
                    mySpriteBatch:draw("Planet Cute:Character Horn Girl",x,y,100,100)
                end
            else
                if not SpriteBatch then
                    sprite("Planet Cute:Character Pink Girl",x,y,100,100)
                else
                    mySpriteBatch:draw("Planet Cute:Character Pink Girl",x,y,100,100)
                end
            end 
        end
    end
    if SpriteBatch then
        mySpriteBatch:End()
    end
    fill(255, 255, 255, 255)
    fontSize(40)
    -- Do your drawing here    
    text("fps:" .. math.floor(1/DeltaTime), 200,100)

end

function touched(touch)
    touching = true
    if touch.state == ENDED then
        touching = false
    end
end



-- spriteBatch
spriteBatch = class()
local myMeshes = {}

function spriteBatch:draw(texture, x, y, w, h, ts, tt, tw, th, colortint, r, sc)
    local x = x or 0 -- x position of sprite
    local y = y or 0 -- y position of sprite
    local w = w or 0 -- w width of sprite
    local h = h or 0 -- h height of sprite
    local ts = ts or 0 -- left edge of texture
    local tt = tt or 0 -- bottom edge of texture
    local tw = tw or 1 -- width of texture 
    local th = th or 1 -- height of texture
    -- colortint is the tint of spite 
    local r = r or 0 -- rotation of sprite
    local sc = sc or 1 -- scale of sprite
    if not myMeshes[texture] then
        myMeshes[texture] = mesh() 
        myMeshes[texture].texture = texture
        -- playing 
        --myMeshes[texture].shader = shader("Effects:Ripple")
        --myMeshes[texture].shader.freq = 1
    end

    local i 

    --myMeshes[texture].shader.time = ElapsedTime

    i = myMeshes[texture]:addRect(x,y,w*sc,h*sc) 
    myMeshes[texture]:setRectTex(i, ts, tt, tw, th)
    if colortint then
         myMeshes[texture]:setRectColor(i,colortint)
    end
    myMeshes[texture]:setRect(i,x,y,w*sc,h*sc,r)

end

function spriteBatch:Start()
    for i,v in pairs(myMeshes) do
        v:clear()
    end
end

function spriteBatch:End()
    for i,v in pairs(myMeshes) do
        v:draw()
    end
end

Sign In or Register to comment.