-
Level Editor
I'm having some trouble making a level editor. Everything works perfectly EXCEPT that when I open it in another program, it crashes. How would I save a 16 by 16 array (using C IO)?
Here's the code for the editor:
http://personal.lig.bellsouth.net/~m...d/probcode.cpp
It won't compile because it needs it's textures, but can anyone tell me the right way to save and open the "world" array?
Thanks,
Valar_King
-
Due world is tile[16][16] you need to save it like
fwrite(world, sizeof(world[0][0]), sizeof(world)/sizeof(world[0][0]), dfile);
Here's an example from libc info to help you understand:
int foo[10];
fwrite(foo, sizeof(int), 10, stdin);
sizeof(world[0][0]) is therefore size of a single element (or you could use sizeof(tile), but if you someday decide to change `tile' to, let's say, `block' it doesn't work anymore (doh!)) and sizeof(world)/sizeof(world[0][0]) is how many elements there is.
-
Hmm...
It works in the level editor without crashing, but the level loader still crashes when it loads it. :confused:
-
Are you sure it's not "fwrite(&world, ..." instead of "fwrite(world, ..."?
-
yup
yes, i'm sure...
here's the full code and bitmaps. note that you must have allegro.dll in your system directory. you can get it from allegro's site.
http://personal.lig.bellsouth.net/~markhand/editor.zip
-
Hi!
The major reason why your code is not working is since it's written the way it is.
Read some information about abstraction and general naming and you'll see it will all work very easily. Basically programming is very easy no matter what you are trying to do, but you can make it the hard way (just make a big mess of all the code) or try to get a good structure of it, which would be the easy way.
/Laos
-
Hmmm
So what you're saying is, write less messy work and Windows won't punish me by crashing my program? What's the problem with my code? Lack of commenting? Indention? Variable names?
-
> What's the problem with my code? Lack of commenting? Indention? Variable names?
I've always find it quite difficult to read code where '{' is in the end of the line:
Code:
if (key[KEY_DOWN]) {
if (ry < 450) {
ry += 30; rest(100);
}
}
Though this is only my opinion, I just find that
Code:
if (key[KEY_DOWN])
{
if (ry < 450)
{
ry += 30; rest(100);
}
}
And even better is to ditch 'em all if not needed:
Code:
if (key[KEY_DOWN])
if (ry < 450)
ry += 30; rest(100);
About the general structuce of your code...
First of all, big functions tend to be somewhat... well, nobody just doesn't like 'em :-)
o Perhaps making an init-function where you start allegro, install mouse/keyb, etc.
o Maybe handling the initialization of `world' in a function of it's own?
o ...and input in some specific function?
Anyway, since I haven't compiled allegro to any Windows-compiler and having to convert your source to fit DJGPP (removing END_OF_MAIN, default_palette -> desktop_palette, GFX_AUTODETECT_WINDOWED -> GFX_AUTODETECT, removing set_window_title()) it crashed immediatly after running it (and since I'm online with a modem I don't have the time to debug it now...)
-
> And even better is to ditch 'em all if not needed:
Really? I think that chicken lips make the code easier to read and almost always leave them in - even for 1-line statements (it at least makes it easier to expand, if you need to)
-
Code:
if(something)
{
for(int i = 0; i < weirdstuff; i++)
{
if(array[i] != -1);
{
switch(array[0])
{
case 0:
case 1:
{
array[i]++;
break;
}
case 2:
{
if(array[i+1] == 0)
{
array--;
}
}
}
}
}
}
???
-
I took at quick look at your code and it appears the problem is as follows:
> fwrite(world, sizeof(world), 1, dfile);
'world' is a pointer. compilers allocate double-dimensioned arrays as pointers of pointers. As such, the 'sizeof(world)' call should yield the value of '4' (4 bytes). That's because that's how long a pointer is.
If it's working for you in some other capacity, you're just lucky.
Instead, you need to pass the address of the data itself and pass the data directly.
---
Anyone have other ideas?
-
> ???
OK - it's not a very strict rule...