function GameworldDrawPrepare()
	if game_resize then
		Camera.height = game.height
		Camera.width = game.width
		Canvas.Darkness:Recreate()
	end
	pcr, pcg, pcb, pca = love.graphics.getColor()
	love.graphics.setScale(game.scale,game.scale)
	love.graphics.setColor(1,1,1,1)
end

function GameworldDrawEnd()
	love.graphics.setColor(pcr, pcg, pcb, pca)
	pcr, pcg, pcb, pca = nil, nil, nil, nil
end

function GameworldDrawBackground()
	-- obscure a bit
	love.graphics.setColor(0.7,0.7,0.7)
	for i = 1, #LevelTiles do
		for j = 1, #LevelTiles[i] do
			if LevelTiles[i][j].id ~= 0 then

				local depth = TileData[LevelTiles[i][j].id].depth
				DrawTile(
					LevelTiles[i][j],
					tileProperties.scale * j * tileProperties.width	+ tileProperties.scale * (levelProperties.offset.x - tileProperties.width)	- Camera.pos.x,
					tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height) - Camera.pos.y,
					"background"
				)

			end
		end
	end
end

function GameworldDrawParticles()
	love.graphics.setColor(0.7,0.7,0.7)
	for _, particle in pairs(LoadedObjects.Particles) do
		particle:HandleAnimation()
	end
end

function GameworldDrawEntitiesBackground()
	for _, enty in pairs(LoadedObjects.Entities) do
		enty:DrawBackground()
	end
end

function GameworldDrawEntities()
	love.graphics.setColor(0.7,0.7,0.7)
	for _, enty in pairs(LoadedObjects.Entities) do
		enty:HandleAnimation()
	end
end

function GameworldDrawForeground()
	love.graphics.setColor(1,1,1)
	for i = 1, #LevelTiles do
		for j = 1, #LevelTiles[i] do
			if LevelTiles[i][j].id ~= 0 then

				local depth = TileData[LevelTiles[i][j].id].depth
				DrawTile(
					LevelTiles[i][j],
					tileProperties.scale * j * tileProperties.width	+ tileProperties.scale * (levelProperties.offset.x - tileProperties.width)	- Camera.pos.x,
					tileProperties.scale * i * tileProperties.height + tileProperties.scale * (levelProperties.offset.y - tileProperties.height) - Camera.pos.y,
					"foreground"
				)

			end
		end
	end
end

function GameworldDrawDarkness()
	Canvas.Darkness:Reset()
	Canvas.Darkness:DrawingStart()
	love.graphics.setBlendMode("replace")
	love.graphics.setColor(0,0,0,0)
	for _, light in pairs(LoadedObjects.Lights) do
		if light.range ~= 0 then
			local position = {
				x = (light.pos.x - Camera.pos.x) / game.scale,
				y = (light.pos.y - Camera.pos.y) / game.scale
			}
			local range = (light.range + light.flicker) / game.scale
			love.graphics.circle(
					"fill",
					position.x,
					position.y,
					range
				)
		end
	end
	Canvas.Darkness:DrawingEnd()
	Canvas.Darkness:Draw()
end

function GameworldDrawLights()
	for _, light in pairs(LoadedObjects.Lights) do
		if light.range ~= 0 then
			love.graphics.setColor(light.color[1],light.color[2],light.color[3],1)

			Shader.RadiusGradient:send("pos_x",- Camera.pos.x + light.pos.x)
			Shader.RadiusGradient:send("pos_y",- Camera.pos.y + light.pos.y)
			Shader.RadiusGradient:send("range",light.range)
			Shader.RadiusGradient:send("scale",game.scale)

			love.graphics.setShader(Shader.RadiusGradient)
			love.graphics.circle(
				"fill",
				- Camera.pos.x + light.pos.x,
				- Camera.pos.y + light.pos.y,
				light.range
			)
			love.graphics.setShader()
		end
	end
end

function GameWorldUpdateLights()
	for _, light in pairs(LoadedObjects.Lights) do
		light:Flicker()
	end
end
