SPRITES = {}

sprites = {}

local basicSprites = {
	"arrow",
	"bolt",
	"bullet",
	"circle",
	"circleOutline",
	"halfCircle",
	"halfCircleOutline",
	"line",
	"plus",
	"poly5",
	"poly6",
	"shadow",
	"shield",
	"square",
	"squareOutline",
	"star",
	"star6",
	"tile",
	"triangle",
	"triangleOutline",
}

function sprites.init()
	for index, spr in pairs(basicSprites) do
		SPRITES[spr] = video.createSpriteState(spr, "basic.dat")
	end

	local names = video.getSpriteStateNames("sprites.dat")
	for index, name in pairs(names) do
		assert(not SPRITES[name])
		SPRITES[name] = video.createSpriteState(name, "sprites.dat")		
	end

	sprites.loadDefs(TILES, "tiles.dat")
end

function sprites.loadDefs(defs, dat)
	for index, def in pairs(defs) do
		sprites.loadDef(def, dat)
	end
end

function sprites.loadDef(def, dat)
	if def.spriteNames then
		def.sprites = {}
		for index, spriteNames in pairs(def.spriteNames) do
			if type(spriteNames) == "table" then
				def.sprites[index] = {}
				for ii, spr in ipairs(spriteNames) do
					local sprite = video.createSpriteState(spr, dat)
					table.insert(def.sprites[index], sprite)					
				end
			else
				local sprite = video.createSpriteState(spriteNames, dat)
				if type(index) == "number" then
					table.insert(def.sprites, sprite)
				else
					def.sprites[index] = sprite
				end
			end
			--print(def.sprites[index], index, "load", spriteNames, dat)
		end
	end
end