| Using SDL to make a WAV Resource creator |
|
riksweeney
|
Could you not convert the wavs to ogg or mp3 files instead?
|
|||||||||||
|
|
||||||||||||
|
djkarstenv
|
hi,
I haven't tried that, but in theory SDL will decompress the OGG or MP3 file before saving it to my resource file anyway, so either way I still stuck with a large resource file. I preferrably don't want all my sound files exposed in the game folder, I am trying to put 10 or so audio files into one single file and call it Audio.1, Audio.2, etc.. I will however give the OGG version a try and see the result, thanks for your input :) K |
|||||||||||
|
|
||||||||||||
|
djkarstenv
|
okay, i tried using ogg, mp3 etc files, but like i suspected, SDL decompresses the files into a raw format in memory.
To elaborate more on what I am doing, here is some code : Mix_Chunk* test(nullptr); test = Mix_LoadWAV("nameofaudiofile.WAV"); now the test pointer points to the uncompressed audio data in memory. I know the "abuf" member variable points to all the actual audio data, which is merely loads and loads of 8-bit values. i then write all of test's data to file : file.write((char*)&test->alen, sizeof(test->alen)); file.write((char*)&test->allocated, sizeof(test->allocated)); file.write((char*)&test->volume, sizeof(test->volume)); file.write((char*)test->abuf, test->alen); now my resource file contains the uncompressed raw audio data, hence resulting in a large file. If i could only find a way to compress the "abuf" 8-bit data, for instance, like remove silences? If anyone has any ideas to share, that would be greatly appreciated Thanks Karsten |
|||||||||||
|
|
||||||||||||
|
riksweeney
|
Why don't you just place the entire wav (or ogg, mp3, jpg, png etc.) into your resource file? You can read the file back out into memory and use SDL_RWops to load it.
http://kekkai.org/roger/sdl/rwops/rwops.html |
|||||||||||
|
|
||||||||||||
|
djkarstenv
|
Thanks for the link, but when trying a few things out, nothing writes to my resource file. Here is a code example :
SDL_RWops* rw(nullptr); FILE* file = fopen("nameoffile.bmp", "r"); rw = SDL_RWFromFP(file, 0); fstream file2("nameofresourcefile.000", ios::binary | ios::out); file2.write((char*)rw, ???); The part with the ??? is where I get stuck, how can I determine the size of the image in memory? I figure I need to know the size of the chunk that rw is pointing to, but how? Before writing the image data to the resource file, I need this data size I tried using fwrite, but the arguments for that function also require data size Any ideas? Thanks |
|||||||||||
|
|
||||||||||||
|
riksweeney
|
You can use ftell to tell you the size of the file when you open it. I don't know C++, so this is the C code
|
|||||||||||||
|
|
||||||||||||||
|
djkarstenv
|
Your code is exactly the same in C++, and I gave it a try. That part of the program works, however, when I write the data to file, the file remains to be 0KB large
For argument sake, I'll use ur prev example and use this code : FILE* outfile = fopen(filename, "wb"); fwrite(rw, 1, fileSize, outfile); I am stuck as to what exact arguments to put in the fwrite function. - rw is the pointer to the image data, - the second argument is the size of each data element to be written, i said 1 byte each, - size is the total size of all data, as aquired using ftell() - and outfile is the file we want to write to After executing this, the resource file stays blank |
|||||||||||
|
|
||||||||||||
|
riksweeney
|
I'm not sure if you can use SDL_RWops to write to a file, I've always just read the file into memory and written it into the file directly:
|
|||||||||||||
|
|
||||||||||||||
|
djkarstenv
|
WOW! Thanks!!
So far this code works like magic!! I noticed that even though its my own resource file, i was still able to open the image in windows paint, so I added a small 1 byte data block at the start of the resource file so that no image editor can read it, do u do that too? I presume from here onwards I load the resource data into memory and use RWops to load it into my relevant pointers? Thanks again for your help, I really appreciate your time!! BTW : Your game looks really good! :) |
|||||||||||
|
|
||||||||||||
|
riksweeney
|
You can do it that way, just make sure when you read the file back in you skip the first byte, otherwise RWops will probably fail since it won't recognise the file either!
|
|||||||||||
|
|
||||||||||||
|
djkarstenv
|
This is really helpful, i have given all filetypes a try, so now i can use png, jpg, bmp, wav etc and it works beautifully.
I have been searching for a solution to this for a long time! Thank you very much again for your help and patience, you are a legend! K |
|||||||||||
|
|
||||||||||||

