PHYSFS + SDL_rwops

so, im trying to load some audio and font files from a zip, right now im doing this:

Code:
if (PHYSFS_exists(file)){
PHYSFS_file *R_File = PHYSFS_openRead(file);
PHYSFS_sint64 Size = PHYSFS_fileLength(R_File);
char *Data = new char[(Uint32)PHYSFS_fileLength(R_File)];
PHYSFS_read(R_File, Data, 1, (PHYSFS_sint32)Size);
SDL_RWops *_RW = SDL_RWFromMem(Data, (int)Size);
PHYSFS_close(R_File);
}

and then i just use the _RW variable to make a open a font like this:
TTF_DroidSerif = TTF_OpenFontRW(_RW, 0, font_size);

my question would be regarding the char* Data, how should i procced to handle its memory so i can do a delete[] in a correct way. …?
if i delete the data after SDL_RWFromMem the rwops does not work anymore, same thing for sound files, im assuming is because fonts and audio files are not loaded as images, you may not use all the glyphs in a font, and you may not play every sound of an audio file. so the data is needed for as long as the struct itself.

both sdl_ttf and sdl_mixer ask me for the “freesrc” parameter in the creation functions, would that be sufficient to free the char* Data from before?. or do i need some kind of struct to keep all the data and then deleting according?

what are the best approaches that you guys have for this kind of stuff?

2014-05-21 7:44 GMT+02:00 javierecf :

so, im trying to load some audio and font files from a zip, right now im
doing this:

Code:

 if (PHYSFS_exists(file)){
    PHYSFS_file *R_File = PHYSFS_openRead(file);
    PHYSFS_sint64 Size = PHYSFS_fileLength(R_File);
    char *Data = new char[(Uint32)PHYSFS_fileLength(R_File)];
    PHYSFS_read(R_File, Data, 1, (PHYSFS_sint32)Size);
    SDL_RWops *_RW = SDL_RWFromMem(Data, (int)Size);
    PHYSFS_close(R_File);

}

and then i just use the _RW variable to make a open a font like this:
TTF_DroidSerif = TTF_OpenFontRW(_RW, 0, font_size);

my question would be regarding the char* Data, how should i procced to
handle its memory so i can do a delete[] in a correct way. …?
if i delete the data after SDL_RWFromMem the rwops does not work anymore,
same thing for sound files, im assuming is because fonts and audio files
are not loaded as images, you may not use all the glyphs in a font, and you
may not play every sound of an audio file. so the data is needed for as
long as the struct itself.

both sdl_ttf and sdl_mixer ask me for the “freesrc” parameter in the
creation functions, would that be sufficient to free the char* Data from
before?. or do i need some kind of struct to keep all the data and then
deleting according?

what are the best approaches that you guys have for this kind of stuff?

One approach is to not allocate a memory chunk at all and just wrap the
PhysFS interface
in a RWops structure. PhysFS itself has an implementation of this I
believe, check under
"extras/physfsrwops.[c,h]" (although I’m not sure if that’s been updated to
work with SDL2,
you have to ask Ryan about that).
I do the same in my engine, if you’re curious about a second implementation:

If you absolutely insist on using a temp memory buffer, you could check in
the SDL source
where it stores the buffer pointer (probably ‘hidden.unknown.data1’) and
write your own
’close’ function which frees that pointer to override the default RWops
handler.

The ‘freesrc’ parameter I believe just calls ‘SDL_RWclose()’ when you eg.
close the font
(typically in SDL; this will internally also free the RWops structure). But
it is of no direct
use for your issue here.

I can confirm that physfsrwops works under SDL2.

Jonny DOn Wed, May 21, 2014 at 11:55 AM, Jonas Kulla wrote:

2014-05-21 7:44 GMT+02:00 javierecf :

so, im trying to load some audio and font files from a zip, right now im

doing this:

Code:

 if (PHYSFS_exists(file)){
    PHYSFS_file *R_File = PHYSFS_openRead(file);
    PHYSFS_sint64 Size = PHYSFS_fileLength(R_File);
    char *Data = new char[(Uint32)PHYSFS_fileLength(R_File)];
    PHYSFS_read(R_File, Data, 1, (PHYSFS_sint32)Size);
    SDL_RWops *_RW = SDL_RWFromMem(Data, (int)Size);
    PHYSFS_close(R_File);

}

and then i just use the _RW variable to make a open a font like this:
TTF_DroidSerif = TTF_OpenFontRW(_RW, 0, font_size);

my question would be regarding the char* Data, how should i procced to
handle its memory so i can do a delete[] in a correct way. …?
if i delete the data after SDL_RWFromMem the rwops does not work anymore,
same thing for sound files, im assuming is because fonts and audio files
are not loaded as images, you may not use all the glyphs in a font, and you
may not play every sound of an audio file. so the data is needed for as
long as the struct itself.

both sdl_ttf and sdl_mixer ask me for the “freesrc” parameter in the
creation functions, would that be sufficient to free the char* Data from
before?. or do i need some kind of struct to keep all the data and then
deleting according?

what are the best approaches that you guys have for this kind of stuff?

One approach is to not allocate a memory chunk at all and just wrap the
PhysFS interface
in a RWops structure. PhysFS itself has an implementation of this I
believe, check under
"extras/physfsrwops.[c,h]" (although I’m not sure if that’s been updated
to work with SDL2,
you have to ask Ryan about that).
I do the same in my engine, if you’re curious about a second
implementation:
https://github.com/Ancurio/mkxp/blob/master/src/filesystem.cpp#L503

If you absolutely insist on using a temp memory buffer, you could check in
the SDL source
where it stores the buffer pointer (probably ‘hidden.unknown.data1’) and
write your own
’close’ function which frees that pointer to override the default RWops
handler.

The ‘freesrc’ parameter I believe just calls ‘SDL_RWclose()’ when you eg.
close the font
(typically in SDL; this will internally also free the RWops structure).
But it is of no direct
use for your issue here.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

is there anything extra i have to do in order to use physfsrwops?, im
guessing just including the .h file to a project file is not enough. do i
need to do something else in the linkage part of the project?.. im using
vc2012. The PHYSFSRWOPS_H file says Physfs should be configured to my
liking?..

2014-05-21 9:59 GMT-06:00 Jonathan Dearborn :> I can confirm that physfsrwops works under SDL2.

Jonny D

On Wed, May 21, 2014 at 11:55 AM, Jonas Kulla wrote:

2014-05-21 7:44 GMT+02:00 javierecf <@Javier_Flores>:

so, im trying to load some audio and font files from a zip, right now

im doing this:

Code:

 if (PHYSFS_exists(file)){
    PHYSFS_file *R_File = PHYSFS_openRead(file);
    PHYSFS_sint64 Size = PHYSFS_fileLength(R_File);
    char *Data = new char[(Uint32)PHYSFS_fileLength(R_File)];
    PHYSFS_read(R_File, Data, 1, (PHYSFS_sint32)Size);
    SDL_RWops *_RW = SDL_RWFromMem(Data, (int)Size);
    PHYSFS_close(R_File);

}

and then i just use the _RW variable to make a open a font like this:
TTF_DroidSerif = TTF_OpenFontRW(_RW, 0, font_size);

my question would be regarding the char* Data, how should i procced to
handle its memory so i can do a delete[] in a correct way. …?
if i delete the data after SDL_RWFromMem the rwops does not work
anymore, same thing for sound files, im assuming is because fonts and audio
files are not loaded as images, you may not use all the glyphs in a font,
and you may not play every sound of an audio file. so the data is needed
for as long as the struct itself.

both sdl_ttf and sdl_mixer ask me for the “freesrc” parameter in the
creation functions, would that be sufficient to free the char* Data from
before?. or do i need some kind of struct to keep all the data and then
deleting according?

what are the best approaches that you guys have for this kind of stuff?

One approach is to not allocate a memory chunk at all and just wrap the
PhysFS interface
in a RWops structure. PhysFS itself has an implementation of this I
believe, check under
"extras/physfsrwops.[c,h]" (although I’m not sure if that’s been updated
to work with SDL2,
you have to ask Ryan about that).
I do the same in my engine, if you’re curious about a second
implementation:
https://github.com/Ancurio/mkxp/blob/master/src/filesystem.cpp#L503

If you absolutely insist on using a temp memory buffer, you could check
in the SDL source
where it stores the buffer pointer (probably ‘hidden.unknown.data1’) and
write your own
’close’ function which frees that pointer to override the default RWops
handler.

The ‘freesrc’ parameter I believe just calls ‘SDL_RWclose()’ when you eg.
close the font
(typically in SDL; this will internally also free the RWops structure).
But it is of no direct
use for your issue here.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Javier Flores

2014-05-21 21:25 GMT+02:00 Javier Flores :

is there anything extra i have to do in order to use physfsrwops?, im
guessing just including the .h file to a project file is not enough. do i
need to do something else in the linkage part of the project?.. im using
vc2012. The PHYSFSRWOPS_H file says Physfs should be configured to my
liking?..

Have you tried dropping physfsrwops.c into your project?

oright,now it seems to be loading the file font correctly, (rw==null) dont
shows up.

SDL_RWops* rw;
rw=PHYSFSRWOPS_openRead(“data/fonts/DroidSerif-Bold.ttf”);
if(rw == NULL){ return; }
TTF_DroidSerif = TTF_OpenFontRW(rw, 0, font_size);

im sure the file is there, i was using it before, but TTF_OpenFontRw now
just crashes for me. would i need to do something else to make it work? or
maybe is a problem with sdl_ttf?

2014-05-21 13:40 GMT-06:00 Jonas Kulla :> 2014-05-21 21:25 GMT+02:00 Javier Flores <@Javier_Flores>:

is there anything extra i have to do in order to use physfsrwops?, im

guessing just including the .h file to a project file is not enough. do i
need to do something else in the linkage part of the project?.. im using
vc2012. The PHYSFSRWOPS_H file says Physfs should be configured to my
liking?..

Have you tried dropping physfsrwops.c into your project?


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Javier Flores

im sure the file is there, i was using it before, but TTF_OpenFontRw now
just crashes for me. would i need to do something else to make it work?
or maybe is a problem with sdl_ttf?

Make sure you’re using the default branch of PhysicsFS…the 2.0.3
release offers a physfsrwops.c that is compatible with SDL 1.2.

The function signatures changed a little for SDL2 and could probably
cause weird crashes. But your compiler should have warned you, too. :slight_smile:

It changed like this…

https://hg.icculus.org/icculus/physfs/diff/e4cc35000826/extras/physfsrwops.c

…we called it SDL 1.3 when this patch happened in 2011.

Other than that: can we get a backtrace?

–ryan.

so, I got the stuff from the default branch on mercurial, and used Cmake to
make the project files, im using vs2012, and in the compiling a bunch of
errors regarding
archiver_iso9660.c show up.

including:
Error 15 error C2275: ‘PHYSFS_sint64’ : illegal use of this type as
an expression D:\mingw_dev_lib\physfs\src\archiver_iso9660.c 268
1 physfs

does it have to do with the version of vs that im using, should i tried to
compile in a newer version?, or did i make something wrong in the cmake
process?.. i used the default options and i just let it generate the
project files.

2014-05-21 15:40 GMT-06:00 Ryan C. Gordon :>

im sure the file is there, i was using it before, but TTF_OpenFontRw now

just crashes for me. would i need to do something else to make it work?
or maybe is a problem with sdl_ttf?

Make sure you’re using the default branch of PhysicsFS…the 2.0.3 release
offers a physfsrwops.c that is compatible with SDL 1.2.

The function signatures changed a little for SDL2 and could probably cause
weird crashes. But your compiler should have warned you, too. :slight_smile:

It changed like this…

https://hg.icculus.org/icculus/physfs/diff/e4cc35000826/extras/
physfsrwops.c

…we called it SDL 1.3 when this patch happened in 2011.

Other than that: can we get a backtrace?

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Javier Flores

Instead of “hacking” my way into fixing it, i rewrote the whole
(font->text) situation, so my text entities share common preloaded fonts,
same for sounds. everything seems ok right now.

2014-05-21 16:52 GMT-06:00 Javier Flores <@Javier_Flores>:> so, I got the stuff from the default branch on mercurial, and used Cmake

to make the project files, im using vs2012, and in the compiling a bunch of
errors regarding
archiver_iso9660.c show up.

including:
Error 15 error C2275: ‘PHYSFS_sint64’ : illegal use of this type as
an expression D:\mingw_dev_lib\physfs\src\archiver_iso9660.c 268
1 physfs

does it have to do with the version of vs that im using, should i tried to
compile in a newer version?, or did i make something wrong in the cmake
process?.. i used the default options and i just let it generate the
project files.

2014-05-21 15:40 GMT-06:00 Ryan C. Gordon :

im sure the file is there, i was using it before, but TTF_OpenFontRw now

just crashes for me. would i need to do something else to make it work?
or maybe is a problem with sdl_ttf?

Make sure you’re using the default branch of PhysicsFS…the 2.0.3
release offers a physfsrwops.c that is compatible with SDL 1.2.

The function signatures changed a little for SDL2 and could probably
cause weird crashes. But your compiler should have warned you, too. :slight_smile:

It changed like this…

https://hg.icculus.org/icculus/physfs/diff/e4cc35000826/extras/
physfsrwops.c

…we called it SDL 1.3 when this patch happened in 2011.

Other than that: can we get a backtrace?

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Javier Flores


Javier Flores

btw: when loading the assets for the game, i use:

char **rc;
char **f;
std::string ptr;

for(i=0; i<dirs.size(); i++){
    rc = PHYSFS_enumerateFiles(dirs[i].c_str());
    for (f = rc; *f != NULL; f++){
        ptr = dirs[i] + "/" + *f;
     }
}

and on windows it works nice, i have 3 sub dirs to look for assets, and
everything is shown as it should, but on linux i have a problem, i have two
files, particles_0.xml and particles_0.png, when assigning to ptr the
direction of the file, particles_0.xml always ends with a ’ " ’ character,
like garbage or something. it only happens on that single file, every other
file is loaded nullterminated and totally fine. i can do a split and
condition the extension of the files and other stuff. except for that
particular file, on linux.

im still using the last release of physfs.

2014-05-22 3:01 GMT-06:00 Javier Flores <@Javier_Flores>:> Instead of “hacking” my way into fixing it, i rewrote the whole

(font->text) situation, so my text entities share common preloaded fonts,
same for sounds. everything seems ok right now.

2014-05-21 16:52 GMT-06:00 Javier Flores <@Javier_Flores>:

so, I got the stuff from the default branch on mercurial, and used Cmake

to make the project files, im using vs2012, and in the compiling a bunch of
errors regarding
archiver_iso9660.c show up.

including:
Error 15 error C2275: ‘PHYSFS_sint64’ : illegal use of this type as
an expression D:\mingw_dev_lib\physfs\src\archiver_iso9660.c 268
1 physfs

does it have to do with the version of vs that im using, should i tried
to compile in a newer version?, or did i make something wrong in the cmake
process?.. i used the default options and i just let it generate the
project files.

2014-05-21 15:40 GMT-06:00 Ryan C. Gordon :

im sure the file is there, i was using it before, but TTF_OpenFontRw now

just crashes for me. would i need to do something else to make it work?
or maybe is a problem with sdl_ttf?

Make sure you’re using the default branch of PhysicsFS…the 2.0.3
release offers a physfsrwops.c that is compatible with SDL 1.2.

The function signatures changed a little for SDL2 and could probably
cause weird crashes. But your compiler should have warned you, too. :slight_smile:

It changed like this…

https://hg.icculus.org/icculus/physfs/diff/e4cc35000826/extras/
physfsrwops.c

…we called it SDL 1.3 when this patch happened in 2011.

Other than that: can we get a backtrace?

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Javier Flores


Javier Flores


Javier Flores

and on windows it works nice, i have 3 sub dirs to look for assets, and
everything is shown as it should, but on linux i have a problem, i have
two files, particles_0.xml and particles_0.png, when assigning to ptr
the direction of the file, particles_0.xml always ends with a ’ " '
character, like garbage or something. it only happens on that single
file, every other file is loaded nullterminated and totally fine. i can
do a split and condition the extension of the files and other stuff.
except for that particular file, on linux.

(I’ll send you an email off-list about this, since we’re off-topic here.)

–ryan.

I’m interested too in asset directory automatic traverse loading
imminently, so, unless that is possible with RWOps ( ? actual question
here), either we come with SDL_filesystem, or take PHYSFS as a friend lib
(kinda ending as SDL_filesystem ;-)).

It’s been talked about before, but there isn’t any directory equivalent
of RWOPS in SDL…RWOPS only handles i/o streams.

I’ve tossed around the idea before, and it hasn’t been compelling enough
(to me) to design an API for it, especially since PhysicsFS fills that
gap pretty well.

I can be convinced otherwise, though.

–ryan.On 5/24/14, 3:48 PM, Juan Manuel Borges Ca?o wrote:

I’m interested too in asset directory automatic traverse loading
imminently, so, unless that is possible with RWOps ( ? actual question
here), either we come with SDL_filesystem, or take PHYSFS as a friend
lib (kinda ending as SDL_filesystem ;-)).