Results 1 to 7 of 7

Thread: [Resolved] maps for 2d games

  1. #1

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802

    [Resolved] maps for 2d games

    I'm trying to use a simple map writen in a txt file in my game but I can't get it to work.

    I have an array of integers: int iMap[10][10];
    And then I read in numbers from a txt file useing for loops and useing iMap[x][y] = fin.get()

    Here's my problem; When reading the file like this it puts all tbe '/n' in the array as well so I have to have the whole map on one line in the txt file. I would like to have it 10 by 10 in the file as well. How can I tell fin.get() to ignore all linebreaks in the txt file?
    Last edited by McCain; Feb 17th, 2003 at 07:18 PM.
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    make 2 for loops
    one to handle x, and one for y

    for(int y=0; y<10; y++)
    {
    for(int x=0; x<10; x++)
    {
    assign to myarray[x][y]
    }
    }

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Steve, you didnt answer the question =P.

    Its actually pretty simple... dont use f.get(). Instead, use the stream operator, like you would if reading an integer from cin. It will use the line feed as a terminator, and move to the next line.

    Z.

  4. #4

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    So I would go fin >> iMap[x][y] ?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Can you show us an example file?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    The text (map) file looks like this:
    Code:
    1111111111
    1000000001
    1000000001
    1000000001
    1000000001
    1000000001
    1000000001
    1000000001
    1000000001
    1111111111
    And in my source I have:
    Code:
    ifstream fin;
    char chrMap[10][10];
    
    fin.open("map.txt");
    
    for(int x = 0; x < 10; x++)
    {
    	for(int y = 0; y < 10; y++)
    	{
    		fin >> chrMap[x][y];
    	}
    }
    And this works pretty well but I want the array to be an integer array and if I just declare it int iMap[10][10]; I get some crazy stuff in that array (stuff like -858993460)... Why does this happen?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Because it reads a whole line at once...

    Here's what you could do, but it's more C-like, which is why I use the C io functions:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    FILE *fin;
    char chrMap[10][10];
    char c;
    int x, y;
    
    fin = fopen("map.txt", "rt");
    // note the inversed!
    for(y=0;y<10;++y) {
      for(x=0;x<10;++x) {
        do {
          c = fgetc(fin);
        } while(!isdigit(c));
        chrMap[x][y] = c - '0';
      }
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width