Thread: I can't explain this

  1. #16
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> MapFile >> buffer;
    what does this line do?

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    68
    it put the buffer into map...

    oh

    may be that is the problem

    you should try these

    strcpy(reinterpret_cast<char *>Map,reinterpret_cast<char *> buffer)

    or do a memcpy() it is lot faster

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    68
    sorry i read it wrong

    it read the file in buffer

  4. #19
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> it read the file in buffer

    So how's buffer supposed to change?

  5. #20
    didn't I say I am using fstream?

    and all this does the same exact thing. what I cannot understand is why the textprintf() that prints out the variable type works in the loop that assigns the values to all the type variables, but when I do it outside of there, it always gives me zero.

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (int i=0; i <= height; i++)
    What are the values of width and height?

    And why isn't it
    for (int i=0; i < height; i++)

    Perhaps you're stepping off the end of the array

  7. #22
    Sebastani, your method made it say "The value is indeed zero" and the %g had the same results as well.

    Anyone want to look at the code at a whole and figure this out? My uncle couldn't figure this out either, and he is a professional C++ programmer.

  8. #23
    width is 60 and height is 45.

    and what is weird is if I don't make it <= it loops forever.

  9. #24
    what the heck? the height and width are read from an external file, and if I change the width and height in the file to 59 and 44, it loops forever as well.

  10. #25
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > width is 60 and height is 45.
    Well this definitely steps off the end of the array big time

    Perhaps you have too much data in your file, like one extra row or column of tile data?

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Anyone want to look at the code at a whole and figure this out?
    If you can reduce it to a test case which doesn't require external libraries (plain ANSI-C++ which everyone can compile), then zip it up with an input file and attach it.

  12. #27
    you have to have allegro

  13. #28
    how is that stepping over? I got a 60 by 45 by 3 array for the tiles, and the map is 60 by 45 by 1!

  14. #29
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    Does the file solely consist of integers? Does it just contain the type or the whole tile? Can you post your whole loadfile function and a sample of the file?

  15. #30
    sure.

    now don't tell me about how messy it is, because I haven't had a chance to clean it up.

    Code:
    void map::LoadMap(char *FileToLoad)
    {
     MapFileName = FileToLoad;
     ExcludeSecondLayer = false;
     char buffer[256];
     ifstream MapFile(FileToLoad);
     
     while (!MapFile.eof() && strcmp(buffer,"[INITIALIZATION]"))
     {
      MapFile >> buffer;
     }
     while (!MapFile.eof() && strcmp(buffer,"[TILES]"))
     {
      MapFile >> buffer;
      if (!strcmp(buffer,"WIDTH"))
      {
       MapFile >> buffer;
       width = atoi(buffer);
      }
      else if (!strcmp(buffer,"HEIGHT"))
      {
       MapFile >> buffer;
       height = atoi(buffer);
      }
      else if (!strcmp(buffer,"S_CAMX"))
      {
       MapFile >> buffer;
       camerax = atoi(buffer);
      }
      else if (!strcmp(buffer,"S_CAMY"))
      {
       MapFile >> buffer;
       cameray = atoi(buffer);
      }
      else if (!strcmp(buffer,"EX_LAYER2"))
       ExcludeSecondLayer = true;
      else if (!strcmp(buffer,"MAPNAME"))
      {
       MapFile.getline(buffer,256);
       MapName = buffer;
      } 
     }
     while (!MapFile.eof() && strcmp(buffer,"ENDMAP"))
     {
      MapFile >> buffer;
      
      if (!strcmp(buffer,"LAYER1"))
      {
       MapFile >> buffer;
       int LoadPositionX = 0;
       int LoadPositionY = 0;
       
       for (int i=0; i <= width; i++)
       {
        for (int j=0; j <= height; j++)
        {
         MapFile >> buffer;
         clear(screen);
         tiles[LoadPositionX][LoadPositionY][0].type = atoi(buffer);
         textprintf(screen,font,0,0,makecol(255,255,255),"%d",tiles[LoadPositionX][LoadPositionY][0].type);
         LoadPositionX++;
        }
        
        LoadPositionX = 0;
        LoadPositionY++;
       }
       MapFile >> buffer;
      }
     }
     MapFile.close();
    }
    now the textprintf() in there as I said b4 displays the number perfectly, but if I try to do the same thing outside of the LoadMap() function, it always gives me 0.

    I attached the map file with this post. It is a supposed to be a .tmf file, but I don't know if it'll let me attach a .tmf file so I renamed it to .txt for attaching sake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you explain these bitwise operations?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 01:19 PM
  2. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  4. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM