Custom file type

BlitzPlus Forums/BlitzPlus Programming/Custom file type

Grovesy(Posted 2004) [#1]
Hi all. I want to create a custom file type that I can use to place multiple "objects" in. I want this file to hold the objects image (probably in BMP format) and also any number of user defind settings along with the setting type, for example: String ObjectName, int ObjectLife etc.

So this file will hold the actual objects image, and any number of settings. Then in my level editor the user can select that file and will then see all the objects within that file that they can add to the game. If they right click on any object, once it has been placed in the level, the user can change the objects settings.

So how do you mix graphics and text information into one file? I guess in a way i want to make my own version on a PAK file! This is a 2D game if that makes any difference!

Cheers


koekjesbaby(Posted 2004) [#2]
check the writeInt, writeFloat, writeString, readInt, readFloat, readString, etc. commands in the help.

and for the image you could read the bytes from the bmp and write it in the file and when reading the file reconstruct the bmp file somewhere temporary. but that might be a bad idea ;)


Grovesy(Posted 2004) [#3]
Yeah. sorry i know how to write binary files with just textual data, its the including of the image that i can't work out! :(

If I can't work it out I may just include the image file name in the file, so i can just reference an external image.

It would just be nice to have it all in one Packed up file.


Rimmsy(Posted 2004) [#4]
Global life=0
Global antialias_on=0
Global something$="hello there"

Graphics 640,480,0,2
SetBuffer BackBuffer()


; to save test
image=LoadImage("test.png")
saveGame("test.dat",image)

FreeImage image ; get rid of our loaded image
Print "Save file saved. Press any key to load it."
WaitKey

; to load test (loads settings AND a new image);
image=loadGame("test.dat")
DrawImage image,0,40
Flip
Print "Done!"
WaitKey
End






; this function saves an image as well as other information in the file
Function saveGame(file$,image)
	If image=0 Then Return
	f=WriteFile(file)
	WriteString f,"File version: 1"
	WriteInt f,life
	WriteByte f,antialias_on
	WriteString f,something
	
	; write w + h
	WriteInt f,ImageWidth(image)
	WriteInt f,ImageHeight(image)
	
	; now write the image
	LockBuffer ImageBuffer(image)
	For i=0 To ImageWidth(image)-1
		For j=0 To ImageHeight(image)-1
			rgb=ReadPixelFast(i,j,ImageBuffer(image))
			WriteInt f,rgb
		Next
	Next
	UnlockBuffer ImageBuffer(image)	
	CloseFile f ; close the file
End Function


; this function returns a new image loaded from the file
Function loadGame(file$)
 ; now do the EXACT opposite here.
	f=ReadFile(file)
	
	file_ver$=ReadString(f)
	life=ReadInt(f)
	antialias_on=ReadByte(f)
	something=ReadString(f)
	
	w=ReadInt(f)
	h=ReadInt(f)
	
	; create a new image
	image=CreateImage(w,h)
	
	; now read the image
	LockBuffer ImageBuffer(image)
	For i=0 To w-1
		For j=0 To h-1
			rgb=ReadInt(f)
			WritePixelFast(i,j,rgb,ImageBuffer(image))
		Next
	Next
	UnlockBuffer ImageBuffer(image)
	
	CloseFile f
	Return image
End Function