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!
#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.
#ifndef _HEADER_H
#define _HEADER_H
#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_image.h"
#include <windows.h>
constint screenx = 1024;
constint screeny = 768;
int titlebgx = 0;
constint 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.