Sign in to follow this  
Deftoned

Values Being Reset After Return

Recommended Posts

Hey guys, got an odd problem. I am a class in which i am setting a value to m_size. It all works fine, but when i leave the class, i look into the class information and find out my values have been reset to what they where prior to function execution. m_size is set a private. The code for the function is:
bool TerrainGeneration::LoadFractalFaultMap(int size, int min, int max, int iterations, float softenFilter)
{
	
	int iterationNum;
	int height;
	int randX1, randX2, randZ1, randZ2;
	int vecX1, vecX2;
	int vecZ1, vecZ2;
	int x, z, i;
	double *tempImageBuffer;

	//Sometimes this crappy compiler won't work on one, but the other
	//Including them both so atleast it will work on one or the other.
	m_size = size;

	//For the love of all things holy seed your random
	//Because seriously without this for some reason
	//terrain will always generate EXACTLY the same???
	srand ( (unsigned int)time ( NULL ) );
	m_heightData.m_data = new double [m_size * m_size];
	std::cout << m_heightData.m_data[0] << std::endl;
	//Make it a float as we need greater precision
	//Because of the equation later on
	tempImageBuffer = new double [m_size * m_size];
	if(m_heightData.m_data == NULL || tempImageBuffer == NULL)
	{
		printf("Error Allocating Memory,\n but since there is no exit here\n I will just go on and crash");
	}

	//Zero out the image buffer
	for(i = 0; i < (m_size * m_size); i++)
	{
		tempImageBuffer[i] = 0;
	}

	for(iterationNum = 0; iterationNum < iterations; iterationNum++)
	{
		//Calculate the linear height for this iteration
		height= max - ((max - min) * iterationNum) / iterations;
		
		//Choose random points on x and z
		randX1 = rand() % m_size;
		randZ1 = rand() % m_size;

		//If they are the same spot
		//Reposition them...
		do
		{
			randX2= rand() % m_size;
			randZ2= rand() % m_size;
		} while( randX2 == randX1 || randZ2== randZ1 );

		//Make Vectorzzzzzzzz
		vecX1 = randX2 - randX1;
		vecZ1 = randZ2 - randZ1;

		//Run through X and Z values on the terrain
		for(z = 0; z < m_size ; z++)
		{
			for(x = 0; x < m_size; x++)
			{
				//Move Vectors plz
				vecX2 = x - randX1;
				vecZ2 = z - randZ1;

				//If the result is up
				//That is greater than 0...go up plz
				if((vecX2 * vecX1 - vecZ1 * vecZ2) > 0)
				{
					tempImageBuffer[(z * m_size ) + x] += (double)height;
				}

			}
		}
		//Errosion
		FilterHeightField(tempImageBuffer, softenFilter);
	}

	//Normalise (set all points between 0 - 255)
	TerrainNormalization(tempImageBuffer);

	for(z = 0; z < m_size; z++)
	{
		for(x = 0; x < m_size; x++)
		{
			SetTrueHeight((double)tempImageBuffer[(z * m_size) + x], x, z);
		}
	}



	//Empty out the junk in image buffer
	delete[] tempImageBuffer;
	printf("%d", m_size);
	return true;
}

and i am calling it from this:
int main(int argc, char **argv)
{
	TerrainGeneration *terrain2 = new TerrainGeneration;
//	GLchar *VertexShaderSource, *FragmentShaderSource
	terrain2->LoadFractalFaultMap(128, 0, 255, 10, 0.3);

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(800, 600);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("Aurora: Overload");
	glutReshapeFunc(ReshapeFunc);
	glutDisplayFunc(DisplayFunc);
	glutIdleFunc(idleFunc);
	glutKeyboardFunc(KeyboardFunc);

	glewInit();

//	OpenGL::InitialiseGL();
	
	atexit(OnExit);

	// initialise the glut keyboard callbacks.
//	InitKeyboard();

	
	glutMainLoop();

	return 0;
}
Umm thanks for any help you can offer =(

Share this post


Link to post
Share on other sites
Quote:
Original post by Deftoned
It all works fine, but when i leave the class, i look into
the class information and find out my values have been reset
to what they where prior to function execution.

What do you mean by "look into the class information"? Are you debugging a release build?

Quote:
Original post by Deftoned
Sometimes this crappy compiler won't work

In 99.9% of the cases, it's not the compilers fault.

Quote:
Original post by Deftoned
//For the love of all things holy seed your random
//Because seriously without this for some reason
//terrain will always generate EXACTLY the same???
srand ( (unsigned int)time ( NULL ) );

OMFG how many times do we haev 2 go thru thiz???
Seed your pseudo random number generator ONCE at the BEGINNING of main, not inside of a function.

Your comments are really... unconventional :)

Share this post


Link to post
Share on other sites
Sorry for not posting back for so long,
the problem was my memory alignment was off...made some
stupid mistakes earlier in design.
Only took one week to figure out =\

Share this post


Link to post
Share on other sites
Just to be perfectly clear, you're saying that you have some instance A of a class, you set A.value = something and A.value != something after the return?

Not something where you you have two instances A,B and set A.value = something, then find that B.value != something, right? That would only be true with static values.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this