Hello everybody. For my game I'm trying to create a load_level function which loads a config file from disk to get all of the parameters for drawing the level. The problem is I have is in the level struct, where I have a int **, in order to hold the order of tiles to be drawn. The level struct looks like this:
The problem is in the load level function, where I try to assign the values from the config file to int **tilemap
Inside the function, getting the values from a_level->tilemap[y][x] returns the expected values. Outside the function, a_level->tilemap[y][x] returns garbage values and crashes the program.
The config file that I am using looks like this:
size_x = 20 size_y = 15 start_x = 32.0 start_y = 416.0 tilesize = 32 tile_file = tiles/tiles.bmp map_data = 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,1,2,2,2,2,2,2,3,3,3,2,2,2,2,2,2,2,2,3,1,0,3,3,3,3,3,3,1,1,1,3,3,3,3,3,3,3,3,1,0,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0
How would I need to change my code in order to access the **tilemap pointer and get correct values?
level hard_level; level *tmp_level;tmp_level = &hard_level;//... return tmp_level;
You are returning the address of a temporary level object. As soon as load_level returns 'hard_level' is no longer a valid object. You have to create a new 'level' using malloc. It crashes because the area in memory where 'hard_level' was has most likely been overwritten or else it causes a segfault.
Ok, I think I see what the problem is now, but I have one more question.
I now have:
level *tmp_level = malloc(sizeof(level));
instead of
level *tmp_level;
but the program gives the error:
"request for member 'tilemap' in something not a structure or a union", so I don't think what I did was right.
Could you point me in the right direction as to what I need to do to malloc() the level?
Thanks
Since you're now dealing with a pointer to a struct instead of a struct itself, you're using the wrong member access operator - "." instead of "->".
Hrm... I fixed that, but now my main loop always crashes when I try to access the tilemap (even though I allocate memory for that and the level struct pointer), while I can access the other values of the level struct just fine.
I feel bad for asking so many questions, because I feel that there's just one really stupid thing I'm missing...
Post your latest load_level function. Either level->size_y or level->size_x is wrong, or you are not allocating level->tilemap correctly.
Here it is:
That should be level* tmp_level = malloc(sizeof(level));, like you had it before. You're only allocating enough memory to hold a pointer.
//without this, I can't access tilesize from outside this function... // tmp_level->tilesize = (int) malloc(sizeof(int)); tmp_level->tilesize = atoi(al_get_config_value(level_file, NULL, "tile_size"));
The line I commented out (the second one) is not good C. You allocate an integer's worth of memory and then cast that address to an integer - it's value would likely be huge, and totally incorrect. The third line there is correct.
I commented out two lines - the first commented line is unnecessary and is overwritten by your second assignment, and the second commented line is a mistake, because it would skip one of your values.
Thank you, it works perfectly!
Glad to hear it. Got a screenshot you could show us?
Here's one. Unfortunately I'm not the best artist, so all I have right now is a stick figure for testing purposes.
{"name":"screenshot.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/6\/069bfb2e47ac9abeeb5ce934152edd50.jpg","w":646,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/6\/069bfb2e47ac9abeeb5ce934152edd50"}
Well, your basic ground tile looks good. It blends perfectly with the tiles next to it.