C++ class variable access

Pages: 12
Now your issue. I don't get it.
1
2
3
//defineMap::loadChunk()
			defineMap::map[whichChunk].map[i][j] = tempChar;
			printf("CHAR: %c and %c\n", tempChar, defineMap::map[whichChunk].map[i][j]);
¿That is printing garbage? ¿Has tempChar the expected value?
In that case check if whichChunk is out of bounds.
IIRC you never set the inuse flag of the chunks.

Another thing
1
2
3
4
5
6
7
8
9
10
			if (mapLoaded == 0)
			{
				defineMap map; //this is a local variable
				//...

				//show the screen
				SDL_Flip(screen);

				mapLoaded = 1;
			}//here it dies 
¿Is that intended?

By the way
1
2
	//Check whether this chunk is 'in use', if = to 0, it means that this chunk is unloaded, 1 = loaded
	int inuse;
I would recommend to use bool inuse; instead. Setting it to true or false.
If later you want several states, you may want to use an enum.


Edit: About your read. Don't loop on feof, but on the reading.
1
2
3
4
5
//char tempChar;
//while (!feof(mapFile))

int tempChar; //because fgetc returns an int
while( (tempChar=fgetc(mapFile)) != EOF )
You may want to check fstream later
Last edited on
Thanks for all the help, this is all very useful info!

cheers to all!

ne555:
With the printing garbarge, this was to test whether tempChar and defineMap.blahblah were the same, incase of some other error. ill test whichchunk
Last edited on
Hey all again, not sure if this would get to anyone...but anyhow...

ive tried various things making the variables either private, public or protected. I tried the debugging and i can only see everything from the main function, of which I see all the weird values popping up. However, what i did notice was a pattern.

1. each chunk (there are 100 of them) had an array of 100, and each chunk would follow like -24, 60, 80, etc for each one

2. the members would ALWAYS be the same. However, when ever i made the members either private, public or protected, those values changed.

Im not entirely sure if its a scope issue. Because i can get the member printing the correct things from inside the function that changed it.

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
void defineMap::loadChunk(int x, int y, const char* filePath)
{
	//this will determine which chunk to load into!
	int whichChunk;

	//Check the various chunks in the map to see whether they are in use
	for (int i = 0; i < (maxLoad * maxLoad); i++)
	{
		if (map[i].inuse == false)
		{
			whichChunk = i;
			break;
		}
	}

	//Set the chunks x and y so we know where to put it!
	map[whichChunk].x = x;
	map[whichChunk].y = y;

	//for the while loop!!
	int i = 0;
	int j = 0;

	char tempChar;

	//Create file stream and read the file
	FILE* mapFile;
	mapFile = fopen(filePath, "r");

	//Actually read the file
	while ((tempChar = fgetc(mapFile)) != EOF)
	{
		tempChar = fgetc(mapFile);

		if (tempChar == '\n')
		{
			j++;
			i = 0;
		}
		else if (tempChar != -1)
		{
			map[whichChunk].map[i][j] = tempChar;
			printf("CHAR: %c and %c\n", tempChar, map[whichChunk].map[i][j]);
			i++;
		}
	}
	fclose(mapFile);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Output:
etc...CHAR: G and G
CHAR: D and D
CHAR: C and C
CHAR: A and A
CHAR: D and D
CHAR: C and C
CHAR: E and E
CHAR: C and C
CHAR: G and G
CHAR: C and C
CHAR: E and E
CHAR: A and A
CHAR: B and B
CHAR: G and G
CHAR: F and F
CHAR: A and A
CHAR: B and B
CHAR: G and G...etc

As it is seen, this is working fine

however... this is where the error occurs

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
//Drawing map function to go through the chunk and put them on the screen according to the textures
void defineMap::drawMap(SDL_Surface* screen)
{
	//SDL_Rect for drawing a pixel!
	SDL_Rect tempRect;
	tempRect.w = 1;
	tempRect.h = 1;

	//this for is for the map
	for (int i = 0; i < (maxLoad * maxLoad); i++)
	{
		if (map[i].inuse == 1)
		{
			//for loops to go through the chunks x and y
			for (int j = 0; j < chunkSize; j++)
			{
				for (int k = 0; k < chunkSize; k++)
				{
					//Put the correct x and y's to place in the correct position
					tempRect.x = j + (map[i].x * chunkSize);
					tempRect.y = k + (map[i].y * chunkSize);

					printf("STOOF: %i\n", map[i].x);

					for (int a = 0; a < chunkSize; a++)
					{
						for (int b = 0; b < chunkSize; b++)
						{
							printf("CHUNK:%i COORD: %i and %i PIXEL: %i, X: %i Y: %i OTHER: %i\n", i, a, b, map[i].map[a][b], map[i].x, map[i].y, strlen(map[i].map[a]));
						}
					}

					//Read the map and draw the pixel arcording to the
					//x and y's values and textures (note: offset is for ASCII because A = 65 which then = 0 for map)

					SDL_FillRect(screen, &tempRect, defineMap::textures.at(defineMap::map[i].map[j][k] - offset).decimalColour.at(randomInt(0, defineMap::textures.at(defineMap::map[i].map[j][k] - offset).decimalColour.size())));
					printf("after\n");
				}
			}
		}
	}
}


The long printf statement within the a and b for loop prints 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
Output:

CHUNK:34 COORD: 0 and 0 PIXEL: -96, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 1 PIXEL: 7, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 2 PIXEL: 0, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 3 PIXEL: 0, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 4 PIXEL: 2, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 5 PIXEL: 0, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 6 PIXEL: 0, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 7 PIXEL: 0, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 8 PIXEL: 32, X: -1072365560 Y: 0 OTHER: 2
CHUNK:34 COORD: 0 and 9 PIXEL: -31, X: -1072365560 Y: 0 OTHER: 2

//...


CHUNK:92 COORD: 0 and 0 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 1 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 2 PIXEL: 83, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 3 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 4 PIXEL: -8, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 5 PIXEL: -65, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 6 PIXEL: 85, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 7 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 8 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 0 and 9 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 1 and 0 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 1 and 1 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 1 and 2 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0
CHUNK:92 COORD: 1 and 3 PIXEL: 0, X: 2004273965 Y: 0 OTHER: 0


Thanks all for your efforts towards this, ive literally spent hours glued to the screen and pulling my hair out!

many thanks super stinger!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//load chunk
		//if (map[i].inuse == false)
		if (not map[i].inuse)
		{
			whichChunk = i;
			break;
		}
//Code that fills map[whichChunk]
//...

//draw map
		//if (map[i].inuse == 1)
		if (map[i].inuse)
		{

You load 'a', but print 'z'
1
2
3
4
5
6
7
8
for (int i = 0; i < (maxLoad * maxLoad); i++)
	{
		if (defineMap::map[i].inuse != 1)
		{
			whichChunk = i;
			break;
		}
	}

You see the error? No? What about now:
1
2
3
4
5
6
7
8
9
for (int i = 0; i < (maxLoad * maxLoad); i++)
	{
		if (defineMap::map[i].inuse != 1)
		{
			whichChunk = i;
			defineMap::map[i].inuse = 1;
			break;
		}
	}


Or did you already set it somewhere else?
Hmm, that is a small error to look upon but thats not the current problem at hand, random member values is what keeps getting me. I tried making a really long post so that every can understand lol, but you did bring up some errors that i didn't see before.

sorry ne555; where was that error you stated about loading a but print z?
That the chunk that you load and the one that you print are not the same.
Because you are not setting inuse
lol think you got my mind ticking the right way, i think i may of fixed it

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 defineMap::drawMap(SDL_Surface* screen)
{
	//SDL_Rect for drawing a pixel!
	SDL_Rect tempRect;
	tempRect.w = 1;
	tempRect.h = 1;

	//this for is for the map
	for (int i = 0; i < (maxLoad * maxLoad); i++)
	{
		if (map[i].inuse == true)
		{
			//for loops to go through the chunks x and y
			for (int j = 0; j < chunkSize; j++)
			{
				for (int k = 0; k < chunkSize; k++)
				{
					//Put the correct x and y's to place in the correct position
					tempRect.x = j + (map[i].x * chunkSize);
					tempRect.y = k + (map[i].y * chunkSize);

					printf("CHUNK:%i COORD: %i and %i PIXEL: %i, X: %i Y: %i OTHER: %i\n", i, j, k, map[i].map[k][j], map[i].x, map[i].y, strlen(map[i].map[k]));

					//Read the map and draw the pixel arcording to the
					//x and y's values and textures (note: offset is for ASCII because A = 65 which then = 0 for map)

					//SDL_FillRect(screen, &tempRect, defineMap::textures.at(defineMap::map[i].map[j][k] - offset).decimalColour.at(randomInt(0, defineMap::textures.at(defineMap::map[i].map[j][k] - offset).decimalColour.size())));
					printf("after\n");
				}
			}
		}
	}
}


thanks for all your help! Ill let you's kno if something else occurs
Topic archived. No new replies allowed.
Pages: 12