Sign in to follow this  

SDL main?

This topic is 4673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering what the arguments in the SDL main mean?, i dont ussually like using stuff i dont understand, and the main looks like this int main(int argc, char *argv[]) I know its a small thing but i would just like to know :)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
its just standart entry point for console program, you can find description of it in the MSDN, argc - number of input command line arguments, arv - arguments themselves

Share this post


Link to post
Share on other sites
just check MSDN. i thought argument and parameter are synonyms. entry point is the part of the program from which it starts. console program .. well dunno how do describe it.. its just console program

Share this post


Link to post
Share on other sites
Man will this ever stop!!? Whats a synonym!?, i looked it up on google, but i couldnt find anything! oh wait never mind, it a word with the same or near the same meaning of the another.

Share this post


Link to post
Share on other sites
K, i got one more question, why does a SDL program need this in the main function? And why would these parameters be usefull? could someone tell me in simple words? lol, i dont have a very good vocabulary.

Share this post


Link to post
Share on other sites
its not SDL. Every Windows program should have either WinMain or main functions which are entry points for executables.

Share this post


Link to post
Share on other sites
Quote:
Original post by X3
its not SDL. Every Windows program should have either WinMain or main functions which are entry points for executables.


Actually it kind of is SDL. The way SDL works is with you declaring your own main() function which it then uses #defines to overwrite with its own so it can initialise your application correctly. Because of the fickleness of #define you have to use the correct declaration otherwise it won't work. You can't for instance have int main() or void main() you need the full declaration int main(int argc, char **argv)

As for dustydoodoo ... how long have you been programming? I suggest you go look up some c/c++ tutorials on google befor launching into SDL if you've never seen main() befor.

Share this post


Link to post
Share on other sites
LOL! i have seen main() but ive never seen "int argc, char *argv[]" in the main functions arguments.

Share this post


Link to post
Share on other sites
Ok well for a console application (an app which has no gui - just uses text output) main accepts those arguments. its basically just a list of everything passed in on the command line.

So if you run your program like this:

myapp.exe -p something.txt

you'll have "myapp.exe" "-p" and "something.txt" as your command line arguments. argc says how many there are (in this case its 3), and argv is the char arrays of each string. This is the full and proper declaration of main - and IMHO should always be used.

As for the entry point: its the part of your program that starts running first - so when you run your program it'll start at the beginning of main.

Share this post


Link to post
Share on other sites
They are command line arguments used when you run the actual program, just like kaysik said. Here's a small demo I put together to test them:

#include <iostream>

int main ( int argc, char *argv[] )
{
// Check if there is only one program argument
if(argc == 2){ // 2 because 1 is the program name
// Display the output
std::cout << "Argument: " << argv[1] << std::endl;
std::cin.get();
} else {
// Display an error using the program name
std::cout<<"Usage: "<< argv[0] <<" <output>\n";
std::cin.get();
}
}

Share this post


Link to post
Share on other sites
hey thanks for all the info, but the one thing i dont get is why would you need this? esspetially(dont know how to spell) when you dont use the command line? because this isnt in one of those dos windows.

Share this post


Link to post
Share on other sites
Quote:
Original post by dustydoodoo
hey thanks for all the info, but the one thing i dont get is why would you need this? esspetially(dont know how to spell) when you dont use the command line? because this isnt in one of those dos windows.


You might not be using it, but trust me when I say lots of other people do. Part of a good game design is to use some sort of Data Driven design. What that means is rathet than hard code in all your runtime settings, such as the width, height, etc.. you just load it from a file. However, in itself, that would still require hard coding in a filename to load. To get around that, you could juse let the user call your program with a parameter that contains the file to load, which would make it nearly all data driven!

Share this post


Link to post
Share on other sites
it also alow u to drop flies in like for editors and compliers
it very powerful not just for dos programs it so important to programmers that
microsoft went ahead and add it to there main()(aka-WinMain(...))
in the WinMain(...) it should be the last field

Share this post


Link to post
Share on other sites
Please learn to use a dictionary instead of asking what every single word means.

As for command-line arguments: you may find them useful for avoiding hard-coding the names of resource files into your app. For example, if you have some SDL demo app and it draws a picture "foo.png", then instead of having that name in your program, you could set up the main() to read the file name as argv[1].

(By the way, the names of those parameters are not special; you can call them what you like, just like for any other function - what's important are the types: int and char*[]. The names 'argc' and 'argv' are just traditional, and meant to give you the warm fuzzy feeling that comes from following in the footsteps of 80s-era C hackers.)

Then, you could have people run your program from the command line, or spiffier yet, create a batch file, like:


rem this is file 'mydemo.bat'
rem on the next line, we put the command line that gets fed to DOS
@mydemo.exe foo.png
rem the '@' tells the terminal window not to echo the command line.
rem 'mydemo.exe' should of course be replaced with the name of your program
rem and 'foo.png' with the filename for the picture to display.


Then when your code runs, it passes argv[1] to the code that opens the file, and voila. Now, if you decide to change the name of the file, you don't have to go hunting down the hard-coded name in your source, but just fix it in the batch file - and you also don't have to recompile, and you also offer users the option to use their own pictures instead.

Of course, this is all Windows-specific with the .bat file stuff, but there are equivalents on other platforms.

Share this post


Link to post
Share on other sites
Thanks even more! and im sorry about asking all the meanings of all the words! my auntie always gets mad at me for asking so many questions, lol

Share this post


Link to post
Share on other sites
Sign in to follow this