Auto Loading Graphics System [SOLVED]

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Kjell Granlund
Prole
Posts: 33
Joined: Tue Jan 08, 2013 12:39 am
Location: Virginia Beach, VA USA

Auto Loading Graphics System [SOLVED]

Post by Kjell Granlund »

Ok well I am back with a new question. I am needing to read the ammount of files in a given folder. Basically I want to make a system that when my program runs it will make a table full of graphics. I want to be able to just drop more images into that folder and have the code automatically asign it to the table. something along these lines

Code: Select all

function image_loader()

	local i = 1 -- where i is the number of files in Graphics/
	local c = 1 -- where c is the current file going down the list 
		
		-- need to make this bit loop
		if c >= i then
			return
		end
		
		if c <= i then
			gfx_table = {[c] = love.graphics.newImage( "Graphics/" .. c .. ".png")	-- assuming images are named 1.png 2.png etc...
			c = c+1
		end
		
		
end
The trouble is that I do not know how to count how many files are in a specific folder or if this is even possible. I am also not entirely sure how to make something loop. I am hoping to make my loader dynamic so that I do not need to add code to add more images. I just need to know what index they are. Thats why naming the images as numbers would work, atleast thats how I think it would work. I am still very new to programming in general. I hope I was able to explain this enough.
Last edited by Kjell Granlund on Mon Jan 14, 2013 1:03 am, edited 1 time in total.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Auto Loading Graphics System

Post by MarekkPie »

LOVE specific questions should usually be reserved for the Support forum.

The second code sample in love.filesystem.enumerate gives you a good idea of how to search through a directory and get its information. Just change it so that rather than print the file information, load the image into a table.
User avatar
Kjell Granlund
Prole
Posts: 33
Joined: Tue Jan 08, 2013 12:39 am
Location: Virginia Beach, VA USA

Re: Auto Loading Graphics System

Post by Kjell Granlund »

I am sorry for posting in the wrong section (perhaps this can be moved), but wow that was exactly what I was looking for. I did not know the meaning of "Enumerate" so I suppose I missed that when searching the wiki. Thank you so much. This will reduce my code SO much. :awesome: You dear sir deserve a cookie!!
Santos
Party member
Posts: 384
Joined: Sat Oct 22, 2011 7:37 am

Re: Auto Loading Graphics System [SOLVED]

Post by Santos »

You could use something like this:

Code: Select all

function love.load()
	images = {}

	for _, filename in pairs(love.filesystem.enumerate('Graphics')) do
		local name = filename:match('(.*)%.png$')
		images[name] = love.graphics.newImage('Graphics/'..filename)
	end
end

function love.draw()
	love.graphics.draw(images.cloud)
end
The filenames in the "Graphics" folder are looped through, and the filename without the ".png" extension is found using the string's match method and a pattern.

Then, a new image is created and put in in the table named images with the key of the filename without the extension.

So now, love.graphics.draw(images.cloud) will draw the image "cloud.png" from the "Graphics" folder.

This might not be how you want to organize things, just an idea. :ultraglee:
User avatar
Kjell Granlund
Prole
Posts: 33
Joined: Tue Jan 08, 2013 12:39 am
Location: Virginia Beach, VA USA

Re: Auto Loading Graphics System [SOLVED]

Post by Kjell Granlund »

Well this is what I did and it worked but it means I need to name images in number order. I just saw your post so I will give that a try too. This is the code that I tried:

Code: Select all

function love.load()

files = love.filesystem.enumerate("Graphics")
gfx_table = {}
gfx_loader()

end

function gfx_loader()
	for k, files in ipairs(files) do

		print(k)
		print("Graphics/" .. k .. ".png")
		
		gfx_table[k] = love.graphics.newImage("Graphics/" .. k .. ".png")
		
		print(gfx_table[k])

	end
	
end

function love.draw()

	
end
Like I said, it worked and was able to save the images to a table. So back to the coding and try out your bit of code. If I get this working then I can just keep dumping images into my folder and it will be added to the codes tables. Perhaps a numbering system wouldnt be so bad as in "01ShipFelconXType.png" "02SomeOtherType.png" etc. This way the order will be the same and adding images will not effect currently done code. If any of that makes sense. For wanting to be lazy this is alot of work :ultrahappy: !!

**EDIT**

I looked at the code posted above and WOW that is exactly what I am looking more. Just a little modifing and I will finally have my auto loading system. No more coding in every single image file! Thanks again for the help. Such a great community here and fast to respond. I think I will turn this function into a library of sorts. Have a function and you simply give it the folder destination and the name you want the table to be and it does the rest. Still deciding if I will use a numbering system of just stick with file names. I organize my graphics like so: "001_001.png" "001_002.png" "002_001.png" where the first three numbers are a graphic and the second three number are a variation of the same graphic like different color. So 001_001 is a blue ship and 001_002 is a red ship same shape and 002_001 is a different style ship altogether. I know many might not like that method but it suits me will since i just have the folder open and see the image and know what to reference without having to type some long name like "shipBlueZealotX1Fighter.png" its simply "001_001.png" and as a plus i know 001_001 will be table ship[1][1] and 001_002 is ship[1][2] is that makes any sense.

I feel bad for anyone who tries to read through my code :shock:

The reason for this btw is that my game (posted here viewtopic.php?f=5&t=12300) will be using ALOT of images. Around 400 or more. And there is no way I wanted to type out all of that.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest