Hello friends,
I have some functions I wanted to share.
This code creates a Textfile with an array based on the input file. The array name can also be defined:
For example:
copyFileByte("music.ogg", "music_array.txt","Music1")
Create array from media:
function copyFileByte(fileName$, newName$,ArrName$)
// open the two files
print("Starting Progress")
sync()
startime# = timer()
endtime# = timer() - startime#
sourceFile = OpenToRead(fileName$)
sourceSize = GetFileSize(sourceFile)
destFile = OpenToWrite(newName$)
endtime# = timer() - startime#
print("files created! Begin reading: "+str(endtime#))
sync()
int$=""
count=0
byte as integer[]
upc=0
// while we are not at the end of the source file
while FileEOF(sourceFile) = 0
// write the next byte in the source file to the dest file
// by going through it byte by byte, this function will be able to copy any file, regardless of format
cbyte = ReadByte(sourceFile)
// byte.length = byte.length +1
// byte[byte.length] = cbyte
byte.insert(cbyte)
upc=upc+1
if upc=100000
print("Reading: "+str(byte.length)+"/"+str(sourceSize))
sync()
upc=0
endif
endwhile
endtime# = timer() - startime#
print("Reading Finished! Begin writing: "+str(endtime#))
sync()
col$=""
mc$=""
mmc$=""
pos=1
upc=0
i=0
while i<byte.length
select pos
case 1:
b1=byte[i]
endcase
case 2:
b2=byte[i]
endcase
case 3:
b3=byte[i]
endcase
case 4:
b4=byte[i]
col$=col$+str((b1 << 24) + (b2 << 16) + (b3 << 8) + b4)+","
if len(col$)>256
mc$=mc$+col$+chr(10)
col$=""
if len(mc$)>16000
mmc$=mmc$+mc$
mc$=""
endif
endif
inc count,4
r=0
g=0
b=0
a=0
pos=0
endcase
endselect
if upc>164000
print("Writing: "+str(i)+"/"+str(byte.length))
sync()
upc=0
endif
inc pos
inc upc
inc i
endwhile
if b1>0 or b2>0 or b3>0
mmc$=mmc$+str((b1 << 24) + (b2 << 16) + (b3 << 8) + b4)+","
if b1>0 then inc count,1
if b1>0 and b2>0 then inc count,1
if b1>0 and b2>0 and b3>0 then inc count,1
endif
WriteLine( destFile, ArrName$+" as integer["+str(count)+"]= ["+TrimString(mmc$,",")+"]" )
// close the files
CloseFile(sourceFile)
CloseFile(destFile)
endfunction
This function call creates a textfile "
music_array.txt" with a copy-paste code of an integer array with the name "
Music1". If you add this code to an AppGameKit project,
you can read it out like this:
msc = LoadMusicFromArray( Music1 )
Read from array:
function LoadMusicFromArray(mscarr as integer[])
destFile = OpenToWrite("tmp.ogg")
for i=0 to mscarr.length
WriteByte(destFile,mscarr[i] >> 24 && val("ff",16))
WriteByte(destFile,mscarr[i] >> 16 && val("ff",16))
WriteByte(destFile,mscarr[i] >> 8 && val("ff",16))
WriteByte(destFile,mscarr[i] && val("ff",16))
next i
CloseFile(destFile)
msc = LoadMusicOGG("tmp.ogg")
DeleteFile("tmp.ogg")
endfunction msc
I have also attached an example project with an old music that I made:
https://forum.thegamecreators.com/attachment/78924
So this code helps you to save small pieces of media inside an array,
you can save anything you like in it. It works with images, 3d models, sound, music.
You can use this functions to hide your media, although the representation in strings
instead of integers (like I did) could reduce the total size to 50%, so there is still a lot of potential in this idea.
If you want to use this heavily, add each Array into one seperate include file, this will still be compiled into the bytecode,
but a lot of chars in a textfile can reduce your editing-speed, so paste this arrays in a seperate include file for maximum performance.
I hope it's useful for someone.
Greetings,
Jack