How to qucikly extract quads from a sprite sheet?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
rougan
Citizen
Posts: 58
Joined: Wed Aug 12, 2015 10:30 am

How to qucikly extract quads from a sprite sheet?

Post by rougan »

Hi there!
I have around 150 different images in a spritesheet that I need to save as quads in my game. However, this takes absolutely agesto figure out the values and type out (x and y on spritesheet and the size of the quad). Most of the images are (slighty) different sizes so I don't think I can use a for loop to load them as there is no recurring pattern?
If anyone has any tips/tricks that'd be great, thanks! :)
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: How to qucikly extract quads from a sprite sheet?

Post by TheMeq »

I would re-arrange your sprite sheet so that all of your sprites are of a uniform size that will allow you to use a loop. It would probably take more processing power trying to read the image to detect the borders of your sprites then it would just to cut them all up at the same size.
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

Re: How to qucikly extract quads from a sprite sheet?

Post by 0x72 »

you can introduce some automated spritesheet generator into your pipeline google suggests this one but I have no idea if it's any good: https://www.codeandweb.com/texturepacker (edit: didn't noticed but it looks like it's a paid app - you consider if worth it for 150 images, in case it's not I suppose there are some alternatives)

or (as it's just 150 images) you can come up with some little tool like the one below and select the quads by hand end export to own lua file (if you add reading from file + naming the quads with love.textinput it would be actually useful :)):

Code: Select all

local lg = love.graphics
local img
local quads = {}
local cquad = nil

function love.load()
  img = lg.newImage('img.jpg')
end

function love.draw()
  lg.setColor(255, 255, 255)
  lg.draw(img, 0, 0)
  if cquad then
    lg.setColor(255, 0, 0)
    local mx, my = love.mouse.getPosition()
    lg.rectangle('line', cquad.x+0.5, cquad.y+0.5, mx-cquad.x, my-cquad.y )
    lg.print(mx .. ', ' .. my, mx + 10, my)
  end
  for index, q in ipairs(quads) do
    lg.setColor(20, 20, 20)
    lg.print(index, q.x, q.y - 15)
    lg.rectangle('line', q.x + 0.5, q.y + 0.5, q.w, q.h)
    lg.setColor(200, 200, 200)
    lg.rectangle('line', q.x + 1.5, q.y + 1.5, q.w, q.h)
  end
end

function love.mousepressed(x, y, btn)
  if btn == 'l' or btn == 1 then
    cquad = { x = x, y = y }
  end
end

function love.keypressed(key)
  if key == 'return' then
    local fp = io.open('out.lua', 'w+')
    fp:write('return {')
    for index, q in ipairs(quads) do
      fp:write('\n  { x = '.. q.x .. ', y = '.. q.y .. ', w = '.. q.w .. ', h = '.. q.h .. ', id = '.. index .. ' }')
      if index ~= #quads then fp:write(',') end
    end
    fp:write('\n}\n')
    fp:close()
  elseif key == 'backspace' then
    quads[#quads] = nil
  end
end

function love.mousereleased(x, y, btn)
  if cquad and (btn == 'l' or btn == 1) then
    local qx, qy

    qx = math.min(x, cquad.x)
    qy = math.min(y, cquad.y)
    cquad.w = math.max(x, cquad.x) - qx
    cquad.h = math.max(y, cquad.y) - qy
    cquad.x = qx
    cquad.y = qy

    quads[#quads+1] = cquad
    cquad = nil
  end
end
User avatar
josefnpat
Inner party member
Posts: 955
Joined: Wed Oct 05, 2011 1:36 am
Location: your basement
Contact:

Re: How to qucikly extract quads from a sprite sheet?

Post by josefnpat »

0x72 wrote:you can introduce some automated spritesheet generator into your pipeline google suggests this one but I have no idea if it's any good: https://www.codeandweb.com/texturepacker (edit: didn't noticed but it looks like it's a paid app - you consider if worth it for 150 images, in case it's not I suppose there are some alternatives)
Take a look at urraka's texpack!
Missing Sentinel Software | Twitter

FORCIBLY IGNORED.
<leafo> when in doubt delete all of your code
<bartbes> git rm -r *
<bartbes> git commit -m "Fixed all bugs"
<bartbes> git push
User avatar
rougan
Citizen
Posts: 58
Joined: Wed Aug 12, 2015 10:30 am

Re: How to qucikly extract quads from a sprite sheet?

Post by rougan »

0x72 wrote:you can introduce some automated spritesheet generator into your pipeline google suggests this one but I have no idea if it's any good: https://www.codeandweb.com/texturepacker (edit: didn't noticed but it looks like it's a paid app - you consider if worth it for 150 images, in case it's not I suppose there are some alternatives)

or (as it's just 150 images) you can come up with some little tool like the one below and select the quads by hand end export to own lua file (if you add reading from file + naming the quads with love.textinput it would be actually useful :)):

Thank you so much, I hadn't thought of approaching it this way before! I think i'll try and make my own tool for educational purposes! :P Thanks again!
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: How to qucikly extract quads from a sprite sheet?

Post by airstruck »

Another thing you could try is linking or embedding the sprite sheet in an svg that's the same size, and then drawing rectangles around all your sprites. Then change the id attribute of each rectangle to something that represents the sprite, and use xpath to export all the rectangle geometry to a Lua table. I'm using inkscape and xmlstarlet to do this and it works pretty well. Here's an example using an svgz where each rectangle has an id starting with "sprite."

Code: Select all

atlas='resource/atlas.lua'

echo 'return {' > $atlas
gzip -dk -S svgz resource/sprite.svgz -c | \
xmlstarlet sel -T -t -m '//svg:rect[starts-with(@id,"sprite.")]' \
-o "['" -v "substring-after(@id, 'sprite.')" \
-o "'] = { x = " -v "@x" -o ", y = " -v "@y" \
-o ", width = " -v "@width" \
-o ", height = " -v "@height" -o " }, " -n >> $atlas
echo '}' >> $atlas
Post Reply

Who is online

Users browsing this forum: No registered users and 87 guests