multiple .cpp files?

Started by
5 comments, last by nfries88 12 years, 11 months ago
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,

SDL_LoadBMP("BlackJack/Art/Table.png");

is not working!
Advertisement

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?

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.

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

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.
I personally like to keep some parts of the program separated from the main program so provide easier readability. I don't know what the professional coders do it for, or what it's called, though.

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.

Mandatory reading.

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.

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,

SDL_LoadBMP("BlackJack/Art/Table.png");

is not working!


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

[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,

SDL_LoadBMP("BlackJack/Art/Table.bmp");

is not working!


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

("BlackJack/Art/Table.bmp");

is not loading that file path.



[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,

SDL_LoadBMP("BlackJack/Art/Table.bmp");

is not working!


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

("BlackJack/Art/Table.bmp");

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".

This topic is closed to new replies.

Advertisement