SDL

Started by
5 comments, last by KindFred 19 years, 4 months ago
Hi all, I started learning SDL from cone3d.gamedev.com, but I encountered a little problem. First of all the printf() function is always used(which I thought was a c function), but that is ok cause it is not THAT hard to get. But then in tutorial nb 3 where you start learning about sprites I get this sort of stuff: printf("ERROR opening file %s\n\n", filename); and fgets(buffer, 255, fp); sscanf(buffer, "FILES: %d", &mNumframes); sscanf(buffer, "%s %d %d %d %d", name, &pause, &r, &g, &b); :( I never enctountered anything of this style during the gametutorials.com c++ tutorials. Anyone can help me out here? If I am correct these principals are basic c, but I never learned c cause I went c++ straight. It should be possible to do the same things with other commands in c++ I guess, but I just can't really figure out whaqt EXACTLY happens.
-----------------------------Sismondi GamesStarted c++ in October 2004...
Advertisement
SDL is a liberary for C/C++
So printf (and it's variants) can be used with it.
And if you learn C++ you also learn C, you just have more options.
Any C command can be used in C++.

Let's go over the commands shall we?

===
printf("ERROR opening file %s\n\n", filename);

Let's say filename is a null terminated char array containing "A File.txt"

Then the output shall be:
"ERROR opening file A File.txt

"

===
fgets(buffer, 255, fp);
buffer is a char array, mostlikly with 255 items.
fp is a file pointer.
fgets reads 255 characters from the file pointed to by fp and puts it into buffer.

===
sscanf(buffer, "FILES: %d", &mNumframes);

sscanf scans the char array (buffer) for a particular patern.
In this case it looks if it finds the word: "FILES: " and what is put behind is read till \0 or \n, converted to a double and placed into "mNumframes"

Also valid would be this:
fscanf(fp, "FILES: %d", &mNumframes);

But I think the sscanf is used so that it can control (better) where the file pointer is at the time.

===
sscanf(buffer, "%s %d %d %d %d", name, &pause, &r, &g, &b);

This is very similar to above.
it first reads a string untill it encounters a space and puts it into name.
Then it does the same (reading untill a space is encounterd) but makes a double of it and puts it into pause.
And the same for r,g and b.


A more detailed explanation can be found in MSDN.

I hope this helps.
wow man thank you very much.
I just want to now if I could have done all that using the "cout". cause I never used print() or any of those commands stated above.

P.S. you were really helpfull, rated you up :D
-----------------------------Sismondi GamesStarted c++ in October 2004...
One quick correction to mldaalder's post:

%d is not a double, it's an int.

%e is a double.

All the %_ values you see up there are called format strings. A google search for "C format strings" will yield much info.

Quote:Original post by vHaB
One quick correction to mldaalder's post:

%d is not a double, it's an int.

%e is a double.

Then what's the i I've been using for my ints???


Well, I just tested this, and the i works for ints as well.
And d doesn't work for doubles like 12.01.
So your right about that.


Quote:Original post by Joshnathan
I just want to now if I could have done all that using the "cout". cause I never used print() or any of those commands stated above.

I would think so...
But when I use Python in my projects, the printf functions only seem to work.
I dislike how cone3d's tutorials are set up. Loading numbered bitmaps into an array to do sprites is horrible. The better way would be to load in a sprite sheet and then blit through each one as needed. I'll make a tutorial about it eventually. The next one I write will be user input in SDL. After that I'll probably get into either sprites or text output, we'll see. Stay in touch though.
Rob Loach [Website] [Projects] [Contact]
Quote:
Then what's the i I've been using for my ints???


that can also be used for ints, but %d is more commonly used.

also %i and %d are signed ints, where %u would be used for unsigned ints. There are more string format specifiers than you could shake a stick at.

The few I know are:
%c char
%s string
%x or %X hexadecimal, if lowercase 'x' is used a-f will be displayed. Uppercase 'X' will display A-F.
%p will display a pointer.

Also, from what I remember you can also specify field width and length. try experimenting with ints by using %#.#d, where '#' is a literal integer.

Have fun!

This topic is closed to new replies.

Advertisement