Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Export un sprite en array
- -- Utilisation d'une palette de 16 couleurs, ici la Pico-8
- -- Correspondance entre les couleurs utilisé dans le sprite avec la palette
- -- Les couleurs utilisé dans le sprite doivent correspondre a la palette
- -- Un fichier 'export.lua' sera créé dans le repertoire du projet
- love.graphics.setDefaultFilter("nearest")
- local palette = {
- {0, 0, 0}, -- black
- {29, 43, 83}, -- dark-blue
- {126, 37, 83}, -- dark-purple
- {0, 135, 81}, -- dark-green
- {171, 82, 54}, -- brown
- {95, 87, 79}, -- dark-gray
- {194, 195, 199}, -- light-gray
- {255, 241, 232}, -- white
- {255, 0, 77}, -- red
- {255, 163, 0}, -- orange
- {255, 236, 39}, -- yellow
- {0, 228, 54}, -- green
- {41, 173, 255}, -- blue
- {131, 118, 156}, -- indigo
- {255, 119, 168}, -- pink
- {255, 204, 170} -- peach
- };
- local img = {}
- function love.load()
- img.img = love.graphics.newImage("stand.png")
- img.w, img.h = img.img:getWidth() , img.img:getHeight()
- -- Creation du canvas de la taille de notre sprite
- img.canvas = love.graphics.newCanvas(img.w,img.h)
- -- On set le canvas créé
- love.graphics.setCanvas(img.canvas)
- -- On affiche le sprite dans ce canvas
- love.graphics.draw(img.img,0,0)
- -- On ferme le canvas
- love.graphics.setCanvas()
- -- On crée un ImageData par rapport au canvas
- -- Celui ci va comporter les info direct, comme les pixel affiché dans le canvas
- img.data = img.canvas:newImageData()
- -- Array qui va comporter les index des couleurs dans la palette
- char = {}
- -- Boucle qui va parcourir notre canvas pixel par pixel
- for x = 0, img.w - 1 do
- for y = 0, img.h - 1 do
- -- On recupere les info RGB de chaque pixel testé dans le canvas
- r,g,b = img.data:getPixel(x,y)
- -- On va checker dans notre palette une correspondance
- for i,v in ipairs(palette) do
- -- Si il y a correspondance, on insert dans notre Array 'char' , l'index
- if v[1] == r and v[2] == g and v[3] == b then
- table.insert(char, i)
- end
- end
- end
- end
- -- Creation du fichier 'export.lua'
- export = io.open ("export.lua", "wb")
- export:write("local sprite = {\n")
- export:write("\t{")
- local size = table.maxn(char)
- print("Size : "..size-1)
- for i=1, size do
- local v = tostring(char[i]-1)
- if i%(img.w) == 0 and i < size then
- if tonumber(v) < 10 then
- export:write(' '..v..' },\n\t{')
- else
- export:write(' '..v..' },\n\t{')
- end
- else
- if tonumber(v) < 10 then
- if i == size then
- export:write(' '..v..' ')
- else
- export:write(' '..v..',')
- end
- else
- if i == size then
- export:write(' '..v..' ')
- else
- export:write(' '..v..',')
- end
- end
- end
- end
- export:write("}\n}")
- -- On ferme le chier
- export:close()
- end
- local width, height = love.window.getMode()
- function love.draw()
- love.graphics.draw(img.canvas, (width-img.canvas:getWidth())/2, (height-img.canvas:getHeight())/2, 0)
- end
- function love.keypressed(key,scancode,isrepeat)
- isrepeat = false
- if key == "escape" then
- love.event.push('quit')
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement