Need program for combining animation frames!

Miscellaneous Forums/General Discussion/Need program for combining animation frames!

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Ya want something done right, ya gotta do it yourself!

I buckled down and wrote a stupidly simple sprite sheet maker. It only loads PNG files and outputs a 24bit PNG file with alpha called spritesheet.png.

To use it, make a directory and stick the program in it. Then make subdirectories with your various aliens or what have you, with the files for that alien in the alien's subdirectory.

Then run the program and click the directory in the file selection box.

The program will then read all the PNG's in the directory, and using the size of the first, and the number of PNG's, make a square PNG with all the smaller PNG's in it called spritesheet.png in the same directory as the program. Then just rename that and run the program again for each directory.

I would have named the sprite sheets after the subdirectories but there wasn't a five second way for me to implement that so I didn't.

Anyway here's the code if anyone else needs it:

	Global FileList:TList = CreateList()
	Global PM:TPixmap, PM_Final:TPixmap
	

	' Open a file dialog so the user can select a directory.
		Path$ = RequestDir("Select a Folder", CurrentDir())


	' Attempt to open the directory for reading.
	' If we fail, exit the program.

		Dir = ReadDir(Path$)
		If Not Dir Then End


	' Get the filename of each PNG file in the specified directory.
	
		Repeat
	
			T$ = NextFile(Dir)
		
			If T$ = "" Exit
			If T$ = "." Or T$ = ".." Continue
		
			If ExtractExt$(T$) = "png" Then FileList.AddLast(Path$ + "" + T$)
		
		Forever

		CloseDir Dir


	' Sort the list of filenames in alphanumeric order.
		SortList(FileList, True, CompareStrings)


	' Get the total number of pixmaps we're dealing with.
		Count = FileList.Count()
		

	' Calculate the number of rows and columns we need in the contact sheet to fit this many pixmaps while having an image as close to square as possible.
	' (Assuming PNG's are square.)
	
		Rows = Sqr(Count)
		If Rows*Rows < Count Then Rows = Rows + 1
		

	' Get the size of the first image.  We assume this is the size of all the PNG's.
		PM = LoadPixmapPNG(String(FileList.First()))
		
		CelWidth  = PM.Width 
		CelHeight = PM.Height
		

	' Calculate the width and height of the final image.
		FinalWidth  = CelWidth * Rows
		FinalHeight = CelHeight * Rows


	' Create the pixmap we're going to paste all the other pixmaps into.
		PM_Final = CreatePixmap(FinalWidth, FinalHeight, PF_RGBA8888)
		

	' Paste the small pixmaps into the big one.
		
		For File$ = EachIn FileList
			
			DebugLog(File$)
			PM = LoadPixmapPNG(File$)

			PM_Final.Paste(PM, X*CelWidth, Y*CelHeight)
			
			X = X + 1
			If X = Rows 
				X = 0
				Y = Y +1
			EndIf
			
		Next
		

	' Save the final pixmap, with maximum compression.
		SavePixmapPNG(PM_Final, "spritesheet.png", 9)
	
		
' ------------------------------
' This function compares two strings and returns true if string 1 is greater than string 2, using alphabetical order.
' ------------------------------

	Function CompareStrings(O1:Object, O2:Object)
		Return String(O1) > String(O2)
	End Function