SDL_RWops to load shader file

I am using SDL 2.0 on iOS and currently i’m trying to load a shader file into a GLchar * to use in my game. The code I’m using is:

Code:

SDL_RWops *file = SDL_RWFromFile(fileName.c_str(), “r”);
char buf[256];
SDL_RWread(file, buf, sizeof (buf), 1);
cout << buf << endl;

However, it is printing out something like this:

Code:

varying lowp vec2 textureVarying;

uniform sampler2D texture;

void main()
{
gl_FragColor = texture2D(texture, textureVarying);
}\247\350\230
\327’\240\247\350\356

Obviously the strange backslash then numbers at the end of the file are not desirable and i’m not sure as to what is causing them to be there. Thanks, I would appreciate any help.

You are reading 256 bytes, but the filesize could be less than that.
you are also not null terminating buf[]

Use the following pseudo code:

SDL_RWSeek(Seek to file end)
size_t filesize = SDL_RWTell();
SDL_RWSeek(Seek to beginning);

size_t readsize = sizeof(buf) - 1;
if(readsize > filesize)
readsize = filesize;

SDL_RWread(file, buf, readsize , 1);
buf[readsize] = 0;
cout << buf << endl;

I am using SDL 2.0 on iOS and currently i’m trying to load a shader
file into a GLchar * to use in my game. The code I’m using is:

Code:

SDL_RWops *file = SDL_RWFromFile(fileName.c_str(), “r”);
char buf[256];
SDL_RWread(file, buf, sizeof (buf), 1);
cout << buf << endl;

However, it is printing out something like this:

Code:

varying lowp vec2 textureVarying;

uniform sampler2D texture;

void main()
{
gl_FragColor = texture2D(texture, textureVarying);
}\247\350\230
\327’\240\247\350\356On 10/24/2013 10:33 AM, Eamonn wrote:

Obviously the strange backslash then numbers at the end of the file
are not desirable and i’m not sure as to what is causing them to be
there. Thanks, I would appreciate any help.


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


Pallav Nawani
Game Designer/CEO
http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming

Thank you very much. I got it to work!

I’m sorry, that is also not really correct. SDL_RWRead can return a
different amount of bytes that were read than the amount you requested.
You should check the return value of SDL_RWRead and put a loop around
the read until you get what you expect or an error is returned.
Otherwise you might only read parts of the file.

You are reading 256 bytes, but the filesize could be less than that.
you are also not null terminating buf[]

Use the following pseudo code:

SDL_RWSeek(Seek to file end)
size_t filesize = SDL_RWTell();
SDL_RWSeek(Seek to beginning);

size_t readsize = sizeof(buf) - 1;
if(readsize > filesize)
readsize = filesize;

SDL_RWread(file, buf, readsize , 1);
buf[readsize] = 0;
cout << buf << endl;

I am using SDL 2.0 on iOS and currently i’m trying to load a shader
file into a GLchar * to use in my game. The code I’m using is:

Code:

SDL_RWops *file = SDL_RWFromFile(fileName.c_str(), “r”);
char buf[256];
SDL_RWread(file, buf, sizeof (buf), 1);
cout << buf << endl;

However, it is printing out something like this:

Code:

varying lowp vec2 textureVarying;

uniform sampler2D texture;

void main()
{
gl_FragColor = texture2D(texture, textureVarying);
}\247\350\230
\327’\240\247\350\356Am 24.10.2013 07:25, schrieb Pallav Nawani:
On 10/24/2013 10:33 AM, Eamonn wrote:

Obviously the strange backslash then numbers at the end of the file
are not desirable and i’m not sure as to what is causing them to be
there. Thanks, I would appreciate any help.


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

Sorry I’m having a hard time following what you mean. Do you mind posting some example code?

Martin Gerhardy wrote:

I’m sorry, that is also not really correct. SDL_RWRead can return a
different amount of bytes that were read than the amount you requested.
You should check the return value of SDL_RWRead and put a loop around
the read until you get what you expect or an error is returned.
Otherwise you might only read parts of the file.

You are reading 256 bytes, but the filesize could be less than that.
you are also not null terminating buf[]

Use the following pseudo code:

SDL_RWSeek(Seek to file end)
size_t filesize = SDL_RWTell();
SDL_RWSeek(Seek to beginning);

size_t readsize = sizeof(buf) - 1;
if(readsize > filesize)
readsize = filesize;

SDL_RWread(file, buf, readsize , 1);
buf[readsize] = 0;
cout << buf << endl;

I am using SDL 2.0 on iOS and currently i’m trying to load a shader
file into a GLchar * to use in my game. The code I’m using is:

Code:

SDL_RWops *file = SDL_RWFromFile(fileName.c_str(), “r”);
char buf[256];
SDL_RWread(file, buf, sizeof (buf), 1);
cout << buf << endl;

However, it is printing out something like this:

Code:

varying lowp vec2 textureVarying;

uniform sampler2D texture;

void main()
{
gl_FragColor = texture2D(texture, textureVarying);
}\247\350\230
\327’\240\247\350\356> Am 24.10.2013 07:25, schrieb Pallav Nawani:

On 10/24/2013 10:33 AM, Eamonn wrote:

Obviously the strange backslash then numbers at the end of the file
are not desirable and i’m not sure as to what is causing them to be
there. Thanks, I would appreciate any help.


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

Silly question, but do you even know how to access files with the
standard library? Because this isn’t any different. The issue would be
obvious if you know how e.g. fread() works.

2013/10/25, Eamonn <eamonn.laffey at gmail.com>:> Sorry I’m having a hard time following what you mean. Do you mind posting

some example code?

Ah my bad. I’m pretty terrible at my C standard library stuff. I’ve only ever used #include . I’ll make sure to read up on fread(). Thanks.

Sik wrote:> Silly question, but do you even know how to access files with the

standard library? Because this isn’t any different. The issue would be
obvious if you know how e.g. fread() works.

For those who don’t like this beating around the bush, you’ll want to set
up a loop like this:

// Change this to GLchar or whatever
#define READ_TYPE char

#define READ_SIZE 1

// …
// setup SDL WRops, i used to variable ctx in my example
// …

READ_TYPE inp[READ_SIZE]; //
while ( (size_t amount_read = SDL_RWread( ctx, inp, sizeof(READ_TYPE),
READ_SIZE ) ) > 0 )
{
printf( “Read %i objects of size %i\n”, amount_read, sizeof(READ_TYPE) );
printf( “%c\n”, (char)inp );
// Append this character to a string or something.
}

// free ctx when done
// …

This reads 1 byte at a time, which means that if it fails, then you are at
the end - if you read more than one byte at a time, then you either need to
be able to guarantee the length of the file (unlikely for a shader), or you
need to check whether the output of SDL_RWread() returns less than the last
parameter of SDL_RWread (“maxnum”). I don’t know that you would get any
sort of performance hit for just reading 1 byte at a time, since it would
be done before you do any sort of rendering.

You can fiddle with the READ_SIZE to read more than 1 byte at once, but I
think reading 1 byte is a good practice.

I hope that helps,
-Alex

PS: Pallav’s answer is considered less correct only because it relies on
the operating system to judge the position of the EOF. I think it would
give you the correct number of most modern operating systems, though - I
have used it across windows and linux in the past, and it gave accurate
numbers.

Come to think of it, there is no reason why SDL_RWread() should read less
objects than is requested. That will only happen when the function hits
EOF, and that problem is already avoided. If there is an error, sitting in
a loop and calling SDL_RWread() is unlikely to help, since SDL_RWread()
return value doesn’t tell you the type of error anyway.On Fri, Oct 25, 2013 at 7:15 PM, Alex Barry <alex.barry at gmail.com> wrote:

For those who don’t like this beating around the bush, you’ll want to set
up a loop like this:

// Change this to GLchar or whatever
#define READ_TYPE char

#define READ_SIZE 1

// …
// setup SDL WRops, i used to variable ctx in my example
// …

READ_TYPE inp[READ_SIZE]; //
while ( (size_t amount_read = SDL_RWread( ctx, inp, sizeof(READ_TYPE),
READ_SIZE ) ) > 0 )
{
printf( “Read %i objects of size %i\n”, amount_read, sizeof(READ_TYPE) );
printf( “%c\n”, (char)inp );
// Append this character to a string or something.
}

// free ctx when done
// …

This reads 1 byte at a time, which means that if it fails, then you are at
the end - if you read more than one byte at a time, then you either need to
be able to guarantee the length of the file (unlikely for a shader), or you
need to check whether the output of SDL_RWread() returns less than the last
parameter of SDL_RWread (“maxnum”). I don’t know that you would get any
sort of performance hit for just reading 1 byte at a time, since it would
be done before you do any sort of rendering.

You can fiddle with the READ_SIZE to read more than 1 byte at once, but I
think reading 1 byte is a good practice.

I hope that helps,
-Alex

PS: Pallav’s answer is considered less correct only because it relies on
the operating system to judge the position of the EOF. I think it would
give you the correct number of most modern operating systems, though - I
have used it across windows and linux in the past, and it gave accurate
numbers.


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


Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

Message-ID:
<CAON8QHV3v5re0VMej_VJtCpTBYHtd9jPeGRibKudDLduHJRoDA at mail.gmail.com>
Content-Type: text/plain; charset=“iso-8859-1”

Come to think of it, there is no reason why SDL_RWread() should read less
objects than is requested. That will only happen when the function hits
EOF, and that problem is already avoided. If there is an error, sitting in
a loop and calling SDL_RWread() is unlikely to help, since SDL_RWread()
return value doesn’t tell you the type of error anyway.

I have an implementation of the “redirect io then spawn” idiom that I
just need to finish bug-fixing (some sort of segfault in a windows
function, mostly certain I’m issuing some bad inputs). Due to the
inherent unpredictabilities of any and all underlying APIs, it’s
impossible to guarantee that all transmitted data will go through when
you expect it, so I don’t even try. It reads what it can get, and
doesn’t worry about the rest. This is presumably even more true of the
SDL_net sockets implementation, since that involves the internet.

In short, failure to read everything requested cannot be counted on to
mean an error, because sometimes it honestly doesn’t mean that.> Date: Sat, 26 Oct 2013 15:22:25 +0530

From: Pallav Nawani
To: SDL Development List
Subject: Re: [SDL] SDL_RWops to load shader file