Seperating Functions

Hello, I have had quite a bit of trouble trying to figure this out. When I started programming simple games I would have everything cluttered in one file but now I want to start spreading functions out to different files. Right now I have a main.cpp file that starts SDL for me and goes to my Title Screen function which is also in the same file. How can I transfer the Title Screen function to its own .cpp file and still have it functioning properly? I have tried transfering the function to its own file and declaring the function in main but my main issue is that I have header files like "sdl.h", "sdl_mixer.h" which are being used in both .cpp files so when I define them in both I cant compile. I know I cant do that but it was worth a shot, so now I have no idea on how to split them up. Any help would be appreciated.

Assuming those headers have the usual multiple-inclusion guards, then of course you can have them included by more than one .cpp file. That's the whole point of header files!
closed account (jwkNwA7f)
You would first create a header file(.h):
header.h:
void myFunction();
Then, you would put the code for it in the c++ source file(.cpp):
header.cpp:
1
2
3
4
void myFunction()
{
    // Code
}

And include it in your main file:
main.cpp:
1
2
3
4
5
6
#include "header.h" // Your header file

int main()
{
     myFunction();
}

Or, you can put the code for your function in the header file, also.
Last edited on
This is what I have done so far:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Header.h"

int main( int argc, char* args[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );
    screen = SDL_SetVideoMode( screenx, screeny, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN );
    SDL_WM_SetCaption( "Monitor Music", NULL );
    Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 );
	titlemusic = Mix_LoadMUS("Title Song.mp3");
	temp = IMG_Load( "title.jpg" );
	titlebg = SDL_DisplayFormat(temp);
    Mix_PlayMusic( titlemusic, -1 );
    while(!done)
    {
    	Title_Screen();
    		if (GetAsyncKeyState(VK_ESCAPE))
    		{
    			done = true;
    		}
	}
    CleanUp();
	return 0;  
}


Header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_image.h"
#include <windows.h>

const int screenx = 1024;
const int screeny = 768;
int titlebgx = 0;
const int titlebgy = 0;
bool done = false;

SDL_Surface* 	titlebg = NULL;
SDL_Surface* 	screen = NULL;
SDL_Surface*	temp = NULL;
Mix_Music *		titlemusic = NULL;

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}


Title.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "Header.h"

int Title_Screen()
{
	
	
	//Scroll background
        titlebgx -= 1;
        
        //If the background has gone too far
        if( titlebgx <= -titlebg->w )
        {
            //Reset the offset
            titlebgx = 0;
        }
        //Show the background
        apply_surface( titlebgx, titlebgy, titlebg, screen );
        apply_surface( titlebgx + titlebg->w, titlebgy, titlebg, screen );
        SDL_Flip( screen );
        SDL_Delay(100);
	return 0;
}

This is where I get my errors. I get issues with Dev C++ saying "multiple definitions of screen", "multiple definitions of titlebgx", etc. Basically is says I have multiple definitions of the variables that I have in Header.h but I dont know what other way of using them globally in both .cpp files.
closed account (jwkNwA7f)
I'm sorry. I forgot this:
Header.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#ifndef _HEADER_H
#define _HEADER_H

#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_image.h"
#include <windows.h>

const int screenx = 1024;
const int screeny = 768;
int titlebgx = 0;
const int titlebgy = 0;
bool done = false;

SDL_Surface* 	titlebg = NULL;
SDL_Surface* 	screen = NULL;
SDL_Surface*	temp = NULL;
Mix_Music *		titlemusic = NULL;

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

#endif 

Only put those in the header file.
You can read about these here:
http://www.cplusplus.com/doc/tutorial/preprocessor/
Go to Conditional inclusions (#ifdef, #ifndef, #if, #endif, #else and #elif) about half way down the page.

EDIT: forgot the closing code tag.
Last edited on
Topic archived. No new replies allowed.