Thread: Level Editor

  1. #1

    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
    -Save the whales. Collect the whole set.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    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.

  3. #3

    Hmm...

    It works in the level editor without crashing, but the level loader still crashes when it loads it.
    -Save the whales. Collect the whole set.

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    Are you sure it's not "fwrite(&world, ..." instead of "fwrite(world, ..."?

  5. #5

    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

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    14
    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

  7. #7

    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?
    -Save the whales. Collect the whole set.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    > 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...)

  9. #9
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > 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)

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    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--;
                          }
                     }
                 }
             }
         }
    }
    ???

  11. #11
    Sayeh
    Guest
    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?

  12. #12
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > ???

    OK - it's not a very strict rule...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2007, 02:46 AM
  2. Replies: 12
    Last Post: 08-05-2003, 02:16 PM
  3. Level Editor
    By jdinger in forum Game Programming
    Replies: 14
    Last Post: 04-03-2003, 06:46 PM
  4. level up 6 times with 40 EXP!?!?!?!
    By Blizzarddog in forum Game Programming
    Replies: 15
    Last Post: 03-05-2003, 12:56 PM
  5. level editor
    By lambs4 in forum Game Programming
    Replies: 3
    Last Post: 10-31-2002, 05:35 AM