Help needed with tile mapping program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void create_map(){
	
int x = 0;
int y = 0;

while (y >= 15)
	{
	game_map[x][y].pos_x = x * tile_size;
	game_map[x][y].pos_y = y * tile_size;
		if (x != 15)
		{	
		x++;
		}
		else
		{
			if (y != 15)
			{
			y++;
			}
		}
	}

}


Objective of the code: to loop through all tiles in the game, setting their X and Y coordinates.

It just doesn't do anything at all :/. No error message. Can someone please help. If you need to see more of the code, then let me know, like the class, initialization of the game_map array, or how I am using this function, let me know :P.
1
2
3
int y = 0;

while (y >= 15)


When you come to the while condition, what value does y hold? Is the condition true? Is 0 greater than or equal to 15?
Oh shit, lol had my conditional backwards. Thank you. Stupid mistake lol.

Hmmm, well I'm still having an issue. It's not working like it should, but this time I think it's in the way I am using it. I'll post my code in a sec :P
Last edited on
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
int cursor_current_x = 0;
int cursor_current_y = 0;
int cursor_x = game_map[cursor_current_x][cursor_current_y].pos_x;
int cursor_y =game_map[cursor_current_x][cursor_current_y].pos_y;

void move_cursor(){
	

	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
	
		if (cursor_current_x >= 0){
		cursor_current_x = cursor_current_y - 1;
		cursor_x = game_map[cursor_current_x][cursor_current_y].pos_x;
		cursor_y =game_map[cursor_current_x][cursor_current_y].pos_y;
		}
		}
		
		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
	
		if (cursor_current_x <= screen_width - tile_size){
		cursor_current_x = cursor_current_y + 1;
		cursor_x = game_map[cursor_current_x][cursor_current_y].pos_x;
		cursor_y =game_map[cursor_current_x][cursor_current_y].pos_y;
		}
		}
	
}


I am using SFML btw. Basically this code checks if the key is pressed, then sets the cursor's current position variable to +1 which should change the tile number, but it still will not move the cursor. I have cursor_move(); inside of the application loop.
Is cursor_current_x = cursor_current_y - 1 really what you want to do there?

I have cursor_move(); inside of the application loop.

Is that a typo or do you have a move_cursor and a cursor_move?
I accidently typed cursor_move(); inside of the forum, it's not like that in the code :P, and I think so, because I want to change the coordinates of the array, not the cursor variable itself, I want to get the cursor_x/cursor_y values from the game_map array.
So you do want to set x based on the value held by y? Maybe it was difficult to see the underlined characters in that post what with the underscores immediately before them.
Haha didn't see the x and y combo XD, well, now it moves, but it needs fixed a little bit still, I think I can handle it now. Thank you :).

Hmmmm, still don't have it completely yet. First larger project in C++.

I do now have the issue narrowed down though, I had create_map(); outside of the application loop. Once I placed it inside of the application loop the program completely crashes when run. I added a little more documentation to the function to see if anyone can solve the issue.

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
void create_map(){
	
int x = 0; //variable used to determine which tile where on x
int y = 0;// and the same for y
bool tiles_create = false;

				//Run through each tile going from x, once done with row in x
				//move to next row of x in y.
				//sets the coordinates of each tile relative to the map size
while (!tiles_create) //game_map is a member of the tile class.
	{
	game_map[x][y].pos_x = x * tile_size;
	game_map[x][y].pos_y = y * tile_size;
		if (x != 15)
		{	
		x++;
		}
		if (x == tiles_x)
		{
			y++;
			x = 0;
		}
		if (y == 16)
		{
		tiles_create = true;
		}
	}

}


Here's a simple diagram of what it should be doing.

while tiles and not created,
set coordinates of current tile (starts at 0, 0)
if x is not at it's maximum, set x++
if x is at it's maximum, set y++ and x = 0
^(loop until y is at it's max)
once y is at it's max set the boolean variable tiles_created = true;


I changed the code a little bit, but still the issue persists.
Last edited on
Ok, so the new issue is with:
1
2
game_map[x][y].pos_x = (x * tile_size);
game_map[x][y].pos_y = (y * tile_size);


It is obviously not writing the correct coordinates to the tiles.

After a complete rewrite of the create_map() function it no longer crashes, but now the issue is the sprite will not display on the screen at it's initial coordinates :/.

I don't think including the class source code in the post is going to help at all, but might as well.

1
2
3
4
5
6
7
8
9
class tile{
public:
	int id;
	int texture;
	int pos_x;
	int pos_y;
	bool solid;

}game_map[tiles_x][tiles_y];


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
void create_map(){
	
int x = 0; //variable used to determine which tile where on x
int y = 0;// and the same for y
bool tiles_create = false;

				//Run through each tile going from x, once done with row in x
				//move to next row of x in y.
				//sets the coordinates of each tile relative to the map size
while (!tiles_create) //game_map is a member of the tile class.
	{
	game_map[x][y].pos_x = (x * tile_size);
	game_map[x][y].pos_y = (y * tile_size);
	
	if (x < tiles_x)
	{
	x++;
	}
	
	if (x == tiles_x)
	{
	y++;
	x = 0;
	}
	
	if (y == tiles_y + 1)
	{
	tiles_create = true;
	}
	
	}

}
Last edited on
if (y == tiles_y + 1)


By the time that's true you've already written past the end of your game_map array.

I would expect create_map to look something like:

1
2
3
4
5
6
7
8
9
10
11
void create_map()
{
    for ( int x=0; x < tiles_x; ++x )
    {
        for ( int y=0; y < tiles_y; ++y )
        {
            game_map[x][y].pos_x = x * tile_size ;
            game_map[x][y].pos_y = y * tile_size ;
        }
    }
}


Presumably the other members of the tiles are initialized to something meaningful elsewhere.
Last edited on
Thank you. Although I feel like I've been kind of reinventing the wheel with this program. The objective of this program overall is to map the game and fstream it to a binary file. I've heard of Tiled for doing this, but I have no idea when it comes to parsing xml files, let alone modifying the code to parse tmx.
Thank you very much :D. Just rushed out a sample map for the game prototype :). Going to try and add it to the game tomorrow :P. I'll post in this forum if I have any questions. :) Seems easy enough though, so I shouldn't :P.
Topic archived. No new replies allowed.