Game maps

Hi i have been following a tutorial on how to make a jump and run game. However im not sure how the game map is generated. I want to make it longer with more obsticles like ledges and gaps to jump over ect. I have included my main.cpp, map.cpp and map.h if anyone can help me achieve this please help :)

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
/*
  map.cpp
   CMap
*/

#include "global.h"




void CMap::loadMap(const char *file){
	FILE *mapfile;
	int i, j, t;

	printf("loading map %s... ", file);


	mapfile = fopen(file, "rb");

	if(tiles)		//map has already been initialized
		freetiles();


	fread(&width, sizeof(int), 10, mapfile);		//read map width
	fread(&height, sizeof(int), 10, mapfile);	//read map height

	tiles = new int[width*height];				//get memory for the map

	t = 0;	
	for(j = 0; j < width; j++){
		for(i = 0; i < height; i++){
			fread(&tiles[t], sizeof(int), 1, mapfile);	//read tile
			t++;
		}
	}
	fclose(mapfile);

	tiles2d = new int*[height];					//build 2d index array to map data
	for(i=0; i<height; i++){					
		tiles2d[i] = &(tiles[i*width]);			//tiles[i] is a pointer to tile row i
	}

	printf("done\n");
}

void CMap::saveMap(const char *file){
	FILE *mapfile;
	int i, j, t;

	printf("saving map %s... ", file);

	mapfile = fopen(file, "wb");

	fwrite(&width, sizeof(int), 1, mapfile);
	fwrite(&height, sizeof(int), 1, mapfile);

	t = 0;
	for(j = 0; j < width; j++){
		for(i = 0; i < height; i++){
			fwrite(&tiles[t], sizeof(int), 1, mapfile);
			t++;
		}
	}
	fclose(mapfile);

	printf("done\n");
}

//create a new map and fill it with tile 0
void CMap::emptymap(int w, int h){
	int i, j, t;

	if(tiles)		//map has already been initialized
		freetiles();	

	width = w;
	height = h;

	tiles = new int[width*height];

	t = 0;
	for(j = 0; j < width; j++){
		for(i = 0; i < height; i++){
			tiles[t] = 0;
			t++;
		}
	}

	tiles2d = new int*[height];					//build 2d index array to map data
	for(i=0; i<height; i++){					
		tiles2d[i] = &(tiles[i*width]);			//tiles[i] is a pointer to tile row i
	}
}

//check if we've scrolled to far and fix scroll_x, _y
void CMap::limit_scroll(){

	if(scroll_x < 0)							//too far left?
		scroll_x = 0;

	else if(scroll_x+640 > width*TILESIZE)		//don't scroll over the map boundaries on the right side (640 is the screenwidth)
		scroll_x = width*TILESIZE-640;
	

	if(scroll_y < 0)							//too far up?
		scroll_y = 0;
	
	else if(scroll_y + 480 > height*TILESIZE)	//bottom map boundaries (480 - screenheight)
		scroll_y = height*TILESIZE-480;
}

void CMap::draw(){
	int x, y;	//screen pixel x,y coordinate to draw the current tile to
	int mx, my;
	int sx = 0-(scroll_x%TILESIZE);
	int smx = (sx+scroll_x)/TILESIZE;

	
	for( y = 0-(scroll_y%TILESIZE), my = (y+scroll_y)/TILESIZE;  y < 480;  y+=TILESIZE){		//y draw coordinate, the offset is for smooth scrolling
		for( x = sx, mx = smx;  x < 640;  x+=TILESIZE){	//x draw coordinate
			if(tileset[ tiles2d[my][mx] ].spr != NULL)
				tileset[ tiles2d[my][mx] ].spr->draw(x,y);
			mx++;
		}
		my++;
	}	
}




CMap::~CMap(){
	if(tiles)
		freetiles();
}



void CMap::freetiles(){
	delete [width*height] tiles;
	delete [height] tiles2d;
	tiles = NULL;
	width = 0;
	height = 0;
}


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
/*
  map.h
   CMap
*/


enum TileType{t_nonsolid, t_solid, t_slopeleft, t_sloperight};



struct CTile{
	TileType	type;	//the type of the tile (solid, slope, ...)
	gfxSprite	*spr;	//sprite to draw

	CTile(){
		type = t_nonsolid;
		spr = NULL;
	};
};




extern CTile		tileset[9];




class CMap{
	public:
		void loadMap(const char *file);		//loads the map from a file
		void saveMap(const char *file);

		~CMap();


		void emptymap(int w, int h);


		void draw();

		TileType map(int x, int y){			//return the tiletype at the specific position (map coordinates)
			return tileset[ tiles[x+y*width] ].type;
		};

		void limit_scroll();

	private:
		int width;
		int height;
		int *tiles;
		int **tiles2d;

		void freetiles();
	

	friend void leveleditor();
};



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
//---------------- includes ----------------
#include "global.h"				



//---------------- global variables ----------------
//"engine"
Uint8			*keystates;
SDL_Surface		*screen;		//the screen (main sdl surface which is visible on the monitor)

//sprites
gfxSprite		spr_t[7];
gfxSprite		spr_player[2];
gfxSprite		spr_player2[2];


gfxSprite		spr_background;

gfxFont			font;

//game objects

CPlayer			player;
CPlayer			player2;

CMap			map;

CTile			tileset[9];


//scroll offset
int scroll_x = 0;
int scroll_y = 0;



//---------------- main ----------------


int main(int argc, char *argv[]){
	unsigned int	framestart;
	float			fps = 0, framefps = 0;
	int				divisor;
	bool			done;
	SDL_Event		event;


	//---------------- init the "engine" ----------------
	//initialize SDL
	gfx_init(640,480, false);
	//get keystate array
	keystates = SDL_GetKeyState(0);


	//---------------- load resources (graphics) ----------------
	spr_t[0].init("gfx/t1.bmp");					//tile graphics
	spr_t[1].init("gfx/t2.bmp");
	spr_t[2].init("gfx/t3.bmp", 255, 0, 255);
	spr_t[3].init("gfx/tsloper.bmp", 255, 0, 255);
	spr_t[4].init("gfx/tslopel.bmp", 255, 0, 255);
	spr_t[5].init("gfx/t6.bmp", 255, 0, 255);
	spr_t[6].init("gfx/t7.bmp", 255, 0, 255);
	

	spr_player[0].init("gfx/left2.bmp", 255,0,255);	//player graphics
	spr_player[1].init("gfx/right2.bmp", 255,0,255);

	spr_player2[0].init("gfx/Rleft.bmp", 255,0,255);	//player graphics
	spr_player2[1].init("gfx/Rright.bmp", 255,0,255);

	

	spr_background.init("gfx/bg.bmp");				//background

	font.init("gfx/font0.bmp");						//font



	//---------------- init the game variables ----------------
	tileset[0].type = t_nonsolid;		tileset[0].spr = NULL;
	tileset[1].type = t_solid;			tileset[1].spr = NULL;
	tileset[2].type = t_solid;			tileset[2].spr = &spr_t[0];
	tileset[3].type = t_solid;			tileset[3].spr = &spr_t[1];
	tileset[4].type = t_nonsolid;		tileset[4].spr = &spr_t[2];
	tileset[5].type = t_sloperight;		tileset[5].spr = &spr_t[3];
	tileset[6].type = t_slopeleft;		tileset[6].spr = &spr_t[4];
	tileset[7].type = t_solid;			tileset[7].spr = &spr_t[5];
	tileset[8].type = t_solid;			tileset[8].spr = &spr_t[6];




	//initialize the map, the player has already been initialized by its constructor
	map.loadMap("maps/map01.map");


	
	printf("\nhere comes the game loop...\n") ;
	done = false;

	//---------------- game loop ----------------
	while (!done){
		framestart = SDL_GetTicks();

		//handle messages
		while(SDL_PollEvent(&event)){
			switch(event.type){
				case SDL_QUIT:
					done = true;
				break;

				default:
				break;
			}
		}

		if(keystates[SDLK_ESCAPE])		//quit?
			done = true;




		//---------------- update objects (game logic) ----------------
		player.think();
		player2.think2();

		






		//---------------- draw everything (render the scene) ----------------
		
		int sdx = (scroll_x%spr_background.getWidth());
		int sdy = (scroll_y%spr_background.getHeight());
		spr_background.draw(-sdx, - sdy);
		spr_background.draw(spr_background.getWidth() - sdx, -sdy);
		spr_background.draw(- sdx, spr_background.getHeight()-sdy);
		spr_background.draw(spr_background.getWidth() - sdx, spr_background.getHeight()-sdy);

		
		map.draw();

		player.draw();
		player2.draw();


		
		//the info text
		font.drawf(0,0, "fps: frame/real/lock: %.1f/%.1f/%.1f", framefps, fps, (float)(1000 / WAITTIME));




		//---------------- that's it, now flip the buffers and keep the framerate constant ----------------

		divisor = SDL_GetTicks()-framestart;
		if(divisor != 0)
			framefps = (float)( 1000 / divisor );	//this is the framerate without vsync and the frame break
		else
			fps = 1111.11f;


		SDL_Flip(screen);	//double buffering -> flip buffers, also waits for vsync

		while((SDL_GetTicks()-framestart) < WAITTIME);	//framebreak - keep framerate constant at 1000/WAITTIME fps
		
		divisor = SDL_GetTicks()-framestart;
		if(divisor != 0)
			fps = (float)( 1000 / (SDL_GetTicks()-framestart) );		//this is the framerate with vsync and the framebreak (should be equal to 1000/WAITTIME)
		else
			fps = 1111.11f;
	}

	printf("\n\nthat's all folks :)\n");

	return 0;
}
Understanding how code works is one of the main steps when learning a new language.

Try tweaking small bits of code and seeing how it affects your program.

I won't analyze your code and then tell you what to do. But if you have specific questions about code parts i will gladly help you. I think the last line applies to many of the forum helpers.
What do you exactly want ?
Topic archived. No new replies allowed.