Jump to content
  • Advertisement
Sign in to follow this  
zyrolasting

Auto switch header definitions (C++)

This topic is 3614 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

Given the practice of using #ifdef _DEBUG to choose between debug or runtime headers for DirectX, how can I make my own framework to decide what header to put in? Is it safe to put preprocessor directives under simple if/else statements? What I want to do is have my game engine project include headers with classes of unique objects familiar to a single game. One such header could have definitions of a television or stereo object for a first person game, or a definition of an asteroid or ship in a ship shooter game. All I'm asking really is the best practice or method to do this. I do not have access to my compiler at the moment, which explains why I didn't just try the above. I never have seen anyone place directives under simple conditions, so I would like to know how that kind of thing is structured should that be the appropriate practice.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by zyrolasting
Given the practice of using #ifdef _DEBUG to choose between debug or runtime headers for DirectX, how can I make my own framework to decide what header to put in? Is it safe to put preprocessor directives under simple if/else statements? What I want to do is have my game engine project include headers with classes of unique objects familiar to a single game. One such header could have definitions of a television or stereo object for a first person game, or a definition of an asteroid or ship in a ship shooter game.

That's just a really bad idea.

Have a set of routines - low-level routines - that can conceivably be shared by both a first-person shooter and a side-scrolling shoot-em-up, things like device enumeration, mode initialization, etc. Then, on top of that, build the frameworks appropriate for your different types of games. Finally, in your client code for your actual game, include the framework header that defines the constructs needed for your game.

Don't use conditional inclusion/compilation as a substitute for proper interface design.



All that said, if you do find a case where you genuinely nee conditional behavior - and this is NOT one of them - then here's how you do it. In your header:
#ifdef SENTINEL_FLAG
// behavior 1
#else
// behavior 2
#endif

This block can occur inside of a function, or inside of a class declaration, or at file scope - anywhere either of behavior 1 or 2 can occur.

In your client code, simply #define SENTINEL_FLAG before you include your header and behavior 1 will result. Leave SENTINEL_FLAG undefined, or explicitly #undef it, and behavior 2 will result. Note that symbol definitions in source code are global to the translation unit only. If you want project-wide symbols, you'll need to define them as compiler inputs.

Share this post


Link to post
Share on other sites
No need to bite my head off about it.

Alright, I should have clarified that I already have base classes for game objects. By the way, if you re-read my OP you should see that I mentioned I already have an engine. Device enumeration and mode initialization has already been taken care of. If that wasn't clear enough, well, sorry.

Where my idea came in was when everything was set up, and all I needed was derivatives to specific objects, and by objects, I mean entities in game such as a basketball or a tree. Of course, those objects were taken care of already. I already have a class of the basics such as position, direction and mesh.

I'm talking about the ones that are really unique. Referring back to my original post, you wouldn't see a stereo or a television in a space shooter game for the most part. I'm thinking such objects could have members of their own for unique interactivity.

As I type this, I had a new idea to generalize the objects. Thanks for the rest of the info, anyway.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!