How to include a header to multiple cpp files?

Hi, I've been working on this for a while now and can't seem to figure it out. I trying to recreate a mario level, so I have main.cpp, a Mario class, a Goomba class, and a Koopa class, and I need them all to have access to Initializer.h, and main to have access to all the classes too. My current solution is to have Koopa include Initializer, Goomba inlcude Koopa, Mario include Goomba, and main include Mario, in hopes that that would fix my problem of multiple redefinition errors. Here's the code

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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "Mario.h"
#include "Clock.h"

//screen width = 920
//screen height = 600
//screen bpp = 32

//frames per second = 10

//level width = 13216
//level height = 1770

SDL_Event event;

void clean_up()
{
    SDL_FreeSurface( mario );

    SDL_Quit();
}

int main( int argc, char* args[] )
{
    bool quit = false;

    if( init() == false )
    {
        return 1;
    }

    if( load_files() == false )
    {
        return 1;
    }

	Clock fps;

	Mario mario;

	set_clips();

    while( quit == false )
    {
		fps.start();

        while( SDL_PollEvent( &event ) )
        {
			mario.handle_events( event );

            if (event.type == SDL_QUIT)
			{
				quit = true;
			}
        }

		mario.move();
		
		mario.set_camera();
		
		SDL_Rect camera = mario.getCamera();

		apply_surface (0, 0, background, screen, &camera);

		//apply_surface(500, 726, bricks, screen, &brick);

		mario.show();

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

		if (fps.get_ticks() < 1000 / 10)
		{
			SDL_Delay( (1000 / 10) - fps.get_ticks() );
		}
    }

    clean_up();

    return 0;
}


Mario.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "Mario.h"
#include "Goomba.h"


//right = 0
//left = 1

//stand = 0
//run = 1
//jump = 2

Mario::Mario()
{
	xOffSet = 0;
	yOffSet = 726;

	xVelocity = 0;
	yVelocity = 0;

	direction = 0;

	status = 0;

	frame = 0;
}

void Mario::handle_events( SDL_Event event )
{
	if (event.type == SDL_KEYDOWN)
	{
		if (event.key.keysym.sym == SDLK_RIGHT)
		{
			xVelocity += 64 / 4;
				frame = 1;
		}
		else if (event.key.keysym.sym == SDLK_LEFT)
		{
			xVelocity -= 64 / 4;
			frame = 1;
		}
		else if (event.key.keysym.sym == SDLK_SPACE)
		{
			void jump();

			frame = 4;
		}
	}
	else if (event.type == SDL_KEYUP)
	{
		if (event.key.keysym.sym == SDLK_RIGHT)
		{
			xVelocity -= 64 / 4;
			frame = 0;
		}
		else if (event.key.keysym.sym == SDLK_LEFT)
		{
			xVelocity += 64 / 4;
			frame = 0;
		}
	}
}

void Mario::move()
{
	xOffSet += xVelocity;

	if ((xOffSet < 0) || (xOffSet + 64 > 13216) )
	{
		xOffSet -= xVelocity;
	}

	yOffSet += yVelocity;
}

void Mario::jump()
{
	int h;

	for (int i = 0; i < 4; i++)
	{
		h = i*16;
		yVelocity = h;
	}
}

void Mario::set_camera()
{
	camera.x =  ( xOffSet + 64 / 2 ) - 960 / 4;
    camera.y = 600 / 2 - 16;
	camera.h = 600;
	camera.w = 960;
   
    if( camera.x < 0 )
    {
        camera.x = 0;    
    }
    if( camera.y < 0 )
    {
        camera.y = 0;    
    }
    if( camera.x > 13216 - camera.w )
    {
        camera.x = 13216 - camera.w;    
    }
    if( camera.y > 1770 - camera.h )
    {
        camera.y = 1770 - camera.h;    
    }    
}

void Mario::show()
{
	if (xVelocity < 0)
	{
		direction = 1;

		status = 1;

		if (frame == 0)
			frame = 1;

		if (frame == 1)
		{
			frame = 2;
			prevFrame = 1;
		}
		else if ((frame == 2 ) && (prevFrame == 1))
			frame = 3;
		else if ((frame == 2 ) && (prevFrame == 3))
			frame = 1;
		else if (frame == 3)
		{
			frame = 2;
			prevFrame = 3;
		}
	}
	else if (xVelocity > 0)
	{
		direction = 0;

		status = 1;

		if (frame == 0)
			frame = 1;

		if (frame == 1)
		{
			frame = 2;
			prevFrame = 1;
		}
		else if ((frame == 2 ) && (prevFrame == 1))
			frame = 3;
		else if ((frame == 2 ) && (prevFrame == 3))
			frame = 1;
		else if (frame == 3)
		{
			frame = 2;
			prevFrame = 3;
		}
	}

	if (yVelocity > 0)
	{
		status = 2;

		frame = 4;
	}

	if ((xVelocity == 0) && (yVelocity == 0) )
	{
		status = 0;

		frame = 0;
	}

	if (direction == 0)
	{
		if (status == 1)
		{
		apply_surface(xOffSet - camera.x, yOffSet - camera.y, mario, screen, 
			&marioRight[frame]);
		}
		if (status == 2)
		{
			apply_surface(xOffSet -camera.x, yOffSet - camera.y, mario, screen, 
				&marioRight[frame]);
		}
		else if (status == 0)
		{
			apply_surface(xOffSet - camera.x, yOffSet - camera.y, mario, screen, 
				&marioRight[frame]);
		}
	}
	else if (direction == 1)
	{
		if (status == 1)
		{
		apply_surface(xOffSet - camera.x, yOffSet - camera.y, mario, screen, 
			&marioLeft[frame]);
		}
		else if (status == 2)
		{
			apply_surface(xOffSet - camera.x, yOffSet - camera.y, mario, screen, 
				&marioLeft[frame]);
		}
		else if (status == 0)
		{
			apply_surface(xOffSet - camera.x, yOffSet - camera.y, mario, screen, 
				&marioLeft[frame]);
		}
	}
}

SDL_Rect Mario::getCamera()
{
	return camera;
}


Mario.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
33
34
35
36
#pragma once
#include "SDL.h"

class Mario
{
private:
	int xOffSet;
	int yOffSet;

	int xVelocity;
	int yVelocity;

	int frame;

	int prevFrame;

	int status;

	int direction;

	SDL_Rect camera;
public:
	Mario();

	void handle_events( SDL_Event );

	void move();

	void show();

	void jump();

	void set_camera();

	SDL_Rect getCamera();
};


Goomba.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "Goomba.h"
#include "Koopa.h"

//goomba dead = 0
//goomba live = 1

Goomba::Goomba(int x, int y)
{
	xOffSet = x;
	yOffSet = y;

	xVelocity = 16;
	yVelocity = 16;

	status = 1;

	frame = 0;
}

void Goomba::move()
{
	xOffSet += xVelocity;

	if ((xOffSet < 0) || (xOffSet + 64 > 13216) )
	{
		xVelocity =  xVelocity * -1;
	}
}

void Goomba::show()	
{
	if (status == 1)
	{
		frame++;

		if (frame >= 2)
		{
			frame = 0;
		}
	}

	else 
	{
		frame = 2;

		xVelocity = 0;
	}

	apply_surface(xOffSet, yOffSet, monsters, screen, &goomba[frame] );
}


Goomba.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
class Goomba
{
private:

	int xOffSet;
	int yOffSet;

	int xVelocity;
	int yVelocity;

	int frame;

	//moving or not
	int status;

public:
	Goomba(int, int);

	void move();

	void show();
};


Koopa.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "Koopa.h"
#include "Initializer.h"

//dead = 0
//live = 1

//left = 0
//right = 1

Koopa::Koopa(int x, int y)
{
	xOffSet = x;
	yOffSet = y;

	xVelocity = -16;
	yVelocity = -16;

	status = 1;

	direction = 0;

	frame = 0;
}

void Koopa::move()
{
	xOffSet += xVelocity;

	if ((xOffSet < 0) || (xOffSet + 64 > 13216) )
	{
		xVelocity =  xVelocity * -1;
	}
}

void Koopa::show()
{
	if (status == 1)
	{
		if (xVelocity < 0)
		{
			direction = 0;

			frame++;
		}

		else if (xVelocity > 0)
		{
			direction = 1;

			frame ++;
		}

		if (frame >= 2)
		{
			frame = 0;
		}
	}

	else 
	{
		frame = 2;

		xVelocity = 0;
	}

	if (direction == 0)
	{
		apply_surface(xOffSet, yOffSet, monsters, screen, &koopaLeft[frame] );
	}
	else if (direction == 1)
	{
		apply_surface(xOffSet, yOffSet, monsters, screen, &koopaRight[frame] );
	}
}


Koopa.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
class Koopa
{
private:
	int xOffSet;
	int yOffSet;

	int xVelocity;
	int yVelocity;

	int frame;

	//moving or not
	int status;

	int direction;
public:
	Koopa(int, int);

	void move();

	void show();
};
Last edited on
And here's Initializer.h

Initializer.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#pragma once
#include "SDL.h"
#include "SDL_image.h"
#include <string>
#include "SDL_ttf.h"
#include "SDL_mixer.h"
#include <sstream>

SDL_Rect marioRight[5];
SDL_Rect marioLeft[5];

SDL_Rect ground;
SDL_Rect brick;
SDL_Rect step;
SDL_Rect mystery[2];

SDL_Rect goomba[3];

SDL_Rect koopaLeft[2];
SDL_Rect koopaRight[2];

SDL_Rect smallPipe;
SDL_Rect medPipe;
SDL_Rect largePipe;

SDL_Surface *mario = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *bricks = NULL;
SDL_Surface *monsters = NULL;

SDL_Surface *load_image( std::string filename )
{
    SDL_Surface* loadedImage = NULL;

    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load( filename.c_str() );

    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );

        SDL_FreeSurface( loadedImage );

        if( optimizedImage != NULL )
        {
			Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF);

            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey);
        }
    }

    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination,
	SDL_Rect* clip = NULL)
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface( source, clip, destination, &offset );
}

bool load_files()
{
    mario = load_image( "mario.png" );

    if( mario == NULL )
    {
        return false;
    }

	background = load_image("bgTest3.png");

	if (background == NULL)
	{
		return false;
	}

	bricks = load_image("bricks.png");

	if (bricks == NULL)
	{
		return false;
	}

	monsters = load_image("monsters.png");

	if (monsters == NULL)
	{
		return false;
	}

    return true;
}

bool init()
{
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    screen = SDL_SetVideoMode( 960, 600, 32, 
		SDL_SWSURFACE );

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

    SDL_WM_SetCaption( "Mario Test", NULL );

    return true;
}

bool check_collision(SDL_Rect A, SDL_Rect B)
{
	int leftA, leftB;
	int rightA, rightB;
	int topA, topB;
	int bottomA, bottomB;

	leftA = A.x;
	rightA = A.x + A.w;
	topA = A.y;
	bottomA = A.y + A.h;

	leftB = B.x;
	rightB = B.x + B.w;
	topB = B.y;
	bottomB = B.y + B.h;


		if (((bottomA <= topB) || (topA >= bottomB) || (rightA < leftB)||
			(leftA > rightB)) == false)
		{
			return true;
		}
		
	return false;
}

void set_clips()
{
	//standing right
	marioRight[ 0 ].x = 9;
    marioRight[ 0 ].y = 4;
    marioRight[ 0 ].w = 48;
    marioRight[ 0 ].h = 64;
	
    //running 1 right
    marioRight[ 1 ].x = 100;
    marioRight[ 1 ].y = 4;
    marioRight[ 1 ].w = 64;
    marioRight[ 1 ].h = 64;

	//running 2 right
	marioRight[ 2 ].x = 9;
	marioRight[ 2 ].y = 100;
	marioRight[ 2 ].w = 44;
	marioRight[ 2 ].h = 64;

	//running 3 right
	marioRight[ 3 ].x = 100;
	marioRight[ 3 ].y = 100;
	marioRight[ 3 ].w = 52;
	marioRight[ 3 ].h = 60;

	//jumping right
	marioRight[ 4 ].x = 200;
	marioRight[ 4 ].y = 4;
	marioRight[ 4 ].w = 64;
	marioRight[ 4 ].h = 56;

	//standing left
    marioLeft[ 0 ].x = 275;
    marioLeft[ 0 ].y = 4;
    marioLeft[ 0 ].w = 48;
    marioLeft[ 0 ].h = 64;

	//running 1 left
    marioLeft[ 1 ].x = 344;
    marioLeft[ 1 ].y = 4;
    marioLeft[ 1 ].w = 64;
    marioLeft[ 1 ].h = 56;

	//running 2 left
	marioLeft[ 2 ].x = 275;
	marioLeft[ 2 ].y = 100;
	marioLeft[ 2 ].w = 44;
	marioLeft[ 2 ].h = 64;

	//running 3 left
	marioLeft[ 3 ].x = 344;
	marioLeft[ 3 ].y = 100;
	marioLeft[ 3 ].w = 52;
	marioLeft[ 3 ].h = 60;

	//jumping left
	marioLeft[ 4 ].x = 440;
	marioLeft[ 4 ].y = 4;
	marioLeft[ 4 ].w = 56;
	marioLeft[ 4 ].h = 60;

	ground.x = 10;
	ground.y = 100;
	ground.w = 64;
	ground.h = 60;

	brick.x = 10;
	brick.y = 10;
	brick.w = 64;
	brick.h = 60;

	step.x = 200;
	step.y = 10;
	step.w = 64;
	step.h = 60;

	//Unhit
    mystery[0].x = 100;
	mystery[0].y = 10;
	mystery[0].w = 64;
	mystery[0].h = 60;

	//Hit
    mystery[1].x = 300;
	mystery[1].y = 10;
	mystery[1].w = 64;
	mystery[1].h = 60;

    goomba[0].x = 10;
	goomba[0].y = 10;
	goomba[0].w = 64;
	goomba[0].h = 64;

    goomba[1].x = 100;
	goomba[1].y = 10;
	goomba[1].w = 64;
	goomba[1].h = 64;

	goomba[2].x = 200;
	goomba[2].y = 10;
	goomba[2].w = 64;
	goomba[2].h = 32;

    koopaLeft[0].x = 300;
	koopaLeft[0].y = 9;
	koopaLeft[0].w = 64;
	koopaLeft[0].h = 92;

    koopaLeft[1].x = 400;
	koopaLeft[1].y = 10;
	koopaLeft[1].w = 64;
	koopaLeft[1].h = 96;

    koopaRight[0].x = 500;
	koopaRight[0].y = 9;
	koopaRight[0].w = 64;
	koopaRight[0].h = 92;

    koopaRight[1].x = 600;
	koopaRight[1].y = 9;
	koopaRight[1].w = 64;
	koopaRight[1].h = 96;

    smallPipe.x = 400;
	smallPipe.y = 10;
	smallPipe.w = 132;
	smallPipe.h = 128;

    medPipe.x = 550;
	medPipe.y = 10;
	medPipe.w = 132;
	medPipe.h = 260;

    largePipe.x = 550;
	largePipe.y = 10;
	largePipe.w = 132;
	largePipe.h = 520;
}
Topic archived. No new replies allowed.