Sign in to follow this  
willthiswork89

code orginization

Recommended Posts

i read the tutoail on here and its by far the worst tutorial i have ever read can somebody explain them? like just normal functions no structs or classes keep it as basic as it gets....i really wanna know what to do and how to use them, along with gettting another cpp file into the mix because so far ive always used 1 MAIN.cpp and nothing else.

Share this post


Link to post
Share on other sites
Normally you declare functions in the header file and the functions go into the cpp file.

Inside myheader.h

bool FunctionOne( const char* param );
void FunctionTwo( int x, int y );
int FunctionThree();


Inside the my.cpp

#include "myheader.h"
bool FunctionOne( const char* param )
{
}
...


So in a third .cpp file you can #include "myheader.h" and use those functions

Now, as to how you add them to your project it depends on your environment ( ie have to add the files to your project, Makefile, Jamfile etc )

Share this post


Link to post
Share on other sites
okay so i have the declaring header file, i have one cpp to say what they do, then i have one to use them? why couldnt i just define what the functions do inside the header? like

//header.h

void blit_image(int x, int y, SDL_Surface* screen, SDL_Surface* apply)
{
//define what to do and actually blit the stuff in the header
}

then

main.cpp

header::blit_image(0,0,screen,background);

see what i mean?

Share this post


Link to post
Share on other sites
You want to minimize the amount of data you have in the header because of the compile times involved. Since there is a potential of having a .h file included into several hundred different files a change in the header will force a rebuild of all those.

Think of the header as something like a 'table of contents' or 'reference'. Headers should just contain the declarations of things and not implementations.

Share this post


Link to post
Share on other sites
then why would i wanna make them? it really isnt saving me a messy code at all... the main thing im trying to do is minimize the ammount of clutter, i want a file to hold all my info on building my sdl screen, then a file to call like BuildSDL::init(); so that really i have an initializer code and a file with the clutter in it, maybe a couple files with clutter.

Share this post


Link to post
Share on other sites
Generally, it is true that you want only function declarations (not implementations) in your header files, but there are sometimes exceptions. If you have a very short function (like an accessor function, for example) it is sometimes more convenient to just implement the function in the header. Most of the time I try to stay away from this approach though.

One other thing to consider...I may be wrong about this, but if you implement a function inside the class declaration, doesn't the compiler treat that as an inline function even if you don't use the "inline" keyword? I may not be remembering the details of this correctly, but I think I remember reading something about that.

If you are accidentally inlining a bunch of functions (especially big functions), that can cause serious code bloat.

Another reason to keep your declarations separate from your implementations is that it makes it easier to look at the code and know what's going on. Sometimes you just want to look at the function's declaration (to remember it's return type and argument list) without being bogged down by seeing the implementation too.

Share this post


Link to post
Share on other sites
You don't have to. You can place everything into the .cpp file if you want. As soon as you start breaking out stuff into multiple files you will see the advantages of properly seperating stuff into headers and source files.

Share this post


Link to post
Share on other sites
Quote:
Original post by willthiswork89
okay so i have the declaring header file, i have one cpp to say what they do, then i have one to use them? why couldnt i just define what the functions do inside the header? like

//header.h

void blit_image(int x, int y, SDL_Surface* screen, SDL_Surface* apply)
{
//define what to do and actually blit the stuff in the header
}

then

main.cpp

header::blit_image(0,0,screen,background);

see what i mean?


yes i do.

but that isnt the way most people would do it. the way you're doing it the compiler sees the final code as one massive source file. so it has to recompile everything if you change one thing. this takes much longer than selective compilation.

it will also cause problems if you need to use the function in two other source files. theis will cause 2 copies of the code to be generated, which confuses the linker.

perhaps an example will illustrate:


your way first:

main.o
{
has symbol:
int main( int, char*[] )
void blit_image(int, int, SDL_Surface*, SDL_Surface*)

needs symbol:
// none

}



this entire thing gets compiled every time

okay say we add another file that needs blit_surface, dependant.o


main.o
{
has symbol:
int main( int, char*[] )
void blit_image(int, int, SDL_Surface*, SDL_Surface*)


needs symbol:
//none
}

dependant.o
{
has symbol:
void some_function_that_needs_blitting()
void blit_image(int, int, SDL_Surface*, SDL_Surface*)

needs symbol:
//none
}



this will confuse the linker because it finds 2 versions of blit_image, that take the same arguments. it doesnt check if they are the same, it will just complain.

the ideal way:

main.o
{
has symbol:
int main( int, char*[] )

needs symbol:
void blit_image(int, int, SDL_Surface*, SDL_Surface*)
}

// blit.cpp gets compiled seperatly.
// blit.h contains the symbol names
blit.o
{
has symbol:
void blit_image(int, int, SDL_Surface*, SDL_Surface*)

needs symbol:
SDL_BlitSurface( SDL_Rect*,SDL_Surface*,SDL_Rect*,SDL_Surface*)
}

dependant.o
{
has symbol:
void some_function_that_needs_blitting()

needs symbol:
void blit_image(int, int, SDL_Surface*, SDL_Surface*)
}



so the linker, while in main.o says "i need a blit_image function that takes a pair of ints and a pair of SDL_Surface pointers." it looks around and finds an exact match in blit.o; its happy and your appllication can run.

does this help?

Share this post


Link to post
Share on other sites
i see that your seperating the things but where in the code are you actually defining the insides of the blit? lets just do somthing simple like

main.cpp

i want to get the number output of 2 * 2

i know i can get it easily througn main but for reason we will say we need to make a header to do this

would i do

multiply::outcome(2);

in main or would i do

multiply:: int outcome(int outcome)
{
outcome = outcome * 2;
return outcome;
}

im probably just confusing you but really what i wanna know is how could i use a header to find 2 * 2 and allow main.cpp use the function from the header to get the answer?
multiply.h

Share this post


Link to post
Share on other sites
main.cpp:

#include "multiply.h"

int main()
{
int result = my_square(2);
...
return 0;
}




multiply.h:

int my_square(int value);




multiply.cpp:

#include "multiply.h"

int my_square(int value)
{
return value * value;
}




Thats the bottom line I think.
As said earlier you must add both main.cpp and multiply.cpp to your project.
Some IDE's will automatically add main.cpp for you, but I believe Dev-C++ will not.

Share this post


Link to post
Share on other sites
alright heres what i got from after reading your posts please tell me if i did somthing wrong or somthing needs to be added. it compiled fine though

//applyimage.h

#include<SDL/SDL.h>
#include<string>
using namespace std;

#define SWIDTH 640
#define SHEIGHT 480
#define BPP 32

SDL_Surface *load_image(string filename);

void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination);



theres my applyimage.h defining my functions and defines.


//applyimage.cpp

#include <SDL/SDL.h>
#include <string>

using namespace std;


SDL_Surface *load_image(string filename)
{
SDL_Surface *loadedimage = NULL;
SDL_Surface *changedbpp = NULL;


loadedimage = SDL_LoadBMP(filename.c_str());


if(loadedimage != NULL)
{
changedbpp = SDL_DisplayFormat(loadedimage);

SDL_FreeSurface(loadedimage);
}
return changedbpp;
}

void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination)
{
SDL_Rect ofs;

ofs.x = x;
ofs.y = y;

SDL_BlitSurface(source,NULL,destination,&ofs);
}





there is the actual definition of the applyimage.h


//main.cpp
#include "applyimage.h"
#include <SDL/SDL.h>



SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

int main(int argc, char* args[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 1;
}

screen = SDL_SetVideoMode(SWIDTH,SHEIGHT,BPP,SDL_SWSURFACE);

if(screen == NULL)
{
return 1;
}

SDL_WM_SetCaption("Blitting Here!", NULL);

message = load_image("hello_world.bmp");
background = load_image("background.bmp");

apply_surface(0,0,background,screen);

apply_surface(180,140,message,screen);

if(SDL_Flip(screen) == -1)
{
return 1;
}

SDL_Delay(10000);

SDL_FreeSurface(message);
SDL_FreeSurface(background);

SDL_Quit();
}



i know main isnt made to handle events i know how to do that but for simplicity's sake i kept it as simple as i could...

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