I am currently making a simple game using different tiles from an image file and placing them with a terrain generator. The terrain generator is based on the diamond square algorithm and I tested it in a smaller scale and it works just fine. However, in my game I want to generate a map made out of 513*513 tiles, which means that I need an int to store the tile values with and array of [513][513], and I also need a double with the same array for the generator.
Declaring the int works just fine, but when I declare the double the program crashes from a stack overflow. Is there any good way to get around this?
PS. Even though this is about a game I thought it would be better to put it in the c++ section since the problem is a generic c++ problem and has nothing to do with the game.
Also, here is the code for my map class (as you can see, I am using SDL):
map.h
Code:
#pragma once
#include "ANewWorld.h"
class Map
{
public:
SDL_Rect position[513][513];
int type[513][513];
void generate();
void render(SDL_Rect camera);
};
map.cpp
Code:
#include "map.h"
#include "ANewWorld.h"
#include <iostream>
using namespace std;
void Map::generate()
{
double tM[513][513]; //temporaryMap
//This is where the problem is.
int temp;
srand(time(0));
int sS = 32; //stepSize
int hS = sS/2; //halfStep
int sX = 0; //startX
int sY = 0; //startY
for(int x = sX; x < 513; x += sS){
for(int y = sY; y < 513; y += sS){
tM[x][y] = rand()%2;
for(int n = 1; n <= 5; n++){
temp = rand()%10;
tM[x][y] += temp/(10^n);
}
}
}
do{
hS = sS/2;
sX = hS;
sY = hS;
for(int x = sX; x < 513; x += sS){
for(int y = sY; y < 513; y += sS){
tM[x][y] = (tM[x-hS][y-hS] + tM[x-hS][y+hS] + tM[x+hS][y-hS] + tM[x+hS][y+hS])/4;
}
}
sX = hS;
sY = 0;
for(int x = sX; x < 513; x += sS){
for(int y = sY; y < 513; y += sS){
if(y==0) tM[x][y] = (tM[x-hS][y] + tM[x+hS][y] + tM[x][y+hS]*2)/4;
else if(y==513) tM[x][y] = (tM[x-hS][y] + tM[x+hS][y] + tM[x][y-hS]*2)/4;
else tM[x][y] = (tM[x-hS][y] + tM[x+hS][y] + tM[x][y+hS] + tM[x][y-hS])/4;
}
}
sX = 0;
sY = hS;
for(int x = sX; x < 513; x += sS){
for(int y = sY; y < 513; y += sS){
if(x==0) tM[x][y] = (tM[x][y-hS] + tM[x][y+hS] + tM[x+hS][y]*2)/4;
else if(x==513) tM[x][y] = (tM[x][y-hS] + tM[x][y+hS] + tM[x-hS][y]*2)/4;
else tM[x][y] = (tM[x][y-hS] + tM[x][y+hS] + tM[x-hS][y] + tM[x+hS][y])/4;
}
}
sS = sS/2;
}while(sS!=1);
for(int x = 0; x < 513; x++){
for(int y = 0; y < 513; y++){
position[x][y].x = x*64;
position[x][y].y = y*64;
if(tM[x][y] < 0.45) type[x][y] = 0;
else if(tM[x][y] > 1.05) type[x][y] = 2;
else type[x][y] = 1;
}
}
}
void Map::render(SDL_Rect camera)
{
SDL_Rect rPos;
int rStartx = camera.x/64;
int rStarty = camera.y/64;
int rEndx = (camera.x + SCREEN_WIDTH)/64;
int rEndy = (camera.y + SCREEN_HEIGHT)/64;
for(int x = rStartx; x <= rEndx; x++){
for(int y = rStarty; y <= rEndy; y++){
rPos.x = position[x][y].x - camera.x;
rPos.y = position[x][y].y - camera.y;
SDL_BlitSurface(tiles, &clipTiles[type[x][y]], screen, &rPos);
}
}
}