Pre-Main() stack-overflow with SDL

Started by
34 comments, last by Twiggy 19 years ago
Quote:Original post by Twiggy
Thanks! :)

Again -> So how can I determine the size of the arrays before run-time?


multiply together all of the sizes.
multiply that by sizeof(type);
divide that by 1048576
Advertisement
But, dude, I can't create a MapTile *Map[][] inside the class. I have to determine it sizes, which is changeable. It should be Map_Size_X and Map_Size_Y
But i'm gonna change the class so that the Map_Size_X and Map_Size_Y can be changed by the programmer. <I'm creating it to be distributed to other folks>

^----Unlike in the block of code of GameCore.h above (I changed it)
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"
Quote:Original post by Twiggy
But, dude, I can't create a MapTile *Map[][] inside the class. I have to determine it sizes, which is changeable. It should be Map_Size_X and Map_Size_Y
But i'm gonna change the class so that the Map_Size_X and Map_Size_Y can be changed by the programmer. <I'm creating it to be distributed to other folks>

^----Unlike in the block of code of GameCore.h above (I changed it)


Simple, in your class declare a double pointer and allocate some dynamic memory in the constructor of the class. I've never really worked with 2D arrays or such, but I believe you would declare

MapTile **Map;

in your class, and in your constructor just say

*Map = new MapTile[foo];
Map = new MapTile[bar];

then just delete the memory in the destructor. That'd work right? Again, not my area of expertise.
You can create MapTile ***Map, but this is nasty. (A two-dimensional array of pointers.)
First you do Map = new MapTile**[Map_Size_X], and then you need to go through all elements and do Map[idx] = new MapTile[Map_Size_Y]. (Idx is a counter which goes from 0 to Map_Size_X - 1)

When you delete the Map array, you need to delete in reverse order. First: delete[] Map[idx] and then delete[] Map. Probably you need to call delete on the Map objects first.

IMHO it's better to use a vector. You can create a two-dimensional vector this way: vector<vector<MapTile *>> Map. Use reserve or the vector constructor to allocate space. You still need to loop through the "inner" arrays. Also, don't forget to delete the Map objects when you're done.

If anyone knows a way to handle this more efficiently, I'd be interested too.
Why would you want to find out map size before run time? you can store map size in the header of the map file (ie. first two numbers are map sizes the rest are tile data) and then at run time make enough room to hold all your map data, no matter how big they are :)

you can do it using vectors as suggested, or:
// define it like thisMapTile **map;...// create a new, custom sized map on the heap ...*map = new MapTile[ map_size_x ];for( int x=0; x<map_size_x; x++ )     map[x]=new MapTile[ map_size_y ];// access it as usual withmap[x][y] = ...// delete it properly..for( int x=0; x<map_size_x; x++ )    delete [] map[x];


hope this helps! if not, i'll try to explain more..

edit: forgot how to use code tags, need to post more often ;)
[imwithstupid]

He has it 100% correct, either that or std::vector/list/map/deque.
Heh, nice way of putting it.

But if I have a pointer-to-pointer array (MapTile **map;) -

Is it legal to acces a certain MapTile in the array by using map[x][y]?

I'm sorry, but I'm not experienced enough in programming. :)

And about the vector approach - thank you very much, but I haven't learnt vectors yet. :)
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"
1. Yes
2. You really oughta, its simple

std::vector<Map> mapvec;

mapvec.pushback(a_map);

mapvec.clear();
paramecij, accessing the map[x][y] = ...

by the weay you described, generates lots of errors

how can i access it?

I tried using *map[x][y]

doesn't work either

how'd you do it?
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"
can someone please help?

how do i access the map[x][y] after using paramecij's method?
___________________________"Peg, is there a certain reason that cactus is standing in the place where my alarm clock should be?"

This topic is closed to new replies.

Advertisement