Archived

This topic is now archived and is closed to further replies.

Project Layout / Design questions

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

Ok I''m relatively beginner at programming and have a few questions. The first is easy. I have my main.cpp and then I know for my classes I split it into Enemy.h which holds the class declaration and then Enemy.cpp which holds all the code for the methods and data. Now obviously Enemy.cpp has to #include "Enemy.h" for the class declaration. But then for instance when you want to use the CEnemy class in main.cpp you include "Enemy.h" and not "Enemy.cpp". So how is the .cpp file included? As far as I can tell the main file only includes the declaration for the class, and not the cpp? Second question isn''t so much a question but just a problem, or a point that is a bit annoying I guess. I have completed Tetris, Pong and Space Invader clones. My first game was pong using SDL. I made an alright start, I had the paddles and the ball and had them moving and such. I then recieved a helpful post from someone to solve a problem. I had a look at the documentation and source for their pong clone and read some of the comments and basically worked through the whole source to see how they had done it. I won''t bother explaining the layout too much but main.cpp was pretty basic, and had a while loop which ran Game.Play(). Where the play method was the main loop. There was the CGame class which basically had all the general stuff, clearing the buffer, drawing text (score and such), accepting input. Game.cpp included "Paddle.h" and "Ball.h". Now, ever since I read that pong source, I always seem to lay out my project code like that. Whatever I try, I''m not happy with the design/layout of classes and files and I resort to having the CGame and then the same general code for each game. Is this a good thing? Having a standard way of laying things out? Or is it a bad thing, probably because it''s not very efficient/nice way of doing things? Should I try different ways for each game? Should I stick to what I know best? Any help is appreciated.

Share this post


Link to post
Share on other sites
As far as I was aware, the compiler goes through every CPP file and tries to compile it first. This is how I think it works, which in my mind would make the most sense. It should go through every cpp file and include the header in the file before it compiles the class declarations. When ever it see''s an include it will compile those headers, if they have already been defined then it will skip it. So it would make the most sence that it would only compile and link each cpp file only once, but since headers can be declared pretty much anywhere they need to have a restriction on how many times they can be compiled\linked. That''s how I think it works.

Well, with the structure question, it really depends what processes you''re program has. I may not be an expert on this, but what you''re trying to reduce is code that is used more then once in your program. In the example of a game.play, the game class has many functions aside from calculate, draw ... etc ... it houses and manipulates it''s own variables that are returned from the other classes. Something like that shouldn''t be in the main structure (because it causes clutter), and sticking it in a class is very useful.

Also game.play''s could be useful when you''re dealing with an interface for the menu also.
int main()
switch(menu.start())
{
case GAME_START: game.play(); break;
etc ...
}

I really havn''t taken the time to learn how win32 applications handle windows and stuff, so I may be wrong. But if you wanted to make it better, the game class could have 3 functions.

while(game.checkendgame())
{
game.processinput();
game.calculate();
game.draw();
}

Share this post


Link to post
Share on other sites