<code>
unsigned char* generateHeightmap(int size)
{
unsigned char oldV;
unsigned char* hm = new unsigned char(size*size);
for (int z = 0; z < (size - 7); z+=8) //Loop through each strip of 8.
{
if (z==16) {
oldV = oldV; //Error checking before problem... Does nothing
}
for (int x = 0; x < size; ++x)
{
if (z==0) //Set initial value
{
oldV = myRand(30,225);
for (int i = 0; i < 8; ++i) //Make slight different values for rest of section
{
if (myBoolRand()) //Choose to increase/decrease values
hm[(z+i)*size + x] = oldV + myRand(0,INC);
else
hm[(z+i)*size + x] = oldV - myRand(0,DEC);
}
}
else
{ //Check and make sure values don't go out of range (0-255)
if (oldV < DEC)
{
oldV = oldV + myRand(0,INC);
}
else if (oldV > (255-INC))
{
oldV = oldV - myRand(0,DEC);
}
else
{
if (myBoolRand())
oldV = oldV + myRand(0,INC);
else
oldV = oldV + myRand(0,DEC);
}
for (int i = 0; i < 8; ++i)
{
if (myBoolRand())
hm[(z+i)*size + x] = oldV + myRand(0,INC);
else
hm[(z+i)*size + x] = oldV - myRand(0,DEC);
}
}
}
}
return hm;
}
</code>
Heightmap Generator problem. (C++)
Here's what I get:
First-chance exception at 0x00401435 in Terrain.exe: 0xC0000005: Access violation writing location 0x003650f8.
Unhandled exception at 0x00401435 in Terrain.exe: 0xC0000005: Access violation writing location 0x003650f8.
It happens when I get the values z=16, i=1, x=0.
I have no idea why this is happening. The heightmap I'm generating is 256*256, and it's an array of size 256*256, so no idea why at 256*17 in the array i get this error.
Here's the code:
that should be
not
I'm not sure but I think the second one will allocate one char and sets its value to size*size, while the first one allocates an array of chars.
Also please put code, /code tags (in square brackets) around your code to make it more readable in future.
Also, use a debugger! It's a lot easier to find where your code is failing by single-stepping through it than trying to figure out which line of your code compiles to address 0x00401435.
new unsigned char[size*size]
not
new unsigned char(size*size)
I'm not sure but I think the second one will allocate one char and sets its value to size*size, while the first one allocates an array of chars.
Also please put code, /code tags (in square brackets) around your code to make it more readable in future.
Also, use a debugger! It's a lot easier to find where your code is failing by single-stepping through it than trying to figure out which line of your code compiles to address 0x00401435.
Pointer arrays truly are evil...
Here's a possible solution that doesn't involve any overhead.
Here's a possible solution that doesn't involve any overhead.
template < unsigned int size >struct HeightMap { HeightMap() { generateHeightMap(); } unsigned int get_size() const { return size; } unsigned char &at( unsigned int x, unsigned int y ) { return hm[y*size + x]; }private: void generateHeightmap() { unsigned char oldV; for (int z = 0; z < (size - 7); z+=8) //Loop through each strip of 8. { ... } // doesn't return anything } char hm[size*size];};... HeightMap<256> hm;// or typedef HeightMap<512> HeightMap512; HeightMap512 *hm = new HeightMap512(); ... delete hm;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement