SDL - Map Loading?

Well, thanks for the help on my last topic, now since I made my program more "Resource efficient", I could debug my map loading. Now since I've done it, I need serious help. My maps fail to load in the correct manner.

Download the project here, http://www.cplusplus.com/forum/lounge/99378/ (At my last thread created) then replace my Map.cpp with this:

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
#include "Map.h"
#include <iostream>

SDL_Surface* TileSheet;

int Map::Init(char* mapinitfile)
{
	MapInitFile = mapinitfile;
	std::ifstream MapInit;
	MapInit.open(MapInitFile, NULL); 
	if(MapInit == NULL)
	{
		printf("Map failed to load\n");
		exit(1);
		return 1;
	}
	MapInit >> MaxTiles;
	MapInit >> LevelW;
	MapInit >> LevelH;
	MapInit.close();

	TileSheet = Draw::LoadGFX(SHEETLOCATION);

	return 0;
}

int Map::SetTiles(char* mapfile)
{
	int X;
	int Y;

	std::ifstream Map/*(mapfile, NULL)*/;
	Map.open(mapfile);
	if(Map == NULL)
	{
		printf("Map failed to load : cannot open map.\n");
		return 1;
	}

	for(int t = 0; t < MaxTiles; t++)
	{
		int tiletype = -1;

		Map >> tiletype;
		if(Map.fail() == true)
		{
			printf("Map failed to load : failed to get tile type\n");
			Map.close();
			return 1;
		}
		if((tiletype >= 0) && (tiletype < TILETYPES))
		{
			 Tiles[t] = new Tile( X, Y, tiletype ); 
		}
		else
		{
			printf("Map failed to load\n");
			Map.close();
			return 1;
		}

		X += TILEWIDTH;
		if(X >= LevelW)
		{
			X  = 0;
			Y += TILEHEIGHT;
		}
	}
	printf("Map successfully loaded\n");
	return 0;
}

bool Map::TouchWall(SDL_Rect CollBox, Tile* tiles[])
{
	for(int t = 0; t < MaxTiles; t++)
	{
		if((tiles[t]->getType() == t_VOID ) || (tiles[t]->getType() == t_STONEWALL))
		{
			if(Collision::ChkCollision(CollBox, tiles[t]->getBox()) == true)
			{
				return true;
			}
		}
	}
	return false;
}

void Map::Render(SDL_Surface* Destination)
{
	for(int t = 0; t < 441; t++)
	{
		Tiles[t]->Render(Destination, TileSheet);
	}
}

void Map::Cleanup()
{
	for(int t = 0; t < MaxTiles; t++)
	{
		delete Tiles[t];
	}

	SDL_FreeSurface(TileSheet);
}


Replace Tile.cpp with this :

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
#include "Tile.h"
#include "Extern.h"

Tile::Tile(int x, int y, int type)
{
	Box.x = x;
	Box.y = y;
	Box.w = TILEWIDTH;
	Box.h = TILEHEIGHT;

	Type = type;

	Clips[0].x = NULL;
	Clips[0].y = NULL;
	Clips[0].w = TILEWIDTH;
	Clips[0].h = TILEHEIGHT;

	Clips[1].x = NULL;
	Clips[1].y = TILEHEIGHT;
	Clips[1].w = TILEWIDTH;
	Clips[1].h = TILEHEIGHT;
	
	Clips[2].x = TILEWIDTH;
	Clips[2].y = NULL;
	Clips[2].w = TILEWIDTH;
	Clips[2].h = TILEHEIGHT;
	
	Clips[3].x = TILEWIDTH;
	Clips[3].y = TILEHEIGHT;
	Clips[3].w = TILEWIDTH;
	Clips[3].h = TILEHEIGHT;

	Clips[4].x = TILEWIDTH * 2;
	Clips[4].y = NULL;
	Clips[4].w = TILEWIDTH;
	Clips[4].h = TILEHEIGHT;
	
	Clips[5].x = TILEWIDTH * 2;
	Clips[5].y = TILEHEIGHT;
	Clips[5].w = TILEWIDTH;
	Clips[5].h = TILEHEIGHT;
	
	Clips[6].x = TILEWIDTH * 3;
	Clips[6].y = NULL;
	Clips[6].w = TILEWIDTH;
	Clips[6].h = TILEHEIGHT;
	
	Clips[7].x = TILEWIDTH * 3;
	Clips[7].y = TILEHEIGHT;
	Clips[7].w = TILEWIDTH;
	Clips[7].h = TILEHEIGHT;
	
	Clips[8].x = TILEWIDTH * 4;
	Clips[8].y = NULL;
	Clips[8].w = TILEWIDTH;
	Clips[8].h = TILEHEIGHT;

	Clips[9].x = TILEWIDTH * 4;
	Clips[9].y = TILEHEIGHT;
	Clips[9].w = TILEWIDTH;
	Clips[9].h = TILEHEIGHT;
	 
	Clips[10].x = TILEWIDTH * 5;
	Clips[10].y = NULL;
	Clips[10].w = TILEWIDTH;
	Clips[10].h = TILEHEIGHT;
	 
	Clips[11].x = TILEWIDTH * 5;
	Clips[11].y = TILEHEIGHT;
	Clips[11].w = TILEWIDTH;
	Clips[11].h = TILEHEIGHT;

	Camera.w = 800;
	Camera.h = 600;
}

void Tile::Render(SDL_Surface* Destin, SDL_Surface* TileSet)
{
	if((Collision::ChkCollision(Camera, Box) == true))
	{
		Draw::DrawGFX(TileSet, Clips[Type].x, Clips[Type].y, Clips[Type].w, Clips[Type].h, Destin, Box.x - Camera.x, Box.y - Camera.y);
	}
}

void Tile::Cleanup()
{
	
}


The problem with this is first off, my collision with my stone wall texture failed. Second, it basically just displays my tileset in the left corner of the screen. (Before helping, compile the changes). I might just have to find another way to load my map. Any help is appreciated. Thank you in advance.

EDIT: Fixed the no code tages for Tile.cpp
Last edited on by Fredbill30
Right away I can see that X and Y are uninitialized in Map::SetTiles.
In Draw::DrawGFX you set the destination x y to X Y instead of X2 Y2.
1
2
Dest.x = X2;
Dest.y = Y2;


You are only checking collisions when a button is pressed. You will have to check collisions every time you update the position of the player. In Player::Update() maybe.

You also need to update CollBox.x and CollBox.y for the collision to work.
I am so stupid. The DrawGFX thing was in my post too.
Before I ask another stupid question, I just have to say... I LOVE YOU GUYS SO MUCH. IT WORKS NOW :DDDD.

However, there is one problem. When I move trigger any event, it all disappears. I have a feeling is because of the "Camera". How can I make a Camera SDL_Rect's info move to another .cpp file?

EDIT - Replace these files:

Map.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
#include "Map.h"
#include <iostream>

SDL_Surface* TileSheet;
int permx;
int permy;

int Map::Init(char* mapinitfile)
{
	MapInitFile = mapinitfile;
	std::ifstream MapInit;
	MapInit.open(MapInitFile, NULL); 
	if(MapInit == NULL)
	{
		printf("Map failed to load\n");
		exit(1);
		return 1;
	}
	MapInit >> MaxTiles;
	MapInit >> LevelW;
	MapInit >> LevelH;
	MapInit.close();

	TileSheet = Draw::LoadGFX(SHEETLOCATION);

	return 0;
}

int Map::SetTiles(char* mapfile)
{
	int X = 0;
	int Y = 0;

	std::ifstream Map/*(mapfile, NULL)*/;
	Map.open(mapfile);
	if(Map == NULL)
	{
		printf("Map failed to load : cannot open map.\n");
		return 1;
	}

	for(int t = 0; t < MaxTiles; t++)
	{
		int tiletype = -1;

		Map >> tiletype;
		if(Map.fail() == true)
		{
			printf("Map failed to load : failed to get tile type\n");
			Map.close();
			return 1;
		}
		if((tiletype >= 0) && (tiletype < TILETYPES))
		{
			 Tiles[t] = new Tile( X, Y, tiletype ); 
		}
		else
		{
			printf("Map failed to load\n");
			Map.close();
			return 1;
		}

		X += TILEWIDTH;
		if(X >= LevelW)
		{
			X  = 0;
			Y += TILEHEIGHT;
		}
		permx = X;
		permy = Y;
	}
	printf("Map successfully loaded\n");
	return 0;
}

bool Map::TouchWall(SDL_Rect CollBox, Tile* tiles[])
{
	for(int t = 0; t < MaxTiles; t++)
	{
		SDL_Rect Toffset;
		Toffset.x = permx;
		Toffset.y = permy;
		Toffset.w = TILEWIDTH;
		Toffset.h = TILEHEIGHT;

		Tiles[t]->setBox(Toffset);
		if((tiles[t]->getType() == t_VOID ) || (tiles[t]->getType() == t_STONEWALL))
		{
			if(Collision::ChkCollision(CollBox, tiles[t]->getBox()) == true)
			{
				return true;
			}
		}
	}
	return false;
}

void Map::Render(SDL_Surface* Destination)
{
	for(int t = 0; t < 441; t++)
	{
		Tiles[t]->Render(Destination, TileSheet);
	}
}

void Map::Cleanup()
{
	for(int t = 0; t < MaxTiles; t++)
	{
		delete Tiles[t];
	}

	SDL_FreeSurface(TileSheet);
}


Tile.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
#include "Draw.h"
#include "Collision.h"

// Header guards
#ifndef _TILE_H_
#define _TILE_H_

#define SHEETLOCATION	"Graphics/Maps/Tileset.png"
#define TILEWIDTH		32
#define TILEHEIGHT		32
#define TILETYPES		12

#define t_GRASS1		0
#define t_GRASS2		1
#define t_VER_PATHG		2
#define t_NE_PATHG		3
#define t_HOR_PATHG		4
#define	t_SE_PATHG		5
#define t_NW_PATHG		6
#define t_SW_PATHG		7
#define t_VOID			8
#define t_STONEWALL		9
#define t_WOODFLOOR		10
#define t_TRANSPARENT	11

class Tile
{
	public:
		Tile(int x, int y, int type);

		void Render(SDL_Surface* Destin, SDL_Surface* TileSet);
		void Cleanup();

		int getType()               {return Type;}
		SDL_Rect getBox()           {return Box;}
		void     setBox(SDL_Rect r) {Box = r;}
		SDL_Rect Clips[TILETYPES];
	private:
		SDL_Rect Box;
		int Type;
};

#endif//_TILE_H_ 


mapfile.RGMAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
00 01 00 00 00 01 00 00 00 00 00 00 01 00 00 00 00 00 00 00 01
04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04 04
00 09 09 09 09 09 09 09 09 09 09 09 09 09 09 00 00 00 00 01 00
01 09 10 10 10 10 10 10 10 10 10 10 10 10 09 00 00 00 00 00 01
00 09 10 10 10 10 10 10 10 10 10 10 10 10 09 00 00 01 00 00 00
00 09 10 10 10 10 10 10 10 10 10 10 10 10 09 01 00 01 00 00 00
00 09 10 10 10 10 10 10 10 10 10 10 10 10 09 00 00 00 00 01 00
00 09 10 10 10 10 10 10 10 10 10 10 10 10 09 00 00 00 00 00 01
01 09 10 10 10 10 10 10 10 10 10 10 10 10 09 00 00 00 01 00 01
00 09 10 10 10 10 10 10 10 10 10 10 10 10 09 00 00 00 00 00 00
00 09 10 10 10 10 10 10 10 10 10 10 10 10 09 00 00 00 00 00 00
00 09 10 10 10 10 10 10 10 10 10 10 10 10 09 01 00 00 00 00 00
01 09 09 09 09 09 09 10 09 09 09 09 09 09 09 00 00 00 00 00 00
00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 00 01 00 00 00 00 00 00 00 00 00 00 01
00 00 00 00 01 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 01 00 01 00 00
00 00 00 00 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00
00 01 00 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00 00 01 00
04 04 04 04 04 04 04 05 00 00 00 00 01 00 00 00 00 00 01 00 00
00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00
Last edited on by Fredbill30
BUMP
Nevermind, I just had to call my "SetTiles()" function every update. But my camera messes up when I near the edge of the screen.
BUMP

I tried to enlarge my map, but it won't load extra tiles.
Fredbill: You're going to have to learn how to use a debugger. You can't run to a forum for every little hiccup you experience. You'll do much better (and have your problems solved much sooner) if you actually work to solve them yourself.

Here's a introduction which describes the basic process:

http://www.cplusplus.com/forum/beginner/75304/#msg403990



I'm fine with answering questions and helping out with the occasional problem... but "please debug my program for me" threads are really the worst.
Figured it out. I need help with understand the math of the "SetCamera()" function though.
Topic archived. No new replies allowed.