Sign in to follow this  
jakobnator

multiple .cpp files?

Recommended Posts

jakobnator    99
So I am just getting started in Game development, and I have been watching some dev marathons, and I noticed they have multiple .cpp files? I already know about header files and they are all incorparated into main.cpp but what is the point of multiple .cpp files? Can you add them all to the main.cpp file whats the difference between the .cpp and .h? Please explain in detail.

EDIT: Also came across another problem, in SDL for my game I am trying to organize my art into files so how do I load something in a file path because,
[code]
SDL_LoadBMP("BlackJack/Art/Table.png");
[/code]
is not working!

Share this post


Link to post
Share on other sites
nfries88    1154
[quote name='jakobnator' timestamp='1304133567' post='4804635']
So I am just getting started in Game development, and I have been watching some dev marathons, and I noticed they have multiple .cpp files?[/quote]
Yes. Generally, you'll have one or more related classes defined in a header file. When you implement the member functions and constructors of these classes, it is common to do so in a source (.cpp) file.

[quote]I already know about header files and they are all incorparated into main.cpp[/quote]
This is not necessary in the least.

[quote]but what is the point of multiple .cpp files? Can you add them all to the main.cpp file whats the difference between the .cpp and .h? Please explain in detail.
[/quote]
.cpp is a "source file" and is compiled and linked automatically by an IDE.
The point in having multiple is that it's easier to manage your code if it's split up in an orderly fashion.

Share this post


Link to post
Share on other sites
KulSeran    3267
[quote name='jakobnator' timestamp='1304133567' post='4804635']
So I am just getting started in Game development, and I have been watching some dev marathons, and I noticed they have multiple .cpp files? I already know about header files and they are all incorparated into main.cpp but what is the point of multiple .cpp files? Can you add them all to the main.cpp file whats the difference between the .cpp and .h? Please explain in detail.
[/quote]
[url="http://www.gamedev.net/page/resources/_/reference/programming/languages/c-and-c/organizing-code-files-in-c-and-c-r1798"]Mandatory reading.[/url]

You split up code into multiple cpp files to organize it. A common suggestion is to place one class per cpp file, or at the very least, give one purpose to each cpp file. Once you code is inside seperate compilation units, you have to provide enough forward declarations/prototypes to make each cpp file compile. That is where the headers come in. You put the declaration of each of your public interfaces into headers, and then include those headers in any cpp file that needs to be able to call those functions. There are some exceptions, like templates, that must be contained in full in your headers so that their full definition is available at point of use.

Share this post


Link to post
Share on other sites
nfries88    1154
[quote name='jakobnator' timestamp='1304133567' post='4804635']
EDIT: Also came across another problem, in SDL for my game I am trying to organize my art into files so how do I load something in a file path because,
[code]
SDL_LoadBMP("BlackJack/Art/Table.png");
[/code]
is not working!
[/quote]

SDL_LoadBMP is for bitmap (.bmp) files. use SDL_Image and IMG_Load for a generic image loading function.

Share this post


Link to post
Share on other sites
jakobnator    99
[quote name='nfries88' timestamp='1304135893' post='4804645']
[quote name='jakobnator' timestamp='1304133567' post='4804635']
EDIT: Also came across another problem, in SDL for my game I am trying to organize my art into files so how do I load something in a file path because,
[code]
SDL_LoadBMP("BlackJack/Art/Table.bmp");
[/code]
is not working!
[/quote]

SDL_LoadBMP is for bitmap (.bmp) files. use SDL_Image and IMG_Load for a generic image loading function.
[/quote]

Thats what I meant I had a predefined loading function so I just changed it to that for that, it was able to load .png files anyway, the problem is the file path what do I use if my art is in a folder inside of where the executable is because
[code]
("BlackJack/Art/Table.bmp");
[/code]
is not loading that file path.


Share this post


Link to post
Share on other sites
nfries88    1154
[quote name='jakobnator' timestamp='1304174569' post='4804757']
[quote name='nfries88' timestamp='1304135893' post='4804645']
[quote name='jakobnator' timestamp='1304133567' post='4804635']
EDIT: Also came across another problem, in SDL for my game I am trying to organize my art into files so how do I load something in a file path because,
[code]
SDL_LoadBMP("BlackJack/Art/Table.bmp");
[/code]
is not working!
[/quote]

SDL_LoadBMP is for bitmap (.bmp) files. use SDL_Image and IMG_Load for a generic image loading function.
[/quote]

Thats what I meant I had a predefined loading function so I just changed it to that for that, it was able to load .png files anyway, the problem is the file path what do I use if my art is in a folder inside of where the executable is because
[code]
("BlackJack/Art/Table.bmp");
[/code]
is not loading that file path.



[/quote]

all directories sent to files will be relative to your PATH environment variable (negligible for this) and your current execution directory, which is by default the same folder as your .exe.

So if your folder is in C:/My Code/BlackJack/, and Table.bmp is in C:/My Code/BlackJack/Art/; then what you need to send to your function is merely "Art/Table.bmp".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this