-- Sprite Editor -- Paste any sized sprite into this block to edit it. -- After drawing, copy the text from the console. img = [[ ...cc.ccc. .cccccccc. .cYYcYYYc. YYgWYYgWcY YYYYYYYYYY .YRYYYYRY. .YYRRRRY.. ....YY.... .MmMYYmMm. .MmMmMmMm. ]] sprite = am.sprite(img) pixels = sprite.spec.texture.image_buffer.buffer:view('uint') size = vec2(sprite.width, sprite.height) win = am.window{ title = "Sprite Editor", width = size.x, height = size.y, clear_color = vec4(0, 0, 0, 1) } win.scene = sprite function rgba2uint(color) return math.floor(color.a * 255) * 16777216 + math.floor(color.b * 255) * 65536 + math.floor(color.g * 255) * 256 + math.floor(color.r * 255) end function uint2rgba(uint) local a = math.floor(uint / 16777216) / 255 uint = uint - math.floor(a * 255) * 16777216 local b = math.floor(uint / 65536) / 255 uint = uint - math.floor(b * 255) * 65536 local g = math.floor(uint / 256) / 255 uint = uint - math.floor(g * 255) * 256 local r = math.floor(uint) / 255 return vec4(r,g,b,a) end -- add clear into the map, so we can draw with it am.ascii_color_map['.'] = vec4(0,0,0,0) -- starting color color = rgba2uint(am.ascii_color_map['W']) function lookup_color(c) local cuint = rgba2uint(c) for k,v in pairs(am.ascii_color_map) do if rgba2uint(v) == cuint then return k end end return "?" end function handle_key(k) if k == 'escape' then pixels:set(0) return end if k == 'period' then k = '.' end if win:key_down("lshift") or win:key_down("rshift") then k = string.upper(k) end local c = am.ascii_color_map[k] if c ~= nil then color = rgba2uint(c) print("color", k, "=", c, string.format("%x",color)) return else print("unrecognized key:", k) end end print("Available colors:") print(table.tostring(am.ascii_color_map)) print("Press any of these keys to pick a color.") print("Press ESCAPE to clear.") sprite:action(function() for _,k in ipairs(win:keys_pressed()) do handle_key(k) end if win:mouse_down"left" then local p = win:mouse_position() + size/2 p = vec2(math.floor(p.x), math.floor(p.y)) local i = p.x + p.y * win.width + 1 if i>=1 and i<=size.x*size.y then pixels[i] = color end end if win:mouse_released"left" then print("img = [[") for y = 0,size.y-1 do local row="" for x = 0,size.x-1 do local i = x + (size.y - y - 1) * size.x + 1 local pix = pixels[i] row = row .. lookup_color(uint2rgba(pix)) end print(row) end print("]]") end end)