SFML community forums
Help => Graphics => Topic started by: Mr Random on July 31, 2011, 06:55:54 am
-
Hi guys, i'm new to both SFML and c++. Im trying to make a dungeon crawler game, im having issues when i try to read from a file containing 1's and 0's (1 for a wall, 0 for blank square).
I'm reading the file into a 2D array and using this array to tell my array of sprites to draw the walls of the level.
Im using this code to read the file (I spent a while looking around these forums trying to get this to work so it may be wrong)
map.open("layout.txt");
while(map && y<80)
{
getline(map,line);
while(x<64)
{
Board[x][y]=line[x]-'0';
x++;
}
y++;
x=0;
}
That part seems to be working fine
To draw the sprites Im using the following
for(int i=0;i<64;++i)
{
for(int j=0;j<80;++j)
{
if(Board[i][j] == 1)
{
Map[i][j].SetImage (Wall);
Map[i][j].SetPosition (WallX,WallY);
WallX = WallX + 20;
}
WallY = WallY + 10;
}
}
When i run the program I get an error saying
Unhandled exception at 0x011b33d7 in Pacman.exe: 0xC00000FD: Stack overflow.
Then visual studio shows me pointing to the probe page line
; Find next lower page and probe
cs20:
sub eax, _PAGESIZE_ ; decrease by PAGESIZE
test dword ptr [eax],eax ; probe page.
jmp short cs10
I assume the error is in the way Im making my walls, is there a better way to do this?
-
You typically get a stack overflow one of two ways:
1) Having an array on the stack that's way too big
int func()
{
int waytoobig[100000]; // bad idea to put all this on the stack!
}
or
2) Having a function that's infinitely recursive (calls itself) -- or a series of functions that loop into calling themselves.
void func1()
{
func2();
}
void func2()
{
func1();
}
// func1 calls func2
// and func2 calls func1
// so this will suck up stack space until you run out
Are you doing either of these?
-
I think I have an array that is to big. How can i fix this?
is there a command to put the array on the heap?
I know in c++ you can use new to add to an array but i haven't learn t much about using it yet, is this a good option to look into?
-
I think I have an array that is to big. How can i fix this?
Don't use arrays, prefer containers. In this case, a dynamic container like std::vector would help.
is there a command to put the array on the heap?
The new and delete operators. But these are really basics, you should read a good C++ book before you work with SFML, or you will have such problems again and again ;)
I know in c++ you can use new to add to an array but i haven't learn t much about using it yet, is this a good option to look into?
Generally, dynamic memory management is important to know. But later, good code style is to avoid it where possible and use abstractions that automatically manage memory, one of which are the STL containers.