I finally got SDL up and running with Microsoft Visual Studio. Yes! In about an hour of messing around, I was able to render a window of a certain size and one static gif image. The gif image happens to be a Charmander =). Anyways, I am definitely foreseeing that many of the processes needed to draw images, initialize SDL systems, etc. could be placed in an external header/implementation file to be called from the main driver. I was wondering if anyone has done this or could guide me in the right direction of how/what to place a lot of the initialization logic into a separate file so that my int main(...) can be as short as possible? I may be way off base with this question, and if that is the case please let me know as well.
Thanks,
Jarrod
Initializing SDL Efficiently
Moderator: PC Supremacists
- JarrodParkes
- ES Beta Backer
- Posts: 325
- Joined: Thu Feb 25, 2010 2:39 pm
- Current Project: Yes, iPhone Application(s)
- Favorite Gaming Platforms: N64, PC
- Programming Language of Choice: C++
- Location: Mountain View, CA
- Contact:
Initializing SDL Efficiently
Jarrod Parkes
Indie Game Development
Email: parkesfjarrod@gmail.com
Portfolio: http://jarrodparkes.com
Twitter: http://www.twitter.com/jarrodparkes
Facebook: http://www.facebook.com/jarrodparkes
Indie Game Development
Email: parkesfjarrod@gmail.com
Portfolio: http://jarrodparkes.com
Twitter: http://www.twitter.com/jarrodparkes
Facebook: http://www.facebook.com/jarrodparkes
- JarrodParkes
- ES Beta Backer
- Posts: 325
- Joined: Thu Feb 25, 2010 2:39 pm
- Current Project: Yes, iPhone Application(s)
- Favorite Gaming Platforms: N64, PC
- Programming Language of Choice: C++
- Location: Mountain View, CA
- Contact:
Re: Initializing SDL Efficiently
Also, I have been going through several beginner SDL tutorials, and I have noticed some differences in conventions for where things are declared, etc. These were the ones I have observed, and I was wondering if there is a most efficient way or if it is a matter of opinion:
1.) some of the tutorials show SDL_Event event being a global, whereas others declare the SDL_Event inside of main.
2.) should SDL_Surfaces be global or not?
3.) is for(;;) and while(1) exactly the same?
I apologize in ahead for the greenness of my questions, but I'm hoping someone can help me out with this confusion as well as the one in the first post.
Thanks,
Jarrod
1.) some of the tutorials show SDL_Event event being a global, whereas others declare the SDL_Event inside of main.
2.) should SDL_Surfaces be global or not?
3.) is for(;;) and while(1) exactly the same?
I apologize in ahead for the greenness of my questions, but I'm hoping someone can help me out with this confusion as well as the one in the first post.
Thanks,
Jarrod
Jarrod Parkes
Indie Game Development
Email: parkesfjarrod@gmail.com
Portfolio: http://jarrodparkes.com
Twitter: http://www.twitter.com/jarrodparkes
Facebook: http://www.facebook.com/jarrodparkes
Indie Game Development
Email: parkesfjarrod@gmail.com
Portfolio: http://jarrodparkes.com
Twitter: http://www.twitter.com/jarrodparkes
Facebook: http://www.facebook.com/jarrodparkes
Re: Initializing SDL Efficiently
Well, what I BELIEVE you mean to say, is putting the initialization code into it's own separate function, and yes that's a great idea, which most people use.
So you can just make a header file and declare a function like: "bool Init();", then in the .c/.cpp file you can define it, putting in all the initialization code, and returning false if there's an error. Alternatively, you can return an int/char with different values corresponding to different errors... Or just use SDL_GetError().
But anyways, I'll try to answer some of your other questions:
1) When the instance of SDL_Event is made global, that allows other files/functions to have access to it. If you declare it in main, anything outside of main would only have access if you passed a pointer to that SDL_Event instance.
2) Usually, it's deemed 'bad style' to declare things globally. But depending on what needs access to that surface, you might opt to make it global.
3) for(;;) and while(1) are the same, I believe. But if your going to use one of these for your game loop, I'd consider making a boolean to know whether the user wants to quit or not.
I did this all of the top of my head, so it may not exactly be right, but I'm quite sure that's almost exactly how LazyFoo has it set up in his tutorials.
But in this way, you can create a header that has all the useful functions you use, and define them in a separate .c/.cpp file.
I hope that helped!
So you can just make a header file and declare a function like: "bool Init();", then in the .c/.cpp file you can define it, putting in all the initialization code, and returning false if there's an error. Alternatively, you can return an int/char with different values corresponding to different errors... Or just use SDL_GetError().
But anyways, I'll try to answer some of your other questions:
1) When the instance of SDL_Event is made global, that allows other files/functions to have access to it. If you declare it in main, anything outside of main would only have access if you passed a pointer to that SDL_Event instance.
2) Usually, it's deemed 'bad style' to declare things globally. But depending on what needs access to that surface, you might opt to make it global.
3) for(;;) and while(1) are the same, I believe. But if your going to use one of these for your game loop, I'd consider making a boolean to know whether the user wants to quit or not.
Code: Select all
int main(int args, char* argv[])
{
if(!Init() || !LoadFiles())
{
return -1;
}
bool quit = false;
while(!quit)
{
if(SDL_PollEvent(&event)
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
Update();
Render();
SDL_Flip(screen);
}
CleanUp();
return 0;
}
But in this way, you can create a header that has all the useful functions you use, and define them in a separate .c/.cpp file.
I hope that helped!
- JarrodParkes
- ES Beta Backer
- Posts: 325
- Joined: Thu Feb 25, 2010 2:39 pm
- Current Project: Yes, iPhone Application(s)
- Favorite Gaming Platforms: N64, PC
- Programming Language of Choice: C++
- Location: Mountain View, CA
- Contact:
Re: Initializing SDL Efficiently
Thanks for the response, it actually helps answer a couple questions. After talking with some other friends and searching around, I am in the process of creating a class to handle the system setup that will be probably instantiated and initialized within the bool Init() function...wherever it be located. Thanks again for the help and I hope to have some good news posted quickly.
Jarrod Parkes
Indie Game Development
Email: parkesfjarrod@gmail.com
Portfolio: http://jarrodparkes.com
Twitter: http://www.twitter.com/jarrodparkes
Facebook: http://www.facebook.com/jarrodparkes
Indie Game Development
Email: parkesfjarrod@gmail.com
Portfolio: http://jarrodparkes.com
Twitter: http://www.twitter.com/jarrodparkes
Facebook: http://www.facebook.com/jarrodparkes
Re: Initializing SDL Efficiently
Happy to help =D.JarrodParkes wrote:Thanks for the response, it actually helps answer a couple questions. After talking with some other friends and searching around, I am in the process of creating a class to handle the system setup that will be probably instantiated and initialized within the bool Init() function...wherever it be located. Thanks again for the help and I hope to have some good news posted quickly.
Yeah, speaking of all this, I need to fix up some stuff in my own SDL_Framework haha
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Initializing SDL Efficiently
XianForce really answered your first question nicely. You should break your SDL initialization into its own function. And as he did, it would be pretty nifty for it to return a boolean (and your main() can check to see if everything went well).
2) Like I said before, they can be. They shouldn't be, though. It's kind of a shitty design decision. If you have an SDL surface for something like a player, enemy, or rectangle, then put the surface into the respective class for the entity.
3) What they do is the exact same. How they do it is slightly different. while(1) evaluates the expression it is given for a boolean value. 1 evaluates to true when it is cast, so the loop continues. The for(;;) loop is not actually "evaluating" anything for truth, it's just doing nothing but repeating. So at a low-level, for(;;) is moleculary more efficient than while(1) (if you want to be picky).
1) It's really a design decision. First of all, globals are generally frowned upon in the programming world. A more "respectable" way to handle the scenario would be to put the SDL_Event in your GetInput() function, or to have an InputManager class that it resides within.JarrodParkes wrote:1.) some of the tutorials show SDL_Event event being a global, whereas others declare the SDL_Event inside of main.
2.) should SDL_Surfaces be global or not?
3.) is for(;;) and while(1) exactly the same?
2) Like I said before, they can be. They shouldn't be, though. It's kind of a shitty design decision. If you have an SDL surface for something like a player, enemy, or rectangle, then put the surface into the respective class for the entity.
3) What they do is the exact same. How they do it is slightly different. while(1) evaluates the expression it is given for a boolean value. 1 evaluates to true when it is cast, so the loop continues. The for(;;) loop is not actually "evaluating" anything for truth, it's just doing nothing but repeating. So at a low-level, for(;;) is moleculary more efficient than while(1) (if you want to be picky).
Re: Initializing SDL Efficiently
huh, that's interesting.GyroVorbis wrote: 3) What they do is the exact same. How they do it is slightly different. while(1) evaluates the expression it is given for a boolean value. 1 evaluates to true when it is cast, so the loop continues. The for(;;) loop is not actually "evaluating" anything for truth, it's just doing nothing but repeating. So at a low-level, for(;;) is moleculary more efficient than while(1) (if you want to be picky).